mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-15 19:50:47 +00:00
General QoL changes
This commit is contained in:
@@ -1,8 +1,4 @@
|
||||
/* file.cpp - Contains all files related utilities
|
||||
*/
|
||||
|
||||
#include <sys/sendfile.h>
|
||||
#include <sys/mman.h>
|
||||
#include <linux/fs.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
@@ -359,7 +355,7 @@ void write_zero(int fd, size_t size) {
|
||||
}
|
||||
}
|
||||
|
||||
void file_readline(const char *file, const function<bool (string_view)> &fn, bool trim) {
|
||||
void file_readline(bool trim, const char *file, const std::function<bool(std::string_view)> &fn) {
|
||||
FILE *fp = xfopen(file, "re");
|
||||
if (fp == nullptr)
|
||||
return;
|
||||
@@ -384,7 +380,7 @@ void file_readline(const char *file, const function<bool (string_view)> &fn, boo
|
||||
}
|
||||
|
||||
void parse_prop_file(const char *file, const function<bool (string_view, string_view)> &fn) {
|
||||
file_readline(file, [&](string_view line_view) -> bool {
|
||||
file_readline(true, file, [&](string_view line_view) -> bool {
|
||||
char *line = (char *) line_view.data();
|
||||
if (line[0] == '#')
|
||||
return true;
|
||||
@@ -393,7 +389,7 @@ void parse_prop_file(const char *file, const function<bool (string_view, string_
|
||||
return true;
|
||||
*eql = '\0';
|
||||
return fn(line, eql + 1);
|
||||
}, true);
|
||||
});
|
||||
}
|
||||
|
||||
void parse_mnt(const char *file, const function<bool (mntent*)> &fn) {
|
||||
|
@@ -1,17 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <mntent.h>
|
||||
#include <functional>
|
||||
#include <string_view>
|
||||
|
||||
#include "xwrap.h"
|
||||
|
||||
#define do_align(p, a) (((p) + (a) - 1) / (a) * (a))
|
||||
#define align_off(p, a) (do_align(p, a) - (p))
|
||||
|
||||
using sFILE = std::unique_ptr<FILE, decltype(&fclose)>;
|
||||
static inline sFILE make_sFILE(FILE *fp = nullptr) {
|
||||
return sFILE(fp, fclose);
|
||||
}
|
||||
|
||||
struct file_attr {
|
||||
struct stat st;
|
||||
char con[128];
|
||||
@@ -36,12 +35,15 @@ void clone_attr(const char *source, const char *target);
|
||||
void fd_full_read(int fd, void **buf, size_t *size);
|
||||
void full_read(const char *filename, void **buf, size_t *size);
|
||||
void write_zero(int fd, size_t size);
|
||||
|
||||
void file_readline(const char *file, const std::function<bool (std::string_view)> &fn, bool trim = false);
|
||||
void parse_prop_file(const char *file, const std::function
|
||||
<bool(std::string_view, std::string_view)> &fn);
|
||||
void file_readline(bool trim, const char *file, const std::function<bool(std::string_view)> &fn);
|
||||
static inline void file_readline(const char *file,
|
||||
const std::function<bool(std::string_view)> &fn) {
|
||||
file_readline(false, file, fn);
|
||||
}
|
||||
void parse_prop_file(const char *file,
|
||||
const std::function<bool(std::string_view, std::string_view)> &fn);
|
||||
void *__mmap(const char *filename, size_t *size, bool rw);
|
||||
void frm_rf(int dirfd, std::initializer_list<const char *> excl = std::initializer_list<const char *>());
|
||||
void frm_rf(int dirfd, std::initializer_list<const char *> excl = {});
|
||||
void clone_dir(int src, int dest, bool overwrite = true);
|
||||
void parse_mnt(const char *file, const std::function<bool(mntent*)> &fn);
|
||||
|
||||
@@ -80,3 +82,22 @@ void mmap_rw(const char *filename, B &buf, L &sz) {
|
||||
buf = (B) __mmap(filename, &__sz, true);
|
||||
sz = __sz;
|
||||
}
|
||||
|
||||
using sFILE = std::unique_ptr<FILE, decltype(&fclose)>;
|
||||
using sDIR = std::unique_ptr<DIR, decltype(&closedir)>;
|
||||
|
||||
static inline sDIR open_dir(const char *path) {
|
||||
return sDIR(opendir(path), closedir);
|
||||
}
|
||||
|
||||
static inline sDIR xopen_dir(const char *path) {
|
||||
return sDIR(xopendir(path), closedir);
|
||||
}
|
||||
|
||||
static inline sFILE open_file(const char *path, const char *mode) {
|
||||
return sFILE(fopen(path, mode), fclose);
|
||||
}
|
||||
|
||||
static inline sFILE xopen_file(const char *path, const char *mode) {
|
||||
return sFILE(xfopen(path, mode), fclose);
|
||||
}
|
||||
|
@@ -5,17 +5,6 @@
|
||||
|
||||
#include "../files.h"
|
||||
|
||||
class stream;
|
||||
|
||||
using stream_ptr = std::unique_ptr<stream>;
|
||||
|
||||
sFILE make_stream(stream_ptr &&strm);
|
||||
|
||||
template <class T, class... Args>
|
||||
sFILE make_stream(Args &&... args) {
|
||||
return make_stream(stream_ptr(new T(std::forward<Args>(args)...)));
|
||||
}
|
||||
|
||||
class stream {
|
||||
public:
|
||||
virtual int read(void *buf, size_t len);
|
||||
@@ -24,35 +13,22 @@ public:
|
||||
virtual ~stream() = default;
|
||||
};
|
||||
|
||||
// Delegates all operations to the base FILE pointer
|
||||
using stream_ptr = std::unique_ptr<stream>;
|
||||
|
||||
// Delegates all operations to base stream
|
||||
class filter_stream : public stream {
|
||||
public:
|
||||
filter_stream(sFILE &&fp = make_sFILE()) : fp(std::move(fp)) {}
|
||||
filter_stream(stream_ptr &&base) : base(std::move(base)) {}
|
||||
|
||||
int read(void *buf, size_t len) override;
|
||||
int write(const void *buf, size_t len) override;
|
||||
|
||||
void set_base(sFILE &&f);
|
||||
template <class T, class... Args >
|
||||
void set_base(Args&&... args) {
|
||||
set_base(make_stream<T>(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
protected:
|
||||
sFILE fp;
|
||||
};
|
||||
|
||||
// Handy interface for classes that need custom seek logic
|
||||
class seekable_stream : public stream {
|
||||
protected:
|
||||
size_t _pos = 0;
|
||||
|
||||
off_t seek_pos(off_t off, int whence);
|
||||
virtual size_t end_pos() = 0;
|
||||
stream_ptr base;
|
||||
};
|
||||
|
||||
// Byte stream that dynamically allocates memory
|
||||
class byte_stream : public seekable_stream {
|
||||
class byte_stream : public stream {
|
||||
public:
|
||||
byte_stream(uint8_t *&buf, size_t &len);
|
||||
template <class byte>
|
||||
@@ -64,10 +40,10 @@ public:
|
||||
private:
|
||||
uint8_t *&_buf;
|
||||
size_t &_len;
|
||||
size_t _pos = 0;
|
||||
size_t _cap = 0;
|
||||
|
||||
void resize(size_t new_pos, bool zero = false);
|
||||
size_t end_pos() final { return _len; }
|
||||
};
|
||||
|
||||
// File stream but does not close the file descriptor at any time
|
||||
@@ -81,3 +57,28 @@ public:
|
||||
private:
|
||||
int fd;
|
||||
};
|
||||
|
||||
/* ****************************************
|
||||
* Bridge between stream class and C stdio
|
||||
* ****************************************/
|
||||
|
||||
// sFILE -> stream_ptr
|
||||
class fp_stream final : public stream {
|
||||
public:
|
||||
fp_stream(FILE *fp = nullptr) : fp(fp, fclose) {}
|
||||
fp_stream(sFILE &&fp) : fp(std::move(fp)) {}
|
||||
int read(void *buf, size_t len) override;
|
||||
int write(const void *buf, size_t len) override;
|
||||
off_t seek(off_t off, int whence) override;
|
||||
|
||||
private:
|
||||
sFILE fp;
|
||||
};
|
||||
|
||||
// stream_ptr -> sFILE
|
||||
sFILE make_stream_fp(stream_ptr &&strm);
|
||||
|
||||
template <class T, class... Args>
|
||||
sFILE make_stream_fp(Args &&... args) {
|
||||
return make_stream_fp(stream_ptr(new T(std::forward<Args>(args)...)));
|
||||
}
|
||||
|
@@ -1,14 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
#include <stdio.h>
|
||||
#include <dirent.h>
|
||||
#include <pthread.h>
|
||||
#include <poll.h>
|
||||
#include <mntent.h>
|
||||
|
||||
#include "../missing.h"
|
||||
#include "../xwrap.h"
|
||||
#include "../files.h"
|
||||
|
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <pthread.h>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <string_view>
|
||||
|
@@ -207,11 +207,10 @@ void restore_rootcon() {
|
||||
setfilecon(MIRRDIR, ROOT_CON);
|
||||
setfilecon(BLOCKDIR, ROOT_CON);
|
||||
|
||||
struct dirent *entry;
|
||||
DIR *dir = xopendir("/sbin");
|
||||
int dfd = dirfd(dir);
|
||||
auto dir = xopen_dir("/sbin");
|
||||
int dfd = dirfd(dir.get());
|
||||
|
||||
while ((entry = xreaddir(dir))) {
|
||||
for (dirent *entry; (entry = xreaddir(dir.get()));) {
|
||||
if (entry->d_name == "."sv || entry->d_name == ".."sv)
|
||||
continue;
|
||||
setfilecon_at(dfd, entry->d_name, ROOT_CON);
|
||||
@@ -220,6 +219,4 @@ void restore_rootcon() {
|
||||
setfilecon("/sbin/magisk.bin", MAGISK_CON);
|
||||
setfilecon("/sbin/magisk", MAGISK_CON);
|
||||
setfilecon("/sbin/magiskinit", MAGISK_CON);
|
||||
|
||||
closedir(dir);
|
||||
}
|
||||
|
@@ -23,7 +23,7 @@ static int strm_close(void *v) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
sFILE make_stream(stream_ptr &&strm) {
|
||||
sFILE make_stream_fp(stream_ptr &&strm) {
|
||||
sFILE fp(funopen(strm.release(), strm_read, strm_write, strm_seek, strm_close), fclose);
|
||||
setbuf(fp.get(), nullptr);
|
||||
return fp;
|
||||
@@ -44,29 +44,24 @@ off_t stream::seek(off_t off, int whence) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int filter_stream::read(void *buf, size_t len) {
|
||||
int fp_stream::read(void *buf, size_t len) {
|
||||
return fread(buf, 1, len, fp.get());
|
||||
}
|
||||
|
||||
int filter_stream::write(const void *buf, size_t len) {
|
||||
int fp_stream::write(const void *buf, size_t len) {
|
||||
return fwrite(buf, 1, len, fp.get());
|
||||
}
|
||||
|
||||
void filter_stream::set_base(sFILE &&f) {
|
||||
fp = std::move(f);
|
||||
off_t fp_stream::seek(off_t off, int whence) {
|
||||
return fseek(fp.get(), off, whence);
|
||||
}
|
||||
|
||||
off_t seekable_stream::seek_pos(off_t off, int whence) {
|
||||
switch (whence) {
|
||||
case SEEK_CUR:
|
||||
return _pos + off;
|
||||
case SEEK_END:
|
||||
return end_pos() + off;
|
||||
case SEEK_SET:
|
||||
return off;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
int filter_stream::read(void *buf, size_t len) {
|
||||
return base->read(buf, len);
|
||||
}
|
||||
|
||||
int filter_stream::write(const void *buf, size_t len) {
|
||||
return base->write(buf, len);
|
||||
}
|
||||
|
||||
byte_stream::byte_stream(uint8_t *&buf, size_t &len) : _buf(buf), _len(len) {
|
||||
@@ -89,9 +84,20 @@ int byte_stream::write(const void *buf, size_t len) {
|
||||
}
|
||||
|
||||
off_t byte_stream::seek(off_t off, int whence) {
|
||||
off_t np = seek_pos(off, whence);
|
||||
if (np < 0)
|
||||
return -1;
|
||||
off_t np;
|
||||
switch (whence) {
|
||||
case SEEK_CUR:
|
||||
np = _pos + off;
|
||||
break;
|
||||
case SEEK_END:
|
||||
np = _len + off;
|
||||
break;
|
||||
case SEEK_SET:
|
||||
np = off;
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
resize(np, true);
|
||||
_pos = np;
|
||||
return np;
|
||||
|
@@ -1,17 +1,6 @@
|
||||
/* xwrap.cpp - wrappers around existing library functions.
|
||||
*
|
||||
* Functions with the x prefix are wrappers that either succeed or log the
|
||||
* error message. They usually have the same arguments and return value
|
||||
* as the function they wrap.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <sched.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/socket.h>
|
||||
|
@@ -1,5 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <dirent.h>
|
||||
#include <stdio.h>
|
||||
#include <poll.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
FILE *xfopen(const char *pathname, const char *mode);
|
||||
FILE *xfdopen(int fd, const char *mode);
|
||||
int xopen(const char *pathname, int flags);
|
||||
|
Reference in New Issue
Block a user