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/ErrorCode.h>
23#include <vrs/FileHandler.h>
24#include <vrs/helpers/Strings.h>
25#include <vrs/os/CompilerAttributes.h>
26#include <vrs/os/Utils.h>
27
28namespace vrs {
29
31 public:
32 DiskFileChunk() = default;
33 DiskFileChunk(string path, int64_t offset, int64_t size)
34 : path_{std::move(path)}, offset_{offset}, size_{size} {}
35 DiskFileChunk(const DiskFileChunk& other) = delete;
36 DiskFileChunk(DiskFileChunk&& other) noexcept {
37 close();
38 file_ = other.file_;
39 path_ = std::move(other.path_);
40 offset_ = other.offset_;
41 size_ = other.size_;
42 other.file_ = nullptr;
43 other.offset_ = 0;
44 other.size_ = 0;
45 }
47 close();
48 }
49
50 DiskFileChunk& operator=(const DiskFileChunk& other) = delete;
51 DiskFileChunk& operator=(DiskFileChunk&& other) = delete;
52
53 int create(const string& newPath, MAYBE_UNUSED const map<string, string>& options) {
54 close();
55 file_ = os::fileOpen(newPath, "wb");
56 if (file_ == nullptr) {
57 return errno;
58 }
59 path_ = newPath;
60 offset_ = 0;
61 size_ = 0;
62 uint64_t bufferSize = 0;
63 if (helpers::getUInt64(options, "io_buffer_size", bufferSize)) {
64 return setvbuf(file_, nullptr, bufferSize == 0 ? _IONBF : _IOFBF, bufferSize);
65 }
66#if IS_ANDROID_PLATFORM()
67 const size_t kBufferingSize = 128 * 1024;
68 return setvbuf(file_, nullptr, _IOFBF, kBufferingSize);
69#else
70 return SUCCESS;
71#endif
72 }
73 int open(bool readOnly, MAYBE_UNUSED const map<string, string>& options) {
74 if (file_ != nullptr) {
75 os::fileClose(file_);
76 }
77 file_ = os::fileOpen(path_, readOnly ? "rb" : "rb+");
78 return file_ != nullptr ? SUCCESS : errno;
79 }
80 bool isOpened() const {
81 return file_ != nullptr;
82 }
83 void rewind() const {
84 ::rewind(file_);
85 }
86 int flush() {
87 return ::fflush(file_) != 0 ? errno : SUCCESS;
88 }
89 int tell(int64_t& outFilepos) const {
90 outFilepos = os::fileTell(file_);
91 return outFilepos < 0 ? errno : SUCCESS;
92 }
93 int seek(int64_t pos, int origin) {
94 return os::fileSeek(file_, pos, origin) != 0 ? errno : SUCCESS;
95 }
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;
99 }
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;
105 }
106 int truncate(int64_t newSize) {
107 if (os::fileSetSize(file_, newSize) == 0) {
108 size_ = newSize;
109 return SUCCESS;
110 }
111 return errno;
112 }
113 int close() {
114 int error = SUCCESS;
115 if (file_ != nullptr) {
116 error = os::fileClose(file_) != 0 ? errno : SUCCESS;
117 file_ = nullptr;
118 }
119 return error;
120 }
121 bool eof() const {
122 return ::feof(file_) != 0;
123 }
124 int64_t getOffset() const {
125 return offset_;
126 }
127 void setOffset(int64_t newOffset) {
128 offset_ = newOffset;
129 }
130 int64_t getSize() const {
131 return size_;
132 }
133 void setSize(int64_t newSize) {
134 size_ = newSize;
135 }
136 const string& getPath() const {
137 return path_;
138 }
139 bool contains(int64_t fileOffset) const {
140 return fileOffset >= offset_ && fileOffset < offset_ + size_;
141 }
142
143 private:
144 FILE* file_{}; // may be nullptr or not!
145 string path_; // path of this chunk
146 int64_t offset_{}; // offset of this chunk in the file
147 int64_t size_{}; // size of the chunk
148};
149
150} // namespace vrs
Definition DiskFileChunk.hpp:30
Definition Compressor.cpp:112