URI: 
       tPerform a smart inspection - pm - barely a pack manager
  HTML git clone git://z3bra.org/pm
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit a90605e2b376486576007c7acb6967ca3af0823f
   DIR parent fb204995f7751e8a1e834455275fbaa2f44f7bab
  HTML Author: z3bra <willyatmailoodotorg>
       Date:   Wed, 30 Dec 2015 16:31:39 +0100
       
       Perform a smart inspection
       
       The -i flag is used to inspect packages. It can decide what to do
       depending on the argument provided with it:
       
               # output the list of installed packs
               pm -i
       
               # output the content of an installed pack
               pm -i <packname>
       
               # output the content of a pack archive
               # (triggered if <pack> contains a '/')
               pm -i ./<packfile>
       
       Diffstat:
         M pm.c                                |     121 +++++++++++++++----------------
       
       1 file changed, 59 insertions(+), 62 deletions(-)
       ---
   DIR diff --git a/pm.c b/pm.c
       t@@ -14,7 +14,6 @@
        
        #define PACKAGE_ROOT  "test/rootfs"
        #define PACKAGE_DATA  "test/metadata"
       -#define PACKAGE_CACHE "test/cache"
        #define PACKAGE_BUFF_SIZE 8192
        #define PACKAGE_EXTENSION ".tar.bz2"
        
       t@@ -30,12 +29,9 @@ enum {
                ACTION_DELETE      = 1,
                ACTION_UPDATE      = 2,
                ACTION_INSPECT     = 3,
       -        ACTION_LIST_FILES  = 4,
       -        ACTION_LIST_DEPS   = 5,
       -        ACTION_LIST_LOCAL  = 6,
       -        ACTION_LIST_REMOTE = 7,
       -        ACTION_PACKAGE     = 8,
       -        ACTION_INVALID     = 9
       +        ACTION_LIST_DEPS   = 4,
       +        ACTION_LIST_REMOTE = 5,
       +        ACTION_INVALID     = 6
        };
        
        /* error codes */
       t@@ -49,9 +45,10 @@ void usage(char *name);
        int d_empty(char *dir);
        int p_mkdir(char *dir, mode_t mode);
        char *base_name(char *path);
       -int inspect(int fd, char *filename);
       -int list_local(const char *datadir);
       -int list_content(const char *name);
       +int inspect(char *name);
       +int list_archive(int fd, char *filename);
       +int list_local(int fd, char *packname);
       +int list_content(int fd, char *packname);
        int metadata(char *datadir, char *filename);
        int pack(char *out, char **filename);
        int unpack(char *root, char *in);
       t@@ -65,7 +62,7 @@ char *argv0;
        void
        usage(char *name)
        {
       -        fprintf(stderr, "usage: %s [-l] [-acir <package> [files..]]\n" , name);
       +        fprintf(stderr, "usage: %s -adi <pack>\n" , name);
        }
        
        /*
       t@@ -125,11 +122,26 @@ base_name(char *path)
                return b ? b + 1 : path;
        }
        
       +int
       +inspect(char *name)
       +{
       +        /* name is NULL, list packages installed */
       +        if (!name)
       +                return list_local(1, PACKAGE_DATA);
       +
       +        /* if name contains a '/', assume it's an archive */
       +        if (strchr(name, '/'))
       +                return list_archive(1, name);
       +
       +        /* otherwise, list files installed by a package */
       +        return list_content(1, name);
       +}
       +
        /*
       - * write the content of an archive to the given file descriptor
       + * write the content of an archive to a filedes
         */
        int
       -inspect(int fd, char *filename)
       +list_archive(int fd, char *filename)
        {
                struct archive *a;
                struct archive_entry *e;
       t@@ -158,46 +170,51 @@ inspect(int fd, char *filename)
        }
        
        
       +/*
       + * write files installed by a package to a filedes
       + */
        int
       -list_local(const char *datadir)
       +list_content(int fd, char *name)
        {
       -        DIR *d;
       -        struct dirent *p;
       +        int meta;
       +        char tmp[PATH_MAX];
        
       -        if (!(d = opendir(datadir))) {
       -                perror("opendir");
       +        snprintf(tmp, PATH_MAX, "%s/%s/files", PACKAGE_DATA, name);
       +        if ((meta = open(tmp, O_RDONLY)) < 0) {
       +                perror("open");
                        return -1;
                }
        
       -        while ((p = readdir(d)))
       -                if (strcmp(p->d_name, ".") && strcmp(p->d_name, ".."))
       -                        printf("%s\n", p->d_name);
       +        while (read(meta, tmp, LINE_MAX))
       +                dprintf(fd, "%s", tmp);
        
       -        closedir(d);
                return 0;
        }
        
       +
        /*
       - * List files installed by a package
       + * write packages installed in PACKAGE_ROOT to a filedes
         */
        int
       -list_content(const char *name)
       +list_local(int fd, char *datadir)
        {
       -        int fd;
       -        char tmp[PATH_MAX];
       +        DIR *d;
       +        struct dirent *p;
        
       -        snprintf(tmp, PATH_MAX, "%s/%s/files", PACKAGE_DATA, name);
       -        if ((fd = open(tmp, O_RDONLY)) < 0) {
       -                perror("open");
       +        if (!(d = opendir(datadir))) {
       +                perror("opendir");
                        return -1;
                }
        
       -        while (read(fd, tmp, LINE_MAX))
       -                printf("%s", tmp);
       +        while ((p = readdir(d)))
       +                if (strcmp(p->d_name, ".") && strcmp(p->d_name, ".."))
       +                        dprintf(fd, "%s\n", p->d_name);
        
       +        closedir(d);
                return 0;
        }
        
       +
        /*
         * write metadata about a package file
         *
       t@@ -226,7 +243,7 @@ metadata(char *datadir, char *filename)
                strncat(tmp, "/files", PATH_MAX);
                fd = open(tmp, O_CREAT|O_WRONLY|O_TRUNC, 0644);
                if (fd > 0) {
       -                inspect(fd, filename);
       +                list_archive(fd, filename);
                        close(fd);
                        return 0;
                }
       t@@ -470,7 +487,6 @@ pack_load(char *name)
                        return NULL;
                }
        
       -        printf("%s - %s (%s)\n", p->name, p->version, p->path);
                return p;
        }
        
       t@@ -482,31 +498,18 @@ main (int argc, char **argv)
                uint8_t action = ACTION_INVALID;
        
                ARGBEGIN{
       -        case 'c':
       -                if (argc <3) /* what a cute variable */
       -                        usage(argv0);
       -                action = ACTION_PACKAGE;
       -                n = ARGF();
       -                break;
                case 'a':
                        action = ACTION_INSTALL;
                        n = EARGF(usage(argv0));
                        break;
       -        case 'i':
       -                action = ACTION_INSPECT;
       +        case 'd':
       +                action = ACTION_DELETE;
                        n = EARGF(usage(argv0));
                        break;
       -        case 'l':
       -                if (argc > 1) {
       -                        action = ACTION_LIST_FILES;
       +        case 'i':
       +                action = ACTION_INSPECT;
       +                if (argc > 1)
                                n = ARGF();
       -                } else {
       -                        action = ACTION_LIST_LOCAL;
       -                }
       -                break;
       -        case 'r':
       -                action = ACTION_DELETE;
       -                n = EARGF(usage(argv0));
                        break;
                case 'h':
                default:
       t@@ -518,6 +521,7 @@ main (int argc, char **argv)
                        p = pack_load(n);
        
                switch (action) {
       +
                case ACTION_INSTALL:
                        if (!p)
                                return ERR_PACKAGE_LOAD;
       t@@ -525,20 +529,13 @@ main (int argc, char **argv)
                                return unpack(PACKAGE_ROOT, p->path);
                        fprintf(stderr, "could not write metadata for %s\n", p->name);
                        return ERR_METADATA;
       +
                case ACTION_DELETE:
                        return delete(PACKAGE_DATA, PACKAGE_ROOT, n);
       +
                case ACTION_INSPECT:
       -                if (!p)
       -                        return ERR_PACKAGE_LOAD;
       -                return inspect(1, p->path);
       -        case ACTION_PACKAGE:
       -                if (!p)
       -                        return ERR_PACKAGE_LOAD;
       -                return pack(p->path, argv);
       -        case ACTION_LIST_LOCAL:
       -                return list_local(PACKAGE_DATA);
       -        case ACTION_LIST_FILES:
       -                return list_content(n);
       +                return inspect(n);
       +
                /* handle me, Octave */
                case ACTION_UPDATE:
                case ACTION_LIST_DEPS: