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 "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_ANDROID) | 6 #if defined(TARGET_OS_ANDROID) |
7 | 7 |
8 #include "bin/directory.h" | 8 #include "bin/directory.h" |
9 | 9 |
10 #include <dirent.h> // NOLINT | 10 #include <dirent.h> // NOLINT |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 if (mktemp(path_template) == NULL) { | 375 if (mktemp(path_template) == NULL) { |
376 return NULL; | 376 return NULL; |
377 } | 377 } |
378 if (mkdir(path_template, 0700) != 0) { | 378 if (mkdir(path_template, 0700) != 0) { |
379 return NULL; | 379 return NULL; |
380 } | 380 } |
381 return path_template; | 381 return path_template; |
382 } | 382 } |
383 | 383 |
384 | 384 |
385 char* Directory::CreateTemp(const char* const_template, bool system) { | 385 char* Directory::SystemTemp() { |
386 // Returns a new, unused directory name, modifying the contents of | 386 // Android does not have a /tmp directory. A partial substitute, |
387 // dir_template. Creates the directory with the permissions specified | 387 // suitable for bring-up work and tests, is to create a tmp |
| 388 // directory in /data/local/tmp. |
| 389 // |
| 390 // TODO(4413): In the long run, when running in an application we should |
| 391 // probably use the appropriate directory from the Android API, |
| 392 // probably what File.createTempFile uses. |
| 393 #define ANDROID_TEMP_DIR "/data/local/tmp" |
| 394 struct stat st; |
| 395 if (stat(ANDROID_TEMP_DIR, &st) != 0) { |
| 396 mkdir(ANDROID_TEMP_DIR, 0777); |
| 397 } |
| 398 return strdup(ANDROID_TEMP_DIR); |
| 399 } |
| 400 |
| 401 |
| 402 char* Directory::CreateTemp(const char* prefix) { |
| 403 // Returns a new, unused directory name, adding characters to the end |
| 404 // of prefix. Creates the directory with the permissions specified |
388 // by the process umask. | 405 // by the process umask. |
389 // The return value must be freed by the caller. | 406 // The return value must be freed by the caller. |
390 PathBuffer path; | 407 PathBuffer path; |
391 if (system) { | 408 path.Add(prefix); |
392 // Android does not have a /tmp directory. A partial substitute, | |
393 // suitable for bring-up work and tests, is to create a tmp | |
394 // directory in /data/local/tmp. | |
395 // | |
396 // TODO(4413): In the long run, when running in an application we should | |
397 // probably use the appropriate directory from the Android API, | |
398 // probably what File.createTempFile uses. | |
399 #define ANDROID_TEMP_DIR "/data/local/tmp" | |
400 struct stat st; | |
401 if (stat(ANDROID_TEMP_DIR, &st) != 0) { | |
402 mkdir(ANDROID_TEMP_DIR, 0777); | |
403 } | |
404 path.Add(ANDROID_TEMP_DIR "/"); | |
405 } | |
406 | |
407 path.Add(const_template); | |
408 if (!path.Add("XXXXXX")) { | 409 if (!path.Add("XXXXXX")) { |
409 // Pattern has overflowed. | 410 // Pattern has overflowed. |
410 return NULL; | 411 return NULL; |
411 } | 412 } |
412 | |
413 char* result; | 413 char* result; |
414 do { | 414 do { |
415 result = MakeTempDirectory(path.AsString()); | 415 result = MakeTempDirectory(path.AsString()); |
416 } while (result == NULL && errno == EINTR); | 416 } while (result == NULL && errno == EINTR); |
417 if (result == NULL) { | 417 if (result == NULL) { |
418 return NULL; | 418 return NULL; |
419 } | 419 } |
420 int length = strnlen(path.AsString(), PATH_MAX); | 420 return strdup(result); |
421 result = static_cast<char*>(malloc(length + 1)); | |
422 strncpy(result, path.AsString(), length); | |
423 result[length] = '\0'; | |
424 return result; | |
425 } | 421 } |
426 | 422 |
427 | 423 |
428 bool Directory::Delete(const char* dir_name, bool recursive) { | 424 bool Directory::Delete(const char* dir_name, bool recursive) { |
429 if (!recursive) { | 425 if (!recursive) { |
430 if (File::GetType(dir_name, false) == File::kIsLink && | 426 if (File::GetType(dir_name, false) == File::kIsLink && |
431 File::GetType(dir_name, true) == File::kIsDirectory) { | 427 File::GetType(dir_name, true) == File::kIsDirectory) { |
432 return (TEMP_FAILURE_RETRY(unlink(dir_name)) == 0); | 428 return (TEMP_FAILURE_RETRY(unlink(dir_name)) == 0); |
433 } | 429 } |
434 return (TEMP_FAILURE_RETRY(rmdir(dir_name)) == 0); | 430 return (TEMP_FAILURE_RETRY(rmdir(dir_name)) == 0); |
(...skipping 10 matching lines...) Expand all Loading... |
445 bool Directory::Rename(const char* path, const char* new_path) { | 441 bool Directory::Rename(const char* path, const char* new_path) { |
446 ExistsResult exists = Exists(path); | 442 ExistsResult exists = Exists(path); |
447 if (exists != EXISTS) return false; | 443 if (exists != EXISTS) return false; |
448 return (TEMP_FAILURE_RETRY(rename(path, new_path)) == 0); | 444 return (TEMP_FAILURE_RETRY(rename(path, new_path)) == 0); |
449 } | 445 } |
450 | 446 |
451 } // namespace bin | 447 } // namespace bin |
452 } // namespace dart | 448 } // namespace dart |
453 | 449 |
454 #endif // defined(TARGET_OS_ANDROID) | 450 #endif // defined(TARGET_OS_ANDROID) |
OLD | NEW |