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