Add cpio-mv action

This commit is contained in:
topjohnwu 2017-09-09 23:05:50 +08:00
parent 7e65296470
commit b7b4164f4f
3 changed files with 40 additions and 10 deletions

View File

@ -40,7 +40,7 @@ static void cpio_vec_insert(struct vector *v, cpio_file *n) {
vec_push_back(v, n); vec_push_back(v, n);
} }
static int cpio_compare(const void *a, const void *b) { static int cpio_cmp(const void *a, const void *b) {
return strcmp((*(cpio_file **) a)->filename, (*(cpio_file **) b)->filename); return strcmp((*(cpio_file **) a)->filename, (*(cpio_file **) b)->filename);
} }
@ -92,7 +92,7 @@ static void dump_cpio(const char *filename, struct vector *v) {
unsigned inode = 300000; unsigned inode = 300000;
char header[111]; char header[111];
// Sort by name // Sort by name
vec_sort(v, cpio_compare); vec_sort(v, cpio_cmp);
cpio_file *f; cpio_file *f;
vec_for_each(v, f) { vec_for_each(v, f) {
if (f->remove) continue; if (f->remove) continue;
@ -139,15 +139,17 @@ static void cpio_vec_destroy(struct vector *v) {
static void cpio_rm(int recursive, const char *entry, struct vector *v) { static void cpio_rm(int recursive, const char *entry, struct vector *v) {
cpio_file *f; cpio_file *f;
vec_for_each(v, f) { vec_for_each(v, f) {
if ((recursive && strncmp(f->filename, entry, strlen(entry)) == 0) if (strncmp(f->filename, entry, strlen(entry)) == 0) {
|| (strcmp(f->filename, entry) == 0) ) { char next = f->filename[strlen(entry)];
if ((recursive && next == '/') || next == '\0') {
if (!f->remove) { if (!f->remove) {
fprintf(stderr, "Remove [%s]\n", entry); fprintf(stderr, "Remove [%s]\n", f->filename);
f->remove = 1; f->remove = 1;
} }
if (!recursive) return; if (!recursive) return;
} }
} }
}
} }
static void cpio_mkdir(mode_t mode, const char *entry, struct vector *v) { static void cpio_mkdir(mode_t mode, const char *entry, struct vector *v) {
@ -345,8 +347,8 @@ static void cpio_backup(const char *orig, struct vector *v) {
cpio_rm(1, ".backup", v); cpio_rm(1, ".backup", v);
// Sort both vectors before comparing // Sort both vectors before comparing
vec_sort(v, cpio_compare); vec_sort(v, cpio_cmp);
vec_sort(o, cpio_compare); vec_sort(o, cpio_cmp);
// Init the directory and rmlist // Init the directory and rmlist
dir->filename = strdup(".backup"); dir->filename = strdup(".backup");
@ -470,6 +472,27 @@ static void cpio_stocksha1(struct vector *v) {
} }
} }
static void cpio_mv(struct vector *v, const char *from, const char *to) {
struct cpio_file *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->namesize = strlen(to) + 1;
f->filename = strdup(to);
return;
}
}
fprintf(stderr, "Cannot find entry %s\n", from);
exit(1);
}
int cpio_commands(const char *command, int argc, char *argv[]) { int cpio_commands(const char *command, int argc, char *argv[]) {
int recursive = 0, ret = 0; int recursive = 0, ret = 0;
command_t cmd; command_t cmd;
@ -491,6 +514,8 @@ int cpio_commands(const char *command, int argc, char *argv[]) {
++argv; ++argv;
--argc; --argc;
} }
} else if (argc == 2 && strcmp(command, "mv") == 0) {
cmd = MV;
} else if (argc == 2 && strcmp(command, "patch") == 0) { } else if (argc == 2 && strcmp(command, "patch") == 0) {
cmd = PATCH; cmd = PATCH;
} else if (argc == 2 && strcmp(command, "extract") == 0) { } else if (argc == 2 && strcmp(command, "extract") == 0) {
@ -532,6 +557,9 @@ int cpio_commands(const char *command, int argc, char *argv[]) {
case ADD: case ADD:
cpio_add(strtoul(argv[0], NULL, 8), argv[1], argv[2], &v); cpio_add(strtoul(argv[0], NULL, 8), argv[1], argv[2], &v);
break; break;
case MV:
cpio_mv(&v, argv[0], argv[1]);
break;
case NONE: case NONE:
return 1; return 1;
} }

View File

@ -52,6 +52,7 @@ typedef enum {
RM, RM,
MKDIR, MKDIR,
ADD, ADD,
MV,
EXTRACT, EXTRACT,
TEST, TEST,
PATCH, PATCH,

View File

@ -24,6 +24,7 @@ static void usage(char *arg0) {
" -rm [-r] <entry>\n Remove entry from <incpio>, flag -r to remove recursively\n" " -rm [-r] <entry>\n Remove entry from <incpio>, flag -r to remove recursively\n"
" -mkdir <mode> <entry>\n Create directory as an <entry>\n" " -mkdir <mode> <entry>\n Create directory as an <entry>\n"
" -add <mode> <entry> <infile>\n Add <infile> as an <entry>; replaces <entry> if already exists\n" " -add <mode> <entry> <infile>\n Add <infile> as an <entry>; replaces <entry> if already exists\n"
" -mv <from-entry> <to-entry>\n Move <from-entry> to <to-entry>\n"
" -extract <entry> <outfile>\n Extract <entry> to <outfile>\n" " -extract <entry> <outfile>\n Extract <entry> to <outfile>\n"
" -test \n Return value: 0/not patched 1/Magisk 2/Other (e.g. phh, SuperSU)\n" " -test \n Return value: 0/not patched 1/Magisk 2/Other (e.g. phh, SuperSU)\n"
" -patch <KEEPVERITY> <KEEPFORCEENCRYPT>\n Patch cpio for Magisk. KEEP**** are true/false values\n" " -patch <KEEPVERITY> <KEEPFORCEENCRYPT>\n Patch cpio for Magisk. KEEP**** are true/false values\n"