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 : data_(NULL), | 670 : file_(base::kInvalidPlatformFileValue), |
| 671 data_(NULL), |
671 length_(0) { | 672 length_(0) { |
672 } | 673 } |
673 | 674 |
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 | |
699 bool MemoryMappedFile::MapFileToMemoryInternal() { | 675 bool MemoryMappedFile::MapFileToMemoryInternal() { |
700 struct stat file_stat; | 676 struct stat file_stat; |
701 if (fstat(file_.fd, &file_stat) == -1) { | 677 if (fstat(file_, &file_stat) == base::kInvalidPlatformFileValue) { |
702 LOG(ERROR) << "Couldn't fstat " << file_.fd << ", errno " << errno; | 678 LOG(ERROR) << "Couldn't fstat " << file_ << ", errno " << errno; |
703 return false; | 679 return false; |
704 } | 680 } |
705 length_ = file_stat.st_size; | 681 length_ = file_stat.st_size; |
706 | 682 |
707 data_ = static_cast<uint8*>( | 683 data_ = static_cast<uint8*>( |
708 mmap(NULL, length_, PROT_READ, MAP_SHARED, file_.fd, 0)); | 684 mmap(NULL, length_, PROT_READ, MAP_SHARED, file_, 0)); |
709 if (data_ == MAP_FAILED) | 685 if (data_ == MAP_FAILED) |
710 LOG(ERROR) << "Couldn't mmap " << file_.fd << ", errno " << errno; | 686 LOG(ERROR) << "Couldn't mmap " << file_ << ", errno " << errno; |
711 | 687 |
712 return data_ != MAP_FAILED; | 688 return data_ != MAP_FAILED; |
713 } | 689 } |
714 | 690 |
715 void MemoryMappedFile::CloseHandles() { | 691 void MemoryMappedFile::CloseHandles() { |
716 if (data_ != NULL) | 692 if (data_ != NULL) |
717 munmap(data_, length_); | 693 munmap(data_, length_); |
718 if (file_.auto_close && file_.fd != -1) | 694 if (file_ != base::kInvalidPlatformFileValue) |
719 close(file_.fd); | 695 close(file_); |
720 | 696 |
721 data_ = NULL; | 697 data_ = NULL; |
722 length_ = 0; | 698 length_ = 0; |
723 file_ = base::FileDescriptor(); | 699 file_ = base::kInvalidPlatformFileValue; |
724 } | 700 } |
725 | 701 |
726 } // namespace file_util | 702 } // namespace file_util |
OLD | NEW |