Magisk/native/jni/magiskboot/cpio.cpp

239 lines
5.6 KiB
C++
Raw Normal View History

2018-10-25 01:08:06 +00:00
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
2019-01-20 04:59:37 +00:00
#include <algorithm>
2019-02-22 07:56:18 +00:00
#include <memory>
2019-02-10 08:57:51 +00:00
#include <utils.h>
#include <logging.h>
2018-10-25 01:08:06 +00:00
#include "cpio.h"
2019-01-20 04:59:37 +00:00
using namespace std;
2019-01-26 11:00:23 +00:00
#define parse_align() lseek(fd, do_align(lseek(fd, 0, SEEK_CUR), 4), SEEK_SET)
2018-10-25 01:08:06 +00:00
static uint32_t x8u(char *hex) {
uint32_t val, inpos = 8, outpos;
char pattern[6];
while (*hex == '0') {
hex++;
if (!--inpos) return 0;
}
// Because scanf gratuitously treats %*X differently than printf does.
sprintf(pattern, "%%%dx%%n", inpos);
sscanf(hex, pattern, &val, &outpos);
if (inpos != outpos)
LOGE("bad cpio header\n");
return val;
}
2019-02-22 07:56:18 +00:00
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)) {
2018-10-25 01:08:06 +00:00
uint32_t namesize = x8u(header.namesize);
2019-01-20 04:59:37 +00:00
filename.resize(namesize - 1);
xxread(fd, &filename[0], namesize);
2018-10-25 01:08:06 +00:00
parse_align();
if (filesize) {
data = xmalloc(filesize);
xxread(fd, data, filesize);
parse_align();
}
}
cpio_entry::~cpio_entry() {
free(data);
}
cpio::cpio(const char *filename) {
int fd = open(filename, O_RDONLY);
if (fd < 0) return;
fprintf(stderr, "Loading cpio: [%s]\n", filename);
cpio_newc_header header;
2019-02-22 07:56:18 +00:00
unique_ptr<cpio_entry> entry;
2018-10-25 01:08:06 +00:00
while(xxread(fd, &header, sizeof(cpio_newc_header)) != -1) {
2019-02-22 07:56:18 +00:00
entry = std::make_unique<cpio_entry>(fd, header);
if (entry->filename == "." || entry->filename == "..")
continue;
2019-02-22 07:56:18 +00:00
if (entry->filename == "TRAILER!!!")
break;
entries.push_back(std::move(entry));
2018-10-25 01:08:06 +00:00
}
close(fd);
}
#define dump_align() write_zero(fd, align_off(lseek(fd, 0, SEEK_CUR), 4))
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);
2019-02-22 07:56:18 +00:00
for (auto &e : entries) {
2018-10-25 01:08:06 +00:00
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,
1, // e->nlink
0, // e->mtime
e->filesize,
0, // e->devmajor
0, // e->devminor
0, // e->rdevmajor
0, // e->rdevminor
2019-01-20 04:59:37 +00:00
(uint32_t) e->filename.size() + 1,
2018-10-25 01:08:06 +00:00
0 // e->check
);
xwrite(fd, header, 110);
2019-01-20 04:59:37 +00:00
xwrite(fd, e->filename.c_str(), e->filename.size() + 1);
2018-10-25 01:08:06 +00:00
dump_align();
if (e->filesize) {
xwrite(fd, e->data, e->filesize);
dump_align();
}
}
// Write trailer
sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
inode++, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 11, 0);
xwrite(fd, header, 110);
xwrite(fd, "TRAILER!!!\0", 11);
dump_align();
close(fd);
}
int cpio::find(const char *name) {
2019-02-22 07:56:18 +00:00
for (int i = 0; i < entries.size(); ++i) {
if (!entries[i])
2018-10-25 01:08:06 +00:00
continue;
2019-02-22 07:56:18 +00:00
if (entries[i]->filename == name)
2018-10-25 01:08:06 +00:00
return i;
}
return -1;
}
void cpio::insert(cpio_entry *e) {
2019-02-22 07:56:18 +00:00
auto ue = unique_ptr<cpio_entry>(e);
insert(ue);
}
void cpio::insert(unique_ptr<cpio_entry> &e) {
2019-01-20 04:59:37 +00:00
int i = find(e->filename.c_str());
2018-10-25 01:08:06 +00:00
if (i >= 0) {
2019-02-22 07:56:18 +00:00
entries[i] = std::move(e);
2018-11-07 07:10:38 +00:00
} else {
2019-02-22 07:56:18 +00:00
entries.push_back(std::move(e));
2018-10-25 01:08:06 +00:00
}
}
2018-11-07 07:10:38 +00:00
void cpio::rm(const char *name, bool r) {
2018-10-25 01:08:06 +00:00
size_t len = strlen(name);
2019-02-22 07:56:18 +00:00
for (auto &e : entries) {
2018-10-25 01:08:06 +00:00
if (!e)
continue;
2019-01-20 04:59:37 +00:00
if (e->filename.compare(0, len, name) == 0 &&
2018-11-07 07:10:38 +00:00
((r && e->filename[len] == '/') || e->filename[len] == '\0')) {
fprintf(stderr, "Remove [%s]\n", e->filename.c_str());
2019-02-22 07:56:18 +00:00
e.reset();
if (!r) return;
2018-10-25 01:08:06 +00:00
}
}
}
void cpio::makedir(mode_t mode, const char *name) {
2019-02-22 07:56:18 +00:00
auto e = make_unique<cpio_entry>(name);
2018-10-25 01:08:06 +00:00
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) {
2019-02-22 07:56:18 +00:00
auto e = make_unique<cpio_entry>(name);
2018-10-25 01:08:06 +00:00
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::add(mode_t mode, const char *name, const char *file) {
2019-02-22 07:56:18 +00:00
void *buf;
size_t sz;
mmap_ro(file, &buf, &sz);
auto e = make_unique<cpio_entry>(name);
2018-10-25 01:08:06 +00:00
e->mode = S_IFREG | mode;
2019-02-22 07:56:18 +00:00
e->filesize = sz;
e->data = xmalloc(sz);
memcpy(e->data, buf, sz);
munmap(buf, sz);
2018-10-25 01:08:06 +00:00
insert(e);
fprintf(stderr, "Add entry [%s] (%04o)\n", name, mode);
}
bool cpio::mv(const char *from, const char *to) {
int f = find(from), t = find(to);
if (f >= 0) {
2019-02-22 07:56:18 +00:00
if (t > 0)
entries[t].reset();
2018-10-25 01:08:06 +00:00
fprintf(stderr, "Move [%s] -> [%s]\n", from, to);
2019-02-22 07:56:18 +00:00
entries[f]->filename = to;
2018-10-25 01:08:06 +00:00
return true;
}
fprintf(stderr, "Cannot find entry %s\n", from);
return false;
}
2018-11-07 07:10:38 +00:00
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;
}
}
2018-10-25 01:08:06 +00:00
void cpio::extract() {
2019-02-22 07:56:18 +00:00
for (auto &e : entries) {
2019-01-20 04:59:37 +00:00
if (!e) continue;
2019-02-22 07:56:18 +00:00
extract_entry(e.get(), e->filename.c_str());
2018-10-25 01:08:06 +00:00
}
}
bool cpio::extract(const char *name, const char *file) {
int i = find(name);
if (i > 0) {
2019-02-22 07:56:18 +00:00
extract_entry(entries[i].get(), file);
2018-10-25 01:08:06 +00:00
return true;
}
fprintf(stderr, "Cannot find the file entry [%s]\n", name);
return false;
}
void cpio::sort() {
2019-02-22 07:56:18 +00:00
std::sort(entries.begin(), entries.end(), []
(const unique_ptr<cpio_entry> &a, const unique_ptr<cpio_entry> &b) -> bool {
if (a == b || !a)
2019-01-20 04:59:37 +00:00
return false;
2019-02-22 07:56:18 +00:00
if (!b)
2019-01-20 04:59:37 +00:00
return true;
2019-02-22 07:56:18 +00:00
return a->filename < b->filename;
2019-01-20 04:59:37 +00:00
});
2019-02-22 07:56:18 +00:00
while (!entries.back())
entries.pop_back();
2018-10-25 01:08:06 +00:00
}