forked from includeos/IncludeOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstat.cpp
More file actions
61 lines (54 loc) · 1.55 KB
/
Copy pathstat.cpp
File metadata and controls
61 lines (54 loc) · 1.55 KB
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
#include "common.hpp"
#include <sys/stat.h>
#include <fs/vfs.hpp>
#include <util/bitops.hpp> // roundto
long sys_stat(const char *path, struct stat *buf)
{
if (UNLIKELY(buf == nullptr))
return -EFAULT;
if (UNLIKELY(path == nullptr))
return -ENOENT;
memset(buf, 0, sizeof(struct stat));
try {
auto ent = fs::VFS::stat_sync(path);
if (ent.is_valid())
{
if (ent.is_file()) buf->st_mode = S_IFREG;
if (ent.is_dir()) buf->st_mode = S_IFDIR;
buf->st_dev = ent.device_id();
buf->st_ino = ent.block();
buf->st_nlink = 1;
buf->st_size = ent.size();
buf->st_atime = ent.modified();
buf->st_ctime = ent.modified();
buf->st_mtime = ent.modified();
buf->st_blocks = buf->st_size > 0 ? util::bits::roundto<512>(buf->st_size) : 0;
buf->st_blksize = ent.fs().block_size();
//printf("Is file? %s\n", S_ISREG(buf->st_mode) ? "YES" : "NO");
//PRINT("stat(%s, %p) == %d\n", path, buf, 0);
return 0;
}
else {
//PRINT("stat(%s, %p) == %d\n", path, buf, -1);
return -EIO;
}
}
catch (...)
{
//PRINT("stat(%s, %p) == %d\n", path, buf, -1);
return -EIO;
}
}
static long sys_lstat(const char *path, struct stat *buf)
{
// NOTE: should stat symlinks, instead of following them
return sys_stat(path, buf);
}
extern "C"
long syscall_SYS_stat(const char *path, struct stat *buf) {
return strace(sys_stat, "stat", path, buf);
}
extern "C"
long syscall_SYS_lstat(const char *path, struct stat *buf) {
return strace(sys_lstat, "lstat", path, buf);
}