Modernize CPIO code

This commit is contained in:
topjohnwu 2019-02-22 02:56:18 -05:00
parent 69d10b747a
commit cb8fe70734
4 changed files with 104 additions and 127 deletions

View File

@ -3,7 +3,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <algorithm> #include <algorithm>
#include <memory>
#include <utils.h> #include <utils.h>
#include <logging.h> #include <logging.h>
@ -30,20 +30,10 @@ static uint32_t x8u(char *hex) {
return val; return val;
} }
cpio_entry::cpio_entry(int fd, cpio_newc_header &header) { cpio_entry::cpio_entry(int fd, cpio_newc_header &header)
// ino = x8u(header.ino); : mode(x8u(header.mode)), uid(x8u(header.uid)), gid(x8u(header.gid)),
mode = x8u(header.mode); filesize(x8u(header.filesize)) {
uid = x8u(header.uid);
gid = x8u(header.gid);
// nlink = x8u(header.nlink);
// mtime = x8u(header.mtime);
filesize = x8u(header.filesize);
// devmajor = x8u(header.devmajor);
// devminor = x8u(header.devminor);
// rdevmajor = x8u(header.rdevmajor);
// rdevminor = x8u(header.rdevminor);
uint32_t namesize = x8u(header.namesize); uint32_t namesize = x8u(header.namesize);
// check = x8u(header.check);
filename.resize(namesize - 1); filename.resize(namesize - 1);
xxread(fd, &filename[0], namesize); xxread(fd, &filename[0], namesize);
parse_align(); parse_align();
@ -63,27 +53,18 @@ cpio::cpio(const char *filename) {
if (fd < 0) return; if (fd < 0) return;
fprintf(stderr, "Loading cpio: [%s]\n", filename); fprintf(stderr, "Loading cpio: [%s]\n", filename);
cpio_newc_header header; cpio_newc_header header;
cpio_entry *entry; unique_ptr<cpio_entry> entry;
int i = 0;
while(xxread(fd, &header, sizeof(cpio_newc_header)) != -1) { while(xxread(fd, &header, sizeof(cpio_newc_header)) != -1) {
entry = new cpio_entry(fd, header); entry = std::make_unique<cpio_entry>(fd, header);
if (entry->filename == "." || entry->filename == ".." || entry->filename == "TRAILER!!!") { if (entry->filename == "." || entry->filename == "..")
bool trailer = entry->filename[0] == 'T';
delete entry;
if (trailer)
break;
continue; continue;
} if (entry->filename == "TRAILER!!!")
arr.push_back(entry); break;
entries.push_back(std::move(entry));
} }
close(fd); close(fd);
} }
cpio::~cpio() {
for (auto &e : arr)
delete e;
}
#define dump_align() write_zero(fd, align_off(lseek(fd, 0, SEEK_CUR), 4)) #define dump_align() write_zero(fd, align_off(lseek(fd, 0, SEEK_CUR), 4))
void cpio::dump(const char *file) { void cpio::dump(const char *file) {
fprintf(stderr, "Dump cpio: [%s]\n", file); fprintf(stderr, "Dump cpio: [%s]\n", file);
@ -91,7 +72,7 @@ void cpio::dump(const char *file) {
char header[111]; char header[111];
sort(); sort();
int fd = creat(file, 0644); int fd = creat(file, 0644);
for (auto &e : arr) { for (auto &e : entries) {
sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x", sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
inode++, // e->ino inode++, // e->ino
e->mode, e->mode,
@ -125,50 +106,52 @@ void cpio::dump(const char *file) {
} }
int cpio::find(const char *name) { int cpio::find(const char *name) {
for (int i = 0; i < arr.size(); ++i) { for (int i = 0; i < entries.size(); ++i) {
if (!arr[i]) if (!entries[i])
continue; continue;
if (arr[i]->filename == name) if (entries[i]->filename == name)
return i; return i;
} }
return -1; return -1;
} }
void cpio::insert(cpio_entry *e) { void cpio::insert(cpio_entry *e) {
auto ue = unique_ptr<cpio_entry>(e);
insert(ue);
}
void cpio::insert(unique_ptr<cpio_entry> &e) {
int i = find(e->filename.c_str()); int i = find(e->filename.c_str());
if (i >= 0) { if (i >= 0) {
delete arr[i]; entries[i] = std::move(e);
arr[i] = e;
} else { } else {
arr.push_back(e); entries.push_back(std::move(e));
} }
} }
void cpio::rm(const char *name, bool r) { void cpio::rm(const char *name, bool r) {
size_t len = strlen(name); size_t len = strlen(name);
for (auto &e : arr) { for (auto &e : entries) {
if (!e) if (!e)
continue; continue;
if (e->filename.compare(0, len, name) == 0 && if (e->filename.compare(0, len, name) == 0 &&
((r && e->filename[len] == '/') || e->filename[len] == '\0')) { ((r && e->filename[len] == '/') || e->filename[len] == '\0')) {
fprintf(stderr, "Remove [%s]\n", e->filename.c_str()); fprintf(stderr, "Remove [%s]\n", e->filename.c_str());
delete e; e.reset();
e = nullptr; if (!r) return;
if (!r)
return;
} }
} }
} }
void cpio::makedir(mode_t mode, const char *name) { void cpio::makedir(mode_t mode, const char *name) {
auto e = new cpio_entry(name); auto e = make_unique<cpio_entry>(name);
e->mode = S_IFDIR | mode; e->mode = S_IFDIR | mode;
insert(e); insert(e);
fprintf(stderr, "Create directory [%s] (%04o)\n", name, mode); fprintf(stderr, "Create directory [%s] (%04o)\n", name, mode);
} }
void cpio::ln(const char *target, const char *name) { void cpio::ln(const char *target, const char *name) {
auto e = new cpio_entry(name); auto e = make_unique<cpio_entry>(name);
e->mode = S_IFLNK; e->mode = S_IFLNK;
e->filesize = strlen(target); e->filesize = strlen(target);
e->data = strdup(target); e->data = strdup(target);
@ -177,14 +160,15 @@ void cpio::ln(const char *target, const char *name) {
} }
void cpio::add(mode_t mode, const char *name, const char *file) { void cpio::add(mode_t mode, const char *name, const char *file) {
int fd = xopen(file, O_RDONLY); void *buf;
auto e = new cpio_entry(name); size_t sz;
mmap_ro(file, &buf, &sz);
auto e = make_unique<cpio_entry>(name);
e->mode = S_IFREG | mode; e->mode = S_IFREG | mode;
e->filesize = lseek(fd, 0, SEEK_END); e->filesize = sz;
lseek(fd, 0, SEEK_SET); e->data = xmalloc(sz);
e->data = xmalloc(e->filesize); memcpy(e->data, buf, sz);
xxread(fd, e->data, e->filesize); munmap(buf, sz);
close(fd);
insert(e); insert(e);
fprintf(stderr, "Add entry [%s] (%04o)\n", name, mode); fprintf(stderr, "Add entry [%s] (%04o)\n", name, mode);
} }
@ -192,12 +176,10 @@ void cpio::add(mode_t mode, const char *name, const char *file) {
bool cpio::mv(const char *from, const char *to) { bool cpio::mv(const char *from, const char *to) {
int f = find(from), t = find(to); int f = find(from), t = find(to);
if (f >= 0) { if (f >= 0) {
if (t > 0) { if (t > 0)
delete arr[t]; entries[t].reset();
arr[t] = nullptr;
}
fprintf(stderr, "Move [%s] -> [%s]\n", from, to); fprintf(stderr, "Move [%s] -> [%s]\n", from, to);
arr[f]->filename = to; entries[f]->filename = to;
return true; return true;
} }
fprintf(stderr, "Cannot find entry %s\n", from); fprintf(stderr, "Cannot find entry %s\n", from);
@ -225,16 +207,16 @@ static void extract_entry(cpio_entry *e, const char *file) {
} }
void cpio::extract() { void cpio::extract() {
for (auto &e : arr) { for (auto &e : entries) {
if (!e) continue; if (!e) continue;
extract_entry(e, e->filename.c_str()); extract_entry(e.get(), e->filename.c_str());
} }
} }
bool cpio::extract(const char *name, const char *file) { bool cpio::extract(const char *name, const char *file) {
int i = find(name); int i = find(name);
if (i > 0) { if (i > 0) {
extract_entry(arr[i], file); extract_entry(entries[i].get(), file);
return true; return true;
} }
fprintf(stderr, "Cannot find the file entry [%s]\n", name); fprintf(stderr, "Cannot find the file entry [%s]\n", name);
@ -242,13 +224,15 @@ bool cpio::extract(const char *name, const char *file) {
} }
void cpio::sort() { void cpio::sort() {
std::sort(arr.begin(), arr.end(), [] (auto a, auto b) -> bool { std::sort(entries.begin(), entries.end(), []
if (a == b || a == nullptr) (const unique_ptr<cpio_entry> &a, const unique_ptr<cpio_entry> &b) -> bool {
if (a == b || !a)
return false; return false;
if (b == nullptr) if (!b)
return true; return true;
return a->filename.compare(b->filename) < 0; return a->filename < b->filename;
}); });
while (arr.back() == nullptr)
arr.pop_back(); while (!entries.back())
entries.pop_back();
} }

View File

@ -4,6 +4,7 @@
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
#include <string> #include <string>
#include <memory>
struct cpio_newc_header { struct cpio_newc_header {
char magic[6]; char magic[6];
@ -23,20 +24,12 @@ struct cpio_newc_header {
} __attribute__((packed)); } __attribute__((packed));
struct cpio_entry { struct cpio_entry {
// uint32_t ino;
uint32_t mode = 0; uint32_t mode = 0;
uint32_t uid = 0; uint32_t uid = 0;
uint32_t gid = 0; uint32_t gid = 0;
// uint32_t nlink;
// uint32_t mtime;
uint32_t filesize = 0; uint32_t filesize = 0;
// uint32_t devmajor;
// uint32_t devminor; /* Dynamic data */
// uint32_t rdevmajor;
// uint32_t rdevminor;
// uint32_t namesize;
// uint32_t check;
// char *filename = nullptr;
std::string filename; std::string filename;
void *data = nullptr; void *data = nullptr;
@ -49,10 +42,10 @@ struct cpio_entry {
class cpio { class cpio {
public: public:
explicit cpio(const char *filename); explicit cpio(const char *filename);
~cpio();
void dump(const char *file); void dump(const char *file);
int find(const char *name); int find(const char *name);
void insert(cpio_entry *e); void insert(cpio_entry *e);
void insert(std::unique_ptr<cpio_entry> &e);
void rm(const char *name, bool r = false); void rm(const char *name, bool r = false);
void makedir(mode_t mode, const char *name); void makedir(mode_t mode, const char *name);
void ln(const char *target, const char *name); void ln(const char *target, const char *name);
@ -63,7 +56,7 @@ public:
void sort(); void sort();
protected: protected:
std::vector<cpio_entry *> arr; std::vector<std::unique_ptr<cpio_entry> > entries;
}; };
#endif #endif

View File

@ -22,9 +22,8 @@ public:
void magisk_cpio::patch(bool keepverity, bool keepforceencrypt) { void magisk_cpio::patch(bool keepverity, bool keepforceencrypt) {
fprintf(stderr, "Patch with flag KEEPVERITY=[%s] KEEPFORCEENCRYPT=[%s]\n", fprintf(stderr, "Patch with flag KEEPVERITY=[%s] KEEPFORCEENCRYPT=[%s]\n",
keepverity ? "true" : "false", keepforceencrypt ? "true" : "false"); keepverity ? "true" : "false", keepforceencrypt ? "true" : "false");
for (auto &e : arr) { for (auto &e : entries) {
if (!e) if (!e) continue;
continue;
bool fstab = (!keepverity || !keepforceencrypt) && bool fstab = (!keepverity || !keepforceencrypt) &&
!str_starts(e->filename, ".backup") && !str_starts(e->filename, ".backup") &&
str_contains(e->filename, "fstab") && S_ISREG(e->mode); str_contains(e->filename, "fstab") && S_ISREG(e->mode);
@ -33,8 +32,7 @@ void magisk_cpio::patch(bool keepverity, bool keepforceencrypt) {
patch_verity(&e->data, &e->filesize, 1); patch_verity(&e->data, &e->filesize, 1);
} else if (e->filename == "verity_key") { } else if (e->filename == "verity_key") {
fprintf(stderr, "Remove [verity_key]\n"); fprintf(stderr, "Remove [verity_key]\n");
delete e; e.reset();
e = nullptr;
continue; continue;
} }
} }
@ -73,7 +71,7 @@ for (line = (char *) buf; line < (char *) buf + size && line[0]; line = strchr(l
char *magisk_cpio::sha1() { char *magisk_cpio::sha1() {
char sha1[41]; char sha1[41];
char *line; char *line;
for (auto &e : arr) { for (auto &e : entries) {
if (!e) continue; if (!e) continue;
if (e->filename == "init.magisk.rc" || e->filename == "overlay/init.magisk.rc") { if (e->filename == "init.magisk.rc" || e->filename == "overlay/init.magisk.rc") {
for_each_line(line, e->data, e->filesize) { for_each_line(line, e->data, e->filesize) {
@ -99,7 +97,7 @@ char *magisk_cpio::sha1() {
} }
void magisk_cpio::restore() { void magisk_cpio::restore() {
for (auto &e : arr) { for (auto &e : entries) {
if (!e) continue; if (!e) continue;
if (str_starts(e->filename, ".backup")) { if (str_starts(e->filename, ".backup")) {
if (e->filename[7] == '\0') continue; if (e->filename[7] == '\0') continue;
@ -122,17 +120,14 @@ void magisk_cpio::restore() {
rm("magisk", true); rm("magisk", true);
} }
#define lhs o.entries[i]
#define rhs entries[j]
void magisk_cpio::backup(const char *orig) { void magisk_cpio::backup(const char *orig) {
vector<cpio_entry*> bak; vector<unique_ptr<cpio_entry> > bkup_entries;
cpio_entry *m, *n, *rem; string remv;
char buf[PATH_MAX];
m = new cpio_entry(".backup"); bkup_entries.emplace_back(new cpio_entry(".backup"));
m->mode = S_IFDIR; bkup_entries.back()->mode = S_IFDIR;
bak.push_back(m);
rem = new cpio_entry(".backup/.rmlist");
rem->mode = S_IFREG;
magisk_cpio o(orig); magisk_cpio o(orig);
@ -146,59 +141,63 @@ void magisk_cpio::backup(const char *orig) {
// Start comparing // Start comparing
size_t i = 0, j = 0; size_t i = 0, j = 0;
while(i != o.arr.size() || j != arr.size()) { while(i != o.entries.size() || j != entries.size()) {
int res; int res;
bool backup = false; bool backup = false;
if (i != o.arr.size() && j != arr.size()) { if (i != o.entries.size() && j != entries.size()) {
m = o.arr[i]; res = lhs->filename.compare(rhs->filename);
n = arr[j]; } else if (i == o.entries.size()) {
res = m->filename.compare(n->filename);
} else if (i == o.arr.size()) {
n = arr[j];
res = 1; res = 1;
} else if (j == arr.size()) { } else if (j == entries.size()) {
m = o.arr[i];
res = -1; res = -1;
} }
if (res < 0) { if (res < 0) {
// Something is missing in new ramdisk, backup! // Something is missing in new ramdisk, backup!
++i;
backup = true; backup = true;
fprintf(stderr, "Backup missing entry: "); fprintf(stderr, "Backup missing entry: ");
} else if (res == 0) { } else if (res == 0) {
++i; ++j; if (memcmp(lhs->data, rhs->data, lhs->filesize) != 0) {
if (m->filesize == n->filesize && memcmp(m->data, n->data, m->filesize) == 0) // Not the same!
continue; backup = true;
// Not the same! fprintf(stderr, "Backup mismatch entry: ");
backup = true; }
fprintf(stderr, "Backup mismatch entry: ");
} else { } else {
// Something new in ramdisk, record in rem // Something new in ramdisk
++j; remv += rhs->filename;
rem->data = xrealloc(rem->data, rem->filesize + n->filename.size()); remv += (char) '\0';
memcpy((char *) rem->data + rem->filesize, n->filename.c_str(), n->filename.size()); fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", rhs->filename.c_str());
rem->filesize += n->filename.size();
fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", n->filename.c_str());
} }
if (backup) { if (backup) {
sprintf(buf, ".backup/%s", m->filename.c_str()); string back_name = ".backup/" + lhs->filename;
fprintf(stderr, "[%s] -> [%s]\n", m->filename.c_str(), buf); fprintf(stderr, "[%s] -> [%s]\n", lhs->filename.c_str(), back_name.c_str());
m->filename = buf; lhs->filename = back_name;
bak.push_back(m); bkup_entries.push_back(std::move(lhs));
// NULL the original entry, so it won't be freed }
o.arr[i - 1] = nullptr;
// Increment positions
if (res < 0) {
++i;
} else if (res == 0) {
++i; ++j;
} else {
++j;
} }
} }
if (rem->filesize) if (!remv.empty()) {
bak.push_back(rem); auto rmlist = make_unique<cpio_entry>(".backup/.rmlist");
else rmlist->mode = S_IFREG;
delete rem; rmlist->filesize = remv.length();
rmlist->data = xmalloc(remv.length());
memcpy(rmlist->data, remv.data(), remv.length());
bkup_entries.push_back(std::move(rmlist));
}
if (bak.size() > 1) if (bkup_entries.size() > 1) {
for (auto item : bak) entries.insert(entries.end(),
insert(item); make_move_iterator(bkup_entries.begin()), make_move_iterator(bkup_entries.end()));
}
} }

View File

@ -10,6 +10,7 @@
#include <poll.h> #include <poll.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/mman.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {