libfilezilla
Loading...
Searching...
No Matches
file.hpp
Go to the documentation of this file.
1#ifndef LIBFILEZILLA_FILE_HEADER
2#define LIBFILEZILLA_FILE_HEADER
3
4#include "fsresult.hpp"
5#include "libfilezilla.hpp"
6
7#ifdef FZ_WINDOWS
8#include "glue/windows.hpp"
9#endif
10
14
15#include <stdint.h>
16
17namespace fz {
18
19class buffer;
20class datetime;
21class secure_buffer;
22
30class FZ_PUBLIC_SYMBOL file final
31{
32public:
33#ifdef FZ_WINDOWS
34 typedef HANDLE file_t;
35#else
36 typedef int file_t;
37#endif
38
40 enum mode {
41 reading,
42 writing,
43 readwrite,
44 appending,
45 };
46
57 existing = 0x1,
58
60 empty = 0x2,
61
69
81
83 fresh = 0x10,
84
86 nocreate = 0x20
87 };
88
89 file() = default;
90 file(native_string const& f, mode m, creation_flags d = existing);
91
92
97 explicit file(file_t fd);
98
99 ~file();
100
101 file(file const&) = delete;
102 file& operator=(file const&) = delete;
103
104 file(file && op) noexcept;
105 file& operator=(file && op) noexcept;
106
107 bool opened() const;
108 explicit operator bool() const { return opened(); }
109
110 /*
111 * \brief Opens the named file with the given mode
112 *
113 * If a (different) file was already opened, it is closed prior to opening the new file.
114 */
115 result open(native_string const& f, mode m, creation_flags d = existing);
116
117 void close();
118
120 file_t fd() {
121 return fd_;
122 }
123
124 file_t detach();
125
130
133
136 };
137
141 int64_t size() const;
142
155 int64_t seek(int64_t offset, seek_mode m);
156
158 int64_t position() { return seek(0, current); }
159
165 bool truncate();
166
180 rwresult read2(void *buf, size_t count);
181
182 [[deprecated]]
183 inline int64_t read(void *buf, int64_t count) {
184 rwresult res = read2(buf, static_cast<size_t>(count));
185 return res ? res.value_ : -1;
186 }
187
198 rwresult write2(void const* buf, size_t count);
199
200 [[deprecated]]
201 inline int64_t write(void const* buf, int64_t count) {
202 rwresult res = write2(buf, static_cast<size_t>(count));
203 return res ? res.value_ : -1;
204 }
205
211 bool fsync();
212
218
224
225private:
226#ifdef FZ_WINDOWS
227 HANDLE fd_{INVALID_HANDLE_VALUE};
228#else
229 int fd_{-1};
230#endif
231};
232
237
242result FZ_PUBLIC_SYMBOL remove_file(native_string const& name, bool missing_file_is_error);
243
245[[deprecated]] inline bool remove_file(native_string const& name) {
246 return bool(remove_file(name, false));
247}
248
250 return static_cast<file::creation_flags>(static_cast<std::underlying_type_t<file::creation_flags>>(lhs) | static_cast<std::underlying_type_t<file::creation_flags>>(rhs));
251}
253 lhs = lhs | rhs;
254 return lhs;
255}
256
263rwresult FZ_PUBLIC_SYMBOL read_file(fz::file & f, buffer & out, size_t max_size);
264rwresult FZ_PUBLIC_SYMBOL read_file(fz::file & f, secure_buffer & out, size_t max_size);
265
272result FZ_PUBLIC_SYMBOL read_file(native_string const& name, buffer & b, size_t max_size);
273result FZ_PUBLIC_SYMBOL read_file(native_string const& name, secure_buffer & b, size_t max_size);
274
275}
276#endif
Definition buffer.hpp:200
Represents a point of time in wallclock, tracking the timestamps accuracy/precision.
Definition time.hpp:41
Lean class for file access.
Definition file.hpp:31
datetime get_modification_time() const
Gets modification time.
mode
Files can be opened for reading, writing, or both.
Definition file.hpp:40
creation_flags
Creation flags when opening file for writing.
Definition file.hpp:55
@ fresh
Opening fails if the file already exists.
Definition file.hpp:83
@ current_user_and_admins_only
Definition file.hpp:80
@ nocreate
Opening fails if the file does not exist.
Definition file.hpp:86
@ existing
Keep existing data if file exists, otherwise create new.
Definition file.hpp:57
@ empty
Truncate file if already existing, otherwise create new.
Definition file.hpp:60
@ current_user_only
Definition file.hpp:68
int64_t seek(int64_t offset, seek_mode m)
Relative seek based on seek mode.
rwresult read2(void *buf, size_t count)
Read data from file.
bool truncate()
Truncate the file to the current position of the file pointer.
int64_t size() const
Gets size of file.
rwresult write2(void const *buf, size_t count)
Write data to file.
file_t fd()
Returns the raw file descriptor, but retains ownership.
Definition file.hpp:120
seek_mode
Used by seek.
Definition file.hpp:127
@ begin
Seek from beginning of file.
Definition file.hpp:129
@ end
Seek from end of file.
Definition file.hpp:135
@ current
Seek from current position in the file.
Definition file.hpp:132
bool set_modification_time(datetime const &t)
Sets modification time to specified time.
file(file_t fd)
Creates file from descriptor.
int64_t position()
Get Current position in file.
Definition file.hpp:158
bool fsync()
Ensure data is flushed to disk.
Small class to return filesystem errors.
Definition fsresult.hpp:26
Holds the result of read/write operations.
Definition fsresult.hpp:80
size_t value_
Undefined if error_ is not none.
Definition fsresult.hpp:125
Definition buffer.hpp:209
result and rwresult wrappers for dealing with file system errors.
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition apply.hpp:17
rwresult read_file(fz::file &f, buffer &out, size_t max_size)
Reads the entire source file and appends if to the buffer.
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition string.hpp:69
result remove_file(native_string const &name, bool missing_file_is_error)
remove the specified file.