Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: util/test/scoped_temp_dir_win.cc

Issue 913273002: win: Implementation of CrashReportDatabase for Windows (for C++ Windows readability review) (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: better unlink checks Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/test/scoped_temp_dir.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Crashpad Authors. All rights reserved. 1 // Copyright 2015 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 // Try to move to a new temporary directory with a randomly generated name. 47 // Try to move to a new temporary directory with a randomly generated name.
48 // If the one we try exists, retry with a new name until we reach some 48 // If the one we try exists, retry with a new name until we reach some
49 // limit. 49 // limit.
50 base::FilePath target_path = GenerateCandidateName(); 50 base::FilePath target_path = GenerateCandidateName();
51 if (MoveFileEx(path_.value().c_str(), target_path.value().c_str(), 0)) { 51 if (MoveFileEx(path_.value().c_str(), target_path.value().c_str(), 0)) {
52 path_ = target_path; 52 path_ = target_path;
53 return; 53 return;
54 } 54 }
55 } 55 }
56 56
57 CHECK(false) << "Couldn't find temp dir name"; 57 CHECK(false) << "Couldn't move to a new unique temp dir";
58 } 58 }
59 59
60 // static 60 // static
61 base::FilePath ScopedTempDir::CreateTemporaryDirectory() { 61 base::FilePath ScopedTempDir::CreateTemporaryDirectory() {
62 for (int count = 0; count < kRetries; ++count) { 62 for (int count = 0; count < kRetries; ++count) {
63 // Try to create a new temporary directory with random generated name. If 63 // Try to create a new temporary directory with random generated name. If
64 // the one we generate exists, keep trying another path name until we reach 64 // the one we generate exists, keep trying another path name until we reach
65 // some limit. 65 // some limit.
66 base::FilePath path_to_create = GenerateCandidateName(); 66 base::FilePath path_to_create = GenerateCandidateName();
67 if (CreateDirectory(path_to_create.value().c_str(), NULL)) 67 if (CreateDirectory(path_to_create.value().c_str(), NULL))
68 return path_to_create; 68 return path_to_create;
69 } 69 }
70 70
71 CHECK(false) << "Couldn't find temp dir name"; 71 CHECK(false) << "Couldn't create a new unique temp dir";
72 return base::FilePath(); 72 return base::FilePath();
73 } 73 }
74 74
75 // static 75 // static
76 void ScopedTempDir::RecursivelyDeleteTemporaryDirectory( 76 void ScopedTempDir::RecursivelyDeleteTemporaryDirectory(
77 const base::FilePath& path) { 77 const base::FilePath& path) {
78 const std::wstring all_files_mask(L"\\*"); 78 const base::string16 all_files_mask(L"\\*");
79 79
80 std::wstring search_mask = path.value() + all_files_mask; 80 base::string16 search_mask = path.value() + all_files_mask;
81 WIN32_FIND_DATA find_data; 81 WIN32_FIND_DATA find_data;
82 HANDLE search_handle = FindFirstFile(search_mask.c_str(), &find_data); 82 HANDLE search_handle = FindFirstFile(search_mask.c_str(), &find_data);
83 if (search_handle == INVALID_HANDLE_VALUE) { 83 if (search_handle == INVALID_HANDLE_VALUE)
84 ASSERT_EQ(GetLastError(), ERROR_FILE_NOT_FOUND); 84 ASSERT_EQ(ERROR_FILE_NOT_FOUND, GetLastError());
85 return; 85 do {
86 } 86 if (wcscmp(find_data.cFileName, L".") == 0 ||
87 for (;;) { 87 wcscmp(find_data.cFileName, L"..") == 0) {
88 if (wcscmp(find_data.cFileName, L".") != 0 && 88 continue;
89 wcscmp(find_data.cFileName, L"..") != 0) {
90 bool is_dir =
91 (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 ||
92 (find_data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0;
93 base::FilePath entry_path = path.Append(find_data.cFileName);
94 if (is_dir)
95 RecursivelyDeleteTemporaryDirectory(entry_path);
96 else
97 EXPECT_TRUE(DeleteFile(entry_path.value().c_str()));
98 } 89 }
99 90 base::FilePath entry_path = path.Append(find_data.cFileName);
100 if (!FindNextFile(search_handle, &find_data)) { 91 ASSERT_FALSE(find_data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT);
101 EXPECT_EQ(GetLastError(), ERROR_NO_MORE_FILES); 92 if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
102 break; 93 RecursivelyDeleteTemporaryDirectory(entry_path);
103 } 94 else
104 } 95 EXPECT_TRUE(DeleteFile(entry_path.value().c_str()));
96 } while (FindNextFile(search_handle, &find_data));
97 EXPECT_EQ(ERROR_NO_MORE_FILES, GetLastError());
105 98
106 EXPECT_TRUE(FindClose(search_handle)); 99 EXPECT_TRUE(FindClose(search_handle));
107 EXPECT_TRUE(RemoveDirectory(path.value().c_str())); 100 EXPECT_TRUE(RemoveDirectory(path.value().c_str()));
108 } 101 }
109 102
110 } // namespace test 103 } // namespace test
111 } // namespace crashpad 104 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/test/scoped_temp_dir.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698