35 : path_{std::move(path)}, offset_{offset}, size_{size} {}
40 path_ = std::move(other.path_);
41 offset_ = other.offset_;
43 other.file_ =
nullptr;
54 int create(
const string& newPath, MAYBE_UNUSED
const map<string, string>& options) {
56 file_ = os::fileOpen(newPath,
"wb");
57 if (file_ ==
nullptr) {
63 uint64_t bufferSize = 0;
64 if (helpers::getUInt64(options,
"io_buffer_size", bufferSize)) {
65 return setvbuf(file_,
nullptr, bufferSize == 0 ? _IONBF : _IOFBF, bufferSize);
67#if IS_ANDROID_PLATFORM()
68 const size_t kBufferingSize = 128 * 1024;
69 return setvbuf(file_,
nullptr, _IOFBF, kBufferingSize);
74 int open(
bool readOnly, MAYBE_UNUSED
const map<string, string>& options) {
75 if (file_ !=
nullptr) {
78 file_ = os::fileOpen(path_, readOnly ?
"rb" :
"rb+");
79 return file_ !=
nullptr ? SUCCESS : errno;
81 bool isOpened()
const {
82 return file_ !=
nullptr;
88 return ::fflush(file_) != 0 ? errno : SUCCESS;
90 int tell(int64_t& outFilepos)
const {
91 outFilepos = os::fileTell(file_);
92 return outFilepos < 0 ? errno : SUCCESS;
94 int seek(int64_t pos,
int origin) {
95 return os::fileSeek(file_, pos, origin) != 0 ? errno : SUCCESS;
97 int read(
void* ptr,
size_t bufferSize,
size_t& outReadSize)
const {
98 outReadSize = ::fread(ptr, 1, bufferSize, file_);
99 return bufferSize == outReadSize ? SUCCESS : ::ferror(file_) ? errno : DISKFILE_NOT_ENOUGH_DATA;
101 int write(
const void* data,
size_t dataSize,
size_t& outWrittenSize)
const {
102 outWrittenSize = ::fwrite(data, 1, dataSize, file_);
103 return dataSize == outWrittenSize ? SUCCESS
104 : ::ferror(file_) ? errno
105 : DISKFILE_PARTIAL_WRITE_ERROR;
107 int truncate(int64_t newSize) {
108 if (os::fileSetSize(file_, newSize) == 0) {
116 if (file_ !=
nullptr) {
117 error = os::fileClose(file_) != 0 ? errno : SUCCESS;
123 return ::feof(file_) != 0;
125 int64_t getOffset()
const {
128 void setOffset(int64_t newOffset) {
131 int64_t getSize()
const {
134 void setSize(int64_t newSize) {
137 const string& getPath()
const {
140 bool contains(int64_t fileOffset)
const {
141 return fileOffset >= offset_ && fileOffset < offset_ + size_;