forked from RPCS3/rpcs3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.h
628 lines (511 loc) · 14 KB
/
File.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#pragma once
#include "types.h"
#include "bit_set.h"
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
namespace fs
{
#ifdef _WIN32
using native_handle = void*;
#else
using native_handle = int;
#endif
// File open mode flags
enum class open_mode : u32
{
read,
write,
append,
create,
trunc,
excl,
lock,
unread,
__bitset_enum_max
};
constexpr auto read = +open_mode::read; // Enable reading
constexpr auto write = +open_mode::write; // Enable writing
constexpr auto append = +open_mode::append; // Always append to the end of the file
constexpr auto create = +open_mode::create; // Create file if it doesn't exist
constexpr auto trunc = +open_mode::trunc; // Clear opened file if it's not empty
constexpr auto excl = +open_mode::excl; // Failure if the file already exists (used with `create`)
constexpr auto lock = +open_mode::lock; // Prevent opening the file more than once
constexpr auto unread = +open_mode::unread; // Aggressively prevent reading the opened file (do not use)
constexpr auto rewrite = open_mode::write + open_mode::create + open_mode::trunc;
// File seek mode
enum class seek_mode : u32
{
seek_set,
seek_cur,
seek_end,
};
constexpr auto seek_set = seek_mode::seek_set; // From beginning
constexpr auto seek_cur = seek_mode::seek_cur; // From current position
constexpr auto seek_end = seek_mode::seek_end; // From end
// File attributes (TODO)
struct stat_t
{
bool is_directory;
bool is_writable;
u64 size;
s64 atime;
s64 mtime;
s64 ctime;
};
// Native handle getter
struct get_native_handle
{
virtual native_handle get() = 0;
};
// File handle base
struct file_base
{
virtual ~file_base();
virtual stat_t stat();
virtual void sync();
virtual bool trunc(u64 length) = 0;
virtual u64 read(void* buffer, u64 size) = 0;
virtual u64 write(const void* buffer, u64 size) = 0;
virtual u64 seek(s64 offset, seek_mode whence) = 0;
virtual u64 size() = 0;
};
// Directory entry (TODO)
struct dir_entry : stat_t
{
std::string name;
};
// Directory handle base
struct dir_base
{
virtual ~dir_base();
virtual bool read(dir_entry&) = 0;
virtual void rewind() = 0;
};
// Device information
struct device_stat
{
u64 block_size;
u64 total_size;
u64 total_free; // Total size of free space
u64 avail_free; // Free space available to unprivileged user
};
// Virtual device
struct device_base
{
virtual ~device_base();
virtual bool stat(const std::string& path, stat_t& info) = 0;
virtual bool statfs(const std::string& path, device_stat& info) = 0;
virtual bool remove_dir(const std::string& path) = 0;
virtual bool create_dir(const std::string& path) = 0;
virtual bool rename(const std::string& from, const std::string& to) = 0;
virtual bool remove(const std::string& path) = 0;
virtual bool trunc(const std::string& path, u64 length) = 0;
virtual bool utime(const std::string& path, s64 atime, s64 mtime) = 0;
virtual std::unique_ptr<file_base> open(const std::string& path, bs_t<open_mode> mode) = 0;
virtual std::unique_ptr<dir_base> open_dir(const std::string& path) = 0;
};
// Get virtual device for specified path (nullptr for real path)
std::shared_ptr<device_base> get_virtual_device(const std::string& path);
// Set virtual device with specified name (nullptr for deletion)
std::shared_ptr<device_base> set_virtual_device(const std::string& root_name, const std::shared_ptr<device_base>&);
// Try to get parent directory (returns empty string on failure)
std::string get_parent_dir(const std::string& path);
// Get file information
bool stat(const std::string& path, stat_t& info);
// Check whether a file or a directory exists (not recommended, use is_file() or is_dir() instead)
bool exists(const std::string& path);
// Check whether the file exists and is NOT a directory
bool is_file(const std::string& path);
// Check whether the directory exists and is NOT a file
bool is_dir(const std::string& path);
// Get filesystem information
bool statfs(const std::string& path, device_stat& info);
// Delete empty directory
bool remove_dir(const std::string& path);
// Create directory
bool create_dir(const std::string& path);
// Create directories
bool create_path(const std::string& path);
// Rename (move) file or directory
bool rename(const std::string& from, const std::string& to, bool overwrite);
// Copy file contents
bool copy_file(const std::string& from, const std::string& to, bool overwrite);
// Delete file
bool remove_file(const std::string& path);
// Change file size (possibly appending zeros)
bool truncate_file(const std::string& path, u64 length);
// Set file access/modification time
bool utime(const std::string& path, s64 atime, s64 mtime);
class file final
{
std::unique_ptr<file_base> m_file;
[[noreturn]] void xnull() const;
[[noreturn]] void xfail() const;
public:
// Default constructor
file() = default;
// Open file with specified mode
explicit file(const std::string& path, bs_t<open_mode> mode = ::fs::read);
// Open memory for read
explicit file(const void* ptr, std::size_t size);
// Open file with specified args (forward to constructor)
template <typename... Args>
bool open(Args&&... args)
{
*this = fs::file(std::forward<Args>(args)...);
return m_file.operator bool();
}
// Check whether the handle is valid (opened file)
explicit operator bool() const
{
return m_file.operator bool();
}
// Close the file explicitly
void close()
{
m_file.reset();
}
void reset(std::unique_ptr<file_base>&& ptr)
{
m_file = std::move(ptr);
}
std::unique_ptr<file_base> release()
{
return std::move(m_file);
}
// Change file size (possibly appending zero bytes)
bool trunc(u64 length) const
{
if (!m_file) xnull();
return m_file->trunc(length);
}
// Get file information
stat_t stat() const
{
if (!m_file) xnull();
return m_file->stat();
}
// Sync file buffers
void sync() const
{
if (!m_file) xnull();
return m_file->sync();
}
// Read the data from the file and return the amount of data written in buffer
u64 read(void* buffer, u64 count) const
{
if (!m_file) xnull();
return m_file->read(buffer, count);
}
// Write the data to the file and return the amount of data actually written
u64 write(const void* buffer, u64 count) const
{
if (!m_file) xnull();
return m_file->write(buffer, count);
}
// Change current position, returns resulting position
u64 seek(s64 offset, seek_mode whence = seek_set) const
{
if (!m_file) xnull();
return m_file->seek(offset, whence);
}
// Get file size
u64 size() const
{
if (!m_file) xnull();
return m_file->size();
}
// Get current position
u64 pos() const
{
if (!m_file) xnull();
return m_file->seek(0, seek_cur);
}
// Write std::string unconditionally
const file& write(const std::string& str) const
{
if (write(str.data(), str.size()) != str.size()) xfail();
return *this;
}
// Write POD unconditionally
template<typename T>
std::enable_if_t<std::is_pod<T>::value && !std::is_pointer<T>::value, const file&> write(const T& data) const
{
if (write(std::addressof(data), sizeof(T)) != sizeof(T)) xfail();
return *this;
}
// Write POD std::vector unconditionally
template<typename T>
std::enable_if_t<std::is_pod<T>::value && !std::is_pointer<T>::value, const file&> write(const std::vector<T>& vec) const
{
if (write(vec.data(), vec.size() * sizeof(T)) != vec.size() * sizeof(T)) xfail();
return *this;
}
// Read std::string, size must be set by resize() method
bool read(std::string& str) const
{
return read(&str[0], str.size()) == str.size();
}
// Read std::string
bool read(std::string& str, std::size_t size) const
{
str.resize(size);
return read(&str[0], size) == size;
}
// Read POD, sizeof(T) is used
template<typename T>
std::enable_if_t<std::is_pod<T>::value && !std::is_pointer<T>::value, bool> read(T& data) const
{
return read(&data, sizeof(T)) == sizeof(T);
}
// Read POD std::vector, size must be set by resize() method
template<typename T>
std::enable_if_t<std::is_pod<T>::value && !std::is_pointer<T>::value, bool> read(std::vector<T>& vec) const
{
return read(vec.data(), sizeof(T) * vec.size()) == sizeof(T) * vec.size();
}
// Read POD std::vector
template<typename T>
std::enable_if_t<std::is_pod<T>::value && !std::is_pointer<T>::value, bool> read(std::vector<T>& vec, std::size_t size) const
{
vec.resize(size);
return read(vec.data(), sizeof(T) * size) == sizeof(T) * size;
}
// Read POD (experimental)
template<typename T>
std::enable_if_t<std::is_pod<T>::value && !std::is_pointer<T>::value, T> read() const
{
T result;
if (!read(result)) xfail();
return result;
}
// Read full file to std::string
std::string to_string() const
{
std::string result;
result.resize(size());
if (seek(0), !read(result)) xfail();
return result;
}
// Read full file to std::vector
template<typename T>
std::enable_if_t<std::is_pod<T>::value && !std::is_pointer<T>::value, std::vector<T>> to_vector() const
{
std::vector<T> result;
result.resize(size() / sizeof(T));
if (seek(0), !read(result)) xfail();
return result;
}
// Get native handle if available
native_handle get_handle() const;
};
class dir final
{
std::unique_ptr<dir_base> m_dir;
[[noreturn]] void xnull() const;
public:
dir() = default;
// Open dir handle
explicit dir(const std::string& path)
{
open(path);
}
// Open specified directory
bool open(const std::string& path);
// Check whether the handle is valid (opened directory)
explicit operator bool() const
{
return m_dir.operator bool();
}
// Close the directory explicitly
void close()
{
m_dir.reset();
}
void reset(std::unique_ptr<dir_base>&& ptr)
{
m_dir = std::move(ptr);
}
std::unique_ptr<dir_base> release()
{
return std::move(m_dir);
}
// Get next directory entry
bool read(dir_entry& out) const
{
if (!m_dir) xnull();
return m_dir->read(out);
}
// Reset to the beginning
void rewind() const
{
if (!m_dir) xnull();
return m_dir->rewind();
}
class iterator
{
const dir* m_parent;
dir_entry m_entry;
public:
enum class mode
{
from_first,
from_current
};
iterator(const dir* parent, mode mode_ = mode::from_first)
: m_parent(parent)
{
if (!m_parent)
{
return;
}
if (mode_ == mode::from_first)
{
m_parent->rewind();
}
if (!m_parent->read(m_entry))
{
m_parent = nullptr;
}
}
dir_entry& operator *()
{
return m_entry;
}
iterator& operator++()
{
*this = {m_parent, mode::from_current};
return *this;
}
bool operator !=(const iterator& rhs) const
{
return m_parent != rhs.m_parent;
}
};
iterator begin() const
{
return {m_dir ? this : nullptr};
}
iterator end() const
{
return {nullptr};
}
};
// Get configuration directory
const std::string& get_config_dir();
// Get data/cache directory for specified prefix and suffix
std::string get_data_dir(const std::string& prefix, const std::string& location, const std::string& suffix);
// Get data/cache directory for specified prefix and path (suffix will be filename)
std::string get_data_dir(const std::string& prefix, const std::string& path);
// Delete directory and all its contents recursively
void remove_all(const std::string& path, bool remove_root = true);
// Get size of all files recursively
u64 get_dir_size(const std::string& path);
enum class error : uint
{
ok = 0,
inval,
noent,
exist,
acces,
notempty,
};
// Error code returned
extern thread_local error g_tls_error;
template <typename T>
struct container_stream final : file_base
{
// T can be a reference, but this is not recommended
using value_type = typename std::remove_reference_t<T>::value_type;
T obj;
u64 pos;
container_stream(T&& obj)
: obj(std::forward<T>(obj))
, pos(0)
{
}
~container_stream() override
{
}
bool trunc(u64 length) override
{
obj.resize(length);
return true;
}
u64 read(void* buffer, u64 size) override
{
const u64 end = obj.size();
if (pos < end)
{
// Get readable size
if (const u64 max = std::min<u64>(size, end - pos))
{
std::copy(obj.cbegin() + pos, obj.cbegin() + pos + max, static_cast<value_type*>(buffer));
pos = pos + max;
return max;
}
}
return 0;
}
u64 write(const void* buffer, u64 size) override
{
const u64 old_size = obj.size();
if (old_size + size < old_size)
{
fmt::raw_error("fs::container_stream<>::write(): overflow");
}
if (pos > old_size)
{
// Fill gap if necessary (default-initialized)
obj.resize(pos);
}
const auto src = static_cast<const value_type*>(buffer);
// Overwrite existing part
const u64 overlap = std::min<u64>(obj.size() - pos, size);
std::copy(src, src + overlap, obj.begin() + pos);
// Append new data
obj.insert(obj.end(), src + overlap, src + size);
pos += size;
return size;
}
u64 seek(s64 offset, seek_mode whence) override
{
const s64 new_pos =
whence == fs::seek_set ? offset :
whence == fs::seek_cur ? offset + pos :
whence == fs::seek_end ? offset + size() :
(fmt::raw_error("fs::container_stream<>::seek(): invalid whence"), 0);
if (new_pos < 0)
{
fs::g_tls_error = fs::error::inval;
return -1;
}
pos = new_pos;
return pos;
}
u64 size() override
{
return obj.size();
}
};
template <typename T>
file make_stream(T&& container = T{})
{
file result;
result.reset(std::make_unique<container_stream<T>>(std::forward<T>(container)));
return result;
}
template <typename... Args>
bool write_file(const std::string& path, bs_t<fs::open_mode> mode, const Args&... args)
{
if (fs::file f{path, mode})
{
// Write args sequentially
int seq[]{ (f.write(args), 0)... };
return true;
}
return false;
}
}