Use ordered map to store CPIO entries

This commit is contained in:
topjohnwu 2019-02-22 22:53:20 -05:00
parent cb8fe70734
commit 6f12c08204
3 changed files with 185 additions and 197 deletions

View File

@ -11,8 +11,6 @@
using namespace std; using namespace std;
#define parse_align() lseek(fd, do_align(lseek(fd, 0, SEEK_CUR), 4), SEEK_SET)
static uint32_t x8u(char *hex) { static uint32_t x8u(char *hex) {
uint32_t val, inpos = 8, outpos; uint32_t val, inpos = 8, outpos;
char pattern[6]; char pattern[6];
@ -30,6 +28,7 @@ static uint32_t x8u(char *hex) {
return val; return val;
} }
#define parse_align() lseek(fd, do_align(lseek(fd, 0, SEEK_CUR), 4), SEEK_SET)
cpio_entry::cpio_entry(int fd, cpio_newc_header &header) cpio_entry::cpio_entry(int fd, cpio_newc_header &header)
: mode(x8u(header.mode)), uid(x8u(header.uid)), gid(x8u(header.gid)), : mode(x8u(header.mode)), uid(x8u(header.uid)), gid(x8u(header.gid)),
filesize(x8u(header.filesize)) { filesize(x8u(header.filesize)) {
@ -53,15 +52,17 @@ 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;
unique_ptr<cpio_entry> entry; cpio_entry *entry = nullptr;
while(xxread(fd, &header, sizeof(cpio_newc_header)) != -1) { while(xxread(fd, &header, sizeof(cpio_newc_header)) != -1) {
entry = std::make_unique<cpio_entry>(fd, header); entry = new cpio_entry(fd, header);
if (entry->filename == "." || entry->filename == "..") if (entry->filename == "." || entry->filename == "..")
continue; continue;
if (entry->filename == "TRAILER!!!") if (entry->filename == "TRAILER!!!")
break; break;
entries.push_back(std::move(entry)); entries[entry->filename].reset(entry);
entry = nullptr;
} }
delete entry;
close(fd); close(fd);
} }
@ -70,29 +71,28 @@ void cpio::dump(const char *file) {
fprintf(stderr, "Dump cpio: [%s]\n", file); fprintf(stderr, "Dump cpio: [%s]\n", file);
unsigned inode = 300000; unsigned inode = 300000;
char header[111]; char header[111];
sort();
int fd = creat(file, 0644); int fd = creat(file, 0644);
for (auto &e : entries) { 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.second->mode,
e->uid, e.second->uid,
e->gid, e.second->gid,
1, // e->nlink 1, // e->nlink
0, // e->mtime 0, // e->mtime
e->filesize, e.second->filesize,
0, // e->devmajor 0, // e->devmajor
0, // e->devminor 0, // e->devminor
0, // e->rdevmajor 0, // e->rdevmajor
0, // e->rdevminor 0, // e->rdevminor
(uint32_t) e->filename.size() + 1, (uint32_t) e.first.size() + 1,
0 // e->check 0 // e->check
); );
xwrite(fd, header, 110); xwrite(fd, header, 110);
xwrite(fd, e->filename.c_str(), e->filename.size() + 1); xwrite(fd, e.first.data(), e.first.size() + 1);
dump_align(); dump_align();
if (e->filesize) { if (e.second->filesize) {
xwrite(fd, e->data, e->filesize); xwrite(fd, e.second->data, e.second->filesize);
dump_align(); dump_align();
} }
} }
@ -105,65 +105,79 @@ void cpio::dump(const char *file) {
close(fd); close(fd);
} }
int cpio::find(const char *name) { void cpio::rm(entry_map::iterator &it) {
for (int i = 0; i < entries.size(); ++i) { fprintf(stderr, "Remove [%s]\n", it->first.data());
if (!entries[i]) entries.erase(it);
continue;
if (entries[i]->filename == name)
return i;
}
return -1;
}
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());
if (i >= 0) {
entries[i] = std::move(e);
} else {
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 : entries) { for (auto it = entries.begin(); it != entries.end();) {
if (!e) if (it->first.compare(0, len, name) == 0 &&
continue; ((r && it->first[len] == '/') || it->first[len] == '\0')) {
if (e->filename.compare(0, len, name) == 0 && auto tmp = it;
((r && e->filename[len] == '/') || e->filename[len] == '\0')) { ++it;
fprintf(stderr, "Remove [%s]\n", e->filename.c_str()); rm(tmp);
e.reset();
if (!r) return; if (!r) return;
} else {
++it;
} }
} }
} }
void cpio::makedir(mode_t mode, const char *name) { static void extract_entry(const entry_map::value_type &e, const char *file) {
auto e = make_unique<cpio_entry>(name); fprintf(stderr, "Extract [%s] to [%s]\n", e.first.data(), file);
e->mode = S_IFDIR | mode; unlink(file);
insert(e); rmdir(file);
fprintf(stderr, "Create directory [%s] (%04o)\n", name, mode); if (S_ISDIR(e.second->mode)) {
mkdir(file, e.second->mode & 0777);
} else if (S_ISREG(e.second->mode)) {
int fd = creat(file, e.second->mode & 0777);
xwrite(fd, e.second->data, e.second->filesize);
fchown(fd, e.second->uid, e.second->gid);
close(fd);
} else if (S_ISLNK(e.second->mode)) {
auto target = strndup((char *) e.second->data, e.second->filesize);
symlink(target, file);
free(target);
}
} }
void cpio::ln(const char *target, const char *name) { void cpio::extract() {
auto e = make_unique<cpio_entry>(name); for (auto &e : entries)
e->mode = S_IFLNK; extract_entry(e, e.first.data());
e->filesize = strlen(target); }
e->data = strdup(target);
insert(e); bool cpio::extract(const char *name, const char *file) {
fprintf(stderr, "Create symlink [%s] -> [%s]\n", name, target); auto it = entries.find(name);
if (it != entries.end()) {
extract_entry(*it, file);
return true;
}
fprintf(stderr, "Cannot find the file entry [%s]\n", name);
return false;
}
bool cpio::exists(const char *name) {
return entries.count(name) != 0;
}
void cpio::insert(cpio_entry *e) {
auto ex = entries.extract(e->filename);
if (!ex) {
entries[e->filename].reset(e);
} else {
ex.key() = e->filename;
ex.mapped().reset(e);
entries.insert(std::move(ex));
}
} }
void cpio::add(mode_t mode, const char *name, const char *file) { void cpio::add(mode_t mode, const char *name, const char *file) {
void *buf; void *buf;
size_t sz; size_t sz;
mmap_ro(file, &buf, &sz); mmap_ro(file, &buf, &sz);
auto e = make_unique<cpio_entry>(name); auto e = new cpio_entry(name);
e->mode = S_IFREG | mode; e->mode = S_IFREG | mode;
e->filesize = sz; e->filesize = sz;
e->data = xmalloc(sz); e->data = xmalloc(sz);
@ -173,66 +187,36 @@ void cpio::add(mode_t mode, const char *name, const char *file) {
fprintf(stderr, "Add entry [%s] (%04o)\n", name, mode); fprintf(stderr, "Add entry [%s] (%04o)\n", name, mode);
} }
void cpio::makedir(mode_t mode, const char *name) {
auto e = new cpio_entry(name);
e->mode = S_IFDIR | mode;
insert(e);
fprintf(stderr, "Create directory [%s] (%04o)\n", name, mode);
}
void cpio::ln(const char *target, const char *name) {
auto e = new cpio_entry(name);
e->mode = S_IFLNK;
e->filesize = strlen(target);
e->data = strdup(target);
insert(e);
fprintf(stderr, "Create symlink [%s] -> [%s]\n", name, target);
}
void cpio::mv(entry_map::iterator &it, const char *to) {
fprintf(stderr, "Move [%s] -> [%s]\n", it->first.data(), to);
auto ex = entries.extract(it);
ex.mapped()->filename = to;
ex.key() = ex.mapped()->filename;
entries.insert(std::move(ex));
}
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); auto it = entries.find(from);
if (f >= 0) { if (it != entries.end()) {
if (t > 0) mv(it, to);
entries[t].reset();
fprintf(stderr, "Move [%s] -> [%s]\n", from, 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);
return false; return false;
} }
static void extract_entry(cpio_entry *e, const char *file) {
fprintf(stderr, "Extract [%s] to [%s]\n", e->filename.c_str(), file);
unlink(file);
rmdir(file);
if (S_ISDIR(e->mode)) {
mkdir(file, e->mode & 0777);
} else if (S_ISREG(e->mode)) {
int fd = creat(file, e->mode & 0777);
xwrite(fd, e->data, e->filesize);
fchown(fd, e->uid, e->gid);
close(fd);
} else if (S_ISLNK(e->mode)) {
auto target = new char[e->filesize + 1];
memcpy(target, e->data, e->filesize);
target[e->filesize] = '\0';
symlink(target, file);
delete[] target;
}
}
void cpio::extract() {
for (auto &e : entries) {
if (!e) continue;
extract_entry(e.get(), e->filename.c_str());
}
}
bool cpio::extract(const char *name, const char *file) {
int i = find(name);
if (i > 0) {
extract_entry(entries[i].get(), file);
return true;
}
fprintf(stderr, "Cannot find the file entry [%s]\n", name);
return false;
}
void cpio::sort() {
std::sort(entries.begin(), entries.end(), []
(const unique_ptr<cpio_entry> &a, const unique_ptr<cpio_entry> &b) -> bool {
if (a == b || !a)
return false;
if (!b)
return true;
return a->filename < b->filename;
});
while (!entries.back())
entries.pop_back();
}

View File

@ -1,10 +1,11 @@
#ifndef _CPIO_H_ #pragma once
#define _CPIO_H_
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
#include <string> #include <string>
#include <memory> #include <memory>
#include <map>
#include <string_view>
struct cpio_newc_header { struct cpio_newc_header {
char magic[6]; char magic[6];
@ -39,24 +40,26 @@ struct cpio_entry {
~cpio_entry(); ~cpio_entry();
}; };
typedef std::map<std::string_view, std::unique_ptr<cpio_entry>> entry_map;
class cpio { class cpio {
public: public:
explicit cpio(const char *filename); explicit cpio(const char *filename);
void dump(const char *file); void dump(const char *file);
int find(const char *name);
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 ln(const char *target, const char *name);
void add(mode_t mode, const char *name, const char *file);
bool mv(const char *from, const char *to);
void extract(); void extract();
bool extract(const char *name, const char *file); bool extract(const char *name, const char *file);
void sort(); bool exists(const char *name);
void insert(cpio_entry *e);
void add(mode_t mode, const char *name, const char *file);
void makedir(mode_t mode, const char *name);
void ln(const char *target, const char *name);
bool mv(const char *from, const char *to);
protected: protected:
std::vector<std::unique_ptr<cpio_entry> > entries; entry_map entries;
};
#endif void rm(entry_map::iterator &it);
void mv(entry_map::iterator &it, const char *to);
};

View File

@ -23,43 +23,43 @@ 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 : entries) { for (auto &e : entries) {
if (!e) continue;
bool fstab = (!keepverity || !keepforceencrypt) && bool fstab = (!keepverity || !keepforceencrypt) &&
!str_starts(e->filename, ".backup") && !str_starts(e.first, ".backup") &&
str_contains(e->filename, "fstab") && S_ISREG(e->mode); str_contains(e.first, "fstab") && S_ISREG(e.second->mode);
if (!keepverity) { if (!keepverity) {
if (fstab) { if (fstab) {
patch_verity(&e->data, &e->filesize, 1); patch_verity(&e.second->data, &e.second->filesize, 1);
} else if (e->filename == "verity_key") { } else if (e.first == "verity_key") {
fprintf(stderr, "Remove [verity_key]\n"); fprintf(stderr, "Remove [verity_key]\n");
e.reset(); e.second.reset();
continue; continue;
} }
} }
if (!keepforceencrypt) { if (!keepforceencrypt) {
if (fstab) { if (fstab) {
patch_encryption(&e->data, &e->filesize); patch_encryption(&e.second->data, &e.second->filesize);
} }
} }
} }
} }
#define STOCK_BOOT 0x0 #define STOCK_BOOT 0x0
#define MAGISK_PATCH 0x1 #define MAGISK_PATCH 0x1
#define UNSUPPORT_PATCH 0x2 #define UNSUPPORT_PATCH 0x2
int magisk_cpio::test() { int magisk_cpio::test() {
static const char *UNSUPPORT_LIST[] = { "sbin/launch_daemonsu.sh", "sbin/su", "init.xposed.rc", static const char *UNSUPPORT_LIST[] =
"boot/sbin/launch_daemonsu.sh" }; { "sbin/launch_daemonsu.sh", "sbin/su", "init.xposed.rc",
static const char *MAGISK_LIST[] = { ".backup/.magisk", "init.magisk.rc", "boot/sbin/launch_daemonsu.sh" };
"overlay/init.magisk.rc" }; static const char *MAGISK_LIST[] =
{ ".backup/.magisk", "init.magisk.rc",
"overlay/init.magisk.rc" };
for (auto file : UNSUPPORT_LIST) for (auto file : UNSUPPORT_LIST)
if (find(file) >= 0) if (exists(file))
return UNSUPPORT_PATCH; return UNSUPPORT_PATCH;
for (auto file : MAGISK_LIST) for (auto file : MAGISK_LIST)
if (find(file) >= 0) if (exists(file))
return MAGISK_PATCH; return MAGISK_PATCH;
return STOCK_BOOT; return STOCK_BOOT;
@ -72,62 +72,66 @@ char *magisk_cpio::sha1() {
char sha1[41]; char sha1[41];
char *line; char *line;
for (auto &e : entries) { for (auto &e : entries) {
if (!e) continue; if (e.first == "init.magisk.rc" || e.first == "overlay/init.magisk.rc") {
if (e->filename == "init.magisk.rc" || e->filename == "overlay/init.magisk.rc") { for_each_line(line, e.second->data, e.second->filesize) {
for_each_line(line, e->data, e->filesize) {
if (strncmp(line, "#STOCKSHA1=", 11) == 0) { if (strncmp(line, "#STOCKSHA1=", 11) == 0) {
strncpy(sha1, line + 12, 40); strncpy(sha1, line + 12, 40);
sha1[40] = '\0'; sha1[40] = '\0';
return strdup(sha1); return strdup(sha1);
} }
} }
} else if (e->filename == ".backup/.magisk") { } else if (e.first == ".backup/.magisk") {
for_each_line(line, e->data, e->filesize) { for_each_line(line, e.second->data, e.second->filesize) {
if (strncmp(line, "SHA1=", 5) == 0) { if (strncmp(line, "SHA1=", 5) == 0) {
strncpy(sha1, line + 5, 40); strncpy(sha1, line + 5, 40);
sha1[40] = '\0'; sha1[40] = '\0';
return strdup(sha1); return strdup(sha1);
} }
} }
} else if (e->filename == ".backup/.sha1") { } else if (e.first == ".backup/.sha1") {
return (char *) e->data; return (char *) e.second->data;
} }
} }
return nullptr; return nullptr;
} }
#define for_each_str(str, buf, size) \
for (str = (char *) buf; str < (char *) buf + size; str = str += strlen(str) + 1)
void magisk_cpio::restore() { void magisk_cpio::restore() {
for (auto &e : entries) { char *file;
if (!e) continue; entry_map::iterator cur;
if (str_starts(e->filename, ".backup")) { auto next = entries.begin();
if (e->filename[7] == '\0') continue; while (next != entries.end()) {
if (e->filename[8] == '.') { cur = next;
if (strcmp(&e->filename[8], ".rmlist") == 0) { ++next;
for (int pos = 0; pos < e->filesize; pos += strlen((char *) e->data + pos) + 1) if (str_starts(cur->first, ".backup")) {
rm((char *) e->data + pos, false); if (cur->first.length() == 7 || cur->first.substr(8) == ".magisk") {
} rm(cur);
} else if (cur->first.substr(8) == ".rmlist") {
for_each_str(file, cur->second->data, cur->second->filesize)
rm(file, false);
rm(cur);
} else { } else {
mv(e->filename.c_str(), &e->filename[8]); mv(cur, &cur->first[8]);
} }
} else if (str_starts(cur->first, "overlay") ||
str_starts(cur->first, "magisk") ||
cur->first == "sbin/magic_mask.sh" ||
cur->first == "init.magisk.rc") {
// Some known stuff we can remove
rm(cur);
} }
} }
// Some known stuff we can remove
rm(".backup", true);
rm("overlay", true);
rm("sbin/magic_mask.sh", false);
rm("init.magisk.rc", false);
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<unique_ptr<cpio_entry> > bkup_entries; entry_map bkup_entries;
string remv; string remv;
bkup_entries.emplace_back(new cpio_entry(".backup")); auto b = new cpio_entry(".backup");
bkup_entries.back()->mode = S_IFDIR; b->mode = S_IFDIR;
bkup_entries[b->filename].reset(b);
magisk_cpio o(orig); magisk_cpio o(orig);
@ -135,20 +139,17 @@ void magisk_cpio::backup(const char *orig) {
o.rm(".backup", true); o.rm(".backup", true);
rm(".backup", true); rm(".backup", true);
// Sort both CPIOs before comparing auto lhs = o.entries.begin();
o.sort(); auto rhs = entries.begin();
sort();
// Start comparing while (lhs != o.entries.end() || rhs != entries.end()) {
size_t i = 0, j = 0;
while(i != o.entries.size() || j != entries.size()) {
int res; int res;
bool backup = false; bool backup = false;
if (i != o.entries.size() && j != entries.size()) { if (lhs != o.entries.end() && rhs != entries.end()) {
res = lhs->filename.compare(rhs->filename); res = lhs->first.compare(rhs->first);
} else if (i == o.entries.size()) { } else if (lhs == o.entries.end()) {
res = 1; res = 1;
} else if (j == entries.size()) { } else if (rhs == entries.end()) {
res = -1; res = -1;
} }
@ -157,50 +158,50 @@ void magisk_cpio::backup(const char *orig) {
backup = true; backup = true;
fprintf(stderr, "Backup missing entry: "); fprintf(stderr, "Backup missing entry: ");
} else if (res == 0) { } else if (res == 0) {
if (memcmp(lhs->data, rhs->data, lhs->filesize) != 0) { if (lhs->second->filesize != rhs->second->filesize ||
memcmp(lhs->second->data, rhs->second->data, lhs->second->filesize) != 0) {
// Not the same! // Not the same!
backup = true; backup = true;
fprintf(stderr, "Backup mismatch entry: "); fprintf(stderr, "Backup mismatch entry: ");
} }
} else { } else {
// Something new in ramdisk // Something new in ramdisk
remv += rhs->filename; remv += rhs->first;
remv += (char) '\0'; remv += (char) '\0';
fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", rhs->filename.c_str()); fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", rhs->first.data());
} }
if (backup) { if (backup) {
string back_name = ".backup/" + lhs->filename; string back_name(".backup/");
fprintf(stderr, "[%s] -> [%s]\n", lhs->filename.c_str(), back_name.c_str()); back_name += lhs->first;
lhs->filename = back_name; fprintf(stderr, "[%s] -> [%s]\n", lhs->first.data(), back_name.data());
bkup_entries.push_back(std::move(lhs)); cpio_entry *ex = lhs->second.release();
ex->filename = back_name;
bkup_entries[ex->filename].reset(ex);
} }
// Increment positions // Increment positions
if (res < 0) { if (res < 0) {
++i; ++lhs;
} else if (res == 0) { } else if (res == 0) {
++i; ++j; ++lhs; ++rhs;
} else { } else {
++j; ++rhs;
} }
} }
if (!remv.empty()) { if (!remv.empty()) {
auto rmlist = make_unique<cpio_entry>(".backup/.rmlist"); auto rmlist = new cpio_entry(".backup/.rmlist");
rmlist->mode = S_IFREG; rmlist->mode = S_IFREG;
rmlist->filesize = remv.length(); rmlist->filesize = remv.length();
rmlist->data = xmalloc(remv.length()); rmlist->data = xmalloc(remv.length());
memcpy(rmlist->data, remv.data(), remv.length()); memcpy(rmlist->data, remv.data(), remv.length());
bkup_entries.push_back(std::move(rmlist)); bkup_entries[rmlist->filename].reset(rmlist);
} }
if (bkup_entries.size() > 1) { if (bkup_entries.size() > 1)
entries.insert(entries.end(), entries.merge(bkup_entries);
make_move_iterator(bkup_entries.begin()), make_move_iterator(bkup_entries.end()));
}
} }
int cpio_commands(int argc, char *argv[]) { int cpio_commands(int argc, char *argv[]) {
char *incpio = argv[0]; char *incpio = argv[0];
++argv; ++argv;
@ -232,7 +233,7 @@ int cpio_commands(int argc, char *argv[]) {
if (sha1) printf("%s\n", sha1); if (sha1) printf("%s\n", sha1);
return 0; return 0;
} else if (cmdc == 2 && strcmp(cmdv[0], "exists") == 0) { } else if (cmdc == 2 && strcmp(cmdv[0], "exists") == 0) {
exit(cpio.find(cmdv[1]) < 0); exit(!cpio.exists(cmdv[1]));
} else if (cmdc == 2 && strcmp(cmdv[0], "backup") == 0) { } else if (cmdc == 2 && strcmp(cmdv[0], "backup") == 0) {
cpio.backup(cmdv[1]); cpio.backup(cmdv[1]);
} else if (cmdc >= 2 && strcmp(cmdv[0], "rm") == 0) { } else if (cmdc >= 2 && strcmp(cmdv[0], "rm") == 0) {