| 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 <windows.h> | 7 #include <windows.h> |
| 8 #include <psapi.h> | 8 #include <psapi.h> |
| 9 #include <shellapi.h> | 9 #include <shellapi.h> |
| 10 #include <shlobj.h> | 10 #include <shlobj.h> |
| (...skipping 737 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 if (file_type_ & FileEnumerator::DIRECTORIES) | 748 if (file_type_ & FileEnumerator::DIRECTORIES) |
| 749 return cur_file; | 749 return cur_file; |
| 750 } else if (file_type_ & FileEnumerator::FILES) { | 750 } else if (file_type_ & FileEnumerator::FILES) { |
| 751 return cur_file; | 751 return cur_file; |
| 752 } | 752 } |
| 753 } | 753 } |
| 754 | 754 |
| 755 return FilePath(); | 755 return FilePath(); |
| 756 } | 756 } |
| 757 | 757 |
| 758 /////////////////////////////////////////////// | |
| 759 // MemoryMappedFile | |
| 760 | |
| 761 MemoryMappedFile::MemoryMappedFile() | |
| 762 : file_(INVALID_HANDLE_VALUE), | |
| 763 file_mapping_(INVALID_HANDLE_VALUE), | |
| 764 data_(NULL), | |
| 765 length_(INVALID_FILE_SIZE) { | |
| 766 } | |
| 767 | |
| 768 bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) { | |
| 769 if (IsValid()) | |
| 770 return false; | |
| 771 file_ = base::CreatePlatformFile( | |
| 772 file_name, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | |
| 773 NULL, NULL); | |
| 774 | |
| 775 if (file_ == base::kInvalidPlatformFileValue) { | |
| 776 DLOG(ERROR) << "Couldn't open " << file_name.value(); | |
| 777 return false; | |
| 778 } | |
| 779 | |
| 780 if (!MapFileToMemoryInternalEx(SEC_IMAGE)) { | |
| 781 CloseHandles(); | |
| 782 return false; | |
| 783 } | |
| 784 | |
| 785 return true; | |
| 786 } | |
| 787 | |
| 788 bool MemoryMappedFile::MapFileToMemoryInternal() { | |
| 789 return MapFileToMemoryInternalEx(0); | |
| 790 } | |
| 791 | |
| 792 bool MemoryMappedFile::MapFileToMemoryInternalEx(int flags) { | |
| 793 base::ThreadRestrictions::AssertIOAllowed(); | |
| 794 | |
| 795 if (file_ == INVALID_HANDLE_VALUE) | |
| 796 return false; | |
| 797 | |
| 798 length_ = ::GetFileSize(file_, NULL); | |
| 799 if (length_ == INVALID_FILE_SIZE) | |
| 800 return false; | |
| 801 | |
| 802 file_mapping_ = ::CreateFileMapping(file_, NULL, PAGE_READONLY | flags, | |
| 803 0, 0, NULL); | |
| 804 if (!file_mapping_) { | |
| 805 // According to msdn, system error codes are only reserved up to 15999. | |
| 806 // http://msdn.microsoft.com/en-us/library/ms681381(v=VS.85).aspx. | |
| 807 UMA_HISTOGRAM_ENUMERATION("MemoryMappedFile.CreateFileMapping", | |
| 808 logging::GetLastSystemErrorCode(), 16000); | |
| 809 return false; | |
| 810 } | |
| 811 | |
| 812 data_ = static_cast<uint8*>( | |
| 813 ::MapViewOfFile(file_mapping_, FILE_MAP_READ, 0, 0, 0)); | |
| 814 if (!data_) { | |
| 815 UMA_HISTOGRAM_ENUMERATION("MemoryMappedFile.MapViewOfFile", | |
| 816 logging::GetLastSystemErrorCode(), 16000); | |
| 817 } | |
| 818 return data_ != NULL; | |
| 819 } | |
| 820 | |
| 821 void MemoryMappedFile::CloseHandles() { | |
| 822 if (data_) | |
| 823 ::UnmapViewOfFile(data_); | |
| 824 if (file_mapping_ != INVALID_HANDLE_VALUE) | |
| 825 ::CloseHandle(file_mapping_); | |
| 826 if (file_ != INVALID_HANDLE_VALUE) | |
| 827 ::CloseHandle(file_); | |
| 828 | |
| 829 data_ = NULL; | |
| 830 file_mapping_ = file_ = INVALID_HANDLE_VALUE; | |
| 831 length_ = INVALID_FILE_SIZE; | |
| 832 } | |
| 833 | |
| 834 bool HasFileBeenModifiedSince(const FileEnumerator::FindInfo& find_info, | 758 bool HasFileBeenModifiedSince(const FileEnumerator::FindInfo& find_info, |
| 835 const base::Time& cutoff_time) { | 759 const base::Time& cutoff_time) { |
| 836 base::ThreadRestrictions::AssertIOAllowed(); | 760 base::ThreadRestrictions::AssertIOAllowed(); |
| 837 FILETIME file_time = cutoff_time.ToFileTime(); | 761 FILETIME file_time = cutoff_time.ToFileTime(); |
| 838 long result = CompareFileTime(&find_info.ftLastWriteTime, // NOLINT | 762 long result = CompareFileTime(&find_info.ftLastWriteTime, // NOLINT |
| 839 &file_time); | 763 &file_time); |
| 840 return result == 1 || result == 0; | 764 return result == 1 || result == 0; |
| 841 } | 765 } |
| 842 | 766 |
| 843 bool NormalizeFilePath(const FilePath& path, FilePath* real_path) { | 767 bool NormalizeFilePath(const FilePath& path, FilePath* real_path) { |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 969 | 893 |
| 970 // Length of |path| with path separator appended. | 894 // Length of |path| with path separator appended. |
| 971 size_t prefix = path.StripTrailingSeparators().value().size() + 1; | 895 size_t prefix = path.StripTrailingSeparators().value().size() + 1; |
| 972 // The whole path string must be shorter than MAX_PATH. That is, it must be | 896 // The whole path string must be shorter than MAX_PATH. That is, it must be |
| 973 // prefix + component_length < MAX_PATH (or equivalently, <= MAX_PATH - 1). | 897 // prefix + component_length < MAX_PATH (or equivalently, <= MAX_PATH - 1). |
| 974 int whole_path_limit = std::max(0, MAX_PATH - 1 - static_cast<int>(prefix)); | 898 int whole_path_limit = std::max(0, MAX_PATH - 1 - static_cast<int>(prefix)); |
| 975 return std::min(whole_path_limit, static_cast<int>(max_length)); | 899 return std::min(whole_path_limit, static_cast<int>(max_length)); |
| 976 } | 900 } |
| 977 | 901 |
| 978 } // namespace file_util | 902 } // namespace file_util |
| OLD | NEW |