34 : path_{std::move(path)}, offset_{offset}, size_{size} {}
39 path_ = std::move(other.path_);
40 offset_ = other.offset_;
42 other.file_ =
nullptr;
53 int create(
const string& newPath, MAYBE_UNUSED
const map<string, string>& options) {
55 file_ = os::fileOpen(newPath,
"wb");
56 if (file_ ==
nullptr) {
62 uint64_t bufferSize = 0;
63 if (helpers::getUInt64(options,
"io_buffer_size", bufferSize)) {
64 return setvbuf(file_,
nullptr, bufferSize == 0 ? _IONBF : _IOFBF, bufferSize);
66#if IS_ANDROID_PLATFORM()
67 const size_t kBufferingSize = 128 * 1024;
68 return setvbuf(file_,
nullptr, _IOFBF, kBufferingSize);
73 int open(
bool readOnly, MAYBE_UNUSED
const map<string, string>& options) {
74 if (file_ !=
nullptr) {
77 file_ = os::fileOpen(path_, readOnly ?
"rb" :
"rb+");
78 return file_ !=
nullptr ? SUCCESS : errno;
80 bool isOpened()
const {
81 return file_ !=
nullptr;
87 return ::fflush(file_) != 0 ? errno : SUCCESS;
89 int tell(int64_t& outFilepos)
const {
90 outFilepos = os::fileTell(file_);
91 return outFilepos < 0 ? errno : SUCCESS;
93 int seek(int64_t pos,
int origin) {
94 return os::fileSeek(file_, pos, origin) != 0 ? errno : SUCCESS;
96 int read(
void* ptr,
size_t bufferSize,
size_t& outReadSize)
const {
97 outReadSize = ::fread(ptr, 1, bufferSize, file_);
98 return bufferSize == outReadSize ? SUCCESS : ::ferror(file_) ? errno : DISKFILE_NOT_ENOUGH_DATA;
100 int write(
const void* data,
size_t dataSize,
size_t& outWrittenSize)
const {
101 outWrittenSize = ::fwrite(data, 1, dataSize, file_);
102 return dataSize == outWrittenSize ? SUCCESS
103 : ::ferror(file_) ? errno
104 : DISKFILE_PARTIAL_WRITE_ERROR;
106 int truncate(int64_t newSize) {
107 if (os::fileSetSize(file_, newSize) == 0) {
115 if (file_ !=
nullptr) {
116 error = os::fileClose(file_) != 0 ? errno : SUCCESS;
122 return ::feof(file_) != 0;
124 int64_t getOffset()
const {
127 void setOffset(int64_t newOffset) {
130 int64_t getSize()
const {
133 void setSize(int64_t newSize) {
136 const string& getPath()
const {
139 bool contains(int64_t fileOffset)
const {
140 return fileOffset >= offset_ && fileOffset < offset_ + size_;