mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-04-23 17:11:43 +00:00
Small improvement to cpio
This commit is contained in:
parent
cd5f5d702f
commit
7e65296470
@ -101,7 +101,7 @@ static struct node_entry *insert_child(struct node_entry *p, struct node_entry *
|
|||||||
if (c->status > e->status) {
|
if (c->status > e->status) {
|
||||||
// Precedence is higher, replace with new node
|
// Precedence is higher, replace with new node
|
||||||
destroy_subtree(e);
|
destroy_subtree(e);
|
||||||
vec_entry(p->children)[_] = c;
|
vec_cur(p->children) = c;
|
||||||
return c;
|
return c;
|
||||||
} else {
|
} else {
|
||||||
// Free the new entry, return old
|
// Free the new entry, return old
|
||||||
@ -654,7 +654,7 @@ void post_fs_data(int client) {
|
|||||||
child->name = strdup("vendor");
|
child->name = strdup("vendor");
|
||||||
child->status = 0;
|
child->status = 0;
|
||||||
// Swap!
|
// Swap!
|
||||||
vec_entry(sys_root->children)[_] = child;
|
vec_cur(sys_root->children) = child;
|
||||||
ven_root->parent = NULL;
|
ven_root->parent = NULL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -32,4 +32,6 @@ struct vector *vec_dup(struct vector *v);
|
|||||||
e = v ? (v)->data[(v)->size - 1] : NULL; \
|
e = v ? (v)->data[(v)->size - 1] : NULL; \
|
||||||
for (size_t _ = (v)->size; v && _ > 0; --_, e = (v)->data[_ - 1])
|
for (size_t _ = (v)->size; v && _ > 0; --_, e = (v)->data[_ - 1])
|
||||||
|
|
||||||
|
#define vec_cur(v) vec_entry(v)[_]
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -28,32 +28,16 @@ static void cpio_free(cpio_file *f) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void cpio_vec_insert(struct vector *v, cpio_file *n) {
|
static void cpio_vec_insert(struct vector *v, cpio_file *n) {
|
||||||
cpio_file *f, *t;
|
cpio_file *f;
|
||||||
int shift = 0;
|
|
||||||
// Insert in alphabet order
|
|
||||||
vec_for_each(v, f) {
|
vec_for_each(v, f) {
|
||||||
if (shift) {
|
|
||||||
vec_entry(v)[_] = t;
|
|
||||||
t = f;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
t = f;
|
|
||||||
if (strcmp(f->filename, n->filename) == 0) {
|
if (strcmp(f->filename, n->filename) == 0) {
|
||||||
// Replace, then all is done
|
// Replace, then all is done
|
||||||
cpio_free(f);
|
cpio_free(f);
|
||||||
vec_entry(v)[_] = n;
|
vec_cur(v) = n;
|
||||||
return;
|
return;
|
||||||
} else if (strcmp(f->filename, n->filename) > 0) {
|
|
||||||
// Insert, then start shifting
|
|
||||||
vec_entry(v)[_] = n;
|
|
||||||
t = f;
|
|
||||||
shift = 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (shift)
|
vec_push_back(v, n);
|
||||||
vec_push_back(v, t);
|
|
||||||
else
|
|
||||||
vec_push_back(v, n);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cpio_compare(const void *a, const void *b) {
|
static int cpio_compare(const void *a, const void *b) {
|
||||||
@ -66,7 +50,7 @@ static void parse_cpio(const char *filename, struct vector *v) {
|
|||||||
int fd = xopen(filename, O_RDONLY);
|
int fd = xopen(filename, O_RDONLY);
|
||||||
cpio_newc_header header;
|
cpio_newc_header header;
|
||||||
cpio_file *f;
|
cpio_file *f;
|
||||||
while(read(fd, &header, 110) == 110) {
|
while(xxread(fd, &header, 110) != -1) {
|
||||||
f = xcalloc(sizeof(*f), 1);
|
f = xcalloc(sizeof(*f), 1);
|
||||||
// f->ino = x8u(header.ino);
|
// f->ino = x8u(header.ino);
|
||||||
f->mode = x8u(header.mode);
|
f->mode = x8u(header.mode);
|
||||||
@ -81,7 +65,7 @@ static void parse_cpio(const char *filename, struct vector *v) {
|
|||||||
// f->rdevminor = x8u(header.rdevminor);
|
// f->rdevminor = x8u(header.rdevminor);
|
||||||
f->namesize = x8u(header.namesize);
|
f->namesize = x8u(header.namesize);
|
||||||
// f->check = x8u(header.check);
|
// f->check = x8u(header.check);
|
||||||
f->filename = malloc(f->namesize);
|
f->filename = xmalloc(f->namesize);
|
||||||
xxread(fd, f->filename, f->namesize);
|
xxread(fd, f->filename, f->namesize);
|
||||||
file_align(fd, 4, 0);
|
file_align(fd, 4, 0);
|
||||||
if (strcmp(f->filename, ".") == 0 || strcmp(f->filename, "..") == 0) {
|
if (strcmp(f->filename, ".") == 0 || strcmp(f->filename, "..") == 0) {
|
||||||
@ -100,8 +84,6 @@ static void parse_cpio(const char *filename, struct vector *v) {
|
|||||||
vec_push_back(v, f);
|
vec_push_back(v, f);
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
// Sort by name
|
|
||||||
vec_sort(v, cpio_compare);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump_cpio(const char *filename, struct vector *v) {
|
static void dump_cpio(const char *filename, struct vector *v) {
|
||||||
@ -109,6 +91,8 @@ static void dump_cpio(const char *filename, struct vector *v) {
|
|||||||
int fd = open_new(filename);
|
int fd = open_new(filename);
|
||||||
unsigned inode = 300000;
|
unsigned inode = 300000;
|
||||||
char header[111];
|
char header[111];
|
||||||
|
// Sort by name
|
||||||
|
vec_sort(v, cpio_compare);
|
||||||
cpio_file *f;
|
cpio_file *f;
|
||||||
vec_for_each(v, f) {
|
vec_for_each(v, f) {
|
||||||
if (f->remove) continue;
|
if (f->remove) continue;
|
||||||
@ -170,8 +154,7 @@ static void cpio_mkdir(mode_t mode, const char *entry, struct vector *v) {
|
|||||||
cpio_file *f = xcalloc(sizeof(*f), 1);
|
cpio_file *f = xcalloc(sizeof(*f), 1);
|
||||||
f->mode = S_IFDIR | mode;
|
f->mode = S_IFDIR | mode;
|
||||||
f->namesize = strlen(entry) + 1;
|
f->namesize = strlen(entry) + 1;
|
||||||
f->filename = xmalloc(f->namesize);
|
f->filename = strdup(entry);
|
||||||
memcpy(f->filename, entry, f->namesize);
|
|
||||||
cpio_vec_insert(v, f);
|
cpio_vec_insert(v, f);
|
||||||
fprintf(stderr, "Create directory [%s] (%04o)\n",entry, mode);
|
fprintf(stderr, "Create directory [%s] (%04o)\n",entry, mode);
|
||||||
}
|
}
|
||||||
@ -181,11 +164,10 @@ static void cpio_add(mode_t mode, const char *entry, const char *filename, struc
|
|||||||
cpio_file *f = xcalloc(sizeof(*f), 1);
|
cpio_file *f = xcalloc(sizeof(*f), 1);
|
||||||
f->mode = S_IFREG | mode;
|
f->mode = S_IFREG | mode;
|
||||||
f->namesize = strlen(entry) + 1;
|
f->namesize = strlen(entry) + 1;
|
||||||
f->filename = xmalloc(f->namesize);
|
f->filename = strdup(entry);
|
||||||
memcpy(f->filename, entry, f->namesize);
|
|
||||||
f->filesize = lseek(fd, 0, SEEK_END);
|
f->filesize = lseek(fd, 0, SEEK_END);
|
||||||
lseek(fd, 0, SEEK_SET);
|
lseek(fd, 0, SEEK_SET);
|
||||||
f->data = malloc(f->filesize);
|
f->data = xmalloc(f->filesize);
|
||||||
xxread(fd, f->data, f->filesize);
|
xxread(fd, f->data, f->filesize);
|
||||||
close(fd);
|
close(fd);
|
||||||
cpio_vec_insert(v, f);
|
cpio_vec_insert(v, f);
|
||||||
@ -362,6 +344,10 @@ static void cpio_backup(const char *orig, struct vector *v) {
|
|||||||
cpio_rm(1, ".backup", o);
|
cpio_rm(1, ".backup", o);
|
||||||
cpio_rm(1, ".backup", v);
|
cpio_rm(1, ".backup", v);
|
||||||
|
|
||||||
|
// Sort both vectors before comparing
|
||||||
|
vec_sort(v, cpio_compare);
|
||||||
|
vec_sort(o, cpio_compare);
|
||||||
|
|
||||||
// Init the directory and rmlist
|
// Init the directory and rmlist
|
||||||
dir->filename = strdup(".backup");
|
dir->filename = strdup(".backup");
|
||||||
dir->namesize = strlen(dir->filename) + 1;
|
dir->namesize = strlen(dir->filename) + 1;
|
||||||
@ -431,9 +417,6 @@ static void cpio_backup(const char *orig, struct vector *v) {
|
|||||||
dir->remove = 1;
|
dir->remove = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort
|
|
||||||
vec_sort(v, cpio_compare);
|
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
cpio_vec_destroy(o);
|
cpio_vec_destroy(o);
|
||||||
}
|
}
|
||||||
@ -454,10 +437,9 @@ static int cpio_restore(struct vector *v) {
|
|||||||
n = xcalloc(sizeof(*n), 1);
|
n = xcalloc(sizeof(*n), 1);
|
||||||
memcpy(n, f, sizeof(*f));
|
memcpy(n, f, sizeof(*f));
|
||||||
n->namesize -= 8;
|
n->namesize -= 8;
|
||||||
n->filename = xmalloc(n->namesize);
|
n->filename = strdup(f->filename + 8);
|
||||||
memcpy(n->filename, f->filename + 8, n->namesize);
|
n->data = f->data;
|
||||||
n->data = xmalloc(n->filesize);
|
f->data = NULL;
|
||||||
memcpy(n->data, f->data, n->filesize);
|
|
||||||
n->remove = 0;
|
n->remove = 0;
|
||||||
fprintf(stderr, "Restoring [%s] -> [%s]\n", f->filename, n->filename);
|
fprintf(stderr, "Restoring [%s] -> [%s]\n", f->filename, n->filename);
|
||||||
cpio_vec_insert(v, n);
|
cpio_vec_insert(v, n);
|
||||||
|
@ -47,4 +47,17 @@ typedef struct cpio_newc_header {
|
|||||||
char check[8];
|
char check[8];
|
||||||
} cpio_newc_header;
|
} cpio_newc_header;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
NONE,
|
||||||
|
RM,
|
||||||
|
MKDIR,
|
||||||
|
ADD,
|
||||||
|
EXTRACT,
|
||||||
|
TEST,
|
||||||
|
PATCH,
|
||||||
|
BACKUP,
|
||||||
|
RESTORE,
|
||||||
|
STOCKSHA1
|
||||||
|
} command_t;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -46,19 +46,6 @@ typedef enum {
|
|||||||
MTK
|
MTK
|
||||||
} file_t;
|
} file_t;
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
NONE,
|
|
||||||
RM,
|
|
||||||
MKDIR,
|
|
||||||
ADD,
|
|
||||||
EXTRACT,
|
|
||||||
TEST,
|
|
||||||
PATCH,
|
|
||||||
BACKUP,
|
|
||||||
RESTORE,
|
|
||||||
STOCKSHA1
|
|
||||||
} command_t;
|
|
||||||
|
|
||||||
extern char *SUP_LIST[];
|
extern char *SUP_LIST[];
|
||||||
extern char *SUP_EXT_LIST[];
|
extern char *SUP_EXT_LIST[];
|
||||||
extern file_t SUP_TYPE_LIST[];
|
extern file_t SUP_TYPE_LIST[];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user