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

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

Issue 837123005: win: Add implementation of ScopedTempDir (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: . Created 5 years, 11 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_posix.cc ('k') | util/util.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "%d_%d", GetCurrentProcessId(), base::RandInt(0, 65536)));
Mark Mentovai 2015/01/09 14:13:47 Can we put something about crashpad in the name so
scottmg 2015/01/13 21:20:56 Done.
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 // SHFILEOPSTRUCT wants the path to be terminated with two NULLs,
56 // so we have to use wcscpy because wcscpy_s writes non-NULLs
57 // into the rest of the buffer.
58 wchar_t double_terminated_path[MAX_PATH + 1] = {0};
Mark Mentovai 2015/01/09 14:13:47 Because this uses clobber-prone wcscpy, it should
59 wcscpy(double_terminated_path, path.value().c_str());
60
61 SHFILEOPSTRUCT file_operation = {0};
62 file_operation.wFunc = FO_DELETE;
63 file_operation.pFrom = double_terminated_path;
64 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION;
65 int err = SHFileOperation(&file_operation);
Mark Mentovai 2015/01/09 14:13:47 I’m kinda unimpressed by this function expanding w
scottmg 2015/01/13 21:20:56 OK. Mostly was based on ::RemoveDirectory suggesti
66
67 // Since we're passing flags to the operation telling it to be silent,
68 // it's possible for the operation to be aborted/cancelled without err
69 // being set (although MSDN doesn't give any scenarios for how this can
70 // happen). See MSDN for SHFileOperation and SHFILEOPTSTRUCT.
71 EXPECT_FALSE(file_operation.fAnyOperationsAborted);
72
73 // Some versions of Windows return ERROR_FILE_NOT_FOUND (0x2) when deleting
74 // an empty directory and some return 0x402 when they should be returning
75 // ERROR_FILE_NOT_FOUND. MSDN says Vista and up won't return 0x402. Windows 7
76 // can return DE_INVALIDFILES (0x7C) for nonexistent directories.
77 EXPECT_TRUE(err == 0 || err == ERROR_FILE_NOT_FOUND || err == 0x402 ||
78 err == 0x7C);
79 }
80
81 } // namespace test
82 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/test/scoped_temp_dir_posix.cc ('k') | util/util.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698