Magisk/core/jni/utils/cpio.c

254 lines
6.4 KiB
C
Raw Normal View History

2017-09-14 23:11:56 +08:00
#include <stdio.h>
#include <unistd.h>
2017-11-10 01:51:41 +08:00
#include <fcntl.h>
2017-09-14 23:11:56 +08:00
2017-03-08 00:54:23 +08:00
#include "cpio.h"
2017-09-14 23:11:56 +08:00
#include "logging.h"
#include "utils.h"
2017-03-08 00:54:23 +08: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);
2017-09-14 23:11:56 +08:00
if (inpos != outpos) LOGE("bad cpio header\n");
2017-03-08 00:54:23 +08:00
return val;
}
2017-09-14 10:47:10 +08:00
static void cpio_free(cpio_entry *f) {
2017-03-10 07:52:59 +08:00
if (f) {
free(f->filename);
free(f->data);
free(f);
}
}
2017-12-07 01:30:48 +08:00
int cpio_cmp(const void *a, const void *b) {
return strcmp((*(cpio_entry **) a)->filename, (*(cpio_entry **) b)->filename);
}
void cpio_vec_insert(struct vector *v, cpio_entry *n) {
2017-09-14 10:47:10 +08:00
cpio_entry *f;
2017-03-08 00:54:23 +08:00
vec_for_each(v, f) {
if (strcmp(f->filename, n->filename) == 0) {
// Replace, then all is done
2017-03-10 07:52:59 +08:00
cpio_free(f);
2017-09-07 19:22:30 +08:00
vec_cur(v) = n;
2017-03-08 00:54:23 +08:00
return;
}
}
2017-09-07 19:22:30 +08:00
vec_push_back(v, n);
2017-03-08 00:54:23 +08:00
}
2017-09-14 10:47:10 +08:00
// Parse cpio file to a vector of cpio_entry
2017-12-04 03:14:04 +08:00
void parse_cpio(struct vector *v, const char *filename) {
2017-07-18 11:53:28 +08:00
fprintf(stderr, "Loading cpio: [%s]\n\n", filename);
2017-12-04 03:14:04 +08:00
int fd = open(filename, O_RDONLY);
if (fd < 0) return;
2017-03-08 00:54:23 +08:00
cpio_newc_header header;
2017-09-14 10:47:10 +08:00
cpio_entry *f;
2017-09-07 19:22:30 +08:00
while(xxread(fd, &header, 110) != -1) {
2017-04-28 21:48:38 +08:00
f = xcalloc(sizeof(*f), 1);
2017-03-08 00:54:23 +08:00
// f->ino = x8u(header.ino);
f->mode = x8u(header.mode);
f->uid = x8u(header.uid);
f->gid = x8u(header.gid);
// f->nlink = x8u(header.nlink);
// f->mtime = x8u(header.mtime);
f->filesize = x8u(header.filesize);
// f->devmajor = x8u(header.devmajor);
// f->devminor = x8u(header.devminor);
// f->rdevmajor = x8u(header.rdevmajor);
// f->rdevminor = x8u(header.rdevminor);
2017-12-04 03:14:04 +08:00
uint32_t namesize = x8u(header.namesize);
2017-03-08 00:54:23 +08:00
// f->check = x8u(header.check);
2017-12-04 03:14:04 +08:00
f->filename = xmalloc(namesize);
xxread(fd, f->filename, namesize);
2017-03-08 00:54:23 +08:00
file_align(fd, 4, 0);
2017-03-16 06:46:19 +08:00
if (strcmp(f->filename, ".") == 0 || strcmp(f->filename, "..") == 0) {
2017-03-10 07:52:59 +08:00
cpio_free(f);
continue;
2017-03-10 04:08:17 +08:00
}
2017-03-16 06:46:19 +08:00
if (strcmp(f->filename, "TRAILER!!!") == 0) {
cpio_free(f);
break;
}
2017-03-08 00:54:23 +08:00
if (f->filesize) {
2017-12-04 03:14:04 +08:00
f->data = xmalloc(f->filesize);
2017-04-28 21:48:38 +08:00
xxread(fd, f->data, f->filesize);
2017-03-08 00:54:23 +08:00
file_align(fd, 4, 0);
}
vec_push_back(v, f);
}
close(fd);
}
2017-12-04 03:14:04 +08:00
void dump_cpio(struct vector *v, const char *filename) {
2017-07-18 11:53:28 +08:00
fprintf(stderr, "\nDump cpio: [%s]\n\n", filename);
2017-11-10 01:51:41 +08:00
int fd = creat(filename, 0644);
2017-03-08 00:54:23 +08:00
unsigned inode = 300000;
char header[111];
2017-09-07 19:22:30 +08:00
// Sort by name
2017-09-09 23:05:50 +08:00
vec_sort(v, cpio_cmp);
2017-09-14 10:47:10 +08:00
cpio_entry *f;
2017-03-08 00:54:23 +08:00
vec_for_each(v, f) {
if (f->remove) continue;
sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
inode++, // f->ino
f->mode,
f->uid,
f->gid,
1, // f->nlink
0, // f->mtime
f->filesize,
0, // f->devmajor
0, // f->devminor
0, // f->rdevmajor
0, // f->rdevminor
2017-12-04 03:14:04 +08:00
(uint32_t) strlen(f->filename) + 1,
2017-03-08 00:54:23 +08:00
0 // f->check
);
2017-04-28 21:48:38 +08:00
xwrite(fd, header, 110);
2017-12-04 03:14:04 +08:00
xwrite(fd, f->filename, strlen(f->filename) + 1);
2017-03-08 00:54:23 +08:00
file_align(fd, 4, 1);
if (f->filesize) {
2017-04-28 21:48:38 +08:00
xwrite(fd, f->data, f->filesize);
2017-03-08 00:54:23 +08:00
file_align(fd, 4, 1);
}
}
2017-03-10 04:08:17 +08:00
// 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);
2017-04-28 21:48:38 +08:00
xwrite(fd, header, 110);
xwrite(fd, "TRAILER!!!\0", 11);
2017-03-10 04:08:17 +08:00
file_align(fd, 4, 1);
2017-03-10 07:52:59 +08:00
close(fd);
2017-03-08 00:54:23 +08:00
}
2017-12-01 17:17:24 +08:00
void cpio_vec_destroy(struct vector *v) {
2017-09-14 10:47:10 +08:00
// Free each cpio_entry
cpio_entry *f;
2017-03-08 00:54:23 +08:00
vec_for_each(v, f) {
2017-03-10 07:52:59 +08:00
cpio_free(f);
2017-03-08 00:54:23 +08:00
}
vec_destroy(v);
}
2017-12-04 03:14:04 +08:00
void cpio_rm(struct vector *v, int recursive, const char *entry) {
2017-09-14 10:47:10 +08:00
cpio_entry *f;
2017-03-08 00:54:23 +08:00
vec_for_each(v, f) {
2017-09-09 23:05:50 +08:00
if (strncmp(f->filename, entry, strlen(entry)) == 0) {
char next = f->filename[strlen(entry)];
if ((recursive && next == '/') || next == '\0') {
if (!f->remove) {
fprintf(stderr, "Remove [%s]\n", f->filename);
f->remove = 1;
}
if (!recursive) return;
2017-03-10 07:52:59 +08:00
}
2017-03-08 00:54:23 +08:00
}
}
}
2017-12-04 03:14:04 +08:00
void cpio_mkdir(struct vector *v, mode_t mode, const char *entry) {
2017-09-14 10:47:10 +08:00
cpio_entry *f = xcalloc(sizeof(*f), 1);
2017-03-08 00:54:23 +08:00
f->mode = S_IFDIR | mode;
2017-09-07 19:22:30 +08:00
f->filename = strdup(entry);
2017-03-08 00:54:23 +08:00
cpio_vec_insert(v, f);
2017-07-18 11:53:28 +08:00
fprintf(stderr, "Create directory [%s] (%04o)\n",entry, mode);
2017-03-08 00:54:23 +08:00
}
2017-12-04 03:22:26 +08:00
void cpio_ln(struct vector *v, const char *target, const char *entry) {
cpio_entry *f = xcalloc(sizeof(*f), 1);
f->mode = S_IFLNK;
f->filename = strdup(entry);
2017-12-05 03:32:37 +08:00
f->filesize = strlen(target);
2017-12-04 03:22:26 +08:00
f->data = strdup(target);
cpio_vec_insert(v, f);
fprintf(stderr, "Create symlink [%s] -> [%s]\n", entry, target);
}
2017-12-04 03:14:04 +08:00
void cpio_add(struct vector *v, mode_t mode, const char *entry, const char *filename) {
2017-04-28 21:48:38 +08:00
int fd = xopen(filename, O_RDONLY);
2017-09-14 10:47:10 +08:00
cpio_entry *f = xcalloc(sizeof(*f), 1);
2017-03-08 00:54:23 +08:00
f->mode = S_IFREG | mode;
2017-09-07 19:22:30 +08:00
f->filename = strdup(entry);
2017-03-08 00:54:23 +08:00
f->filesize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
2017-09-07 19:22:30 +08:00
f->data = xmalloc(f->filesize);
2017-04-28 21:48:38 +08:00
xxread(fd, f->data, f->filesize);
2017-03-10 07:52:59 +08:00
close(fd);
2017-03-08 00:54:23 +08:00
cpio_vec_insert(v, f);
2017-07-18 11:53:28 +08:00
fprintf(stderr, "Add entry [%s] (%04o)\n", entry, mode);
2017-03-08 00:54:23 +08:00
}
2017-03-10 04:08:17 +08:00
2017-12-04 03:14:04 +08:00
int cpio_mv(struct vector *v, const char *from, const char *to) {
2017-12-01 17:17:24 +08:00
struct cpio_entry *f, *t;
vec_for_each(v, f) {
if (strcmp(f->filename, from) == 0) {
fprintf(stderr, "Move [%s] -> [%s]\n", from, to);
vec_for_each(v, t) {
if (strcmp(t->filename, to) == 0) {
t->remove = 1;
break;
}
}
free(f->filename);
f->filename = strdup(to);
2017-12-04 03:14:04 +08:00
return 0;
2017-12-01 17:17:24 +08:00
}
}
fprintf(stderr, "Cannot find entry %s\n", from);
2017-12-04 03:14:04 +08:00
return 1;
2017-12-01 17:17:24 +08:00
}
2017-12-04 03:14:04 +08:00
int cpio_extract(struct vector *v, const char *entry, const char *filename) {
cpio_entry *f;
vec_for_each(v, f) {
if (strcmp(f->filename, entry) == 0) {
fprintf(stderr, "Extracting [%s] to [%s]\n\n", entry, filename);
if (S_ISREG(f->mode)) {
int fd = creat(filename, f->mode & 0777);
xwrite(fd, f->data, f->filesize);
fchown(fd, f->uid, f->gid);
close(fd);
} else if (S_ISLNK(f->mode)) {
2017-12-05 03:32:37 +08:00
char *target = xcalloc(f->filesize + 1, 1);
memcpy(target, f->data, f->filesize);
2017-12-06 12:51:16 +08:00
unlink(filename);
2017-12-05 03:32:37 +08:00
symlink(target, filename);
2017-12-04 03:14:04 +08:00
}
return 0;
}
}
fprintf(stderr, "Cannot find the file entry [%s]\n", entry);
return 1;
}
2017-12-05 03:32:37 +08:00
void cpio_extract_all(struct vector *v) {
cpio_entry *f;
vec_for_each(v, f) {
fprintf(stderr, "Extracting [%s]\n", f->filename);
unlink(f->filename);
rmdir(f->filename);
if (S_ISDIR(f->mode)) {
mkdir(f->filename, f->mode & 0777);
} else if (S_ISREG(f->mode)) {
int fd = creat(f->filename, f->mode & 0777);
xwrite(fd, f->data, f->filesize);
fchown(fd, f->uid, f->gid);
close(fd);
} else if (S_ISLNK(f->mode)) {
char *target = xcalloc(f->filesize + 1, 1);
memcpy(target, f->data, f->filesize);
symlink(target, f->filename);
}
}
}