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  };
43 
52  existing = 0x1,
53 
55  empty = 0x2,
56 
63  current_user_only = 0x4,
64 
75  current_user_and_admins_only = 0x8
76  };
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 
111  enum seek_mode {
114 
117 
119  end
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  int64_t read(void *buf, int64_t count);
165 
176  int64_t write(void const* buf, int64_t count);
177 
183  bool fsync();
184 
189  bool set_modification_time(datetime const& t);
190 
191 private:
192 #ifdef FZ_WINDOWS
193  HANDLE fd_{INVALID_HANDLE_VALUE};
194 #else
195  int fd_{-1};
196 #endif
197 };
198 
203 bool FZ_PUBLIC_SYMBOL remove_file(native_string const& name);
204 
206  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));
207 }
208 inline file::creation_flags& operator|=(file::creation_flags & lhs, file::creation_flags rhs) {
209  lhs = lhs | rhs;
210  return lhs;
211 }
212 
213 }
214 #endif
Data has become available.
fz::result and fz::rwresult wrappers for dealing with file system errors.
mode
Files can be opened for reading, writing, or both.
Definition: file.hpp:38
Represents a point of time in wallclock, tracking the timestamps accuracy/precision.
Definition: time.hpp:40
Seek from current position in the file.
Definition: file.hpp:116
int64_t position()
Get Current position in file.
Definition: file.hpp:142
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
Seek from beginning of file.
Definition: file.hpp:113
bool remove_file(native_string const &name)
remove the specified file.
The namespace used by libfilezilla.
Definition: apply.hpp:17
seek_mode
Used by seek.
Definition: file.hpp:111
creation_flags
Creation flags when opening file for writing.
Definition: file.hpp:50
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:104