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> |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 ASSERT(errno == ELOOP || | 369 ASSERT(errno == ELOOP || |
370 errno == ENAMETOOLONG || | 370 errno == ENAMETOOLONG || |
371 errno == ENOENT || | 371 errno == ENOENT || |
372 errno == ENOTDIR); | 372 errno == ENOTDIR); |
373 return DOES_NOT_EXIST; | 373 return DOES_NOT_EXIST; |
374 } | 374 } |
375 } | 375 } |
376 | 376 |
377 | 377 |
378 char* Directory::Current() { | 378 char* Directory::Current() { |
379 return getcwd(NULL, 0); | 379 // Android's getcwd adheres closely to the POSIX standard. It won't |
380 // allocate memory. We need to make our own copy. | |
381 | |
382 char buffer[PATH_MAX]; | |
383 if (NULL == getcwd(buffer, PATH_MAX)) { | |
384 return NULL; | |
385 } | |
386 | |
387 return strdup(buffer); | |
380 } | 388 } |
381 | 389 |
382 | 390 |
383 bool Directory::Create(const char* dir_name) { | 391 bool Directory::Create(const char* dir_name) { |
384 // Create the directory with the permissions specified by the | 392 // Create the directory with the permissions specified by the |
385 // process umask. | 393 // process umask. |
386 return (TEMP_FAILURE_RETRY(mkdir(dir_name, 0777)) == 0); | 394 return (TEMP_FAILURE_RETRY(mkdir(dir_name, 0777)) == 0); |
387 } | 395 } |
388 | 396 |
389 | 397 |
398 // Android doesn't currently provide mkdtemp | |
399 static char* Mkdtemp(char* _template) { | |
cshapiro
2012/08/07 22:39:40
Sorry I missed the first time but _template should
jackpal
2012/08/08 00:26:19
Done.
| |
400 if (mktemp(_template) == NULL) { | |
401 return NULL; | |
402 } | |
403 if (mkdir(_template, 0700) != 0) { | |
404 return NULL; | |
405 } | |
406 return _template; | |
407 } | |
408 | |
409 | |
390 char* Directory::CreateTemp(const char* const_template) { | 410 char* Directory::CreateTemp(const char* const_template) { |
391 // Returns a new, unused directory name, modifying the contents of | 411 // Returns a new, unused directory name, modifying the contents of |
392 // dir_template. Creates the directory with the permissions specified | 412 // dir_template. Creates the directory with the permissions specified |
393 // by the process umask. | 413 // by the process umask. |
394 // The return value must be freed by the caller. | 414 // The return value must be freed by the caller. |
395 char* path = static_cast<char*>(malloc(PATH_MAX + 1)); | 415 char* path = static_cast<char*>(malloc(PATH_MAX + 1)); |
396 SafeStrNCpy(path, const_template, PATH_MAX + 1); | 416 SafeStrNCpy(path, const_template, PATH_MAX + 1); |
397 int path_length = strlen(path); | 417 int path_length = strlen(path); |
398 if (path_length > 0) { | 418 if (path_length > 0) { |
399 if ((path)[path_length - 1] == '/') { | 419 if ((path)[path_length - 1] == '/') { |
400 snprintf(path + path_length, PATH_MAX - path_length, "temp_dir_XXXXXX"); | 420 snprintf(path + path_length, PATH_MAX - path_length, "temp_dir_XXXXXX"); |
401 } else { | 421 } else { |
402 snprintf(path + path_length, PATH_MAX - path_length, "XXXXXX"); | 422 snprintf(path + path_length, PATH_MAX - path_length, "XXXXXX"); |
403 } | 423 } |
404 } else { | 424 } else { |
405 snprintf(path, PATH_MAX, "/tmp/temp_dir1_XXXXXX"); | 425 snprintf(path, PATH_MAX, "/tmp/temp_dir1_XXXXXX"); |
406 } | 426 } |
407 char* result; | 427 char* result; |
408 do { | 428 do { |
409 result = mkdtemp(path); | 429 result = Mkdtemp(path); |
410 } while (result == NULL && errno == EINTR); | 430 } while (result == NULL && errno == EINTR); |
411 if (result == NULL) { | 431 if (result == NULL) { |
412 free(path); | 432 free(path); |
413 return NULL; | 433 return NULL; |
414 } | 434 } |
415 return path; | 435 return path; |
416 } | 436 } |
417 | 437 |
418 | 438 |
419 bool Directory::Delete(const char* dir_name, bool recursive) { | 439 bool Directory::Delete(const char* dir_name, bool recursive) { |
420 if (!recursive) { | 440 if (!recursive) { |
421 return (TEMP_FAILURE_RETRY(remove(dir_name)) == 0); | 441 return (TEMP_FAILURE_RETRY(remove(dir_name)) == 0); |
422 } else { | 442 } else { |
423 return DeleteRecursively(dir_name); | 443 return DeleteRecursively(dir_name); |
424 } | 444 } |
425 } | 445 } |
426 | 446 |
427 | 447 |
428 bool Directory::Rename(const char* path, const char* new_path) { | 448 bool Directory::Rename(const char* path, const char* new_path) { |
429 ExistsResult exists = Exists(path); | 449 ExistsResult exists = Exists(path); |
430 if (exists != EXISTS) return false; | 450 if (exists != EXISTS) return false; |
431 return (TEMP_FAILURE_RETRY(rename(path, new_path)) == 0); | 451 return (TEMP_FAILURE_RETRY(rename(path, new_path)) == 0); |
432 } | 452 } |
OLD | NEW |