-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallhandlerutil.cpp
More file actions
91 lines (73 loc) · 1.7 KB
/
Copy pathcallhandlerutil.cpp
File metadata and controls
91 lines (73 loc) · 1.7 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
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
#include "callhandlerutil.h"
#include "intercept.h"
#include "util.h"
#include <errno.h>
#include <sys/stat.h>
CallHandlerUtil::CallHandlerUtil(ICallHandler* next)
: next(next), ctx({get_tls(), nullptr, nullptr, 0}) {};
int CallHandlerUtil::openat(int dirfd,
const char* path,
int flags,
mode_t mode) {
if (!path) {
return -EFAULT;
}
int ret = {0};
CallOpen call;
call.from_openat(&ret, dirfd, path, flags, mode);
this->next->next(&ctx, &call);
return ret;
};
int CallHandlerUtil::fstat(int fd, struct stat* statbuf) {
if (!statbuf) {
return -EFAULT;
}
int ret = {0};
CallStat call;
call.from_fstat(&ret, fd, statbuf);
this->next->next(&ctx, &call);
return ret;
}
int CallHandlerUtil::close(int fd) {
int ret = {0};
CallClose call;
call.from_close(&ret, fd);
this->next->next(&ctx, &call);
return ret;
}
ssize_t CallHandlerUtil::read_file(char** out, const char* path) {
ssize_t ret;
ssize_t size;
int fd;
char* buf;
struct stat st;
*out = nullptr;
ret = this->openat(AT_FDCWD, path, O_RDONLY, 0);
if (ret < 0) {
return ret;
}
fd = ret;
ret = this->fstat(fd, &st);
if (ret < 0) {
this->close(fd);
return ret;
}
if (!st.st_size) {
this->close(fd);
return 0;
}
size = st.st_size;
buf = (char*)malloc(size);
if (!buf) {
this->close(fd);
return -ENOMEM;
}
ret = read_full(fd, buf, size);
this->close(fd);
if (ret != size) {
free(buf);
return -EAGAIN;
}
*out = buf;
return size;
}