| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (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 | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 | |
| 15 #include "util/test/scoped_temp_dir.h" | |
| 16 | |
| 17 #include <windows.h> | |
| 18 | |
| 19 #include "base/logging.h" | |
| 20 #include "base/rand_util.h" | |
| 21 #include "base/strings/string16.h" | |
| 22 #include "base/strings/stringprintf.h" | |
| 23 #include "base/strings/utf_string_conversions.h" | |
| 24 #include "gtest/gtest.h" | |
| 25 | |
| 26 namespace crashpad { | |
| 27 namespace test { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 base::FilePath GenerateCandidateName() { | |
| 32 wchar_t temp_path[MAX_PATH + 1]; | |
| 33 DWORD path_len = GetTempPath(MAX_PATH, temp_path); | |
| 34 PCHECK(path_len != 0) << "GetTempPath"; | |
| 35 base::FilePath system_temp_dir(temp_path); | |
| 36 base::string16 new_dir_name = base::UTF8ToUTF16(base::StringPrintf( | |
| 37 "crashpad.test.%d.%I64x", GetCurrentProcessId(), base::RandUint64())); | |
| 38 return system_temp_dir.Append(new_dir_name); | |
| 39 } | |
| 40 | |
| 41 const int kRetries = 50; | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 void ScopedTempDir::Rename() { | |
| 46 for (int count = 0; count < kRetries; ++count) { | |
| 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 | |
| 49 // limit. | |
| 50 base::FilePath target_path = GenerateCandidateName(); | |
| 51 if (MoveFileEx(path_.value().c_str(), target_path.value().c_str(), 0)) { | |
| 52 path_ = target_path; | |
| 53 return; | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 CHECK(false) << "Couldn't move to a new unique temp dir"; | |
| 58 } | |
| 59 | |
| 60 // static | |
| 61 base::FilePath ScopedTempDir::CreateTemporaryDirectory() { | |
| 62 for (int count = 0; count < kRetries; ++count) { | |
| 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 | |
| 65 // some limit. | |
| 66 base::FilePath path_to_create = GenerateCandidateName(); | |
| 67 if (CreateDirectory(path_to_create.value().c_str(), NULL)) | |
| 68 return path_to_create; | |
| 69 } | |
| 70 | |
| 71 CHECK(false) << "Couldn't create a new unique temp dir"; | |
| 72 return base::FilePath(); | |
| 73 } | |
| 74 | |
| 75 // static | |
| 76 void ScopedTempDir::RecursivelyDeleteTemporaryDirectory( | |
| 77 const base::FilePath& path) { | |
| 78 const base::string16 all_files_mask(L"\\*"); | |
| 79 | |
| 80 base::string16 search_mask = path.value() + all_files_mask; | |
| 81 WIN32_FIND_DATA find_data; | |
| 82 HANDLE search_handle = FindFirstFile(search_mask.c_str(), &find_data); | |
| 83 if (search_handle == INVALID_HANDLE_VALUE) | |
| 84 ASSERT_EQ(ERROR_FILE_NOT_FOUND, GetLastError()); | |
| 85 do { | |
| 86 if (wcscmp(find_data.cFileName, L".") == 0 || | |
| 87 wcscmp(find_data.cFileName, L"..") == 0) { | |
| 88 continue; | |
| 89 } | |
| 90 base::FilePath entry_path = path.Append(find_data.cFileName); | |
| 91 ASSERT_FALSE(find_data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT); | |
| 92 if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) | |
| 93 RecursivelyDeleteTemporaryDirectory(entry_path); | |
| 94 else | |
| 95 EXPECT_TRUE(DeleteFile(entry_path.value().c_str())); | |
| 96 } while (FindNextFile(search_handle, &find_data)); | |
| 97 EXPECT_EQ(ERROR_NO_MORE_FILES, GetLastError()); | |
| 98 | |
| 99 EXPECT_TRUE(FindClose(search_handle)); | |
| 100 EXPECT_TRUE(RemoveDirectory(path.value().c_str())); | |
| 101 } | |
| 102 | |
| 103 } // namespace test | |
| 104 } // namespace crashpad | |
| OLD | NEW |