OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 | 6 |
7 #include <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <libgen.h> | 10 #include <libgen.h> |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 #if defined(OS_ANDROID) | 456 #if defined(OS_ANDROID) |
457 return PathService::Get(base::DIR_CACHE, path); | 457 return PathService::Get(base::DIR_CACHE, path); |
458 #else | 458 #else |
459 *path = FilePath("/tmp"); | 459 *path = FilePath("/tmp"); |
460 #endif | 460 #endif |
461 } | 461 } |
462 return true; | 462 return true; |
463 } | 463 } |
464 #endif // !defined(OS_MACOSX) | 464 #endif // !defined(OS_MACOSX) |
465 | 465 |
466 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | |
467 // This is implemented in file_util_mac.mm and file_util_android.cc for those | |
468 // platforms. | |
469 bool GetShmemTempDir(bool executable, FilePath* path) { | |
470 #if defined(OS_LINUX) | |
471 bool use_dev_shm = true; | |
472 if (executable) { | |
473 static const bool s_dev_shm_executable = DetermineDevShmExecutable(); | |
474 use_dev_shm = s_dev_shm_executable; | |
475 } | |
476 if (use_dev_shm) { | |
477 *path = FilePath("/dev/shm"); | |
478 return true; | |
479 } | |
480 #endif | |
481 return GetTempDir(path); | |
482 } | |
483 #endif // !defined(OS_MACOSX) && !defined(OS_ANDROID) | |
484 | |
485 #if !defined(OS_MACOSX) // Mac implementation is in file_util_mac.mm. | 466 #if !defined(OS_MACOSX) // Mac implementation is in file_util_mac.mm. |
486 FilePath GetHomeDir() { | 467 FilePath GetHomeDir() { |
487 #if defined(OS_CHROMEOS) | 468 #if defined(OS_CHROMEOS) |
488 if (SysInfo::IsRunningOnChromeOS()) | 469 if (SysInfo::IsRunningOnChromeOS()) |
489 return FilePath("/home/chronos/user"); | 470 return FilePath("/home/chronos/user"); |
490 #endif | 471 #endif |
491 | 472 |
492 const char* home_dir = getenv("HOME"); | 473 const char* home_dir = getenv("HOME"); |
493 if (home_dir && home_dir[0]) | 474 if (home_dir && home_dir[0]) |
494 return FilePath(home_dir); | 475 return FilePath(home_dir); |
(...skipping 26 matching lines...) Expand all Loading... |
521 FilePath directory; | 502 FilePath directory; |
522 if (!GetTempDir(&directory)) | 503 if (!GetTempDir(&directory)) |
523 return false; | 504 return false; |
524 int fd = CreateAndOpenFdForTemporaryFile(directory, path); | 505 int fd = CreateAndOpenFdForTemporaryFile(directory, path); |
525 if (fd < 0) | 506 if (fd < 0) |
526 return false; | 507 return false; |
527 close(fd); | 508 close(fd); |
528 return true; | 509 return true; |
529 } | 510 } |
530 | 511 |
531 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path, bool executable) { | |
532 FilePath directory; | |
533 if (!GetShmemTempDir(executable, &directory)) | |
534 return NULL; | |
535 | |
536 return CreateAndOpenTemporaryFileInDir(directory, path); | |
537 } | |
538 | |
539 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { | 512 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { |
540 int fd = CreateAndOpenFdForTemporaryFile(dir, path); | 513 int fd = CreateAndOpenFdForTemporaryFile(dir, path); |
541 if (fd < 0) | 514 if (fd < 0) |
542 return NULL; | 515 return NULL; |
543 | 516 |
544 FILE* file = fdopen(fd, "a+"); | 517 FILE* file = fdopen(fd, "a+"); |
545 if (!file) | 518 if (!file) |
546 close(fd); | 519 close(fd); |
547 return file; | 520 return file; |
548 } | 521 } |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
840 return VerifyPathControlledByUser( | 813 return VerifyPathControlledByUser( |
841 kFileSystemRoot, path, kRootUid, allowed_group_ids); | 814 kFileSystemRoot, path, kRootUid, allowed_group_ids); |
842 } | 815 } |
843 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 816 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
844 | 817 |
845 int GetMaximumPathComponentLength(const FilePath& path) { | 818 int GetMaximumPathComponentLength(const FilePath& path) { |
846 ThreadRestrictions::AssertIOAllowed(); | 819 ThreadRestrictions::AssertIOAllowed(); |
847 return pathconf(path.value().c_str(), _PC_NAME_MAX); | 820 return pathconf(path.value().c_str(), _PC_NAME_MAX); |
848 } | 821 } |
849 | 822 |
| 823 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 824 // This is implemented in file_util_mac.mm and file_util_android.cc for those |
| 825 // platforms. |
| 826 bool GetShmemTempDir(bool executable, FilePath* path) { |
| 827 #if defined(OS_LINUX) |
| 828 bool use_dev_shm = true; |
| 829 if (executable) { |
| 830 static const bool s_dev_shm_executable = DetermineDevShmExecutable(); |
| 831 use_dev_shm = s_dev_shm_executable; |
| 832 } |
| 833 if (use_dev_shm) { |
| 834 *path = FilePath("/dev/shm"); |
| 835 return true; |
| 836 } |
| 837 #endif |
| 838 return GetTempDir(path); |
| 839 } |
| 840 #endif // !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 841 |
| 842 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path, bool executable) { |
| 843 FilePath directory; |
| 844 if (!GetShmemTempDir(executable, &directory)) |
| 845 return NULL; |
| 846 |
| 847 return CreateAndOpenTemporaryFileInDir(directory, path); |
| 848 } |
| 849 |
850 // ----------------------------------------------------------------------------- | 850 // ----------------------------------------------------------------------------- |
851 | 851 |
852 namespace internal { | 852 namespace internal { |
853 | 853 |
854 bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) { | 854 bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) { |
855 ThreadRestrictions::AssertIOAllowed(); | 855 ThreadRestrictions::AssertIOAllowed(); |
856 // Windows compatibility: if to_path exists, from_path and to_path | 856 // Windows compatibility: if to_path exists, from_path and to_path |
857 // must be the same type, either both files, or both directories. | 857 // must be the same type, either both files, or both directories. |
858 stat_wrapper_t to_file_info; | 858 stat_wrapper_t to_file_info; |
859 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) { | 859 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
921 result = false; | 921 result = false; |
922 if (IGNORE_EINTR(close(outfile)) < 0) | 922 if (IGNORE_EINTR(close(outfile)) < 0) |
923 result = false; | 923 result = false; |
924 | 924 |
925 return result; | 925 return result; |
926 } | 926 } |
927 #endif // !defined(OS_MACOSX) | 927 #endif // !defined(OS_MACOSX) |
928 | 928 |
929 } // namespace internal | 929 } // namespace internal |
930 } // namespace base | 930 } // namespace base |
OLD | NEW |