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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « util/test/scoped_temp_dir_posix.cc ('k') | util/util.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: util/test/scoped_temp_dir_win.cc
diff --git a/util/test/scoped_temp_dir_win.cc b/util/test/scoped_temp_dir_win.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d1ef6346fb6ba886a2836d118e78ecdfd108b6b8
--- /dev/null
+++ b/util/test/scoped_temp_dir_win.cc
@@ -0,0 +1,82 @@
+// Copyright 2015 The Crashpad Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "util/test/scoped_temp_dir.h"
+
+#include <windows.h>
+
+#include "base/logging.h"
+#include "base/rand_util.h"
+#include "base/strings/string16.h"
+#include "base/strings/stringprintf.h"
+#include "base/strings/utf_string_conversions.h"
+#include "gtest/gtest.h"
+
+namespace crashpad {
+namespace test {
+
+// static
+base::FilePath ScopedTempDir::CreateTemporaryDirectory() {
+ wchar_t temp_path[MAX_PATH + 1];
+ DWORD path_len = GetTempPath(MAX_PATH, temp_path);
+ PCHECK(path_len != 0) << "GetTempPath";
+ base::FilePath system_temp_dir(temp_path);
+
+ for (int count = 0; count < 50; ++count) {
+ // Try create a new temporary directory with random generated name. If the
+ // one we generate exists, keep trying another path name until we reach some
+ // limit.
+ base::string16 new_dir_name = base::UTF8ToUTF16(base::StringPrintf(
+ "%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.
+
+ base::FilePath path_to_create = system_temp_dir.Append(new_dir_name);
+ if (CreateDirectory(path_to_create.value().c_str(), NULL))
+ return path_to_create;
+ }
+
+ CHECK(false) << "Couldn't find temp dir name";
+ return base::FilePath();
+}
+
+// static
+void ScopedTempDir::RecursivelyDeleteTemporaryDirectory(
+ const base::FilePath& path) {
+ // SHFILEOPSTRUCT wants the path to be terminated with two NULLs,
+ // so we have to use wcscpy because wcscpy_s writes non-NULLs
+ // into the rest of the buffer.
+ 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
+ wcscpy(double_terminated_path, path.value().c_str());
+
+ SHFILEOPSTRUCT file_operation = {0};
+ file_operation.wFunc = FO_DELETE;
+ file_operation.pFrom = double_terminated_path;
+ file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION;
+ 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
+
+ // Since we're passing flags to the operation telling it to be silent,
+ // it's possible for the operation to be aborted/cancelled without err
+ // being set (although MSDN doesn't give any scenarios for how this can
+ // happen). See MSDN for SHFileOperation and SHFILEOPTSTRUCT.
+ EXPECT_FALSE(file_operation.fAnyOperationsAborted);
+
+ // Some versions of Windows return ERROR_FILE_NOT_FOUND (0x2) when deleting
+ // an empty directory and some return 0x402 when they should be returning
+ // ERROR_FILE_NOT_FOUND. MSDN says Vista and up won't return 0x402. Windows 7
+ // can return DE_INVALIDFILES (0x7C) for nonexistent directories.
+ EXPECT_TRUE(err == 0 || err == ERROR_FILE_NOT_FOUND || err == 0x402 ||
+ err == 0x7C);
+}
+
+} // namespace test
+} // namespace crashpad
« 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