libfilezilla
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 
15 #include <stdint.h>
16 
17 namespace fz {
18 
19 class datetime;
20 
28 class FZ_PUBLIC_SYMBOL file final
29 {
30 public:
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  appending,
43  };
44 
55  existing = 0x1,
56 
58  empty = 0x2,
59 
66  current_user_only = 0x4,
67 
78  current_user_and_admins_only = 0x8,
79 
81  fresh = 0x10,
82 
84  nocreate = 0x20
85  };
86 
87  file() = default;
88  file(native_string const& f, mode m, creation_flags d = existing);
89 
90 
95  explicit file(file_t fd);
96 
97  ~file();
98 
99  file(file const&) = delete;
100  file& operator=(file const&) = delete;
101 
102  file(file && op) noexcept;
103  file& operator=(file && op) noexcept;
104 
105  bool opened() const;
106  explicit operator bool() const { return opened(); }
107 
108  /*
109  * \brief Opens the named file with the given mode
110  *
111  * If a (different) file was already opened, it is closed prior to opening the new file.
112  */
113  result open(native_string const& f, mode m, creation_flags d = existing);
114 
115  void close();
116 
118  file_t fd() {
119  return fd_;
120  }
121 
122  file_t detach();
123 
125  enum seek_mode {
128 
131 
133  end
134  };
135 
139  int64_t size() const;
140 
153  int64_t seek(int64_t offset, seek_mode m);
154 
156  int64_t position() { return seek(0, current); }
157 
163  bool truncate();
164 
178  rwresult read2(void *buf, size_t count);
179 
180  [[deprecated]]
181  inline int64_t read(void *buf, int64_t count) {
182  rwresult res = read2(buf, static_cast<size_t>(count));
183  return res ? res.value_ : -1;
184  }
185 
196  rwresult write2(void const* buf, size_t count);
197 
198  [[deprecated]]
199  inline int64_t write(void const* buf, int64_t count) {
200  rwresult res = write2(buf, static_cast<size_t>(count));
201  return res ? res.value_ : -1;
202  }
203 
209  bool fsync();
210 
215  bool set_modification_time(datetime const& t);
216 
221  datetime get_modification_time() const;
222 
223 private:
224 #ifdef FZ_WINDOWS
225  HANDLE fd_{INVALID_HANDLE_VALUE};
226 #else
227  int fd_{-1};
228 #endif
229 };
230 
240 result FZ_PUBLIC_SYMBOL remove_file(native_string const& name, bool missing_file_is_error);
241 
243 [[deprecated]] inline bool remove_file(native_string const& name) {
244  return bool(remove_file(name, false));
245 }
246 
248  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));
249 }
250 inline file::creation_flags& operator|=(file::creation_flags & lhs, file::creation_flags rhs) {
251  lhs = lhs | rhs;
252  return lhs;
253 }
254 
261 rwresult FZ_PUBLIC_SYMBOL read_file(fz::file & f, buffer & out, size_t max_size);
262 
269 result FZ_PUBLIC_SYMBOL read_file(native_string const& name, buffer & b, size_t max_size);
270 
271 }
272 #endif
Data has become available.
fz::result and fz::rwresult wrappers for dealing with file system errors.
result remove_file(native_string const &name, bool missing_file_is_error)
remove the specified file.
mode
Files can be opened for reading, writing, or both.
Definition: file.hpp:38
Holds the result of read/write operations.
Definition: fsresult.hpp:79
Seek from current position in the file.
Definition: file.hpp:130
int64_t position()
Get Current position in file.
Definition: file.hpp:156
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
size_t value_
Undefined if error_ is not none.
Definition: fsresult.hpp:125
Seek from beginning of file.
Definition: file.hpp:127
The namespace used by libfilezilla.
Definition: apply.hpp:17
seek_mode
Used by seek.
Definition: file.hpp:125
creation_flags
Creation flags when opening file for writing.
Definition: file.hpp:53
Lean class for file access.
Definition: file.hpp:28
Sets some global macros and further includes string.hpp.
data can be written.
file_t fd()
Returns the raw file descriptor, but retains ownership.
Definition: file.hpp:118
rwresult read_file(fz::file &f, buffer &out, size_t max_size)
Reads the entire source file and appends if to the buffer.