OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <shellapi.h> | 8 #include <shellapi.h> |
9 #include <shlobj.h> | 9 #include <shlobj.h> |
10 #include <time.h> | 10 #include <time.h> |
(...skipping 686 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 /////////////////////////////////////////////// | 697 /////////////////////////////////////////////// |
698 // MemoryMappedFile | 698 // MemoryMappedFile |
699 | 699 |
700 MemoryMappedFile::MemoryMappedFile() | 700 MemoryMappedFile::MemoryMappedFile() |
701 : file_(INVALID_HANDLE_VALUE), | 701 : file_(INVALID_HANDLE_VALUE), |
702 file_mapping_(INVALID_HANDLE_VALUE), | 702 file_mapping_(INVALID_HANDLE_VALUE), |
703 data_(NULL), | 703 data_(NULL), |
704 length_(INVALID_FILE_SIZE) { | 704 length_(INVALID_FILE_SIZE) { |
705 } | 705 } |
706 | 706 |
707 MemoryMappedFile::~MemoryMappedFile() { | |
708 CloseHandles(); | |
709 } | |
710 | |
711 bool MemoryMappedFile::Initialize(const FilePath& file_name) { | |
712 if (IsValid()) | |
713 return false; | |
714 | |
715 if (!MapFileToMemory(file_name)) { | |
716 CloseHandles(); | |
717 return false; | |
718 } | |
719 | |
720 return true; | |
721 } | |
722 | |
723 bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) { | 707 bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) { |
724 file_ = ::CreateFile(file_name.value().c_str(), GENERIC_READ, | 708 file_ = ::CreateFile(file_name.value().c_str(), GENERIC_READ, |
725 FILE_SHARE_READ, NULL, OPEN_EXISTING, | 709 FILE_SHARE_READ, NULL, OPEN_EXISTING, |
726 FILE_ATTRIBUTE_NORMAL, NULL); | 710 FILE_ATTRIBUTE_NORMAL, NULL); |
727 if (file_ == INVALID_HANDLE_VALUE) | 711 if (file_ == INVALID_HANDLE_VALUE) |
728 return false; | 712 return false; |
729 | 713 |
730 length_ = ::GetFileSize(file_, NULL); | 714 length_ = ::GetFileSize(file_, NULL); |
731 if (length_ == INVALID_FILE_SIZE) | 715 if (length_ == INVALID_FILE_SIZE) |
732 return false; | 716 return false; |
733 | 717 |
734 file_mapping_ = ::CreateFileMapping(file_, NULL, PAGE_READONLY, | 718 file_mapping_ = ::CreateFileMapping(file_, NULL, PAGE_READONLY, |
735 0, length_, NULL); | 719 0, length_, NULL); |
736 if (file_mapping_ == INVALID_HANDLE_VALUE) | 720 if (file_mapping_ == INVALID_HANDLE_VALUE) |
737 return false; | 721 return false; |
738 | 722 |
739 data_ = reinterpret_cast<const uint8*>( | 723 data_ = static_cast<uint8*>( |
740 ::MapViewOfFile(file_mapping_, FILE_MAP_READ, 0, 0, length_)); | 724 ::MapViewOfFile(file_mapping_, FILE_MAP_READ, 0, 0, length_)); |
741 return data_ != NULL; | 725 return data_ != NULL; |
742 } | 726 } |
743 | 727 |
744 bool MemoryMappedFile::IsValid() { | |
745 return data_ != NULL; | |
746 } | |
747 | |
748 void MemoryMappedFile::CloseHandles() { | 728 void MemoryMappedFile::CloseHandles() { |
749 if (data_) | 729 if (data_) |
750 ::UnmapViewOfFile(data_); | 730 ::UnmapViewOfFile(data_); |
751 if (file_mapping_ != INVALID_HANDLE_VALUE) | 731 if (file_mapping_ != INVALID_HANDLE_VALUE) |
752 ::CloseHandle(file_mapping_); | 732 ::CloseHandle(file_mapping_); |
753 if (file_ != INVALID_HANDLE_VALUE) | 733 if (file_ != INVALID_HANDLE_VALUE) |
754 ::CloseHandle(file_); | 734 ::CloseHandle(file_); |
755 | 735 |
756 data_ = NULL; | 736 data_ = NULL; |
757 file_mapping_ = file_ = INVALID_HANDLE_VALUE; | 737 file_mapping_ = file_ = INVALID_HANDLE_VALUE; |
(...skipping 11 matching lines...) Expand all Loading... |
769 void PathComponents(const std::wstring& path, | 749 void PathComponents(const std::wstring& path, |
770 std::vector<std::wstring>* components) { | 750 std::vector<std::wstring>* components) { |
771 PathComponents(FilePath(path), components); | 751 PathComponents(FilePath(path), components); |
772 } | 752 } |
773 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { | 753 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { |
774 FilePath path(*file_name); | 754 FilePath path(*file_name); |
775 ReplaceExtension(&path, extension); | 755 ReplaceExtension(&path, extension); |
776 file_name->assign(path.value()); | 756 file_name->assign(path.value()); |
777 } | 757 } |
778 } // namespace file_util | 758 } // namespace file_util |
OLD | NEW |