| 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 621 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 // directory. | 632 // directory. |
| 633 stat_wrapper_t file_info; | 633 stat_wrapper_t file_info; |
| 634 if (CallStat(real_path_result.value().c_str(), &file_info) != 0 || | 634 if (CallStat(real_path_result.value().c_str(), &file_info) != 0 || |
| 635 S_ISDIR(file_info.st_mode)) | 635 S_ISDIR(file_info.st_mode)) |
| 636 return false; | 636 return false; |
| 637 | 637 |
| 638 *normalized_path = real_path_result; | 638 *normalized_path = real_path_result; |
| 639 return true; | 639 return true; |
| 640 } | 640 } |
| 641 | 641 |
| 642 } // namespace base | |
| 643 | |
| 644 // ----------------------------------------------------------------------------- | |
| 645 | |
| 646 namespace file_util { | |
| 647 | |
| 648 using base::stat_wrapper_t; | |
| 649 using base::CallStat; | |
| 650 using base::CallLstat; | |
| 651 using base::CreateAndOpenFdForTemporaryFile; | |
| 652 using base::DirectoryExists; | |
| 653 using base::FileEnumerator; | |
| 654 using base::FilePath; | |
| 655 using base::MakeAbsoluteFilePath; | |
| 656 using base::VerifySpecificPathControlledByUser; | |
| 657 | |
| 658 base::FilePath MakeUniqueDirectory(const base::FilePath& path) { | |
| 659 const int kMaxAttempts = 20; | |
| 660 for (int attempts = 0; attempts < kMaxAttempts; attempts++) { | |
| 661 int uniquifier = | |
| 662 GetUniquePathNumber(path, base::FilePath::StringType()); | |
| 663 if (uniquifier < 0) | |
| 664 break; | |
| 665 base::FilePath test_path = (uniquifier == 0) ? path : | |
| 666 path.InsertBeforeExtensionASCII( | |
| 667 base::StringPrintf(" (%d)", uniquifier)); | |
| 668 if (mkdir(test_path.value().c_str(), 0777) == 0) | |
| 669 return test_path; | |
| 670 else if (errno != EEXIST) | |
| 671 break; | |
| 672 } | |
| 673 return base::FilePath(); | |
| 674 } | |
| 675 | |
| 676 // TODO(rkc): Refactor GetFileInfo and FileEnumerator to handle symlinks | 642 // TODO(rkc): Refactor GetFileInfo and FileEnumerator to handle symlinks |
| 677 // correctly. http://code.google.com/p/chromium-os/issues/detail?id=15948 | 643 // correctly. http://code.google.com/p/chromium-os/issues/detail?id=15948 |
| 678 bool IsLink(const FilePath& file_path) { | 644 bool IsLink(const FilePath& file_path) { |
| 679 stat_wrapper_t st; | 645 stat_wrapper_t st; |
| 680 // If we can't lstat the file, it's safe to assume that the file won't at | 646 // If we can't lstat the file, it's safe to assume that the file won't at |
| 681 // least be a 'followable' link. | 647 // least be a 'followable' link. |
| 682 if (CallLstat(file_path.value().c_str(), &st) != 0) | 648 if (CallLstat(file_path.value().c_str(), &st) != 0) |
| 683 return false; | 649 return false; |
| 684 | 650 |
| 685 if (S_ISLNK(st.st_mode)) | 651 if (S_ISLNK(st.st_mode)) |
| 686 return true; | 652 return true; |
| 687 else | 653 else |
| 688 return false; | 654 return false; |
| 689 } | 655 } |
| 690 | 656 |
| 691 bool GetFileInfo(const FilePath& file_path, base::PlatformFileInfo* results) { | 657 bool GetFileInfo(const FilePath& file_path, PlatformFileInfo* results) { |
| 692 stat_wrapper_t file_info; | 658 stat_wrapper_t file_info; |
| 693 #if defined(OS_ANDROID) | 659 #if defined(OS_ANDROID) |
| 694 if (file_path.IsContentUri()) { | 660 if (file_path.IsContentUri()) { |
| 695 int fd = OpenContentUriForRead(file_path); | 661 int fd = OpenContentUriForRead(file_path); |
| 696 if (fd < 0) | 662 if (fd < 0) |
| 697 return false; | 663 return false; |
| 698 ScopedFD scoped_fd(&fd); | 664 file_util::ScopedFD scoped_fd(&fd); |
| 699 if (base::CallFstat(fd, &file_info) != 0) | 665 if (CallFstat(fd, &file_info) != 0) |
| 700 return false; | 666 return false; |
| 701 } else { | 667 } else { |
| 702 #endif // defined(OS_ANDROID) | 668 #endif // defined(OS_ANDROID) |
| 703 if (CallStat(file_path.value().c_str(), &file_info) != 0) | 669 if (CallStat(file_path.value().c_str(), &file_info) != 0) |
| 704 return false; | 670 return false; |
| 705 #if defined(OS_ANDROID) | 671 #if defined(OS_ANDROID) |
| 706 } | 672 } |
| 707 #endif // defined(OS_ANDROID) | 673 #endif // defined(OS_ANDROID) |
| 708 results->is_directory = S_ISDIR(file_info.st_mode); | 674 results->is_directory = S_ISDIR(file_info.st_mode); |
| 709 results->size = file_info.st_size; | 675 results->size = file_info.st_size; |
| 710 #if defined(OS_MACOSX) | 676 #if defined(OS_MACOSX) |
| 711 results->last_modified = base::Time::FromTimeSpec(file_info.st_mtimespec); | 677 results->last_modified = Time::FromTimeSpec(file_info.st_mtimespec); |
| 712 results->last_accessed = base::Time::FromTimeSpec(file_info.st_atimespec); | 678 results->last_accessed = Time::FromTimeSpec(file_info.st_atimespec); |
| 713 results->creation_time = base::Time::FromTimeSpec(file_info.st_ctimespec); | 679 results->creation_time = Time::FromTimeSpec(file_info.st_ctimespec); |
| 714 #elif defined(OS_ANDROID) | 680 #elif defined(OS_ANDROID) |
| 715 results->last_modified = base::Time::FromTimeT(file_info.st_mtime); | 681 results->last_modified = Time::FromTimeT(file_info.st_mtime); |
| 716 results->last_accessed = base::Time::FromTimeT(file_info.st_atime); | 682 results->last_accessed = Time::FromTimeT(file_info.st_atime); |
| 717 results->creation_time = base::Time::FromTimeT(file_info.st_ctime); | 683 results->creation_time = Time::FromTimeT(file_info.st_ctime); |
| 718 #else | 684 #else |
| 719 results->last_modified = base::Time::FromTimeSpec(file_info.st_mtim); | 685 results->last_modified = Time::FromTimeSpec(file_info.st_mtim); |
| 720 results->last_accessed = base::Time::FromTimeSpec(file_info.st_atim); | 686 results->last_accessed = Time::FromTimeSpec(file_info.st_atim); |
| 721 results->creation_time = base::Time::FromTimeSpec(file_info.st_ctim); | 687 results->creation_time = Time::FromTimeSpec(file_info.st_ctim); |
| 722 #endif | 688 #endif |
| 723 return true; | 689 return true; |
| 724 } | 690 } |
| 725 | 691 |
| 692 } // namespace base |
| 693 |
| 694 // ----------------------------------------------------------------------------- |
| 695 |
| 696 namespace file_util { |
| 697 |
| 698 using base::stat_wrapper_t; |
| 699 using base::CallStat; |
| 700 using base::CallLstat; |
| 701 using base::CreateAndOpenFdForTemporaryFile; |
| 702 using base::DirectoryExists; |
| 703 using base::FileEnumerator; |
| 704 using base::FilePath; |
| 705 using base::MakeAbsoluteFilePath; |
| 706 using base::VerifySpecificPathControlledByUser; |
| 707 |
| 708 base::FilePath MakeUniqueDirectory(const base::FilePath& path) { |
| 709 const int kMaxAttempts = 20; |
| 710 for (int attempts = 0; attempts < kMaxAttempts; attempts++) { |
| 711 int uniquifier = |
| 712 GetUniquePathNumber(path, base::FilePath::StringType()); |
| 713 if (uniquifier < 0) |
| 714 break; |
| 715 base::FilePath test_path = (uniquifier == 0) ? path : |
| 716 path.InsertBeforeExtensionASCII( |
| 717 base::StringPrintf(" (%d)", uniquifier)); |
| 718 if (mkdir(test_path.value().c_str(), 0777) == 0) |
| 719 return test_path; |
| 720 else if (errno != EEXIST) |
| 721 break; |
| 722 } |
| 723 return base::FilePath(); |
| 724 } |
| 725 |
| 726 bool GetInode(const FilePath& path, ino_t* inode) { | 726 bool GetInode(const FilePath& path, ino_t* inode) { |
| 727 base::ThreadRestrictions::AssertIOAllowed(); // For call to stat(). | 727 base::ThreadRestrictions::AssertIOAllowed(); // For call to stat(). |
| 728 struct stat buffer; | 728 struct stat buffer; |
| 729 int result = stat(path.value().c_str(), &buffer); | 729 int result = stat(path.value().c_str(), &buffer); |
| 730 if (result < 0) | 730 if (result < 0) |
| 731 return false; | 731 return false; |
| 732 | 732 |
| 733 *inode = buffer.st_ino; | 733 *inode = buffer.st_ino; |
| 734 return true; | 734 return true; |
| 735 } | 735 } |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 969 result = false; | 969 result = false; |
| 970 if (IGNORE_EINTR(close(outfile)) < 0) | 970 if (IGNORE_EINTR(close(outfile)) < 0) |
| 971 result = false; | 971 result = false; |
| 972 | 972 |
| 973 return result; | 973 return result; |
| 974 } | 974 } |
| 975 #endif // !defined(OS_MACOSX) | 975 #endif // !defined(OS_MACOSX) |
| 976 | 976 |
| 977 } // namespace internal | 977 } // namespace internal |
| 978 } // namespace base | 978 } // namespace base |
| OLD | NEW |