1*a03ca8b9SKrzysztof Kosiński // Copyright 2017 The Chromium Authors. All rights reserved. 2*a03ca8b9SKrzysztof Kosiński // Use of this source code is governed by a BSD-style license that can be 3*a03ca8b9SKrzysztof Kosiński // found in the LICENSE file. 4*a03ca8b9SKrzysztof Kosiński 5*a03ca8b9SKrzysztof Kosiński #ifndef COMPONENTS_ZUCCHINI_MAPPED_FILE_H_ 6*a03ca8b9SKrzysztof Kosiński #define COMPONENTS_ZUCCHINI_MAPPED_FILE_H_ 7*a03ca8b9SKrzysztof Kosiński 8*a03ca8b9SKrzysztof Kosiński #include <stddef.h> 9*a03ca8b9SKrzysztof Kosiński #include <stdint.h> 10*a03ca8b9SKrzysztof Kosiński 11*a03ca8b9SKrzysztof Kosiński #include <string> 12*a03ca8b9SKrzysztof Kosiński 13*a03ca8b9SKrzysztof Kosiński #include "base/files/file.h" 14*a03ca8b9SKrzysztof Kosiński #include "base/files/file_path.h" 15*a03ca8b9SKrzysztof Kosiński #include "base/files/memory_mapped_file.h" 16*a03ca8b9SKrzysztof Kosiński #include "components/zucchini/buffer_view.h" 17*a03ca8b9SKrzysztof Kosiński 18*a03ca8b9SKrzysztof Kosiński namespace zucchini { 19*a03ca8b9SKrzysztof Kosiński 20*a03ca8b9SKrzysztof Kosiński // A file reader wrapper. 21*a03ca8b9SKrzysztof Kosiński class MappedFileReader { 22*a03ca8b9SKrzysztof Kosiński public: 23*a03ca8b9SKrzysztof Kosiński // Maps |file| to memory for reading. Also validates |file|. Errors are 24*a03ca8b9SKrzysztof Kosiński // available via HasError() and error(). 25*a03ca8b9SKrzysztof Kosiński explicit MappedFileReader(base::File file); 26*a03ca8b9SKrzysztof Kosiński MappedFileReader(const MappedFileReader&) = delete; 27*a03ca8b9SKrzysztof Kosiński const MappedFileReader& operator=(const MappedFileReader&) = delete; 28*a03ca8b9SKrzysztof Kosiński data()29*a03ca8b9SKrzysztof Kosiński const uint8_t* data() const { return buffer_.data(); } length()30*a03ca8b9SKrzysztof Kosiński size_t length() const { return buffer_.length(); } region()31*a03ca8b9SKrzysztof Kosiński zucchini::ConstBufferView region() const { return {data(), length()}; } 32*a03ca8b9SKrzysztof Kosiński HasError()33*a03ca8b9SKrzysztof Kosiński bool HasError() { return !error_.empty() || !buffer_.IsValid(); } error()34*a03ca8b9SKrzysztof Kosiński const std::string& error() { return error_; } 35*a03ca8b9SKrzysztof Kosiński 36*a03ca8b9SKrzysztof Kosiński private: 37*a03ca8b9SKrzysztof Kosiński std::string error_; 38*a03ca8b9SKrzysztof Kosiński base::MemoryMappedFile buffer_; 39*a03ca8b9SKrzysztof Kosiński }; 40*a03ca8b9SKrzysztof Kosiński 41*a03ca8b9SKrzysztof Kosiński // A file writer wrapper. The target file is deleted on destruction unless 42*a03ca8b9SKrzysztof Kosiński // Keep() is called. 43*a03ca8b9SKrzysztof Kosiński class MappedFileWriter { 44*a03ca8b9SKrzysztof Kosiński public: 45*a03ca8b9SKrzysztof Kosiński // Maps |file| to memory for writing. |file_path| is needed for auto delete on 46*a03ca8b9SKrzysztof Kosiński // UNIX systems, but can be empty if auto delete is not needed. Errors are 47*a03ca8b9SKrzysztof Kosiński // available via HasError() and error(). 48*a03ca8b9SKrzysztof Kosiński MappedFileWriter(const base::FilePath& file_path, 49*a03ca8b9SKrzysztof Kosiński base::File file, 50*a03ca8b9SKrzysztof Kosiński size_t length); 51*a03ca8b9SKrzysztof Kosiński MappedFileWriter(const MappedFileWriter&) = delete; 52*a03ca8b9SKrzysztof Kosiński const MappedFileWriter& operator=(const MappedFileWriter&) = delete; 53*a03ca8b9SKrzysztof Kosiński ~MappedFileWriter(); 54*a03ca8b9SKrzysztof Kosiński data()55*a03ca8b9SKrzysztof Kosiński uint8_t* data() { return buffer_.data(); } length()56*a03ca8b9SKrzysztof Kosiński size_t length() const { return buffer_.length(); } region()57*a03ca8b9SKrzysztof Kosiński zucchini::MutableBufferView region() { return {data(), length()}; } 58*a03ca8b9SKrzysztof Kosiński HasError()59*a03ca8b9SKrzysztof Kosiński bool HasError() { return !error_.empty() || !buffer_.IsValid(); } error()60*a03ca8b9SKrzysztof Kosiński const std::string& error() { return error_; } 61*a03ca8b9SKrzysztof Kosiński 62*a03ca8b9SKrzysztof Kosiński // Indicates that the file should not be deleted on destruction. Returns true 63*a03ca8b9SKrzysztof Kosiński // iff the operation succeeds. 64*a03ca8b9SKrzysztof Kosiński bool Keep(); 65*a03ca8b9SKrzysztof Kosiński 66*a03ca8b9SKrzysztof Kosiński private: 67*a03ca8b9SKrzysztof Kosiński enum OnCloseDeleteBehavior { 68*a03ca8b9SKrzysztof Kosiński kKeep, 69*a03ca8b9SKrzysztof Kosiński kAutoDeleteOnClose, 70*a03ca8b9SKrzysztof Kosiński kManualDeleteOnClose 71*a03ca8b9SKrzysztof Kosiński }; 72*a03ca8b9SKrzysztof Kosiński 73*a03ca8b9SKrzysztof Kosiński std::string error_; 74*a03ca8b9SKrzysztof Kosiński base::FilePath file_path_; 75*a03ca8b9SKrzysztof Kosiński base::File file_handle_; 76*a03ca8b9SKrzysztof Kosiński base::MemoryMappedFile buffer_; 77*a03ca8b9SKrzysztof Kosiński OnCloseDeleteBehavior delete_behavior_; 78*a03ca8b9SKrzysztof Kosiński }; 79*a03ca8b9SKrzysztof Kosiński 80*a03ca8b9SKrzysztof Kosiński } // namespace zucchini 81*a03ca8b9SKrzysztof Kosiński 82*a03ca8b9SKrzysztof Kosiński #endif // COMPONENTS_ZUCCHINI_MAPPED_FILE_H_ 83