VRS
A file format for sensor data.
Loading...
Searching...
No Matches
DiskFileChunk.hpp
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cerrno>
20#include <cstdio>
21
22#include <vrs/FileHandler.h>
23#include <vrs/helpers/Strings.h>
24#include <vrs/os/CompilerAttributes.h>
25#include <vrs/os/Utils.h>
26
27namespace vrs {
28
30 public:
31 DiskFileChunk() = default;
32 DiskFileChunk(string path, int64_t offset, int64_t size)
33 : path_{std::move(path)}, offset_{offset}, size_{size} {}
34 DiskFileChunk(DiskFileChunk&& other) noexcept {
35 close();
36 file_ = other.file_;
37 path_ = std::move(other.path_);
38 offset_ = other.offset_;
39 size_ = other.size_;
40 other.file_ = nullptr;
41 other.offset_ = 0;
42 other.size_ = 0;
43 }
45 close();
46 }
47 int create(const string& newPath, MAYBE_UNUSED const map<string, string>& options) {
48 close();
49 file_ = os::fileOpen(newPath, "wb");
50 if (file_ == nullptr) {
51 return errno;
52 }
53 path_ = newPath;
54 offset_ = 0;
55 size_ = 0;
56 uint64_t bufferSize = 0;
57 if (helpers::getUInt64(options, "io_buffer_size", bufferSize)) {
58 return setvbuf(file_, nullptr, bufferSize == 0 ? _IONBF : _IOFBF, bufferSize);
59 }
60#if IS_ANDROID_PLATFORM()
61 const size_t kBufferingSize = 128 * 1024;
62 return setvbuf(file_, nullptr, _IOFBF, kBufferingSize);
63#else
64 return SUCCESS;
65#endif
66 }
67 int open(bool readOnly, MAYBE_UNUSED const map<string, string>& options) {
68 if (file_ != nullptr) {
69 os::fileClose(file_);
70 }
71 file_ = os::fileOpen(path_, readOnly ? "rb" : "rb+");
72 return file_ != nullptr ? SUCCESS : errno;
73 }
74 bool isOpened() const {
75 return file_ != nullptr;
76 }
77 void rewind() const {
78 ::rewind(file_);
79 }
80 int flush() {
81 return ::fflush(file_) != 0 ? errno : SUCCESS;
82 }
83 int tell(int64_t& outFilepos) const {
84 outFilepos = os::fileTell(file_);
85 return outFilepos < 0 ? errno : SUCCESS;
86 }
87 int seek(int64_t pos, int origin) {
88 return os::fileSeek(file_, pos, origin) != 0 ? errno : SUCCESS;
89 }
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;
93 }
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;
99 }
100 int truncate(int64_t newSize) {
101 if (os::fileSetSize(file_, newSize) == 0) {
102 size_ = newSize;
103 return SUCCESS;
104 }
105 return errno;
106 }
107 int close() {
108 int error = SUCCESS;
109 if (file_ != nullptr) {
110 error = os::fileClose(file_) != 0 ? errno : SUCCESS;
111 file_ = nullptr;
112 }
113 return error;
114 }
115 bool eof() const {
116 return ::feof(file_) != 0;
117 }
118 int64_t getOffset() const {
119 return offset_;
120 }
121 void setOffset(int64_t newOffset) {
122 offset_ = newOffset;
123 }
124 int64_t getSize() const {
125 return size_;
126 }
127 void setSize(int64_t newSize) {
128 size_ = newSize;
129 }
130 const string& getPath() const {
131 return path_;
132 }
133 bool contains(int64_t fileOffset) const {
134 return fileOffset >= offset_ && fileOffset < offset_ + size_;
135 }
136
137 private:
138 FILE* file_{}; // may be nullptr or not!
139 string path_; // path of this chunk
140 int64_t offset_{}; // offset of this chunk in the file
141 int64_t size_{}; // size of the chunk
142};
143
144} // namespace vrs
Definition DiskFileChunk.hpp:29
Definition AsyncDiskFileChunk.hpp:49