Rename classes and small adjustments
This commit is contained in:
parent
debd1d7d54
commit
947dae4900
@ -67,7 +67,7 @@ static bool check_key_combo() {
|
||||
if (events.empty())
|
||||
return false;
|
||||
|
||||
RunFinally fin([&]() -> void {
|
||||
run_finally fin([&]() -> void {
|
||||
for (const int &fd : events)
|
||||
close(fd);
|
||||
});
|
||||
|
@ -22,7 +22,7 @@ uint32_t dyn_img_hdr::j32 = 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) {
|
||||
ptr->set_out(make_unique<FDOutStream>(fd));
|
||||
ptr->setOut(make_unique<FDOutStream>(fd));
|
||||
if (!ptr->write(in, size))
|
||||
return -1;
|
||||
return ptr->finalize();
|
||||
|
@ -62,7 +62,7 @@ void decompress(char *infile, const char *outfile) {
|
||||
|
||||
out_fd = strcmp(outfile, "-") == 0 ?
|
||||
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 (!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);
|
||||
}
|
||||
|
||||
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 {
|
||||
if (!cmp->write(buf, len))
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <lz4.h>
|
||||
#include <lz4frame.h>
|
||||
#include <lz4hc.h>
|
||||
#include <OutStream.h>
|
||||
#include <stream.h>
|
||||
|
||||
#include "format.h"
|
||||
|
||||
|
@ -246,13 +246,13 @@ void magisk_cpio::compress() {
|
||||
fprintf(stderr, "Compressing cpio -> [%s]\n", RAMDISK_XZ);
|
||||
auto init = entries.extract("init");
|
||||
XZEncoder encoder;
|
||||
encoder.set_out(make_unique<BufOutStream>());
|
||||
encoder.setOut(make_unique<BufOutStream>());
|
||||
output(encoder);
|
||||
encoder.finalize();
|
||||
entries.clear();
|
||||
entries.insert(std::move(init));
|
||||
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);
|
||||
}
|
||||
|
||||
@ -262,13 +262,13 @@ void magisk_cpio::decompress() {
|
||||
return;
|
||||
fprintf(stderr, "Decompressing cpio [%s]\n", RAMDISK_XZ);
|
||||
LZMADecoder decoder;
|
||||
decoder.set_out(make_unique<BufOutStream>());
|
||||
decoder.setOut(make_unique<BufOutStream>());
|
||||
decoder.write(it->second->data, it->second->filesize);
|
||||
decoder.finalize();
|
||||
entries.erase(it);
|
||||
char *buf;
|
||||
size_t sz;
|
||||
static_cast<BufOutStream *>(decoder.get_out())->getbuf(buf, sz);
|
||||
static_cast<BufOutStream *>(decoder.getOut())->getbuf(buf, sz);
|
||||
load_cpio(buf, sz);
|
||||
}
|
||||
|
||||
|
@ -36,7 +36,7 @@ static inline void lazy_unmount(const char* mountpoint) {
|
||||
}
|
||||
|
||||
void hide_daemon(int pid) {
|
||||
RunFinally fin([=]() -> void {
|
||||
run_finally fin([=]() -> void {
|
||||
// Send resume signal
|
||||
kill(pid, SIGCONT);
|
||||
_exit(0);
|
||||
|
@ -98,7 +98,7 @@ static int add_list(const char *pkg, const char *proc = "") {
|
||||
|
||||
// Critical region
|
||||
{
|
||||
MutexGuard lock(monitor_lock);
|
||||
mutex_guard lock(monitor_lock);
|
||||
hide_set.emplace(pkg, proc);
|
||||
}
|
||||
|
||||
@ -119,7 +119,7 @@ int add_list(int client) {
|
||||
static int rm_list(const char *pkg, const char *proc = "") {
|
||||
{
|
||||
// Critical region
|
||||
MutexGuard lock(monitor_lock);
|
||||
mutex_guard lock(monitor_lock);
|
||||
bool remove = false;
|
||||
auto next = hide_set.begin();
|
||||
decltype(next) cur;
|
||||
|
@ -76,7 +76,7 @@ static inline long xptrace(int request, pid_t pid, void *addr = nullptr, intptr_
|
||||
}
|
||||
|
||||
void update_uid_map() {
|
||||
MutexGuard lock(monitor_lock);
|
||||
mutex_guard lock(monitor_lock);
|
||||
uid_proc_map.clear();
|
||||
string data_path(APP_DATA_DIR);
|
||||
data_path += "/0/";
|
||||
@ -336,7 +336,7 @@ void proc_monitor() {
|
||||
continue;
|
||||
}
|
||||
bool detach = false;
|
||||
RunFinally detach_task([&] {
|
||||
run_finally f([&] {
|
||||
if (detach)
|
||||
// Non of our business now
|
||||
detach_pid(pid);
|
||||
|
@ -89,7 +89,7 @@ static shared_ptr<su_info> get_su_info(unsigned uid) {
|
||||
shared_ptr<su_info> info;
|
||||
|
||||
{
|
||||
MutexGuard lock(cache_lock);
|
||||
mutex_guard lock(cache_lock);
|
||||
if (!cached || cached->uid != uid || !cached->is_fresh())
|
||||
cached = make_shared<su_info>(uid);
|
||||
cached->refresh();
|
||||
@ -97,7 +97,7 @@ static shared_ptr<su_info> get_su_info(unsigned uid) {
|
||||
}
|
||||
|
||||
info->lock();
|
||||
RunFinally unlock([&] {
|
||||
run_finally unlock([&] {
|
||||
info->unlock();
|
||||
});
|
||||
|
||||
|
@ -2,41 +2,41 @@
|
||||
|
||||
#include <pthread.h>
|
||||
#include <deque>
|
||||
#include <misc.h>
|
||||
|
||||
template<typename T>
|
||||
class BlockingQueue {
|
||||
class blocking_queue {
|
||||
std::deque<T> deque{};
|
||||
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
||||
bool cancelled = false;
|
||||
public:
|
||||
~BlockingQueue();
|
||||
~blocking_queue();
|
||||
T take();
|
||||
T &front();
|
||||
T &back();
|
||||
void put(const T&);
|
||||
void put(T&&);
|
||||
T &front() const;
|
||||
T &back() const;
|
||||
void push(const T&);
|
||||
void push(T&&);
|
||||
template< class... Args >
|
||||
void emplace_back(Args&&... args);
|
||||
void emplace(Args &&... args);
|
||||
void clear();
|
||||
void cancel();
|
||||
};
|
||||
|
||||
#define run_and_notify(block) \
|
||||
pthread_mutex_lock(&this->lock); \
|
||||
mutex_guard g(this->lock); \
|
||||
block \
|
||||
pthread_cond_signal(&this->cond); \
|
||||
pthread_mutex_unlock(&this->lock);
|
||||
pthread_cond_signal(&this->cond);
|
||||
|
||||
template<typename T>
|
||||
BlockingQueue<T>::~BlockingQueue() {
|
||||
blocking_queue<T>::~blocking_queue() {
|
||||
pthread_mutex_destroy(&lock);
|
||||
pthread_cond_destroy(&cond);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T BlockingQueue<T>::take() {
|
||||
pthread_mutex_lock(&lock);
|
||||
T blocking_queue<T>::take() {
|
||||
mutex_guard g(lock);
|
||||
cancelled = false;
|
||||
while (deque.empty() && !cancelled)
|
||||
pthread_cond_wait(&cond, &lock);
|
||||
@ -44,55 +44,51 @@ T BlockingQueue<T>::take() {
|
||||
pthread_exit(nullptr);
|
||||
T ret(std::move(deque.front()));
|
||||
deque.pop_front();
|
||||
pthread_mutex_unlock(&lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
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); })
|
||||
}
|
||||
|
||||
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)); })
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T &BlockingQueue<T>::front() {
|
||||
pthread_mutex_lock(&lock);
|
||||
auto &ret = deque.front();
|
||||
pthread_mutex_unlock(&lock);
|
||||
return ret;
|
||||
T &blocking_queue<T>::front() const {
|
||||
mutex_guard g(lock);
|
||||
return deque.front();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T &BlockingQueue<T>::back() {
|
||||
pthread_mutex_lock(&lock);
|
||||
auto &ret = deque.back();
|
||||
pthread_mutex_unlock(&lock);
|
||||
return ret;
|
||||
T &blocking_queue<T>::back() const {
|
||||
mutex_guard g(lock);
|
||||
return deque.back();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
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)...); })
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void BlockingQueue<T>::clear() {
|
||||
pthread_mutex_lock(&lock);
|
||||
void blocking_queue<T>::clear() {
|
||||
mutex_guard g(lock);
|
||||
std::deque<T> t;
|
||||
deque.swap(t);
|
||||
pthread_mutex_unlock(&lock);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void BlockingQueue<T>::cancel() {
|
||||
void blocking_queue<T>::cancel() {
|
||||
run_and_notify({
|
||||
cancelled = true;
|
||||
std::deque<T> t;
|
||||
deque.swap(t);
|
||||
})
|
||||
}
|
||||
|
||||
#undef run_and_notify
|
@ -6,7 +6,7 @@
|
||||
#include <map>
|
||||
#include <string_view>
|
||||
|
||||
#include <OutStream.h>
|
||||
#include <stream.h>
|
||||
|
||||
struct cpio_newc_header;
|
||||
|
||||
|
@ -19,9 +19,9 @@ public:
|
||||
|
||||
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 {
|
||||
return out ? out->write(buf, len) : false;
|
@ -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_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0)
|
||||
|
||||
class MutexGuard {
|
||||
class mutex_guard {
|
||||
public:
|
||||
explicit MutexGuard(pthread_mutex_t &m): mutex(&m) {
|
||||
explicit mutex_guard(pthread_mutex_t &m): mutex(&m) {
|
||||
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);
|
||||
}
|
||||
|
||||
~MutexGuard() {
|
||||
~mutex_guard() {
|
||||
pthread_mutex_unlock(mutex);
|
||||
}
|
||||
|
||||
@ -45,13 +45,13 @@ private:
|
||||
pthread_mutex_t *mutex;
|
||||
};
|
||||
|
||||
class RunFinally {
|
||||
class run_finally {
|
||||
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; }
|
||||
|
||||
~RunFinally() { if (fn) fn(); }
|
||||
~run_finally() { if (fn) fn(); }
|
||||
|
||||
private:
|
||||
std::function<void ()> fn;
|
||||
|
Loading…
Reference in New Issue
Block a user