Add symlink feature to cpio

This commit is contained in:
topjohnwu 2017-12-04 03:22:26 +08:00
parent 2e25431bb6
commit 2212800a23
4 changed files with 15 additions and 1 deletions

View File

@ -47,6 +47,7 @@ void dump_cpio(struct vector *v, const char *filename);
void cpio_vec_destroy(struct vector *v); void cpio_vec_destroy(struct vector *v);
void cpio_rm(struct vector *v, int recursive, const char *entry); void cpio_rm(struct vector *v, int recursive, const char *entry);
void cpio_mkdir(struct vector *v, mode_t mode, const char *entry); void cpio_mkdir(struct vector *v, mode_t mode, const char *entry);
void cpio_ln(struct vector *v, const char *target, const char *entry);
void cpio_add(struct vector *v, mode_t mode, const char *entry, const char *filename); void cpio_add(struct vector *v, mode_t mode, const char *entry, const char *filename);
int cpio_mv(struct vector *v, const char *from, const char *to); int cpio_mv(struct vector *v, const char *from, const char *to);
int cpio_extract(struct vector *v, const char *entry, const char *filename); int cpio_extract(struct vector *v, const char *entry, const char *filename);

View File

@ -35,9 +35,11 @@ static void usage(char *arg0) {
" Do cpio related cmds to <incpio> (modifications are done directly)\n" " Do cpio related cmds to <incpio> (modifications are done directly)\n"
" Supported commands:\n" " Supported commands:\n"
" -rm [-r] <entry>\n" " -rm [-r] <entry>\n"
" Remove entry from <incpio>, flag -r to remove recursively\n" " Remove entry from <incpio>, flag [-r] to remove recursively\n"
" -mkdir <mode> <entry>\n" " -mkdir <mode> <entry>\n"
" Create directory as an <entry>\n" " Create directory as an <entry>\n"
" -ln <target> <entry>\n"
" Create symlink <entry> to point to <target>\n"
" -add <mode> <entry> <infile>\n" " -add <mode> <entry> <infile>\n"
" Add <infile> as an <entry>; replaces <entry> if already exists\n" " Add <infile> as an <entry>; replaces <entry> if already exists\n"
" -mv <from-entry> <to-entry>\n" " -mv <from-entry> <to-entry>\n"

View File

@ -105,6 +105,8 @@ int cpio_commands(const char *command, int argc, char *argv[]) {
return cpio_extract(&v, argv[0], argv[1]); return cpio_extract(&v, argv[0], argv[1]);
} else if (argc == 2 && strcmp(command, "mkdir") == 0) { } else if (argc == 2 && strcmp(command, "mkdir") == 0) {
cpio_mkdir(&v, strtoul(argv[0], NULL, 8), argv[1]); cpio_mkdir(&v, strtoul(argv[0], NULL, 8), argv[1]);
} else if (argc == 2 && strcmp(command, "ln") == 0) {
cpio_ln(&v, argv[0], argv[1]);
} else if (argc == 3 && strcmp(command, "add") == 0) { } else if (argc == 3 && strcmp(command, "add") == 0) {
cpio_add(&v, strtoul(argv[0], NULL, 8), argv[1], argv[2]); cpio_add(&v, strtoul(argv[0], NULL, 8), argv[1], argv[2]);
} else { } else {

View File

@ -164,6 +164,15 @@ void cpio_mkdir(struct vector *v, mode_t mode, const char *entry) {
fprintf(stderr, "Create directory [%s] (%04o)\n",entry, mode); fprintf(stderr, "Create directory [%s] (%04o)\n",entry, mode);
} }
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);
f->data = strdup(target);
cpio_vec_insert(v, f);
fprintf(stderr, "Create symlink [%s] -> [%s]\n", entry, target);
}
void cpio_add(struct vector *v, mode_t mode, const char *entry, const char *filename) { void cpio_add(struct vector *v, mode_t mode, const char *entry, const char *filename) {
int fd = xopen(filename, O_RDONLY); int fd = xopen(filename, O_RDONLY);
cpio_entry *f = xcalloc(sizeof(*f), 1); cpio_entry *f = xcalloc(sizeof(*f), 1);