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 // static |
| 30 base::FilePath ScopedTempDir::CreateTemporaryDirectory() { |
| 31 wchar_t temp_path[MAX_PATH + 1]; |
| 32 DWORD path_len = GetTempPath(MAX_PATH, temp_path); |
| 33 PCHECK(path_len != 0) << "GetTempPath"; |
| 34 base::FilePath system_temp_dir(temp_path); |
| 35 |
| 36 for (int count = 0; count < 50; ++count) { |
| 37 // Try create a new temporary directory with random generated name. If the |
| 38 // one we generate exists, keep trying another path name until we reach some |
| 39 // limit. |
| 40 base::string16 new_dir_name = base::UTF8ToUTF16(base::StringPrintf( |
| 41 "crashpad.test.%d.%I64x", GetCurrentProcessId(), base::RandUint64())); |
| 42 |
| 43 base::FilePath path_to_create = system_temp_dir.Append(new_dir_name); |
| 44 if (CreateDirectory(path_to_create.value().c_str(), NULL)) |
| 45 return path_to_create; |
| 46 } |
| 47 |
| 48 CHECK(false) << "Couldn't find temp dir name"; |
| 49 return base::FilePath(); |
| 50 } |
| 51 |
| 52 // static |
| 53 void ScopedTempDir::RecursivelyDeleteTemporaryDirectory( |
| 54 const base::FilePath& path) { |
| 55 const std::wstring all_files_mask(L"\\*"); |
| 56 |
| 57 std::wstring search_mask = path.value() + all_files_mask; |
| 58 WIN32_FIND_DATA find_data; |
| 59 HANDLE search_handle = FindFirstFile(search_mask.c_str(), &find_data); |
| 60 if (search_handle == INVALID_HANDLE_VALUE) { |
| 61 ASSERT_EQ(GetLastError(), ERROR_FILE_NOT_FOUND); |
| 62 return; |
| 63 } |
| 64 for (;;) { |
| 65 if (wcscmp(find_data.cFileName, L".") != 0 && |
| 66 wcscmp(find_data.cFileName, L"..") != 0) { |
| 67 bool is_dir = |
| 68 (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 || |
| 69 (find_data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0; |
| 70 base::FilePath entry_path = path.Append(find_data.cFileName); |
| 71 if (is_dir) |
| 72 RecursivelyDeleteTemporaryDirectory(entry_path); |
| 73 else |
| 74 EXPECT_TRUE(DeleteFile(entry_path.value().c_str())); |
| 75 } |
| 76 |
| 77 if (!FindNextFile(search_handle, &find_data)) { |
| 78 EXPECT_EQ(GetLastError(), ERROR_NO_MORE_FILES); |
| 79 break; |
| 80 } |
| 81 } |
| 82 |
| 83 EXPECT_TRUE(FindClose(search_handle)); |
| 84 EXPECT_TRUE(RemoveDirectory(path.value().c_str())); |
| 85 } |
| 86 |
| 87 } // namespace test |
| 88 } // namespace crashpad |
OLD | NEW |