forked from RPCS3/rpcs3
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFile.h
143 lines (115 loc) · 3.15 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
#pragma once
enum class fsm : u32 // file seek mode
{
begin,
cur,
end,
};
namespace fom // file open mode
{
enum : u32
{
read = 1 << 0,
write = 1 << 1,
append = 1 << 2,
create = 1 << 3,
trunc = 1 << 4,
excl = 1 << 5,
};
};
enum class fse : u32 // filesystem (file or dir) error
{
ok, // no error
invalid_arguments,
};
enum : u32 // obsolete flags
{
o_read = fom::read,
o_write = fom::write,
o_append = fom::append,
o_create = fom::create,
o_trunc = fom::trunc,
o_excl = fom::excl,
};
namespace fs
{
thread_local extern fse g_tls_error;
struct stat_t
{
bool is_directory;
bool is_writable;
u64 size;
s64 atime;
s64 mtime;
s64 ctime;
};
bool stat(const std::string& path, stat_t& info);
bool exists(const std::string& path);
bool is_file(const std::string& file);
bool is_dir(const std::string& dir);
bool remove_dir(const std::string& dir);
bool create_dir(const std::string& dir);
bool create_path(const std::string& path);
bool rename(const std::string& from, const std::string& to);
bool copy_file(const std::string& from, const std::string& to, bool overwrite);
bool remove_file(const std::string& file);
bool truncate_file(const std::string& file, u64 length);
struct file final
{
using handle_type = std::intptr_t;
static const handle_type null = -1;
private:
handle_type m_fd = null;
public:
file() = default;
~file();
explicit file(const std::string& filename, u32 mode = fom::read) { open(filename, mode); }
file(const file&) = delete;
file(file&&) = delete; // possibly TODO
file& operator =(const file&) = delete;
file& operator =(file&&) = delete; // possibly TODO
operator bool() const { return m_fd != null; }
void import(handle_type fd) { this->~file(); m_fd = fd; }
bool open(const std::string& filename, u32 mode = fom::read);
bool is_opened() const { return m_fd != null; }
bool trunc(u64 size) const; // change file size (possibly appending zero bytes)
bool stat(stat_t& info) const; // get file info
bool close();
u64 read(void* buffer, u64 count) const;
u64 write(const void* buffer, u64 count) const;
u64 seek(s64 offset, fsm seek_mode = fsm::begin) const;
u64 size() const;
};
struct dir final
{
#ifdef _WIN32
using handle_type = intptr_t;
using name_type = std::unique_ptr<wchar_t[]>;
static const handle_type null = -1;
#else
using handle_type = intptr_t;
using name_type = std::unique_ptr<char[]>;
static const handle_type null = 0;
#endif
private:
handle_type m_dd = null;
name_type m_path;
public:
dir() = default;
~dir();
explicit dir(const std::string& dirname) { open(dirname); }
dir(const dir&) = delete;
dir(dir&&) = delete; // possibly TODO
dir& operator =(const dir&) = delete;
dir& operator =(dir&&) = delete; // possibly TODO
operator bool() const { return m_path.operator bool(); }
void import(handle_type dd, const std::string& path);
bool open(const std::string& dirname);
bool is_opened() const { return *this; }
bool close();
bool get_first(std::string& name, stat_t& info);
//bool get_first(std::string& name);
bool get_next(std::string& name, stat_t& info);
//bool get_next(std::string& name);
};
}