33 : path_{std::move(path)}, offset_{offset}, size_{size} {}
37 path_ = std::move(other.path_);
38 offset_ = other.offset_;
40 other.file_ =
nullptr;
47 int create(
const string& newPath, MAYBE_UNUSED
const map<string, string>& options) {
49 file_ = os::fileOpen(newPath,
"wb");
50 if (file_ ==
nullptr) {
56 uint64_t bufferSize = 0;
57 if (helpers::getUInt64(options,
"io_buffer_size", bufferSize)) {
58 return setvbuf(file_,
nullptr, bufferSize == 0 ? _IONBF : _IOFBF, bufferSize);
60#if IS_ANDROID_PLATFORM()
61 const size_t kBufferingSize = 128 * 1024;
62 return setvbuf(file_,
nullptr, _IOFBF, kBufferingSize);
67 int open(
bool readOnly, MAYBE_UNUSED
const map<string, string>& options) {
68 if (file_ !=
nullptr) {
71 file_ = os::fileOpen(path_, readOnly ?
"rb" :
"rb+");
72 return file_ !=
nullptr ? SUCCESS : errno;
74 bool isOpened()
const {
75 return file_ !=
nullptr;
81 return ::fflush(file_) != 0 ? errno : SUCCESS;
83 int tell(int64_t& outFilepos)
const {
84 outFilepos = os::fileTell(file_);
85 return outFilepos < 0 ? errno : SUCCESS;
87 int seek(int64_t pos,
int origin) {
88 return os::fileSeek(file_, pos, origin) != 0 ? errno : SUCCESS;
90 int read(
void* ptr,
size_t bufferSize,
size_t& outReadSize)
const {
91 outReadSize = ::fread(ptr, 1, bufferSize, file_);
92 return bufferSize == outReadSize ? SUCCESS : ::ferror(file_) ? errno : DISKFILE_NOT_ENOUGH_DATA;
94 int write(
const void* data,
size_t dataSize,
size_t& outWrittenSize)
const {
95 outWrittenSize = ::fwrite(data, 1, dataSize, file_);
96 return dataSize == outWrittenSize ? SUCCESS
97 : ::ferror(file_) ? errno
98 : DISKFILE_PARTIAL_WRITE_ERROR;
100 int truncate(int64_t newSize) {
101 if (os::fileSetSize(file_, newSize) == 0) {
109 if (file_ !=
nullptr) {
110 error = os::fileClose(file_) != 0 ? errno : SUCCESS;
116 return ::feof(file_) != 0;
118 int64_t getOffset()
const {
121 void setOffset(int64_t newOffset) {
124 int64_t getSize()
const {
127 void setSize(int64_t newSize) {
130 const string& getPath()
const {
133 bool contains(int64_t fileOffset)
const {
134 return fileOffset >= offset_ && fileOffset < offset_ + size_;