261 lines
6.6 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>
2018-09-27 03:11:10 -04:00
#include <stdlib.h>
#include <string.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-12-21 03:36:18 +08:00
void cpio_free(cpio_entry *e) {
if (e) {
free(e->filename);
free(e->data);
free(e);
2017-03-10 07:52:59 +08:00
}
}
2017-12-21 03:36:18 +08:00
int cpio_find(struct vector *v, const char *entry) {
cpio_entry *e;
vec_for_each(v, e) {
if (!e) continue;
if (strcmp(e->filename, entry) == 0)
return _;
}
return -1;
}
2017-12-07 01:30:48 +08:00
int cpio_cmp(const void *a, const void *b) {
2017-12-21 03:36:18 +08:00
return strcmp(((cpio_entry *) a)->filename, ((cpio_entry *) b)->filename);
2017-12-07 01:30:48 +08:00
}
void cpio_vec_insert(struct vector *v, cpio_entry *n) {
2017-12-21 03:36:18 +08:00
int i = cpio_find(v, n->filename);
if (i >= 0) {
2017-12-21 03:36:18 +08:00
// Replace, then all is done
cpio_free(vec_entry(v)[i]);
vec_entry(v)[i] = n;
return;
2017-03-08 00:54:23 +08:00
}
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
2018-01-29 22:16:02 +08:00
#define parse_align() lseek(fd, align(lseek(fd, 0, SEEK_CUR), 4), SEEK_SET)
2017-12-04 03:14:04 +08:00
void parse_cpio(struct vector *v, const char *filename) {
int fd = open(filename, O_RDONLY);
if (fd < 0) return;
fprintf(stderr, "Loading cpio: [%s]\n", filename);
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);
2018-01-29 22:16:02 +08:00
parse_align();
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);
2018-01-29 22:16:02 +08:00
parse_align();
2017-03-08 00:54:23 +08:00
}
vec_push_back(v, f);
}
close(fd);
}
2018-01-29 22:16:02 +08:00
#define dump_align() write_zero(fd, align_off(lseek(fd, 0, SEEK_CUR), 4))
2017-12-04 03:14:04 +08:00
void dump_cpio(struct vector *v, const char *filename) {
2017-12-21 03:36:18 +08:00
fprintf(stderr, "Dump cpio: [%s]\n", filename);
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-12-21 03:36:18 +08:00
cpio_entry *e;
int fd = creat(filename, 0644);
vec_for_each(v, e) {
2017-03-08 00:54:23 +08:00
sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
2017-12-21 03:36:18 +08:00
inode++, // e->ino
e->mode,
e->uid,
e->gid,
1, // e->nlink
0, // e->mtime
e->filesize,
0, // e->devmajor
0, // e->devminor
0, // e->rdevmajor
0, // e->rdevminor
(uint32_t) strlen(e->filename) + 1,
0 // e->check
2017-03-08 00:54:23 +08:00
);
2017-04-28 21:48:38 +08:00
xwrite(fd, header, 110);
2017-12-21 03:36:18 +08:00
xwrite(fd, e->filename, strlen(e->filename) + 1);
2018-01-29 22:16:02 +08:00
dump_align();
2017-12-21 03:36:18 +08:00
if (e->filesize) {
xwrite(fd, e->data, e->filesize);
2018-01-29 22:16:02 +08:00
dump_align();
2017-03-08 00:54:23 +08:00
}
}
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);
2018-01-29 22:16:02 +08:00
dump_align();
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
2017-12-21 03:36:18 +08:00
cpio_entry *e;
vec_for_each(v, e)
cpio_free(e);
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-12-21 03:36:18 +08:00
cpio_entry *e;
size_t len = strlen(entry);
vec_for_each(v, e) {
if (!e) continue;
if (strncmp(e->filename, entry, len) == 0) {
if ((recursive && e->filename[len] == '/') || e->filename[len] == '\0') {
fprintf(stderr, "Remove [%s]\n", e->filename);
cpio_free(e);
vec_cur(v) = NULL;
2017-09-09 23:05:50 +08:00
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-12-21 03:36:18 +08:00
cpio_entry *e = xcalloc(sizeof(*e), 1);
e->mode = S_IFDIR | mode;
e->filename = strdup(entry);
cpio_vec_insert(v, e);
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) {
2017-12-21 03:36:18 +08:00
cpio_entry *e = xcalloc(sizeof(*e), 1);
e->mode = S_IFLNK;
e->filename = strdup(entry);
e->filesize = strlen(target);
e->data = strdup(target);
cpio_vec_insert(v, e);
2017-12-04 03:22:26 +08:00
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-12-21 03:36:18 +08:00
cpio_entry *e = xcalloc(sizeof(*e), 1);
e->mode = S_IFREG | mode;
e->filename = strdup(entry);
e->filesize = lseek(fd, 0, SEEK_END);
2017-03-08 00:54:23 +08:00
lseek(fd, 0, SEEK_SET);
2017-12-21 03:36:18 +08:00
e->data = xmalloc(e->filesize);
xxread(fd, e->data, e->filesize);
2017-03-10 07:52:59 +08:00
close(fd);
2017-12-21 03:36:18 +08:00
cpio_vec_insert(v, e);
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-21 03:36:18 +08:00
struct cpio_entry *e;
int f = cpio_find(v, from), t = cpio_find(v, to);
if (f > 0) {
if (t > 0) {
cpio_free(vec_entry(v)[t]);
vec_entry(v)[t] = NULL;
2017-12-01 17:17:24 +08:00
}
2017-12-21 03:36:18 +08:00
e = vec_entry(v)[f];
free(e->filename);
e->filename = strdup(to);
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) {
2017-12-21 03:36:18 +08:00
int i = cpio_find(v, entry);
if (i > 0) {
cpio_entry *e = vec_entry(v)[i];
fprintf(stderr, "Extracting [%s] to [%s]\n", entry, filename);
if (S_ISREG(e->mode)) {
int fd = creat(filename, e->mode & 0777);
xwrite(fd, e->data, e->filesize);
fchown(fd, e->uid, e->gid);
close(fd);
} else if (S_ISLNK(e->mode)) {
char *target = xcalloc(e->filesize + 1, 1);
memcpy(target, e->data, e->filesize);
unlink(filename);
symlink(target, filename);
2017-12-04 03:14:04 +08:00
}
2017-12-21 03:36:18 +08:00
return 0;
2017-12-04 03:14:04 +08:00
}
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) {
2017-12-21 03:36:18 +08:00
cpio_entry *e;
vec_for_each(v, e) {
if (!e) continue;
fprintf(stderr, "Extracting [%s]\n", e->filename);
unlink(e->filename);
rmdir(e->filename);
if (S_ISDIR(e->mode)) {
mkdir(e->filename, e->mode & 0777);
} else if (S_ISREG(e->mode)) {
int fd = creat(e->filename, e->mode & 0777);
xwrite(fd, e->data, e->filesize);
fchown(fd, e->uid, e->gid);
2017-12-05 03:32:37 +08:00
close(fd);
2017-12-21 03:36:18 +08:00
} else if (S_ISLNK(e->mode)) {
char *target = xcalloc(e->filesize + 1, 1);
memcpy(target, e->data, e->filesize);
symlink(target, e->filename);
2017-12-05 03:32:37 +08:00
}
}
}