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