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 datetime;
20
28class FZ_PUBLIC_SYMBOL file final
29{
30public:
31#ifdef FZ_WINDOWS
32 typedef HANDLE file_t;
33#else
34 typedef int file_t;
35#endif
36
38 enum mode {
39 reading,
40 writing,
41 readwrite
42 };
43
77
78 file() = default;
79 file(native_string const& f, mode m, creation_flags d = existing);
80
81
86 explicit file(file_t fd);
87
88 ~file();
89
90 file(file const&) = delete;
91 file& operator=(file const&) = delete;
92
93 file(file && op) noexcept;
94 file& operator=(file && op) noexcept;
95
96 bool opened() const;
97 explicit operator bool() const { return opened(); }
98
99 result open(native_string const& f, mode m, creation_flags d = existing);
100
101 void close();
102
104 file_t fd() {
105 return fd_;
106 }
107
108 file_t detach();
109
114
117
120 };
121
125 int64_t size() const;
126
139 int64_t seek(int64_t offset, seek_mode m);
140
142 int64_t position() { return seek(0, current); }
143
149 bool truncate();
150
164 rwresult read2(void *buf, size_t count);
165
166 [[deprecated]]
167 inline int64_t read(void *buf, int64_t count) {
168 rwresult res = read2(buf, static_cast<size_t>(count));
169 return res ? res.value_ : -1;
170 }
171
182 rwresult write2(void const* buf, size_t count);
183
184 [[deprecated]]
185 inline int64_t write(void const* buf, int64_t count) {
186 rwresult res = write2(buf, static_cast<size_t>(count));
187 return res ? res.value_ : -1;
188 }
189
195 bool fsync();
196
202
208
209private:
210#ifdef FZ_WINDOWS
211 HANDLE fd_{INVALID_HANDLE_VALUE};
212#else
213 int fd_{-1};
214#endif
215};
216
221
226result FZ_PUBLIC_SYMBOL remove_file(native_string const& name, bool missing_file_is_error);
227
229[[deprecated]] inline bool remove_file(native_string const& name) {
230 return bool(remove_file(name, false));
231}
232
234 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));
235}
237 lhs = lhs | rhs;
238 return lhs;
239}
240
247rwresult FZ_PUBLIC_SYMBOL read_file(fz::file & f, buffer & out, size_t max_size);
248
249}
250#endif
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition buffer.hpp:27
Represents a point of time in wallclock, tracking the timestamps accuracy/precision.
Definition time.hpp:41
Lean class for file access.
Definition file.hpp:29
mode
Files can be opened for reading, writing, or both.
Definition file.hpp:38
creation_flags
Creation flags when opening file for writing.
Definition file.hpp:50
@ current_user_and_admins_only
Definition file.hpp:75
@ existing
Keep existing data if file exists, otherwise create new.
Definition file.hpp:52
@ empty
Truncate file if already existing, otherwise create new.
Definition file.hpp:55
@ current_user_only
Definition file.hpp:63
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.
datetime get_modification_time()
Gets modification time.
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:104
seek_mode
Used by seek.
Definition file.hpp:111
@ begin
Seek from beginning of file.
Definition file.hpp:113
@ end
Seek from end of file.
Definition file.hpp:119
@ current
Seek from current position in the file.
Definition file.hpp:116
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:142
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:77
size_t value_
Undefined if error_ is not none.
Definition fsresult.hpp:121
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:34
result remove_file(native_string const &name, bool missing_file_is_error)
remove the specified file.