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

Unified Diff: runtime/bin/directory_win.cc

Issue 8588029: Add Directory.createTemp() to asynchronously create a temporary directory. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Use snprintf. Created 9 years, 1 month 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
Index: runtime/bin/directory_win.cc
diff --git a/runtime/bin/directory_win.cc b/runtime/bin/directory_win.cc
index 0f48408adc3cc71788df0c547de0dc2c7fb1ebc4..a4d32281fcd25c0c7bc20f52f2db44196c02e3c8 100644
--- a/runtime/bin/directory_win.cc
+++ b/runtime/bin/directory_win.cc
@@ -5,6 +5,7 @@
#include "bin/directory.h"
#include <errno.h>
+#include <time.h>
Mads Ager (google) 2011/11/21 16:29:37 I don't think you need time.h now?
Bill Hesse 2011/11/22 12:49:39 Done.
#include <sys/stat.h>
#include "bin/platform.h"
@@ -238,6 +239,44 @@ bool Directory::Create(const char* dir_name) {
}
+char* Directory::CreateTemp(const char* const_template, int64_t number) {
+ // Returns a new, unused directory name, modifying the contents of
+ // dir_template. Creates this directory, with mode ???.
Mads Ager (google) 2011/11/21 16:29:37 Please fill in the mode part of the comments.
Bill Hesse 2011/11/22 12:49:39 Done.
+ // The return value must be freed by the caller.
+ char* path = static_cast<char*>(malloc(MAX_PATH));
+ int path_length;
+ if (0 == strncmp(const_template, "", 1)) {
+ path_length = GetTempPath(MAX_PATH, path);
+ } else {
+ snprintf(path, MAX_PATH, "%s", const_template);
+ path_length = strlen(path);
+ }
+ if (path_length > MAX_PATH - 14) {
+ path[0] = '\0';
+ return path;
+ }
+ if (path[path_length - 1] == '\\') {
+ // No base name for the directory - use "tempdir"
+ snprintf(path + path_length, MAX_PATH - path_length, "tempdir");
+ path_length = strlen(path);
+ }
+
+ int tries = 0;
+ int numeric_part = number % 1000000;
+ while (true) {
+ snprintf(path + path_length, MAX_PATH - path_length, "%.6d", numeric_part);
+ if (CreateDirectory(path, NULL)) break;
+ numeric_part++;
+ tries++;
+ if (tries > 100) {
+ path[0] = '\0';
+ break;
+ }
+ }
+ return path;
+}
+
+
bool Directory::Delete(const char* dir_name) {
return (RemoveDirectory(dir_name) != 0);
}

Powered by Google App Engine
This is Rietveld 408576698