OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/win/jumplist_file_util.h" |
| 6 |
| 7 #include <Shlwapi.h> |
| 8 #include <windows.h> |
| 9 |
| 10 #include "base/files/file_enumerator.h" |
| 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/threading/thread_restrictions.h" |
| 13 |
| 14 FolderDeleteResult DeleteFiles(const base::FilePath& path, |
| 15 const base::FilePath::StringType& pattern, |
| 16 int max_file_deleted) { |
| 17 int success_count = 0; |
| 18 int failure_count = 0; |
| 19 FolderDeleteResult delete_status = SUCCEED; |
| 20 |
| 21 base::FileEnumerator traversal( |
| 22 path, false, |
| 23 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES, pattern); |
| 24 for (base::FilePath current = traversal.Next(); !current.empty(); |
| 25 current = traversal.Next()) { |
| 26 // Try to clear the read-only bit if we find it. |
| 27 base::FileEnumerator::FileInfo info = traversal.GetInfo(); |
| 28 if (info.find_data().dwFileAttributes & FILE_ATTRIBUTE_READONLY) { |
| 29 SetFileAttributes( |
| 30 current.value().c_str(), |
| 31 info.find_data().dwFileAttributes & ~FILE_ATTRIBUTE_READONLY); |
| 32 } |
| 33 |
| 34 if (info.IsDirectory()) { |
| 35 // JumpListIcons{,Old} directories shouldn't have sub-directories. |
| 36 // If any of them does for unknown reasons, don't delete them. Instead, |
| 37 // increment the failure count and record this information. |
| 38 delete_status = FAIL_SUBDIRECTORY_EXISTS; |
| 39 failure_count++; |
| 40 } else if (!::DeleteFile(current.value().c_str())) { |
| 41 failure_count++; |
| 42 } else { |
| 43 success_count++; |
| 44 } |
| 45 // If it deletes max_file_deleted files with any attempt failures, record |
| 46 // this information in |delete_status|. |
| 47 if (success_count >= max_file_deleted) { |
| 48 // The desired max number of files have been deleted. |
| 49 return failure_count ? FAIL_DELETE_MAX_FILES_WITH_ERRORS : delete_status; |
| 50 } |
| 51 if (failure_count >= max_file_deleted) { |
| 52 // The desired max number of failures have been hit. |
| 53 return FAIL_MAX_DELETE_FAILURES; |
| 54 } |
| 55 } |
| 56 return delete_status; |
| 57 } |
| 58 |
| 59 FolderDeleteResult DeleteDirectoryContent(const base::FilePath& path, |
| 60 int max_file_deleted) { |
| 61 base::ThreadRestrictions::AssertIOAllowed(); |
| 62 |
| 63 if (path.empty()) |
| 64 return SUCCEED; |
| 65 |
| 66 // For JumpListIcons{,Old} directories, since their names are shorter than |
| 67 // MAX_PATH, hitting the code in the if-block below is unexpected. |
| 68 if (path.value().length() >= MAX_PATH) |
| 69 return FAIL_INVALID_FILE_PATH; |
| 70 |
| 71 DWORD attr = GetFileAttributes(path.value().c_str()); |
| 72 // We're done if we can't find the path. |
| 73 if (attr == INVALID_FILE_ATTRIBUTES) |
| 74 return SUCCEED; |
| 75 // Try to clear the read-only bit if we find it. |
| 76 if ((attr & FILE_ATTRIBUTE_READONLY) && |
| 77 !SetFileAttributes(path.value().c_str(), |
| 78 attr & ~FILE_ATTRIBUTE_READONLY)) { |
| 79 return FAIL_READ_ONLY_DIRECTORY; |
| 80 } |
| 81 |
| 82 // If |path| is a file, simply delete it. However, since JumpListIcons{,Old} |
| 83 // are directories, hitting the code inside the if-block below is unexpected. |
| 84 if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) { |
| 85 ::DeleteFile(path.value().c_str()); |
| 86 return FAIL_DELETE_SINGLE_FILE; |
| 87 } |
| 88 |
| 89 // If |path| is a directory, delete at most |max_file_deleted| files in it. |
| 90 return DeleteFiles(path, L"*", max_file_deleted); |
| 91 } |
| 92 |
| 93 FolderDeleteResult DeleteDirectory(const base::FilePath& path, |
| 94 int max_file_deleted) { |
| 95 base::ThreadRestrictions::AssertIOAllowed(); |
| 96 // Delete at most |max_file_deleted| files in |path|. |
| 97 FolderDeleteResult delete_status = |
| 98 DeleteDirectoryContent(path, max_file_deleted); |
| 99 // Since DeleteDirectoryContent() can only delete at most |max_file_deleted| |
| 100 // files, its return value cannot indicate if |path| is empty or not. |
| 101 // Instead, use PathIsDirectoryEmpty to check if |path| is empty and remove it |
| 102 // if it is. |
| 103 if (::PathIsDirectoryEmpty(path.value().c_str()) && |
| 104 !::RemoveDirectory(path.value().c_str())) { |
| 105 delete_status = FAIL_REMOVE_RAW_DIRECTORY; |
| 106 } |
| 107 return delete_status; |
| 108 } |
| 109 |
| 110 void DeleteDirectoryAndLogResults(const base::FilePath& path, |
| 111 int max_file_deleted) { |
| 112 FolderDeleteResult delete_status = DeleteDirectory(path, max_file_deleted); |
| 113 UMA_HISTOGRAM_ENUMERATION("WinJumplist.DeleteStatusJumpListIconsOld", |
| 114 delete_status, END); |
| 115 } |
OLD | NEW |