-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinode.hpp
83 lines (67 loc) · 2.22 KB
/
inode.hpp
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
#pragma once
//generic abstraction layer for common storage operations against both files and directories
//these functions are not recursive; use directory::create() and directory::remove() for recursion
#include <nall/platform.hpp>
#include <nall/string.hpp>
namespace nall {
struct inode {
enum class time : uint { access, modify };
static auto exists(const string& name) -> bool {
return access(name, F_OK) == 0;
}
static auto readable(const string& name) -> bool {
return access(name, R_OK) == 0;
}
static auto writable(const string& name) -> bool {
return access(name, W_OK) == 0;
}
static auto executable(const string& name) -> bool {
return access(name, X_OK) == 0;
}
static auto uid(const string& name) -> uint {
struct stat data{0};
stat(name, &data);
return data.st_uid;
}
static auto gid(const string& name) -> uint {
struct stat data{0};
stat(name, &data);
return data.st_gid;
}
static auto mode(const string& name) -> uint {
struct stat data{0};
stat(name, &data);
return data.st_mode;
}
static auto timestamp(const string& name, time mode = time::modify) -> uint64_t {
struct stat data = {0};
stat(name, &data);
switch(mode) { default:
case time::access: return data.st_atime;
case time::modify: return data.st_mtime;
}
}
//returns true if 'name' already exists
static auto create(const string& name, uint permissions = 0755) -> bool {
if(exists(name)) return true;
if(name.endsWith("/")) return mkdir(name, permissions) == 0;
int fd = open(name, O_CREAT | O_EXCL, permissions);
if(fd < 0) return false;
return close(fd), true;
}
//returns false if 'name' and 'targetname' are on different file systems (requires copy)
static auto rename(const string& name, const string& targetname) -> bool {
return ::rename(name, targetname) == 0;
}
//returns false if 'name' is a directory that is not empty
static auto remove(const string& name) -> bool {
#if defined(PLATFORM_WINDOWS)
if(name.endsWith("/")) return _wrmdir(utf16_t(name)) == 0;
return _wunlink(utf16_t(name)) == 0;
#else
if(name.endsWith("/")) return rmdir(name) == 0;
return unlink(name) == 0;
#endif
}
};
}