| 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 <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 |
| 17 static char* SafeStrNCpy(char* dest, const char* src, size_t n) { |
| 18 strncpy(dest, src, n); |
| 19 dest[n - 1] = '\0'; |
| 20 return dest; |
| 21 } |
| 22 |
| 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 |
| 16 // Forward declaration. | 30 // Forward declaration. |
| 17 static bool ListRecursively(const char* dir_name, | 31 static bool ListRecursively(const char* dir_name, |
| 18 bool recursive, | 32 bool recursive, |
| 19 Dart_Port dir_port, | 33 Dart_Port dir_port, |
| 20 Dart_Port file_port, | 34 Dart_Port file_port, |
| 21 Dart_Port done_port, | 35 Dart_Port done_port, |
| 22 Dart_Port error_port); | 36 Dart_Port error_port); |
| 23 | 37 |
| 24 | 38 |
| 25 static void ComputeFullPath(const char* dir_name, | 39 static void ComputeFullPath(const char* dir_name, |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 } | 260 } |
| 247 | 261 |
| 248 | 262 |
| 249 bool Directory::Create(const char* dir_name) { | 263 bool Directory::Create(const char* dir_name) { |
| 250 // Create the directory with the permissions specified by the | 264 // Create the directory with the permissions specified by the |
| 251 // process umask. | 265 // process umask. |
| 252 return (mkdir(dir_name, 0777) == 0); | 266 return (mkdir(dir_name, 0777) == 0); |
| 253 } | 267 } |
| 254 | 268 |
| 255 | 269 |
| 256 char* Directory::CreateTemp(const char* const_template, int64_t number) { | 270 int Directory::CreateTemp(const char* const_template, |
| 271 int64_t number, |
| 272 char** path, |
| 273 char* os_error_message, |
| 274 int os_error_message_len) { |
| 257 // Returns a new, unused directory name, modifying the contents of | 275 // Returns a new, unused directory name, modifying the contents of |
| 258 // dir_template. Creates the directory with the permissions specified | 276 // dir_template. Creates the directory with the permissions specified |
| 259 // by the process umask. | 277 // by the process umask. |
| 260 // The return value must be freed by the caller. | 278 // The return value must be freed by the caller. |
| 261 char* path = static_cast<char*>(malloc(PATH_MAX + 1)); | 279 *path = static_cast<char*>(malloc(PATH_MAX + 1)); |
| 262 strncpy(path, const_template, PATH_MAX + 1); | 280 SafeStrNCpy(*path, const_template, PATH_MAX + 1); |
| 263 path[PATH_MAX] = '\0'; | 281 int path_length = strlen(*path); |
| 264 int path_length = strlen(path); | |
| 265 if (path_length > 0) { | 282 if (path_length > 0) { |
| 266 if (path[path_length - 1] == '/') { | 283 if ((*path)[path_length - 1] == '/') { |
| 267 snprintf(path + path_length, PATH_MAX - path_length, "temp_dir_XXXXXX"); | 284 snprintf(*path + path_length, PATH_MAX - path_length, "temp_dir_XXXXXX"); |
| 268 } else { | 285 } else { |
| 269 snprintf(path + path_length, PATH_MAX - path_length, "XXXXXX"); | 286 snprintf(*path + path_length, PATH_MAX - path_length, "XXXXXX"); |
| 270 } | 287 } |
| 271 } else { | 288 } else { |
| 272 snprintf(path, PATH_MAX, "/tmp/temp_dir1_XXXXXX"); | 289 snprintf(*path, PATH_MAX, "/tmp/temp_dir1_XXXXXX"); |
| 273 } | 290 } |
| 274 char* result = mkdtemp(path); | 291 char* result = mkdtemp(*path); |
| 275 if (result == NULL) { | 292 if (result == NULL) { |
| 276 // Return the empty string, but as a freeable array. | 293 SetOsErrorMessage(os_error_message, os_error_message_len); |
| 277 path[0] = '\0'; | 294 free(*path); |
| 295 *path = NULL; |
| 296 return errno; |
| 278 } | 297 } |
| 279 return path; | 298 return 0; |
| 280 } | 299 } |
| 281 | 300 |
| 282 | 301 |
| 283 bool Directory::Delete(const char* dir_name) { | 302 bool Directory::Delete(const char* dir_name) { |
| 284 return (rmdir(dir_name) == 0); | 303 return (rmdir(dir_name) == 0); |
| 285 } | 304 } |
| OLD | NEW |