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

Unified Diff: base/test/launcher/test_launcher.cc

Issue 2663183003: Make sure tests delete temp files on Windows (Closed)
Patch Set: Created 3 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/test/launcher/test_launcher.cc
diff --git a/base/test/launcher/test_launcher.cc b/base/test/launcher/test_launcher.cc
index 2f22d32d50a4dba63b902aa4006e94a22db08071..b6c4d0c87807b7dcd16a86b38324e3b4a6eb5641 100644
--- a/base/test/launcher/test_launcher.cc
+++ b/base/test/launcher/test_launcher.cc
@@ -57,6 +57,7 @@
#endif
#if defined(OS_WIN)
+#include <limits>
Paweł Hajdan Jr. 2017/02/01 11:10:48 nit: Include system headers before chromium header
#include "base/win/windows_version.h"
#endif
@@ -170,6 +171,53 @@ void OnShutdownPipeReadable() {
}
#endif // defined(OS_POSIX)
+#if defined(OS_WIN)
+// Does the same as base::ReadFileToString() but uses FILE_SHARE_DELETE flag
Paweł Hajdan Jr. 2017/02/01 11:10:48 I'm worried about duplication here. Actually from
Tomasz Moniuszko 2017/02/02 16:16:43 I tried to add the handle to temp file to |handles
grt (UTC plus 2) 2017/02/03 09:53:23 I wrote that in the bug before realizing that spec
+// when opening file.
+bool ReadFileToStringShareDelete(const FilePath& path, std::string* contents) {
+ if (contents)
+ contents->clear();
+ if (path.ReferencesParent())
+ return false;
+
+ win::ScopedHandle handle(
+ CreateFile(path.value().c_str(), GENERIC_READ,
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
+ OPEN_EXISTING, FILE_ATTRIBUTE_TEMPORARY, NULL));
+ if (!handle.IsValid())
+ return false;
+
+ const size_t max_size = std::numeric_limits<size_t>::max();
+ const size_t kBufferSize = 1 << 16;
+ std::unique_ptr<char[]> buf(new char[kBufferSize]);
+ DWORD len;
+ size_t size = 0;
+ bool read_status = true;
+
+ // Many files supplied in |path| have incorrect size (proc files etc).
+ // Hence, the file is read sequentially as opposed to a one-shot read.
+ do {
+ read_status =
+ !!::ReadFile(handle.Get(), buf.get(), kBufferSize, &len, NULL);
+ if (!read_status || len <= 0)
+ break;
+ if (contents) {
+ contents->append(buf.get(),
+ std::min(static_cast<size_t>(len), max_size - size));
+ }
+
+ if ((max_size - size) < len) {
+ read_status = false;
+ break;
+ }
+
+ size += len;
+ } while (true);
+
+ return read_status;
+}
+#endif // OS_WIN
+
// Parses the environment variable var as an Int32. If it is unset, returns
// true. If it is set, unsets it then converts it to Int32 before
// returning it in |result|. Returns true on success.
@@ -453,12 +501,13 @@ void DoLaunchChildTestProcess(
}
std::string output_file_contents;
+#if defined(OS_WIN)
+ CHECK(ReadFileToStringShareDelete(output_file, &output_file_contents));
+#else
CHECK(ReadFileToString(output_file, &output_file_contents));
+#endif
- if (!DeleteFile(output_file, false)) {
- // This needs to be non-fatal at least for Windows.
- LOG(WARNING) << "Failed to delete " << output_file.AsUTF8Unsafe();
- }
+ CHECK(DeleteFile(output_file, false));
// Run target callback on the thread it was originating from, not on
// a worker pool thread.
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698