forked from idealvin/coost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.cc
157 lines (128 loc) · 3.47 KB
/
fs.cc
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
#ifndef _WIN32
#include "../fs.h"
#include <assert.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
namespace fs {
bool exists(const char* path) {
struct stat attr;
return ::lstat(path, &attr) == 0;
}
bool isdir(const char* path) {
struct stat attr;
if (::lstat(path, &attr) != 0) return false;
return S_ISDIR(attr.st_mode);
}
int64 mtime(const char* path) {
struct stat attr;
if (::lstat(path, &attr) != 0) return -1;
return attr.st_mtime;
}
int64 fsize(const char* path) {
struct stat attr;
if (::lstat(path, &attr) != 0) return -1;
return attr.st_size;
}
// p = false -> mkdir
// p = true -> mkdir -p
bool mkdir(const char* path, bool p) {
if (!p) return ::mkdir(path, 0755) == 0;
const char* s = strrchr(path, '/');
if (s == 0) return ::mkdir(path, 0755) == 0;
fastring parent(path, s - path);
if (fs::exists(parent.c_str())) {
return ::mkdir(path, 0755) == 0;
} else {
return fs::mkdir(parent.c_str(), true) && ::mkdir(path, 0755) == 0;
}
}
// rf = false -> rm or rmdir
// rf = true -> rm -rf
bool remove(const char* path, bool rf) {
if (!fs::exists(path)) return true;
if (!rf) {
if (fs::isdir(path)) return ::rmdir(path) == 0;
return ::unlink(path) == 0;
} else {
fastring cmd(strlen(path) + 9);
cmd.append("rm -rf \"").append(path).append('"');
return system(cmd.c_str()) != -1;
}
}
bool rename(const char* from, const char* to) {
return ::rename(from, to) == 0;
}
bool symlink(const char* dst, const char* lnk) {
fs::remove(lnk);
return ::symlink(dst, lnk) == 0;
}
#define nullfd -1
namespace xx {
int open(const char* path, char mode) {
if (mode == 'r') {
return ::open(path, O_RDONLY);
} else if (mode == 'a') {
return ::open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
} else if (mode == 'w') {
return ::open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
} else if (mode == 'm') {
return ::open(path, O_WRONLY | O_CREAT, 0644);
} else {
return nullfd;
}
}
} // xx
struct fctx {
int fd;
fastring path;
};
file::~file() {
if (!_p) return;
this->close();
delete (fctx*) _p;
_p = 0;
}
file::operator bool() const {
fctx* p = (fctx*) _p;
return p && p->fd != nullfd;
}
const fastring& file::path() const {
fctx* p = (fctx*) _p;
return p->path;
}
bool file::open(const char* path, char mode) {
this->close();
fctx* p = (fctx*) _p;
if (!p) _p = p = new fctx;
p->path = path;
return (p->fd = xx::open(path, mode)) != nullfd;
}
void file::close() {
fctx* p = (fctx*) _p;
if (!p || p->fd == nullfd) return;
::close(p->fd);
p->fd = nullfd;
}
void file::seek(int64 off, int whence) {
static int seekfrom[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
whence = seekfrom[whence];
::lseek(((fctx*)_p)->fd, off, whence);
}
size_t file::read(void* s, size_t n) {
int64 r = ::read(((fctx*)_p)->fd, s, n);
return r < 0 ? 0 : (size_t)r;
}
fastring file::read(size_t n) {
fastring s(n + 1);
s.resize(this->read((void*)s.data(), n));
return s;
}
size_t file::write(const void* s, size_t n) {
int64 r = ::write(((fctx*)_p)->fd, s, n);
return r < 0 ? 0 : (size_t)r;
}
#undef nullfd
} // namespace fs
#endif