| 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 <string.h> | 9 #include <string.h> |
| 10 #include <sys/param.h> | 10 #include <sys/param.h> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 char data[PATH_MAX + 1]; | 23 char data[PATH_MAX + 1]; |
| 24 int length; | 24 int length; |
| 25 | 25 |
| 26 bool Add(const char* name) { | 26 bool Add(const char* name) { |
| 27 size_t written = snprintf(data + length, | 27 size_t written = snprintf(data + length, |
| 28 PATH_MAX - length, | 28 PATH_MAX - length, |
| 29 "%s", | 29 "%s", |
| 30 name); | 30 name); |
| 31 data[PATH_MAX] = '\0'; | 31 data[PATH_MAX] = '\0'; |
| 32 if (written == strnlen(name, PATH_MAX + 1)) { | 32 if (written == strlen(name)) { |
| 33 length += written; | 33 length += written; |
| 34 return true; | 34 return true; |
| 35 } else { | 35 } else { |
| 36 errno = ENAMETOOLONG; | 36 errno = ENAMETOOLONG; |
| 37 return false; | 37 return false; |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 | 40 |
| 41 void Reset(int new_length) { | 41 void Reset(int new_length) { |
| 42 length = new_length; | 42 length = new_length; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 62 static PathBuffer* ComputeFullPath(const char* dir_name) { | 62 static PathBuffer* ComputeFullPath(const char* dir_name) { |
| 63 PathBuffer* path = new PathBuffer(); | 63 PathBuffer* path = new PathBuffer(); |
| 64 char* abs_path; | 64 char* abs_path; |
| 65 do { | 65 do { |
| 66 abs_path = realpath(dir_name, path->data); | 66 abs_path = realpath(dir_name, path->data); |
| 67 } while (abs_path == NULL && errno == EINTR); | 67 } while (abs_path == NULL && errno == EINTR); |
| 68 if (abs_path == NULL) { | 68 if (abs_path == NULL) { |
| 69 delete path; | 69 delete path; |
| 70 return NULL; | 70 return NULL; |
| 71 } | 71 } |
| 72 path->length = strnlen(path->data, PATH_MAX); | 72 path->length = strlen(path->data); |
| 73 if (path->Add(File::PathSeparator())) { | 73 if (path->Add(File::PathSeparator())) { |
| 74 return path; | 74 return path; |
| 75 } else { | 75 } else { |
| 76 delete path; | 76 delete path; |
| 77 return NULL; | 77 return NULL; |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 | 80 |
| 81 static bool HandleDir(char* dir_name, | 81 static bool HandleDir(char* dir_name, |
| 82 PathBuffer* path, | 82 PathBuffer* path, |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 return NULL; | 377 return NULL; |
| 378 } | 378 } |
| 379 char* result; | 379 char* result; |
| 380 do { | 380 do { |
| 381 result = mkdtemp(path->data); | 381 result = mkdtemp(path->data); |
| 382 } while (result == NULL && errno == EINTR); | 382 } while (result == NULL && errno == EINTR); |
| 383 if (result == NULL) { | 383 if (result == NULL) { |
| 384 delete path; | 384 delete path; |
| 385 return NULL; | 385 return NULL; |
| 386 } | 386 } |
| 387 int length = strnlen(path->data, PATH_MAX); | 387 int length = strlen(path->data); |
| 388 result = static_cast<char*>(malloc(length + 1)); | 388 result = static_cast<char*>(malloc(length + 1)); |
| 389 strncpy(result, path->data, length); | 389 strncpy(result, path->data, length); |
| 390 result[length] = '\0'; | 390 result[length] = '\0'; |
| 391 delete path; | 391 delete path; |
| 392 return result; | 392 return result; |
| 393 } | 393 } |
| 394 | 394 |
| 395 | 395 |
| 396 bool Directory::Delete(const char* dir_name, bool recursive) { | 396 bool Directory::Delete(const char* dir_name, bool recursive) { |
| 397 if (!recursive) { | 397 if (!recursive) { |
| 398 return (TEMP_FAILURE_RETRY(remove(dir_name)) == 0); | 398 return (TEMP_FAILURE_RETRY(remove(dir_name)) == 0); |
| 399 } else { | 399 } else { |
| 400 return DeleteRecursively(dir_name); | 400 return DeleteRecursively(dir_name); |
| 401 } | 401 } |
| 402 } | 402 } |
| 403 | 403 |
| 404 | 404 |
| 405 bool Directory::Rename(const char* path, const char* new_path) { | 405 bool Directory::Rename(const char* path, const char* new_path) { |
| 406 ExistsResult exists = Exists(path); | 406 ExistsResult exists = Exists(path); |
| 407 if (exists != EXISTS) return false; | 407 if (exists != EXISTS) return false; |
| 408 return (TEMP_FAILURE_RETRY(rename(path, new_path)) == 0); | 408 return (TEMP_FAILURE_RETRY(rename(path, new_path)) == 0); |
| 409 } | 409 } |
| OLD | NEW |