Magisk/native/jni/utils/file.cpp

393 lines
9.2 KiB
C++
Raw Normal View History

2018-11-03 03:06:01 -04:00
/* file.cpp - Contains all files related utilities
2017-10-12 02:57:18 +08:00
*/
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
2017-11-15 14:00:52 +01:00
#include <errno.h>
#include <string.h>
2017-11-27 15:37:28 +08:00
#include <libgen.h>
2017-10-12 02:57:18 +08:00
#include <sys/sendfile.h>
#include <sys/mman.h>
2017-11-20 03:40:37 +08:00
#include <linux/fs.h>
2017-10-14 21:10:22 +08:00
2019-02-10 03:57:51 -05:00
#include <magisk.h>
#include <utils.h>
#include <selinux.h>
2017-10-12 02:57:18 +08:00
2019-01-19 23:59:37 -05:00
using namespace std;
2018-11-03 03:06:01 -04:00
const char **excl_list = nullptr;
2017-10-12 02:57:18 +08:00
static int is_excl(const char *name) {
if (excl_list)
for (int i = 0; excl_list[i]; ++i)
if (strcmp(name, excl_list[i]) == 0)
return 1;
2017-10-12 02:57:18 +08:00
return 0;
}
2018-09-27 00:09:59 -04:00
int fd_getpath(int fd, char *path, size_t size) {
2017-10-14 21:10:22 +08:00
snprintf(path, size, "/proc/self/fd/%d", fd);
2018-09-27 00:09:59 -04:00
return xreadlink(path, path, size) == -1;
}
2018-09-27 00:09:59 -04:00
int fd_getpathat(int dirfd, const char *name, char *path, size_t size) {
if (fd_getpath(dirfd, path, size))
return 1;
snprintf(path, size, "%s/%s", path, name);
return 0;
2017-10-14 21:10:22 +08:00
}
int mkdirs(const char *pathname, mode_t mode) {
2017-10-12 02:57:18 +08:00
char *path = strdup(pathname), *p;
errno = 0;
for (p = path + 1; *p; ++p) {
if (*p == '/') {
*p = '\0';
if (mkdir(path, mode) == -1) {
if (errno != EEXIST)
return -1;
}
*p = '/';
}
}
if (mkdir(path, mode) == -1) {
if (errno != EEXIST)
return -1;
}
free(path);
return 0;
}
2019-02-14 00:52:59 -05:00
void post_order_walk(int dirfd, void (*fn)(int, struct dirent *)) {
2017-10-12 02:57:18 +08:00
struct dirent *entry;
int newfd;
2017-12-16 02:02:17 +08:00
DIR *dir = fdopendir(dirfd);
2018-11-03 03:06:01 -04:00
if (dir == nullptr) return;
2017-10-12 02:57:18 +08:00
2017-10-14 00:08:12 +08:00
while ((entry = xreaddir(dir))) {
2017-10-12 02:57:18 +08:00
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
if (is_excl(entry->d_name))
continue;
if (entry->d_type == DT_DIR) {
2017-10-14 00:08:12 +08:00
newfd = xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC);
2019-02-14 00:52:59 -05:00
post_order_walk(newfd, fn);
2017-10-12 02:57:18 +08:00
close(newfd);
}
2019-02-14 00:52:59 -05:00
fn(dirfd, entry);
}
}
void rm_rf(const char *path) {
2019-02-12 02:44:46 -05:00
struct stat st;
if (lstat(path, &st) < 0)
return;
if (S_ISDIR(st.st_mode)) {
int fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2017-12-16 02:02:17 +08:00
frm_rf(fd);
close(fd);
}
remove(path);
}
void frm_rf(int dirfd) {
2019-02-14 00:52:59 -05:00
post_order_walk(dirfd, [](auto dirfd, auto entry) -> void {
2019-02-12 02:44:46 -05:00
unlinkat(dirfd, entry->d_name, entry->d_type == DT_DIR ? AT_REMOVEDIR : 0);
});
}
2017-10-12 02:57:18 +08:00
/* This will only on the same file system */
void mv_f(const char *source, const char *destination) {
struct stat st;
xlstat(source, &st);
int src, dest;
struct file_attr a;
if (S_ISDIR(st.st_mode)) {
xmkdirs(destination, st.st_mode & 0777);
2017-10-12 02:57:18 +08:00
src = xopen(source, O_RDONLY | O_CLOEXEC);
dest = xopen(destination, O_RDONLY | O_CLOEXEC);
fclone_attr(src, dest);
mv_dir(src, dest);
close(src);
close(dest);
} else{
getattr(source, &a);
2017-10-14 00:08:12 +08:00
xrename(source, destination);
2017-10-12 02:57:18 +08:00
setattr(destination, &a);
}
rmdir(source);
}
/* This will only on the same file system */
void mv_dir(int src, int dest) {
struct dirent *entry;
DIR *dir;
int newsrc, newdest;
struct file_attr a;
2017-10-14 00:08:12 +08:00
dir = xfdopendir(src);
while ((entry = xreaddir(dir))) {
2017-10-12 02:57:18 +08:00
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
if (is_excl(entry->d_name))
continue;
getattrat(src, entry->d_name, &a);
switch (entry->d_type) {
case DT_DIR:
2017-10-14 00:08:12 +08:00
xmkdirat(dest, entry->d_name, a.st.st_mode & 0777);
newsrc = xopenat(src, entry->d_name, O_RDONLY | O_CLOEXEC);
newdest = xopenat(dest, entry->d_name, O_RDONLY | O_CLOEXEC);
2017-10-12 02:57:18 +08:00
fsetattr(newdest, &a);
mv_dir(newsrc, newdest);
close(newsrc);
close(newdest);
unlinkat(src, entry->d_name, AT_REMOVEDIR);
break;
case DT_LNK:
case DT_REG:
renameat(src, entry->d_name, dest, entry->d_name);
setattrat(dest, entry->d_name, &a);
break;
}
}
}
void cp_afc(const char *source, const char *destination) {
int src, dest;
struct file_attr a;
getattr(source, &a);
if (S_ISDIR(a.st.st_mode)) {
xmkdirs(destination, a.st.st_mode & 0777);
2017-10-12 02:57:18 +08:00
src = xopen(source, O_RDONLY | O_CLOEXEC);
dest = xopen(destination, O_RDONLY | O_CLOEXEC);
clone_dir(src, dest);
close(src);
close(dest);
} else{
unlink(destination);
if (S_ISREG(a.st.st_mode)) {
src = xopen(source, O_RDONLY);
dest = xopen(destination, O_WRONLY | O_CREAT | O_TRUNC);
2018-11-03 03:06:01 -04:00
xsendfile(dest, src, nullptr, a.st.st_size);
2017-10-12 02:57:18 +08:00
close(src);
close(dest);
} else if (S_ISLNK(a.st.st_mode)) {
char buf[PATH_MAX];
xreadlink(source, buf, sizeof(buf));
xsymlink(buf, destination);
}
}
2018-06-17 01:25:27 +08:00
setattr(destination, &a);
2017-10-12 02:57:18 +08:00
}
void clone_dir(int src, int dest) {
struct dirent *entry;
DIR *dir;
int srcfd, destfd, newsrc, newdest;
char buf[PATH_MAX];
struct file_attr a;
2017-10-14 00:08:12 +08:00
dir = xfdopendir(src);
while ((entry = xreaddir(dir))) {
2017-10-12 02:57:18 +08:00
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
if (is_excl(entry->d_name))
continue;
getattrat(src, entry->d_name, &a);
switch (entry->d_type) {
case DT_DIR:
2017-10-14 00:08:12 +08:00
xmkdirat(dest, entry->d_name, a.st.st_mode & 0777);
2017-10-12 02:57:18 +08:00
setattrat(dest, entry->d_name, &a);
2017-10-14 00:08:12 +08:00
newsrc = xopenat(src, entry->d_name, O_RDONLY | O_CLOEXEC);
newdest = xopenat(dest, entry->d_name, O_RDONLY | O_CLOEXEC);
2017-10-12 02:57:18 +08:00
clone_dir(newsrc, newdest);
close(newsrc);
close(newdest);
break;
case DT_REG:
2017-10-14 00:08:12 +08:00
destfd = xopenat(dest, entry->d_name, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC);
srcfd = xopenat(src, entry->d_name, O_RDONLY | O_CLOEXEC);
xsendfile(destfd, srcfd, 0, a.st.st_size);
2017-10-12 02:57:18 +08:00
fsetattr(destfd, &a);
close(destfd);
close(srcfd);
break;
case DT_LNK:
2017-10-14 00:08:12 +08:00
xreadlinkat(src, entry->d_name, buf, sizeof(buf));
xsymlinkat(buf, dest, entry->d_name);
2017-10-12 02:57:18 +08:00
setattrat(dest, entry->d_name, &a);
break;
}
}
}
2018-02-02 03:22:38 +08:00
void link_dir(int src, int dest) {
struct dirent *entry;
DIR *dir;
int newsrc, newdest;
struct file_attr a;
dir = xfdopendir(src);
while ((entry = xreaddir(dir))) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
if (is_excl(entry->d_name))
continue;
if (entry->d_type == DT_DIR) {
getattrat(src, entry->d_name, &a);
xmkdirat(dest, entry->d_name, a.st.st_mode & 0777);
setattrat(dest, entry->d_name, &a);
newsrc = xopenat(src, entry->d_name, O_RDONLY | O_CLOEXEC);
newdest = xopenat(dest, entry->d_name, O_RDONLY | O_CLOEXEC);
link_dir(newsrc, newdest);
close(newsrc);
close(newdest);
} else {
xlinkat(src, entry->d_name, dest, entry->d_name, 0);
2018-02-02 03:22:38 +08:00
}
}
}
2017-10-12 02:57:18 +08:00
int getattr(const char *path, struct file_attr *a) {
2017-10-14 21:10:22 +08:00
if (xlstat(path, &a->st) == -1)
2017-10-12 02:57:18 +08:00
return -1;
2018-09-27 00:09:59 -04:00
char *con;
2017-10-14 21:10:22 +08:00
if (lgetfilecon(path, &con) == -1)
return -1;
strcpy(a->con, con);
freecon(con);
return 0;
2017-10-12 02:57:18 +08:00
}
int getattrat(int dirfd, const char *pathname, struct file_attr *a) {
char path[PATH_MAX];
fd_getpathat(dirfd, pathname, path, sizeof(path));
return getattr(path, a);
2017-10-12 02:57:18 +08:00
}
int fgetattr(int fd, struct file_attr *a) {
2017-10-14 21:10:22 +08:00
char path[PATH_MAX];
fd_getpath(fd, path, sizeof(path));
return getattr(path, a);
2017-10-12 02:57:18 +08:00
}
int setattr(const char *path, struct file_attr *a) {
2017-10-14 21:10:22 +08:00
if (chmod(path, a->st.st_mode & 0777) < 0)
2017-10-12 02:57:18 +08:00
return -1;
2017-10-14 21:10:22 +08:00
if (chown(path, a->st.st_uid, a->st.st_gid) < 0)
return -1;
if (strlen(a->con) && lsetfilecon(path, a->con) < 0)
return -1;
return 0;
2017-10-12 02:57:18 +08:00
}
int setattrat(int dirfd, const char *pathname, struct file_attr *a) {
char path[PATH_MAX];
fd_getpathat(dirfd, pathname, path, sizeof(path));
return setattr(path, a);
2017-10-12 02:57:18 +08:00
}
int fsetattr(int fd, struct file_attr *a) {
2017-10-14 21:10:22 +08:00
char path[PATH_MAX];
fd_getpath(fd, path, sizeof(path));
return setattr(path, a);
2017-10-12 02:57:18 +08:00
}
void clone_attr(const char *source, const char *target) {
struct file_attr a;
getattr(source, &a);
setattr(target, &a);
}
2019-01-19 23:59:37 -05:00
void fclone_attr(int sourcefd, int targetfd) {
2017-10-12 02:57:18 +08:00
struct file_attr a;
fgetattr(sourcefd, &a);
fsetattr(targetfd, &a);
}
2017-10-12 03:39:39 +08:00
2019-02-24 23:09:34 -05:00
void *__mmap(const char *filename, size_t *size, bool rw) {
2017-11-20 03:40:37 +08:00
struct stat st;
2019-02-24 23:09:34 -05:00
void *buf;
2018-04-15 03:18:18 +08:00
int fd = xopen(filename, (rw ? O_RDWR : O_RDONLY) | O_CLOEXEC);
2017-12-06 12:51:16 +08:00
fstat(fd, &st);
2017-11-20 03:40:37 +08:00
if (S_ISBLK(st.st_mode))
ioctl(fd, BLKGETSIZE64, size);
else
*size = st.st_size;
2019-02-24 23:09:34 -05:00
buf = *size > 0 ? xmmap(nullptr, *size, PROT_READ | (rw ? PROT_WRITE : 0), MAP_SHARED, fd, 0) : nullptr;
2017-11-10 01:51:41 +08:00
close(fd);
2019-02-24 23:09:34 -05:00
return buf;
2017-11-10 01:51:41 +08:00
}
2018-08-09 18:13:07 +08:00
void mmap_ro(const char *filename, void **buf, size_t *size) {
2019-02-24 23:09:34 -05:00
*buf = __mmap(filename, size, false);
2017-11-10 01:51:41 +08:00
}
void fd_full_read(int fd, void **buf, size_t *size) {
*size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
*buf = xmalloc(*size + 1);
xxread(fd, *buf, *size);
((char *) *buf)[*size] = '\0';
}
2017-12-07 01:30:48 +08:00
void full_read(const char *filename, void **buf, size_t *size) {
2018-04-15 03:18:18 +08:00
int fd = xopen(filename, O_RDONLY | O_CLOEXEC);
2017-12-07 01:30:48 +08:00
if (fd < 0) {
2018-11-03 03:06:01 -04:00
*buf = nullptr;
2017-12-07 01:30:48 +08:00
*size = 0;
return;
}
fd_full_read(fd, buf, size);
close(fd);
}
void full_read_at(int dirfd, const char *filename, void **buf, size_t *size) {
2018-04-15 03:18:18 +08:00
int fd = xopenat(dirfd, filename, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
2018-11-03 03:06:01 -04:00
*buf = nullptr;
*size = 0;
return;
}
fd_full_read(fd, buf, size);
2017-12-07 01:30:48 +08:00
close(fd);
}
2017-11-10 01:51:41 +08:00
void write_zero(int fd, size_t size) {
char buf[4096] = {0};
size_t len;
while (size > 0) {
len = sizeof(buf) > size ? size : sizeof(buf);
write(fd, buf, len);
size -= len;
}
2017-11-10 01:51:41 +08:00
}
2018-11-03 03:06:01 -04:00
void file_readline(const char *filename, const function<bool (string_view&)> &fn, bool trim) {
2018-11-23 21:15:44 -05:00
FILE *fp = xfopen(filename, "re");
2018-11-03 03:06:01 -04:00
if (fp == nullptr)
return;
size_t len = 1024;
char *buf = (char *) malloc(len);
char *start;
ssize_t read;
while ((read = getline(&buf, &len, fp)) >= 0) {
start = buf;
if (trim) {
while (buf[read - 1] == '\n' || buf[read - 1] == ' ')
buf[read-- - 1] = '\0';
while (*start == ' ')
++start;
}
string_view s(start);
if (!fn(s))
break;
2018-11-03 03:06:01 -04:00
}
fclose(fp);
free(buf);
2018-11-03 03:06:01 -04:00
}