OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <sys/param.h> | 9 #include <sys/param.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 | 12 |
13 #include "bin/file.h" | 13 #include "bin/file.h" |
14 #include "bin/platform.h" | 14 #include "bin/platform.h" |
15 | 15 |
16 | 16 |
17 static char* SafeStrNCpy(char* dest, const char* src, size_t n) { | 17 static char* SafeStrNCpy(char* dest, const char* src, size_t n) { |
18 strncpy(dest, src, n); | 18 strncpy(dest, src, n); |
19 dest[n - 1] = '\0'; | 19 dest[n - 1] = '\0'; |
20 return dest; | 20 return dest; |
21 } | 21 } |
22 | 22 |
23 | 23 |
24 static void SetOsErrorMessage(char* os_error_message, | |
25 int os_error_message_len) { | |
26 SafeStrNCpy(os_error_message, strerror(errno), os_error_message_len); | |
27 } | |
28 | |
29 | |
30 // Forward declarations. | 24 // Forward declarations. |
31 static bool ListRecursively(const char* dir_name, | 25 static bool ListRecursively(const char* dir_name, |
32 bool recursive, | 26 bool recursive, |
33 DirectoryListing* listing); | 27 DirectoryListing* listing); |
34 static bool DeleteRecursively(const char* dir_name); | 28 static bool DeleteRecursively(const char* dir_name); |
35 | 29 |
36 | 30 |
37 static bool ComputeFullPath(const char* dir_name, | 31 static bool ComputeFullPath(const char* dir_name, |
38 char* path, | 32 char* path, |
39 int* path_length) { | 33 int* path_length) { |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 } | 376 } |
383 | 377 |
384 | 378 |
385 bool Directory::Create(const char* dir_name) { | 379 bool Directory::Create(const char* dir_name) { |
386 // Create the directory with the permissions specified by the | 380 // Create the directory with the permissions specified by the |
387 // process umask. | 381 // process umask. |
388 return (TEMP_FAILURE_RETRY(mkdir(dir_name, 0777)) == 0); | 382 return (TEMP_FAILURE_RETRY(mkdir(dir_name, 0777)) == 0); |
389 } | 383 } |
390 | 384 |
391 | 385 |
392 int Directory::CreateTemp(const char* const_template, | 386 char* Directory::CreateTemp(const char* const_template) { |
393 char** path, | |
394 char* os_error_message, | |
395 int os_error_message_len) { | |
396 // Returns a new, unused directory name, modifying the contents of | 387 // Returns a new, unused directory name, modifying the contents of |
397 // dir_template. Creates the directory with the permissions specified | 388 // dir_template. Creates the directory with the permissions specified |
398 // by the process umask. | 389 // by the process umask. |
399 // The return value must be freed by the caller. | 390 // The return value must be freed by the caller. |
400 *path = static_cast<char*>(malloc(PATH_MAX + 1)); | 391 char* path = static_cast<char*>(malloc(PATH_MAX + 1)); |
401 SafeStrNCpy(*path, const_template, PATH_MAX + 1); | 392 SafeStrNCpy(path, const_template, PATH_MAX + 1); |
402 int path_length = strlen(*path); | 393 int path_length = strlen(path); |
403 if (path_length > 0) { | 394 if (path_length > 0) { |
404 if ((*path)[path_length - 1] == '/') { | 395 if ((path)[path_length - 1] == '/') { |
405 snprintf(*path + path_length, PATH_MAX - path_length, "temp_dir_XXXXXX"); | 396 snprintf(path + path_length, PATH_MAX - path_length, "temp_dir_XXXXXX"); |
406 } else { | 397 } else { |
407 snprintf(*path + path_length, PATH_MAX - path_length, "XXXXXX"); | 398 snprintf(path + path_length, PATH_MAX - path_length, "XXXXXX"); |
408 } | 399 } |
409 } else { | 400 } else { |
410 snprintf(*path, PATH_MAX, "/tmp/temp_dir1_XXXXXX"); | 401 snprintf(path, PATH_MAX, "/tmp/temp_dir1_XXXXXX"); |
411 } | 402 } |
412 char* result; | 403 char* result; |
413 do { | 404 do { |
414 result = mkdtemp(*path); | 405 result = mkdtemp(path); |
415 } while (result == NULL && errno == EINTR); | 406 } while (result == NULL && errno == EINTR); |
416 if (result == NULL) { | 407 if (result == NULL) { |
417 SetOsErrorMessage(os_error_message, os_error_message_len); | 408 free(path); |
418 free(*path); | 409 return NULL; |
419 *path = NULL; | |
420 return errno; | |
421 } | 410 } |
422 return 0; | 411 return path; |
423 } | 412 } |
424 | 413 |
425 | 414 |
426 bool Directory::Delete(const char* dir_name, bool recursive) { | 415 bool Directory::Delete(const char* dir_name, bool recursive) { |
427 if (!recursive) { | 416 if (!recursive) { |
428 return (TEMP_FAILURE_RETRY(remove(dir_name)) == 0); | 417 return (TEMP_FAILURE_RETRY(remove(dir_name)) == 0); |
429 } else { | 418 } else { |
430 return DeleteRecursively(dir_name); | 419 return DeleteRecursively(dir_name); |
431 } | 420 } |
432 } | 421 } |
OLD | NEW |