| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bin/directory.h" | 5 #include "bin/directory.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 9 | 9 |
| 10 #include "bin/platform.h" | 10 #include "bin/platform.h" |
| 11 | 11 |
| 12 |
| 13 static int SetOsErrorMessage(char* os_error_message, |
| 14 int os_error_message_len) { |
| 15 int error_code = GetLastError(); |
| 16 DWORD message_size = |
| 17 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 18 NULL, |
| 19 error_code, |
| 20 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 21 os_error_message, |
| 22 os_error_message_len, |
| 23 NULL); |
| 24 if (message_size == 0) { |
| 25 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
| 26 fprintf(stderr, "FormatMessage failed %d\n", GetLastError()); |
| 27 } |
| 28 snprintf(os_error_message, os_error_message_len, "OS Error %d", error_code); |
| 29 } |
| 30 os_error_message[os_error_message_len - 1] = '\0'; |
| 31 return error_code; |
| 32 } |
| 33 |
| 34 |
| 12 // Forward declaration. | 35 // Forward declaration. |
| 13 static bool ListRecursively(const char* dir_name, | 36 static bool ListRecursively(const char* dir_name, |
| 14 bool recursive, | 37 bool recursive, |
| 15 Dart_Port dir_port, | 38 Dart_Port dir_port, |
| 16 Dart_Port file_port, | 39 Dart_Port file_port, |
| 17 Dart_Port done_port, | 40 Dart_Port done_port, |
| 18 Dart_Port error_port); | 41 Dart_Port error_port); |
| 19 | 42 |
| 20 | 43 |
| 21 static bool HandleDir(char* dir_name, | 44 static bool HandleDir(char* dir_name, |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 return DOES_NOT_EXIST; | 254 return DOES_NOT_EXIST; |
| 232 } | 255 } |
| 233 } | 256 } |
| 234 | 257 |
| 235 | 258 |
| 236 bool Directory::Create(const char* dir_name) { | 259 bool Directory::Create(const char* dir_name) { |
| 237 return (CreateDirectory(dir_name, NULL) != 0); | 260 return (CreateDirectory(dir_name, NULL) != 0); |
| 238 } | 261 } |
| 239 | 262 |
| 240 | 263 |
| 241 char* Directory::CreateTemp(const char* const_template, int64_t number) { | 264 int Directory::CreateTemp(const char* const_template, |
| 265 int64_t number, |
| 266 char** path, |
| 267 char* os_error_message, |
| 268 int os_error_message_len) { |
| 242 // Returns a new, unused directory name, modifying the contents of | 269 // Returns a new, unused directory name, modifying the contents of |
| 243 // dir_template. Creates this directory, with a default security | 270 // dir_template. Creates this directory, with a default security |
| 244 // descriptor inherited from its parent directory. | 271 // descriptor inherited from its parent directory. |
| 245 // The return value must be freed by the caller. | 272 // The return value must be freed by the caller. |
| 246 char* path = static_cast<char*>(malloc(MAX_PATH)); | 273 *path = static_cast<char*>(malloc(MAX_PATH)); |
| 247 int path_length; | 274 int path_length; |
| 248 if (0 == strncmp(const_template, "", 1)) { | 275 if (0 == strncmp(const_template, "", 1)) { |
| 249 path_length = GetTempPath(MAX_PATH, path); | 276 path_length = GetTempPath(MAX_PATH, *path); |
| 277 if (path_length == 0) { |
| 278 free(*path); |
| 279 *path = NULL; |
| 280 int error_code = |
| 281 SetOsErrorMessage(os_error_message, os_error_message_len); |
| 282 return error_code; |
| 283 } |
| 250 } else { | 284 } else { |
| 251 snprintf(path, MAX_PATH, "%s", const_template); | 285 snprintf(*path, MAX_PATH, "%s", const_template); |
| 252 path_length = strlen(path); | 286 path_length = strlen(*path); |
| 253 } | 287 } |
| 254 if (path_length > MAX_PATH - 14) { | 288 if (path_length > MAX_PATH - 14) { |
| 255 path[0] = '\0'; | 289 free(*path); |
| 256 return path; | 290 *path = NULL; |
| 291 return -1; |
| 257 } | 292 } |
| 258 if (path[path_length - 1] == '\\') { | 293 if ((*path)[path_length - 1] == '\\') { |
| 259 // No base name for the directory - use "tempdir" | 294 // No base name for the directory - use "tempdir" |
| 260 snprintf(path + path_length, MAX_PATH - path_length, "tempdir"); | 295 snprintf(*path + path_length, MAX_PATH - path_length, "tempdir"); |
| 261 path_length = strlen(path); | 296 path_length = strlen(*path); |
| 262 } | 297 } |
| 263 | 298 |
| 264 int tries = 0; | 299 int tries = 0; |
| 265 int numeric_part = number % 1000000; | 300 int numeric_part = number % 1000000; |
| 266 while (true) { | 301 while (true) { |
| 267 snprintf(path + path_length, MAX_PATH - path_length, "%.6d", numeric_part); | 302 snprintf(*path + path_length, MAX_PATH - path_length, "%.6d", numeric_part); |
| 268 if (CreateDirectory(path, NULL)) break; | 303 if (CreateDirectory(*path, NULL)) break; |
| 269 numeric_part++; | 304 numeric_part++; |
| 270 tries++; | 305 tries++; |
| 271 if (tries > 100) { | 306 if (tries > 100) { |
| 272 path[0] = '\0'; | 307 free(*path); |
| 273 break; | 308 *path = NULL; |
| 309 int error_code = |
| 310 SetOsErrorMessage(os_error_message, os_error_message_len); |
| 311 return error_code; |
| 274 } | 312 } |
| 275 } | 313 } |
| 276 return path; | 314 return 0; |
| 277 } | 315 } |
| 278 | 316 |
| 279 | 317 |
| 280 bool Directory::Delete(const char* dir_name) { | 318 bool Directory::Delete(const char* dir_name) { |
| 281 return (RemoveDirectory(dir_name) != 0); | 319 return (RemoveDirectory(dir_name) != 0); |
| 282 } | 320 } |
| OLD | NEW |