pm

barely a pack manager
git clone git://z3bra.org/pm
Log | Files | Refs | README | LICENSE

commit 1e59dea52f46876bd239166d24fdf680bf8965f1
parent cdc9e1e65dc1b8a5d90ea60d708e81f599c0f2bf
Author: z3bra <willyatmailoodotorg>
Date:   Tue Jan 12 13:35:50 2016

Move all libarchive code to unpack()

Diffstat:
 pm.c | 57 ++++++++++++++++++++++++++++-----------------------------
 1 file changed, 28 insertions(+), 29 deletions(-)

diff --git a/pm.c b/pm.c @@ -58,13 +58,15 @@ int inspect(char *datadir, char *packname); int write_metadata(char *datadir, struct pack *pack); int write_entry(struct archive *a, struct archive *w); -int unpack(struct archive *a, char *metafile, int mask); -int install(char *rootfs, char *datadir, struct pack *p, int overwrite); +int unpack(char *datadir, struct pack *p); +int install(char *rootfs, char *datadir, struct pack *p); int delete_content(FILE *metafile); int delete(const char *rootfs, const char *datadir, const char *name); char *argv0; +int overwrite = 0; + void usage(char *name) { @@ -313,17 +315,34 @@ write_entry(struct archive *a, struct archive *w) * Extract a tarball to the given directory */ int -unpack(struct archive *a, char *meta, int mask) +unpack(char *datadir, struct pack *p) { int r, fd; + struct archive *a; struct archive *w; struct archive_entry *e; + char tmp[PATH_MAX] = ""; - if ((fd = open(meta, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) { - perror(meta); + int mask = ARCHIVE_EXTRACT_PERM + |ARCHIVE_EXTRACT_SECURE_NODOTDOT; + + snprintf(tmp, PATH_MAX, "%s/%s/files", datadir, p->name); + if ((fd = open(tmp, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) { + perror(tmp); return -1; } + a = archive_read_new(); + archive_read_support_filter_bzip2(a); + archive_read_support_format_tar(a); + + /* try and open the tarball of our pack */ + if ((r = archive_read_open_filename(a, p->path, 0)) != ARCHIVE_OK) { + fprintf(stderr, "%s: %s\n", p->name, archive_error_string(a)); + archive_read_free(a); + return r; + } + w = archive_write_disk_new(); archive_write_disk_set_options(w, mask); @@ -350,8 +369,8 @@ unpack(struct archive *a, char *meta, int mask) archive_write_finish_entry(w); } - archive_write_close(w); archive_write_free(w); + archive_read_free(a); return r == ARCHIVE_EOF ? ARCHIVE_OK : r; } @@ -362,18 +381,13 @@ unpack(struct archive *a, char *meta, int mask) * If overwrite is set to 1, it will overwrite all files */ int -install(char *rootfs, char *datadir, struct pack *p, int overwrite) +install(char *rootfs, char *datadir, struct pack *p) { - struct archive *a; - char meta[PATH_MAX] = ""; int r; - int mask = ARCHIVE_EXTRACT_PERM - |ARCHIVE_EXTRACT_SECURE_NODOTDOT; if (overwrite == 0) { if (inspect_collision(rootfs, p) != 0) return -1; - mask |= ARCHIVE_EXTRACT_NO_OVERWRITE; } /* @@ -390,21 +404,7 @@ install(char *rootfs, char *datadir, struct pack *p, int overwrite) return -1; } - a = archive_read_new(); - archive_read_support_filter_bzip2(a); - archive_read_support_format_tar(a); - - /* try and open the tarball of our pack */ - if ((r = archive_read_open_filename(a, p->path, 0)) != ARCHIVE_OK) { - fprintf(stderr, "%s: %s\n", p->name, archive_error_string(a)); - return r; - } - - snprintf(meta, PATH_MAX, "%s/%s/files", datadir, p->name); - r = unpack(a, meta, mask); - - archive_read_close(a); - archive_read_free(a); + r = unpack(datadir, p); return r; } @@ -581,7 +581,6 @@ pack_free(struct pack *p) int main (int argc, char **argv) { - int overwrite = 0; char *n = NULL; struct pack *p = NULL; uint8_t action = ACTION_INSPECT; @@ -609,7 +608,7 @@ main (int argc, char **argv) case ACTION_INSTALL: while (*argv) { if ((p = pack_load(*(argv++)))) { - install(PACK_ROOT, PACK_DATA, p, overwrite); + install(PACK_ROOT, PACK_DATA, p); pack_free(p); } }