pm

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

commit b524aa9f8ac73b9a4c65824380ed5375f6fcef5c
parent 249db914430a74a809ef89a9fee35f745295bdf1
Author: z3bra <willyatmailoodotorg>
Date:   Tue Dec 22 14:10:11 2015

Rename 'pack' to 'pm'

Diffstat:
 makefile |   2 +-
 pack.c   | 191 +----------------------------------------------------------------
 pm.c     | 191 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 192 insertions(+), 192 deletions(-)

diff --git a/makefile b/makefile @@ -1,6 +1,6 @@ include config.mk -BIN = pack +BIN = pm all: $(BIN) diff --git a/pack.c b/pack.c @@ -1,191 +0,0 @@ -#include <fcntl.h> -#include <stdio.h> -#include <stdlib.h> -#include <sys/stat.h> -#include <sys/types.h> - -#include <archive.h> -#include <archive_entry.h> - -#include "arg.h" - -#define PACKAGE_ROOT "." -#define PACKAGE_BUFF_SIZE 8192 - - -char *argv0; - -void -usage(char *name) -{ - fprintf(stderr, "usage: %s -cel <archive> [files..]\n", name); - exit(1); -} - -/* - * output the content of an archive - */ -int -inspect(char *filename) -{ - struct archive *a; - struct archive_entry *e; - int r; - - /* configure archive to support all types */ - a = archive_read_new(); - archive_read_support_filter_all(a); - archive_read_support_format_all(a); - - /* set blocksize to 0 as libarchive will choose the best size anyway */ - r = archive_read_open_filename(a, filename, 0); - if (r != ARCHIVE_OK) - return r; - - /* output each filename to stdout */ - while(archive_read_next_header(a, &e) == ARCHIVE_OK) { - puts(archive_entry_pathname(e)); - } - - r = archive_read_free(a); - if (r != ARCHIVE_OK) - return r; - - return 0; -} - -/* - * pack mutliple files into an archive - */ -int -pack(const char *out, char **filename) -{ - struct archive *a; - struct archive_entry *e; - struct stat st; - char buf[PACKAGE_BUFF_SIZE]; - size_t len; - int fd, r; - - a = archive_write_new(); - archive_write_add_filter_bzip2(a); - archive_write_set_format_pax_restricted(a); - - r = archive_write_open_filename(a, out); - if (r != ARCHIVE_OK) - return r; - - while (*filename) { - stat(*filename, &st); - if (!S_ISDIR(st.st_mode)) { - e = archive_entry_new(); - - archive_entry_set_pathname(e, *filename); - archive_entry_set_size(e, st.st_size); - archive_entry_set_filetype(e, AE_IFREG); - archive_entry_set_mode(e, st.st_mode); - archive_write_header(a, e); - - fd = open(*filename, O_RDONLY); - if (fd < 0) - return fd; - - while ((len = read(fd, buf, sizeof(buf))) > 0) - archive_write_data(a, buf, len); - - close(fd); - archive_entry_free(e); - } - filename++; - } - archive_write_free(a); - return 0; -} - - -/* - * extract files into the gien directory - */ -int -unpack(char *root, const char *in) -{ - struct archive *a; - struct archive *w; /* write */ - struct archive_entry *e; - const void *buf; - size_t len; - off_t off; - int r; - int mask = ARCHIVE_EXTRACT_PERM - |ARCHIVE_EXTRACT_NO_OVERWRITE - |ARCHIVE_EXTRACT_SECURE_NODOTDOT; - - /* extract the package at the specified root */ - if (chdir(root) < 0) { - perror("chdir"); - return -1; - } - - a = archive_read_new(); - archive_read_support_filter_all(a); - archive_read_support_format_all(a); - - w = archive_write_disk_new(); - archive_write_disk_set_options(w, mask); - archive_write_disk_set_standard_lookup(w); - - r = archive_read_open_filename(a, in, 0); - if (r != ARCHIVE_OK) - return r; - - while (archive_read_next_header(a, &e) != ARCHIVE_EOF) { - - r = archive_write_header(w, e); - if (r < ARCHIVE_OK) { - fprintf(stderr, "%s\n", archive_error_string(w)); - return r; - } - - if (archive_entry_size(e) > 0) - do { - r = archive_read_data_block(a, &buf, &len, &off); - if (r != ARCHIVE_OK) - return r; - /* returns the number of bytes written */ - r = archive_write_data_block(w, buf, len, off); - } while (r > 0); - if (r < 0) - return r; - } - archive_read_close(a); - archive_read_free(a); - archive_write_close(w); - archive_write_free(w); - - return 0; -} - -int -main (int argc, char **argv) -{ - const char *fn; - - ARGBEGIN{ - case 'c': - if (argc <3) /* what a cute variable */ - usage(argv0); - fn = ARGF(); - pack(fn, ++argv); - break; - case 'e': - unpack(PACKAGE_ROOT, EARGF(usage(argv0))); - break; - case 'l': - inspect(EARGF(usage(argv0))); - break; - default: - usage(argv0); - }ARGEND; - - return 0; -}- \ No newline at end of file diff --git a/pm.c b/pm.c @@ -0,0 +1,191 @@ +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <sys/types.h> + +#include <archive.h> +#include <archive_entry.h> + +#include "arg.h" + +#define PACKAGE_ROOT "." +#define PACKAGE_BUFF_SIZE 8192 + + +char *argv0; + +void +usage(char *name) +{ + fprintf(stderr, "usage: %s -cel <archive> [files..]\n", name); + exit(1); +} + +/* + * output the content of an archive + */ +int +inspect(char *filename) +{ + struct archive *a; + struct archive_entry *e; + int r; + + /* configure archive to support all types */ + a = archive_read_new(); + archive_read_support_filter_all(a); + archive_read_support_format_all(a); + + /* set blocksize to 0 as libarchive will choose the best size anyway */ + r = archive_read_open_filename(a, filename, 0); + if (r != ARCHIVE_OK) + return r; + + /* output each filename to stdout */ + while(archive_read_next_header(a, &e) == ARCHIVE_OK) { + puts(archive_entry_pathname(e)); + } + + r = archive_read_free(a); + if (r != ARCHIVE_OK) + return r; + + return 0; +} + +/* + * pack mutliple files into an archive + */ +int +pack(const char *out, char **filename) +{ + struct archive *a; + struct archive_entry *e; + struct stat st; + char buf[PACKAGE_BUFF_SIZE]; + size_t len; + int fd, r; + + a = archive_write_new(); + archive_write_add_filter_bzip2(a); + archive_write_set_format_pax_restricted(a); + + r = archive_write_open_filename(a, out); + if (r != ARCHIVE_OK) + return r; + + while (*filename) { + stat(*filename, &st); + if (!S_ISDIR(st.st_mode)) { + e = archive_entry_new(); + + archive_entry_set_pathname(e, *filename); + archive_entry_set_size(e, st.st_size); + archive_entry_set_filetype(e, AE_IFREG); + archive_entry_set_mode(e, st.st_mode); + archive_write_header(a, e); + + fd = open(*filename, O_RDONLY); + if (fd < 0) + return fd; + + while ((len = read(fd, buf, sizeof(buf))) > 0) + archive_write_data(a, buf, len); + + close(fd); + archive_entry_free(e); + } + filename++; + } + archive_write_free(a); + return 0; +} + + +/* + * extract files into the gien directory + */ +int +unpack(char *root, const char *in) +{ + struct archive *a; + struct archive *w; /* write */ + struct archive_entry *e; + const void *buf; + size_t len; + off_t off; + int r; + int mask = ARCHIVE_EXTRACT_PERM + |ARCHIVE_EXTRACT_NO_OVERWRITE + |ARCHIVE_EXTRACT_SECURE_NODOTDOT; + + /* extract the package at the specified root */ + if (chdir(root) < 0) { + perror("chdir"); + return -1; + } + + a = archive_read_new(); + archive_read_support_filter_all(a); + archive_read_support_format_all(a); + + w = archive_write_disk_new(); + archive_write_disk_set_options(w, mask); + archive_write_disk_set_standard_lookup(w); + + r = archive_read_open_filename(a, in, 0); + if (r != ARCHIVE_OK) + return r; + + while (archive_read_next_header(a, &e) != ARCHIVE_EOF) { + + r = archive_write_header(w, e); + if (r < ARCHIVE_OK) { + fprintf(stderr, "%s\n", archive_error_string(w)); + return r; + } + + if (archive_entry_size(e) > 0) + do { + r = archive_read_data_block(a, &buf, &len, &off); + if (r != ARCHIVE_OK) + return r; + /* returns the number of bytes written */ + r = archive_write_data_block(w, buf, len, off); + } while (r > 0); + if (r < 0) + return r; + } + archive_read_close(a); + archive_read_free(a); + archive_write_close(w); + archive_write_free(w); + + return 0; +} + +int +main (int argc, char **argv) +{ + const char *fn; + + ARGBEGIN{ + case 'c': + if (argc <3) /* what a cute variable */ + usage(argv0); + fn = ARGF(); + pack(fn, ++argv); + break; + case 'e': + unpack(PACKAGE_ROOT, EARGF(usage(argv0))); + break; + case 'l': + inspect(EARGF(usage(argv0))); + break; + default: + usage(argv0); + }ARGEND; + + return 0; +}+ \ No newline at end of file