Chromium Code Reviews| 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 strncpy(*path, const_template, PATH_MAX + 1); |
|
Bill Hesse
2011/12/13 13:51:03
Use SafeStrNCpy here, or remove SafeStrNCpy.
Søren Gjesse
2011/12/13 14:07:40
Done.
| |
| 263 path[PATH_MAX] = '\0'; | 281 (*path)[PATH_MAX] = '\0'; |
| 264 int path_length = strlen(path); | 282 int path_length = strlen(*path); |
| 265 if (path_length > 0) { | 283 if (path_length > 0) { |
| 266 if (path[path_length - 1] == '/') { | 284 if ((*path)[path_length - 1] == '/') { |
| 267 snprintf(path + path_length, PATH_MAX - path_length, "temp_dir_XXXXXX"); | 285 snprintf(*path + path_length, PATH_MAX - path_length, "temp_dir_XXXXXX"); |
| 268 } else { | 286 } else { |
| 269 snprintf(path + path_length, PATH_MAX - path_length, "XXXXXX"); | 287 snprintf(*path + path_length, PATH_MAX - path_length, "XXXXXX"); |
| 270 } | 288 } |
| 271 } else { | 289 } else { |
| 272 snprintf(path, PATH_MAX, "/tmp/temp_dir1_XXXXXX"); | 290 snprintf(*path, PATH_MAX, "/tmp/temp_dir1_XXXXXX"); |
| 273 } | 291 } |
| 274 char* result = mkdtemp(path); | 292 char* result = mkdtemp(*path); |
| 275 if (result == NULL) { | 293 if (result == NULL) { |
| 276 // Return the empty string, but as a freeable array. | 294 SetOsErrorMessage(os_error_message, os_error_message_len); |
| 277 path[0] = '\0'; | 295 free(*path); |
| 296 *path = NULL; | |
| 297 return errno; | |
| 278 } | 298 } |
| 279 return path; | 299 return 0; |
| 280 } | 300 } |
| 281 | 301 |
| 282 | 302 |
| 283 bool Directory::Delete(const char* dir_name) { | 303 bool Directory::Delete(const char* dir_name) { |
| 284 return (rmdir(dir_name) == 0); | 304 return (rmdir(dir_name) == 0); |
| 285 } | 305 } |
| OLD | NEW |