Rename classes and small adjustments

This commit is contained in:
topjohnwu 2019-09-25 23:55:39 -04:00
parent debd1d7d54
commit 947dae4900
13 changed files with 53 additions and 57 deletions

View File

@ -67,7 +67,7 @@ static bool check_key_combo() {
if (events.empty()) if (events.empty())
return false; return false;
RunFinally fin([&]() -> void { run_finally fin([&]() -> void {
for (const int &fd : events) for (const int &fd : events)
close(fd); close(fd);
}); });

View File

@ -22,7 +22,7 @@ uint32_t dyn_img_hdr::j32 = 0;
uint64_t dyn_img_hdr::j64 = 0; uint64_t dyn_img_hdr::j64 = 0;
static int64_t one_step(unique_ptr<Compression> &&ptr, int fd, const void *in, size_t size) { static int64_t one_step(unique_ptr<Compression> &&ptr, int fd, const void *in, size_t size) {
ptr->set_out(make_unique<FDOutStream>(fd)); ptr->setOut(make_unique<FDOutStream>(fd));
if (!ptr->write(in, size)) if (!ptr->write(in, size))
return -1; return -1;
return ptr->finalize(); return ptr->finalize();

View File

@ -62,7 +62,7 @@ void decompress(char *infile, const char *outfile) {
out_fd = strcmp(outfile, "-") == 0 ? out_fd = strcmp(outfile, "-") == 0 ?
STDOUT_FILENO : xopen(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); STDOUT_FILENO : xopen(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
cmp->set_out(make_unique<FDOutStream>(out_fd)); cmp->setOut(make_unique<FDOutStream>(out_fd));
if (ext) *ext = '.'; if (ext) *ext = '.';
} }
if (!cmp->write(buf, len)) if (!cmp->write(buf, len))
@ -108,7 +108,7 @@ void compress(const char *method, const char *infile, const char *outfile) {
STDOUT_FILENO : xopen(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); STDOUT_FILENO : xopen(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
} }
cmp->set_out(make_unique<FDOutStream>(out_fd)); cmp->setOut(make_unique<FDOutStream>(out_fd));
read_file(in_file, [&](void *buf, size_t len) -> void { read_file(in_file, [&](void *buf, size_t len) -> void {
if (!cmp->write(buf, len)) if (!cmp->write(buf, len))

View File

@ -6,7 +6,7 @@
#include <lz4.h> #include <lz4.h>
#include <lz4frame.h> #include <lz4frame.h>
#include <lz4hc.h> #include <lz4hc.h>
#include <OutStream.h> #include <stream.h>
#include "format.h" #include "format.h"

View File

@ -246,13 +246,13 @@ void magisk_cpio::compress() {
fprintf(stderr, "Compressing cpio -> [%s]\n", RAMDISK_XZ); fprintf(stderr, "Compressing cpio -> [%s]\n", RAMDISK_XZ);
auto init = entries.extract("init"); auto init = entries.extract("init");
XZEncoder encoder; XZEncoder encoder;
encoder.set_out(make_unique<BufOutStream>()); encoder.setOut(make_unique<BufOutStream>());
output(encoder); output(encoder);
encoder.finalize(); encoder.finalize();
entries.clear(); entries.clear();
entries.insert(std::move(init)); entries.insert(std::move(init));
auto xz = new cpio_entry(RAMDISK_XZ, S_IFREG); auto xz = new cpio_entry(RAMDISK_XZ, S_IFREG);
static_cast<BufOutStream *>(encoder.get_out())->release(xz->data, xz->filesize); static_cast<BufOutStream *>(encoder.getOut())->release(xz->data, xz->filesize);
insert(xz); insert(xz);
} }
@ -262,13 +262,13 @@ void magisk_cpio::decompress() {
return; return;
fprintf(stderr, "Decompressing cpio [%s]\n", RAMDISK_XZ); fprintf(stderr, "Decompressing cpio [%s]\n", RAMDISK_XZ);
LZMADecoder decoder; LZMADecoder decoder;
decoder.set_out(make_unique<BufOutStream>()); decoder.setOut(make_unique<BufOutStream>());
decoder.write(it->second->data, it->second->filesize); decoder.write(it->second->data, it->second->filesize);
decoder.finalize(); decoder.finalize();
entries.erase(it); entries.erase(it);
char *buf; char *buf;
size_t sz; size_t sz;
static_cast<BufOutStream *>(decoder.get_out())->getbuf(buf, sz); static_cast<BufOutStream *>(decoder.getOut())->getbuf(buf, sz);
load_cpio(buf, sz); load_cpio(buf, sz);
} }

View File

@ -36,7 +36,7 @@ static inline void lazy_unmount(const char* mountpoint) {
} }
void hide_daemon(int pid) { void hide_daemon(int pid) {
RunFinally fin([=]() -> void { run_finally fin([=]() -> void {
// Send resume signal // Send resume signal
kill(pid, SIGCONT); kill(pid, SIGCONT);
_exit(0); _exit(0);

View File

@ -98,7 +98,7 @@ static int add_list(const char *pkg, const char *proc = "") {
// Critical region // Critical region
{ {
MutexGuard lock(monitor_lock); mutex_guard lock(monitor_lock);
hide_set.emplace(pkg, proc); hide_set.emplace(pkg, proc);
} }
@ -119,7 +119,7 @@ int add_list(int client) {
static int rm_list(const char *pkg, const char *proc = "") { static int rm_list(const char *pkg, const char *proc = "") {
{ {
// Critical region // Critical region
MutexGuard lock(monitor_lock); mutex_guard lock(monitor_lock);
bool remove = false; bool remove = false;
auto next = hide_set.begin(); auto next = hide_set.begin();
decltype(next) cur; decltype(next) cur;

View File

@ -76,7 +76,7 @@ static inline long xptrace(int request, pid_t pid, void *addr = nullptr, intptr_
} }
void update_uid_map() { void update_uid_map() {
MutexGuard lock(monitor_lock); mutex_guard lock(monitor_lock);
uid_proc_map.clear(); uid_proc_map.clear();
string data_path(APP_DATA_DIR); string data_path(APP_DATA_DIR);
data_path += "/0/"; data_path += "/0/";
@ -336,7 +336,7 @@ void proc_monitor() {
continue; continue;
} }
bool detach = false; bool detach = false;
RunFinally detach_task([&] { run_finally f([&] {
if (detach) if (detach)
// Non of our business now // Non of our business now
detach_pid(pid); detach_pid(pid);

View File

@ -89,7 +89,7 @@ static shared_ptr<su_info> get_su_info(unsigned uid) {
shared_ptr<su_info> info; shared_ptr<su_info> info;
{ {
MutexGuard lock(cache_lock); mutex_guard lock(cache_lock);
if (!cached || cached->uid != uid || !cached->is_fresh()) if (!cached || cached->uid != uid || !cached->is_fresh())
cached = make_shared<su_info>(uid); cached = make_shared<su_info>(uid);
cached->refresh(); cached->refresh();
@ -97,7 +97,7 @@ static shared_ptr<su_info> get_su_info(unsigned uid) {
} }
info->lock(); info->lock();
RunFinally unlock([&] { run_finally unlock([&] {
info->unlock(); info->unlock();
}); });

View File

@ -2,41 +2,41 @@
#include <pthread.h> #include <pthread.h>
#include <deque> #include <deque>
#include <misc.h>
template<typename T> template<typename T>
class BlockingQueue { class blocking_queue {
std::deque<T> deque{}; std::deque<T> deque{};
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
bool cancelled = false; bool cancelled = false;
public: public:
~BlockingQueue(); ~blocking_queue();
T take(); T take();
T &front(); T &front() const;
T &back(); T &back() const;
void put(const T&); void push(const T&);
void put(T&&); void push(T&&);
template< class... Args > template< class... Args >
void emplace_back(Args&&... args); void emplace(Args &&... args);
void clear(); void clear();
void cancel(); void cancel();
}; };
#define run_and_notify(block) \ #define run_and_notify(block) \
pthread_mutex_lock(&this->lock); \ mutex_guard g(this->lock); \
block \ block \
pthread_cond_signal(&this->cond); \ pthread_cond_signal(&this->cond);
pthread_mutex_unlock(&this->lock);
template<typename T> template<typename T>
BlockingQueue<T>::~BlockingQueue() { blocking_queue<T>::~blocking_queue() {
pthread_mutex_destroy(&lock); pthread_mutex_destroy(&lock);
pthread_cond_destroy(&cond); pthread_cond_destroy(&cond);
} }
template<typename T> template<typename T>
T BlockingQueue<T>::take() { T blocking_queue<T>::take() {
pthread_mutex_lock(&lock); mutex_guard g(lock);
cancelled = false; cancelled = false;
while (deque.empty() && !cancelled) while (deque.empty() && !cancelled)
pthread_cond_wait(&cond, &lock); pthread_cond_wait(&cond, &lock);
@ -44,55 +44,51 @@ T BlockingQueue<T>::take() {
pthread_exit(nullptr); pthread_exit(nullptr);
T ret(std::move(deque.front())); T ret(std::move(deque.front()));
deque.pop_front(); deque.pop_front();
pthread_mutex_unlock(&lock);
return ret; return ret;
} }
template<typename T> template<typename T>
void BlockingQueue<T>::put(const T &s) { void blocking_queue<T>::push(const T &s) {
run_and_notify({ deque.push_back(s); }) run_and_notify({ deque.push_back(s); })
} }
template<typename T> template<typename T>
void BlockingQueue<T>::put(T &&s) { void blocking_queue<T>::push(T &&s) {
run_and_notify({ deque.push_back(std::move(s)); }) run_and_notify({ deque.push_back(std::move(s)); })
} }
template<typename T> template<typename T>
T &BlockingQueue<T>::front() { T &blocking_queue<T>::front() const {
pthread_mutex_lock(&lock); mutex_guard g(lock);
auto &ret = deque.front(); return deque.front();
pthread_mutex_unlock(&lock);
return ret;
} }
template<typename T> template<typename T>
T &BlockingQueue<T>::back() { T &blocking_queue<T>::back() const {
pthread_mutex_lock(&lock); mutex_guard g(lock);
auto &ret = deque.back(); return deque.back();
pthread_mutex_unlock(&lock);
return ret;
} }
template<typename T> template<typename T>
template<class... Args> template<class... Args>
void BlockingQueue<T>::emplace_back(Args &&... args) { void blocking_queue<T>::emplace(Args &&... args) {
run_and_notify({ deque.emplace_back(std::forward<Args>(args)...); }) run_and_notify({ deque.emplace_back(std::forward<Args>(args)...); })
} }
template<typename T> template<typename T>
void BlockingQueue<T>::clear() { void blocking_queue<T>::clear() {
pthread_mutex_lock(&lock); mutex_guard g(lock);
std::deque<T> t; std::deque<T> t;
deque.swap(t); deque.swap(t);
pthread_mutex_unlock(&lock);
} }
template<typename T> template<typename T>
void BlockingQueue<T>::cancel() { void blocking_queue<T>::cancel() {
run_and_notify({ run_and_notify({
cancelled = true; cancelled = true;
std::deque<T> t; std::deque<T> t;
deque.swap(t); deque.swap(t);
}) })
} }
#undef run_and_notify

View File

@ -6,7 +6,7 @@
#include <map> #include <map>
#include <string_view> #include <string_view>
#include <OutStream.h> #include <stream.h>
struct cpio_newc_header; struct cpio_newc_header;

View File

@ -19,9 +19,9 @@ public:
FilterOutStream(strm_ptr &&ptr) : out(std::move(ptr)) {} FilterOutStream(strm_ptr &&ptr) : out(std::move(ptr)) {}
void set_out(strm_ptr &&ptr) { out = std::move(ptr); } void setOut(strm_ptr &&ptr) { out = std::move(ptr); }
OutStream *get_out() { return out.get(); } OutStream *getOut() { return out.get(); }
bool write(const void *buf, size_t len) override { bool write(const void *buf, size_t len) override {
return out ? out->write(buf, len) : false; return out ? out->write(buf, len) : false;

View File

@ -27,17 +27,17 @@ void gen_rand_str(char *buf, int len, bool varlen = true);
#define str_contains(s, ss) ((ss) != nullptr && (s).find(ss) != std::string::npos) #define str_contains(s, ss) ((ss) != nullptr && (s).find(ss) != std::string::npos)
#define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0) #define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0)
class MutexGuard { class mutex_guard {
public: public:
explicit MutexGuard(pthread_mutex_t &m): mutex(&m) { explicit mutex_guard(pthread_mutex_t &m): mutex(&m) {
pthread_mutex_lock(mutex); pthread_mutex_lock(mutex);
} }
explicit MutexGuard(pthread_mutex_t *m): mutex(m) { explicit mutex_guard(pthread_mutex_t *m): mutex(m) {
pthread_mutex_lock(mutex); pthread_mutex_lock(mutex);
} }
~MutexGuard() { ~mutex_guard() {
pthread_mutex_unlock(mutex); pthread_mutex_unlock(mutex);
} }
@ -45,13 +45,13 @@ private:
pthread_mutex_t *mutex; pthread_mutex_t *mutex;
}; };
class RunFinally { class run_finally {
public: public:
explicit RunFinally(std::function<void()> &&fn): fn(std::move(fn)) {} explicit run_finally(std::function<void()> &&fn): fn(std::move(fn)) {}
void disable() { fn = nullptr; } void disable() { fn = nullptr; }
~RunFinally() { if (fn) fn(); } ~run_finally() { if (fn) fn(); }
private: private:
std::function<void ()> fn; std::function<void ()> fn;