mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-11 04:22:26 +00:00
Use ordered map to store CPIO entries
This commit is contained in:
@@ -11,8 +11,6 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define parse_align() lseek(fd, do_align(lseek(fd, 0, SEEK_CUR), 4), SEEK_SET)
|
||||
|
||||
static uint32_t x8u(char *hex) {
|
||||
uint32_t val, inpos = 8, outpos;
|
||||
char pattern[6];
|
||||
@@ -30,6 +28,7 @@ static uint32_t x8u(char *hex) {
|
||||
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)
|
||||
: mode(x8u(header.mode)), uid(x8u(header.uid)), gid(x8u(header.gid)),
|
||||
filesize(x8u(header.filesize)) {
|
||||
@@ -53,15 +52,17 @@ cpio::cpio(const char *filename) {
|
||||
if (fd < 0) return;
|
||||
fprintf(stderr, "Loading cpio: [%s]\n", filename);
|
||||
cpio_newc_header header;
|
||||
unique_ptr<cpio_entry> entry;
|
||||
cpio_entry *entry = nullptr;
|
||||
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 == "..")
|
||||
continue;
|
||||
if (entry->filename == "TRAILER!!!")
|
||||
break;
|
||||
entries.push_back(std::move(entry));
|
||||
entries[entry->filename].reset(entry);
|
||||
entry = nullptr;
|
||||
}
|
||||
delete entry;
|
||||
close(fd);
|
||||
}
|
||||
|
||||
@@ -70,29 +71,28 @@ void cpio::dump(const char *file) {
|
||||
fprintf(stderr, "Dump cpio: [%s]\n", file);
|
||||
unsigned inode = 300000;
|
||||
char header[111];
|
||||
sort();
|
||||
int fd = creat(file, 0644);
|
||||
for (auto &e : entries) {
|
||||
sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
|
||||
inode++, // e->ino
|
||||
e->mode,
|
||||
e->uid,
|
||||
e->gid,
|
||||
e.second->mode,
|
||||
e.second->uid,
|
||||
e.second->gid,
|
||||
1, // e->nlink
|
||||
0, // e->mtime
|
||||
e->filesize,
|
||||
e.second->filesize,
|
||||
0, // e->devmajor
|
||||
0, // e->devminor
|
||||
0, // e->rdevmajor
|
||||
0, // e->rdevminor
|
||||
(uint32_t) e->filename.size() + 1,
|
||||
(uint32_t) e.first.size() + 1,
|
||||
0 // e->check
|
||||
);
|
||||
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();
|
||||
if (e->filesize) {
|
||||
xwrite(fd, e->data, e->filesize);
|
||||
if (e.second->filesize) {
|
||||
xwrite(fd, e.second->data, e.second->filesize);
|
||||
dump_align();
|
||||
}
|
||||
}
|
||||
@@ -105,65 +105,79 @@ void cpio::dump(const char *file) {
|
||||
close(fd);
|
||||
}
|
||||
|
||||
int cpio::find(const char *name) {
|
||||
for (int i = 0; i < entries.size(); ++i) {
|
||||
if (!entries[i])
|
||||
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(entry_map::iterator &it) {
|
||||
fprintf(stderr, "Remove [%s]\n", it->first.data());
|
||||
entries.erase(it);
|
||||
}
|
||||
|
||||
void cpio::rm(const char *name, bool r) {
|
||||
size_t len = strlen(name);
|
||||
for (auto &e : entries) {
|
||||
if (!e)
|
||||
continue;
|
||||
if (e->filename.compare(0, len, name) == 0 &&
|
||||
((r && e->filename[len] == '/') || e->filename[len] == '\0')) {
|
||||
fprintf(stderr, "Remove [%s]\n", e->filename.c_str());
|
||||
e.reset();
|
||||
for (auto it = entries.begin(); it != entries.end();) {
|
||||
if (it->first.compare(0, len, name) == 0 &&
|
||||
((r && it->first[len] == '/') || it->first[len] == '\0')) {
|
||||
auto tmp = it;
|
||||
++it;
|
||||
rm(tmp);
|
||||
if (!r) return;
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void cpio::makedir(mode_t mode, const char *name) {
|
||||
auto e = make_unique<cpio_entry>(name);
|
||||
e->mode = S_IFDIR | mode;
|
||||
insert(e);
|
||||
fprintf(stderr, "Create directory [%s] (%04o)\n", name, mode);
|
||||
static void extract_entry(const entry_map::value_type &e, const char *file) {
|
||||
fprintf(stderr, "Extract [%s] to [%s]\n", e.first.data(), file);
|
||||
unlink(file);
|
||||
rmdir(file);
|
||||
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) {
|
||||
auto e = make_unique<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::extract() {
|
||||
for (auto &e : entries)
|
||||
extract_entry(e, e.first.data());
|
||||
}
|
||||
|
||||
bool cpio::extract(const char *name, const char *file) {
|
||||
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 *buf;
|
||||
size_t 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->filesize = 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);
|
||||
}
|
||||
|
||||
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) {
|
||||
int f = find(from), t = find(to);
|
||||
if (f >= 0) {
|
||||
if (t > 0)
|
||||
entries[t].reset();
|
||||
fprintf(stderr, "Move [%s] -> [%s]\n", from, to);
|
||||
entries[f]->filename = to;
|
||||
auto it = entries.find(from);
|
||||
if (it != entries.end()) {
|
||||
mv(it, to);
|
||||
return true;
|
||||
}
|
||||
fprintf(stderr, "Cannot find entry %s\n", from);
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user