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