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 <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <fnmatch.h> | 10 #include <fnmatch.h> |
(...skipping 649 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 } | 660 } |
661 | 661 |
662 closedir(dir); | 662 closedir(dir); |
663 return true; | 663 return true; |
664 } | 664 } |
665 | 665 |
666 /////////////////////////////////////////////// | 666 /////////////////////////////////////////////// |
667 // MemoryMappedFile | 667 // MemoryMappedFile |
668 | 668 |
669 MemoryMappedFile::MemoryMappedFile() | 669 MemoryMappedFile::MemoryMappedFile() |
670 : file_(base::kInvalidPlatformFileValue), | 670 : data_(NULL), |
671 data_(NULL), | |
672 length_(0) { | 671 length_(0) { |
673 } | 672 } |
674 | 673 |
| 674 bool MemoryMappedFile::Initialize(const base::FileDescriptor& fd) { |
| 675 if (IsValid()) |
| 676 return false; |
| 677 |
| 678 file_ = fd; |
| 679 |
| 680 if (!MapFileToMemoryInternal()) { |
| 681 CloseHandles(); |
| 682 return false; |
| 683 } |
| 684 |
| 685 return true; |
| 686 } |
| 687 |
| 688 bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) { |
| 689 file_ = base::FileDescriptor(open(file_name.value().c_str(), O_RDONLY), true); |
| 690 |
| 691 if (file_.fd == -1) { |
| 692 LOG(ERROR) << "Couldn't open " << file_name.value(); |
| 693 return false; |
| 694 } |
| 695 |
| 696 return MapFileToMemoryInternal(); |
| 697 } |
| 698 |
675 bool MemoryMappedFile::MapFileToMemoryInternal() { | 699 bool MemoryMappedFile::MapFileToMemoryInternal() { |
676 struct stat file_stat; | 700 struct stat file_stat; |
677 if (fstat(file_, &file_stat) == base::kInvalidPlatformFileValue) { | 701 if (fstat(file_.fd, &file_stat) == -1) { |
678 LOG(ERROR) << "Couldn't fstat " << file_ << ", errno " << errno; | 702 LOG(ERROR) << "Couldn't fstat " << file_.fd << ", errno " << errno; |
679 return false; | 703 return false; |
680 } | 704 } |
681 length_ = file_stat.st_size; | 705 length_ = file_stat.st_size; |
682 | 706 |
683 data_ = static_cast<uint8*>( | 707 data_ = static_cast<uint8*>( |
684 mmap(NULL, length_, PROT_READ, MAP_SHARED, file_, 0)); | 708 mmap(NULL, length_, PROT_READ, MAP_SHARED, file_.fd, 0)); |
685 if (data_ == MAP_FAILED) | 709 if (data_ == MAP_FAILED) |
686 LOG(ERROR) << "Couldn't mmap " << file_ << ", errno " << errno; | 710 LOG(ERROR) << "Couldn't mmap " << file_.fd << ", errno " << errno; |
687 | 711 |
688 return data_ != MAP_FAILED; | 712 return data_ != MAP_FAILED; |
689 } | 713 } |
690 | 714 |
691 void MemoryMappedFile::CloseHandles() { | 715 void MemoryMappedFile::CloseHandles() { |
692 if (data_ != NULL) | 716 if (data_ != NULL) |
693 munmap(data_, length_); | 717 munmap(data_, length_); |
694 if (file_ != base::kInvalidPlatformFileValue) | 718 if (file_.auto_close && file_.fd != -1) |
695 close(file_); | 719 close(file_.fd); |
696 | 720 |
697 data_ = NULL; | 721 data_ = NULL; |
698 length_ = 0; | 722 length_ = 0; |
699 file_ = base::kInvalidPlatformFileValue; | 723 file_ = base::FileDescriptor(); |
700 } | 724 } |
701 | 725 |
702 } // namespace file_util | 726 } // namespace file_util |
OLD | NEW |