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. Once Android provied mkdtemp, |
| 399 // remove this function in favor of calling mkdtemp directly. |
| 400 static char* MakeTempDirectory(char* path_template) { |
| 401 if (mktemp(path_template) == NULL) { |
| 402 return NULL; |
| 403 } |
| 404 if (mkdir(path_template, 0700) != 0) { |
| 405 return NULL; |
| 406 } |
| 407 return path_template; |
| 408 } |
| 409 |
| 410 |
390 char* Directory::CreateTemp(const char* const_template) { | 411 char* Directory::CreateTemp(const char* const_template) { |
391 // Returns a new, unused directory name, modifying the contents of | 412 // Returns a new, unused directory name, modifying the contents of |
392 // dir_template. Creates the directory with the permissions specified | 413 // dir_template. Creates the directory with the permissions specified |
393 // by the process umask. | 414 // by the process umask. |
394 // The return value must be freed by the caller. | 415 // The return value must be freed by the caller. |
395 char* path = static_cast<char*>(malloc(PATH_MAX + 1)); | 416 char* path = static_cast<char*>(malloc(PATH_MAX + 1)); |
396 SafeStrNCpy(path, const_template, PATH_MAX + 1); | 417 SafeStrNCpy(path, const_template, PATH_MAX + 1); |
397 int path_length = strlen(path); | 418 int path_length = strlen(path); |
398 if (path_length > 0) { | 419 if (path_length > 0) { |
399 if ((path)[path_length - 1] == '/') { | 420 if ((path)[path_length - 1] == '/') { |
400 snprintf(path + path_length, PATH_MAX - path_length, "temp_dir_XXXXXX"); | 421 snprintf(path + path_length, PATH_MAX - path_length, "temp_dir_XXXXXX"); |
401 } else { | 422 } else { |
402 snprintf(path + path_length, PATH_MAX - path_length, "XXXXXX"); | 423 snprintf(path + path_length, PATH_MAX - path_length, "XXXXXX"); |
403 } | 424 } |
404 } else { | 425 } else { |
405 snprintf(path, PATH_MAX, "/tmp/temp_dir1_XXXXXX"); | 426 // Android does not have a /tmp directory. A partial substitute, |
| 427 // suitable for bring-up work and tests, is to create a tmp |
| 428 // directory in /data/local/tmp. |
| 429 // |
| 430 // TODO(4413): In the long run, when running in an application we should |
| 431 // probably use android.content.Context.getCacheDir(). |
| 432 #define ANDROID_TEMP_DIR "/data/local/tmp" |
| 433 struct stat st; |
| 434 if (stat(ANDROID_TEMP_DIR, &st) != 0) { |
| 435 mkdir(ANDROID_TEMP_DIR, 0777); |
| 436 } |
| 437 snprintf(path, PATH_MAX, ANDROID_TEMP_DIR "/temp_dir1_XXXXXX"); |
406 } | 438 } |
407 char* result; | 439 char* result; |
408 do { | 440 do { |
409 result = mkdtemp(path); | 441 result = MakeTempDirectory(path); |
410 } while (result == NULL && errno == EINTR); | 442 } while (result == NULL && errno == EINTR); |
411 if (result == NULL) { | 443 if (result == NULL) { |
412 free(path); | 444 free(path); |
413 return NULL; | 445 return NULL; |
414 } | 446 } |
415 return path; | 447 return path; |
416 } | 448 } |
417 | 449 |
418 | 450 |
419 bool Directory::Delete(const char* dir_name, bool recursive) { | 451 bool Directory::Delete(const char* dir_name, bool recursive) { |
420 if (!recursive) { | 452 if (!recursive) { |
421 return (TEMP_FAILURE_RETRY(remove(dir_name)) == 0); | 453 return (TEMP_FAILURE_RETRY(remove(dir_name)) == 0); |
422 } else { | 454 } else { |
423 return DeleteRecursively(dir_name); | 455 return DeleteRecursively(dir_name); |
424 } | 456 } |
425 } | 457 } |
426 | 458 |
427 | 459 |
428 bool Directory::Rename(const char* path, const char* new_path) { | 460 bool Directory::Rename(const char* path, const char* new_path) { |
429 ExistsResult exists = Exists(path); | 461 ExistsResult exists = Exists(path); |
430 if (exists != EXISTS) return false; | 462 if (exists != EXISTS) return false; |
431 return (TEMP_FAILURE_RETRY(rename(path, new_path)) == 0); | 463 return (TEMP_FAILURE_RETRY(rename(path, new_path)) == 0); |
432 } | 464 } |
OLD | NEW |