Magisk/native/src/base/files.cpp

144 lines
3.5 KiB
C++
Raw Normal View History

2023-06-06 17:11:42 -07:00
#include <sys/mman.h>
2019-06-23 03:53:41 -07:00
#include <sys/sendfile.h>
2022-12-27 02:01:45 +08:00
#include <sys/sysmacros.h>
2019-06-23 03:53:41 -07:00
#include <linux/fs.h>
2017-10-12 02:57:18 +08:00
#include <fcntl.h>
#include <unistd.h>
2017-11-27 15:37:28 +08:00
#include <libgen.h>
2017-10-14 21:10:22 +08:00
2022-05-12 02:03:42 -07:00
#include <base.hpp>
2017-10-12 02:57:18 +08:00
2019-01-19 23:59:37 -05:00
using namespace std;
2019-03-14 06:34:22 -04:00
int fd_pathat(int dirfd, const char *name, char *path, size_t size) {
2023-06-07 16:49:40 -07:00
if (fd_path(dirfd, byte_data(path, size)) < 0)
return -1;
auto len = strlen(path);
path[len] = '/';
2022-09-15 01:17:05 -07:00
strscpy(path + len + 1, name, size - len - 1);
return 0;
2017-10-12 02:57:18 +08:00
}
2022-06-17 02:36:04 -07:00
void full_read(int fd, string &str) {
char buf[4096];
for (ssize_t len; (len = xread(fd, buf, sizeof(buf))) > 0;)
str.insert(str.end(), buf, buf + len);
}
void full_read(const char *filename, string &str) {
if (int fd = xopen(filename, O_RDONLY | O_CLOEXEC); fd >= 0) {
2022-06-17 02:36:04 -07:00
full_read(fd, str);
close(fd);
}
}
2022-06-17 02:36:04 -07:00
string full_read(int fd) {
string str;
2022-06-17 02:36:04 -07:00
full_read(fd, str);
return str;
2020-04-25 23:19:36 -07:00
}
string full_read(const char *filename) {
string str;
full_read(filename, str);
return str;
2020-04-25 23:19:36 -07:00
}
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(bool trim, FILE *fp, const function<bool(string_view)> &fn) {
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 (read && "\n\r "sv.find(buf[read - 1]) != string::npos)
--read;
buf[read] = '\0';
while (*start == ' ')
++start;
}
if (!fn(start))
break;
}
free(buf);
2018-11-03 03:06:01 -04:00
}
2019-03-05 20:27:09 -05:00
2022-06-19 00:43:27 -07:00
void file_readline(bool trim, const char *file, const function<bool(string_view)> &fn) {
2024-02-29 02:36:05 -08:00
if (auto fp = open_file(file, "re"))
file_readline(trim, fp.get(), fn);
2022-06-19 00:43:27 -07:00
}
2024-02-24 00:45:07 -08:00
2022-06-19 00:43:27 -07:00
void file_readline(const char *file, const function<bool(string_view)> &fn) {
file_readline(false, file, fn);
}
void parse_prop_file(FILE *fp, const function<bool(string_view, string_view)> &fn) {
file_readline(true, fp, [&](string_view line_view) -> bool {
char *line = (char *) line_view.data();
if (line[0] == '#')
return true;
char *eql = strchr(line, '=');
if (eql == nullptr || eql == line)
return true;
*eql = '\0';
return fn(line, eql + 1);
});
2019-03-05 20:27:09 -05:00
}
2019-06-23 03:53:41 -07:00
2022-06-19 00:43:27 -07:00
void parse_prop_file(const char *file, const function<bool(string_view, string_view)> &fn) {
2024-02-29 02:36:05 -08:00
if (auto fp = open_file(file, "re"))
parse_prop_file(fp.get(), fn);
2022-06-19 00:43:27 -07:00
}
2020-12-03 20:15:18 -08:00
sDIR make_dir(DIR *dp) {
return sDIR(dp, [](DIR *dp){ return dp ? closedir(dp) : 1; });
2020-12-03 20:15:18 -08:00
}
sFILE make_file(FILE *fp) {
return sFILE(fp, [](FILE *fp){ return fp ? fclose(fp) : 1; });
2020-12-03 20:15:18 -08:00
}
2021-11-30 01:50:55 -08:00
mmap_data::mmap_data(const char *name, bool rw) {
2023-12-26 23:08:06 +08:00
auto slice = rust::map_file(name, rw);
if (!slice.empty()) {
_buf = slice.data();
_sz = slice.size();
2021-11-30 01:50:55 -08:00
}
2023-06-06 17:11:42 -07:00
}
mmap_data::mmap_data(int fd, size_t sz, bool rw) {
auto slice = rust::map_fd(fd, sz, rw);
if (!slice.empty()) {
_buf = slice.data();
_sz = slice.size();
}
}
2022-04-08 18:03:58 +08:00
2023-06-06 17:11:42 -07:00
mmap_data::~mmap_data() {
if (_buf)
munmap(_buf, _sz);
}
string resolve_preinit_dir(const char *base_dir) {
string dir = base_dir;
if (access((dir + "/unencrypted").data(), F_OK) == 0) {
dir += "/unencrypted/magisk";
} else if (access((dir + "/adb").data(), F_OK) == 0) {
dir += "/adb/modules";
} else {
dir += "/magisk";
}
return dir;
}