| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 7 мар. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_IO_NATIVEFILE_H_ |
| 23 |
#define LSP_PLUG_IN_IO_NATIVEFILE_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 27 |
#include <lsp-plug.in/common/types.h> |
| 28 |
#include <lsp-plug.in/io/Path.h> |
| 29 |
#include <lsp-plug.in/io/File.h> |
| 30 |
|
| 31 |
namespace lsp |
| 32 |
{ |
| 33 |
namespace io |
| 34 |
{ |
| 35 |
/** |
| 36 |
* This class provides file interface that uses low-level file functions |
| 37 |
* at the backend for file operations. Also this class allows to |
| 38 |
* wrap native file descriptor into an object instance. |
| 39 |
*/ |
| 40 |
class NativeFile: public File |
| 41 |
{ |
| 42 |
private: |
| 43 |
enum flags_t |
| 44 |
{ |
| 45 |
SF_READ = 1 << 0, |
| 46 |
SF_WRITE = 1 << 1, |
| 47 |
SF_CLOSE = 1 << 2 |
| 48 |
}; |
| 49 |
|
| 50 |
protected: |
| 51 |
fhandle_t hFD; |
| 52 |
size_t nFlags; |
| 53 |
|
| 54 |
public: |
| 55 |
explicit NativeFile(); |
| 56 |
NativeFile(const NativeFile &) = delete; |
| 57 |
NativeFile(NativeFile &&) = delete; |
| 58 |
virtual ~NativeFile(); |
| 59 |
|
| 60 |
NativeFile &operator = (const NativeFile &) = delete; |
| 61 |
NativeFile &operator = (NativeFile &&) = delete; |
| 62 |
|
| 63 |
public: |
| 64 |
/** |
| 65 |
* Open file |
| 66 |
* @param path file location |
| 67 |
* @param mode open mode |
| 68 |
* @return status of operation |
| 69 |
*/ |
| 70 |
status_t open(const char *path, size_t mode); |
| 71 |
|
| 72 |
/** |
| 73 |
* Open file |
| 74 |
* @param path file location |
| 75 |
* @param mode open mode |
| 76 |
* @return status of operation |
| 77 |
*/ |
| 78 |
status_t open(const LSPString *path, size_t mode); |
| 79 |
|
| 80 |
/** |
| 81 |
* Open file |
| 82 |
* @param path file location |
| 83 |
* @param mode open mode |
| 84 |
* @return status of operation |
| 85 |
*/ |
| 86 |
status_t open(const Path *path, size_t mode); |
| 87 |
|
| 88 |
/** |
| 89 |
* Create temporary file and open it for writing. |
| 90 |
* The caller is responsible for deleting the file. |
| 91 |
* @param path output pointer to store the file name |
| 92 |
* @param prefix optional file name prefix in UTF-8 |
| 93 |
* @return status of operation |
| 94 |
*/ |
| 95 |
status_t open_temp(io::Path *path, const char *prefix = NULL); |
| 96 |
|
| 97 |
/** |
| 98 |
* Create temporary file and open it for writing. |
| 99 |
* The caller is responsible for deleting the file. |
| 100 |
* @param path output pointer to store the file name |
| 101 |
* @param prefix optional file name prefix |
| 102 |
* @return status of operation |
| 103 |
*/ |
| 104 |
status_t open_temp(io::Path *path, const LSPString *prefix); |
| 105 |
|
| 106 |
/** |
| 107 |
* Create temporary file and open it for writing. |
| 108 |
* The caller is responsible for deleting the file. |
| 109 |
* @param path output pointer to store the file name |
| 110 |
* @param prefix optional file name prefix in UTF-8 |
| 111 |
* @return status of operation |
| 112 |
*/ |
| 113 |
status_t open_temp(LSPString *path, const char *prefix = NULL); |
| 114 |
|
| 115 |
/** |
| 116 |
* Create temporary file and open it for writing. |
| 117 |
* The caller is responsible for deleting the file. |
| 118 |
* @param path output pointer to store the file name |
| 119 |
* @param prefix optional file name prefix in UTF-8 |
| 120 |
* @return status of operation |
| 121 |
*/ |
| 122 |
status_t open_temp(LSPString *path, const LSPString *prefix); |
| 123 |
|
| 124 |
/** |
| 125 |
* Wrap the standard file descriptor and allow both read |
| 126 |
* and write operations |
| 127 |
* @param fd file descriptor to wrap |
| 128 |
* @param close close the file descriptor on close() call |
| 129 |
* @return status of operation |
| 130 |
*/ |
| 131 |
status_t wrap(fhandle_t fd, bool close); |
| 132 |
|
| 133 |
/** |
| 134 |
* Wrap the standard file descriptor |
| 135 |
* @param fd file descriptor to wrap |
| 136 |
* @param mode allowed access modes (read, write or both) |
| 137 |
* @param close close the file descriptor on close() call |
| 138 |
* @return status of operation |
| 139 |
*/ |
| 140 |
status_t wrap(fhandle_t fd, size_t mode, bool close); |
| 141 |
|
| 142 |
/** |
| 143 |
* Read binary file |
| 144 |
* @param dst target buffer to perform read |
| 145 |
* @param count amount of bytes to read |
| 146 |
* @return number of bytes read or negative status of operation, |
| 147 |
* on end of file -STATUS_EOF is returned |
| 148 |
*/ |
| 149 |
virtual ssize_t read(void *dst, size_t count); |
| 150 |
|
| 151 |
/** |
| 152 |
* Perform positioned read of binary file |
| 153 |
* @param pos offset in bytes relative to the beginning of the file |
| 154 |
* @param dst target buffer to perform read |
| 155 |
* @param count amount of bytes to read |
| 156 |
* @return number of bytes read or status of operation |
| 157 |
*/ |
| 158 |
virtual ssize_t pread(wsize_t pos, void *dst, size_t count); |
| 159 |
|
| 160 |
/** |
| 161 |
* Write binary file |
| 162 |
* @param src source buffer to perform write |
| 163 |
* @param count number of bytes to write |
| 164 |
* @return number of bytes written or negative status of operation |
| 165 |
*/ |
| 166 |
virtual ssize_t write(const void *src, size_t count); |
| 167 |
|
| 168 |
/** |
| 169 |
* Perform positioned write of binary file |
| 170 |
* @param pos offset in bytes relative to the beginning of the file |
| 171 |
* @param src source buffer to perform write |
| 172 |
* @param count number of bytes to write |
| 173 |
* @return status of operation |
| 174 |
*/ |
| 175 |
virtual ssize_t pwrite(wsize_t pos, const void *src, size_t count); |
| 176 |
|
| 177 |
/** |
| 178 |
* Perform seek to the specified position |
| 179 |
* @param pos position to perform seek |
| 180 |
* @param type seek type |
| 181 |
* @return status of operation |
| 182 |
*/ |
| 183 |
virtual status_t seek(wssize_t pos, size_t type = FSK_SET); |
| 184 |
|
| 185 |
/** |
| 186 |
* Obtain current file's position |
| 187 |
* @return current file's position or negative error code |
| 188 |
*/ |
| 189 |
virtual wssize_t position(); |
| 190 |
|
| 191 |
/** |
| 192 |
* Obtain current file's size |
| 193 |
* @return current file's size or negative error code |
| 194 |
*/ |
| 195 |
virtual wssize_t size(); |
| 196 |
|
| 197 |
/** |
| 198 |
* Get file attributes |
| 199 |
* @param attr file attributes |
| 200 |
* @return status of operation |
| 201 |
*/ |
| 202 |
virtual status_t stat(fattr_t *attr); |
| 203 |
|
| 204 |
/** |
| 205 |
* Truncate the file |
| 206 |
* @param length the final file length |
| 207 |
* @return status of operation |
| 208 |
*/ |
| 209 |
virtual status_t truncate(wsize_t length); |
| 210 |
|
| 211 |
/** |
| 212 |
* Flush file buffer to underlying storage |
| 213 |
* @return status of operation |
| 214 |
*/ |
| 215 |
virtual status_t flush(); |
| 216 |
|
| 217 |
/** |
| 218 |
* Sync file with the underlying storage |
| 219 |
* @return status of operation |
| 220 |
*/ |
| 221 |
virtual status_t sync(); |
| 222 |
|
| 223 |
/** |
| 224 |
* Close file |
| 225 |
* @return status of operation |
| 226 |
*/ |
| 227 |
virtual status_t close(); |
| 228 |
}; |
| 229 |
|
| 230 |
} /* namespace io */ |
| 231 |
} /* namespace lsp */ |
| 232 |
|
| 233 |
#endif /* LSP_PLUG_IN_IO_NATIVEFILE_H_ */ |