-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSystem.cpp
180 lines (145 loc) · 5.12 KB
/
FileSystem.cpp
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
/*******************************************************************************
* libunix++: C++ wrapper for Linux system calls
* Static filesystem operations
*
* © 2019—2023, Sauron <[email protected]>
******************************************************************************/
#include <linux/limits.h>
#include <sys/time.h>
#include <sys/xattr.h>
#include "exception.hppi"
#include "FileSystem.hpp"
#include <cstdio>
using namespace upp;
using std::string;
bool FileSystem::access(const char * pathname, int mode) {
int retval=::access(pathname, mode);
if (retval<0) {
if ((mode==F_OK)&&(errno==ENOENT))
return false;
else if (errno==EACCES)
return false;
else
THROW_SYSTEM_ERROR;
}
else
return true;
}
void FileSystem::chdir(const char * path) {
NORMAL_OP_WRAPPER(::chdir(path));
}
void FileSystem::chmod(const char * pathname, mode_t mode) {
NORMAL_OP_WRAPPER(::chmod(pathname, mode));
}
void FileSystem::chown(const char * pathname, uid_t owner, gid_t group) {
NORMAL_OP_WRAPPER(::chown(pathname, owner, group));
}
void FileSystem::chroot(const char * path) {
NORMAL_OP_WRAPPER(::chroot(path));
}
string FileSystem::getcwd() {
char buffer[PATH_MAX];
NORMAL_POP_WRAPPER(::getcwd(buffer, sizeof(buffer)));
return buffer;
}
void FileSystem::lchown(const char * pathname, uid_t owner, gid_t group) {
NORMAL_OP_WRAPPER(::lchown(pathname, owner, group));
}
void FileSystem::link(const char * oldpath, const char * newpath) {
NORMAL_OP_WRAPPER(::link(oldpath, newpath));
}
void FileSystem::lstat(const char * pathname, Stat * statbuf) {
NORMAL_OP_WRAPPER(::lstat(pathname, statbuf))
}
FileSystem::Stat FileSystem::lstat(const char * pathname) {
Stat statbuf;
FileSystem::lstat(pathname, &statbuf);
return statbuf;
}
void FileSystem::mkdir(const char * pathname, mode_t mode) {
NORMAL_OP_WRAPPER(::mkdir(pathname, mode))
}
bool FileSystem::mkdirp(const char * pathname, mode_t mode) {
int retval=::mkdir(pathname, mode);
if (retval>=0)
return false;
else if (errno==EEXIST)
return true;
else
THROW_SYSTEM_ERROR;
}
void FileSystem::mknod(const char * pathname, mode_t mode, dev_t dev) {
NORMAL_OP_WRAPPER(::mknod(pathname, mode, dev));
}
ssize_t FileSystem::readlink(const char * pathname, char * buf, size_t bufsiz) {
THROW_NOT_IMPLEMENTED;
}
void FileSystem::rename(const char * oldpath, const char * newpath) {
NORMAL_OP_WRAPPER(::rename(oldpath, newpath))
}
void FileSystem::rmdir(const char * pathname) {
NORMAL_OP_WRAPPER(::rmdir(pathname))
}
void FileSystem::stat(const char * pathname, Stat * statbuf) {
NORMAL_OP_WRAPPER(::stat(pathname, statbuf))
}
FileSystem::Stat FileSystem::stat(const char * pathname) {
Stat statbuf;
FileSystem::stat(pathname, &statbuf);
return statbuf;
}
void FileSystem::statfs(const char * path, StatFS &buf) {
NORMAL_OP_WRAPPER(::statfs(path, &buf));
}
FileSystem::StatFS FileSystem::statfs(const char * path) {
StatFS result;
FileSystem::statfs(path, result);
return result;
}
void FileSystem::symlink(const char * target, const char * linkpath) {
NORMAL_OP_WRAPPER(::symlink(target, linkpath));
}
void FileSystem::sync() {
::sync();
}
void FileSystem::truncate(const char * pathname, off_t length) {
NORMAL_OP_WRAPPER(::truncate(pathname, length));
}
void FileSystem::unlink(const char * pathname) {
NORMAL_OP_WRAPPER(::unlink(pathname))
}
void FileSystem::utimes(const char * filename, const struct timeval times[2]) {
NORMAL_OP_WRAPPER(::utimes(filename, times));
}
void FileSystem::setAttribute(const char * pathname, const char * name, const char * value, size_t size, int flags) {
THROW_SYSTEM_ERROR_STD(setxattr(pathname, name, value, size, flags));
}
void FileSystem::setAttributeL(const char * pathname, const char * name, const char * value, size_t size, int flags) {
THROW_SYSTEM_ERROR_STD(lsetxattr(pathname, name, value, size, flags));
}
size_t FileSystem::getAttribute(const char * pathname, const char * name, char * value, size_t size) {
ssize_t retval=getxattr(pathname, name, value, size);
THROW_SYSTEM_ERROR_IF(retval<0);
return size_t(retval);
}
size_t FileSystem::getAttributeL(const char * pathname, const char * name, char * value, size_t size) {
ssize_t retval=lgetxattr(pathname, name, value, size);
THROW_SYSTEM_ERROR_IF(retval<0);
return size_t(retval);
}
size_t FileSystem::listAttributes(const char * pathname, char * list, size_t size) {
ssize_t retval=listxattr(pathname, list, size);
THROW_SYSTEM_ERROR_IF(retval<0);
return size_t(retval);
}
size_t FileSystem::listAttributesL(const char * pathname, char * list, size_t size) {
ssize_t retval=llistxattr(pathname, list, size);
THROW_SYSTEM_ERROR_IF(retval<0);
return size_t(retval);
}
void FileSystem::removeAttribute(const char * pathname, const char * name) {
THROW_SYSTEM_ERROR_STD(removexattr(pathname, name));
}
void FileSystem::removeAttributeL(const char * pathname, const char * name) {
THROW_SYSTEM_ERROR_STD(lremovexattr(pathname, name));
}