tCode cleanup - pm - barely a pack manager
 (HTM) git clone git://z3bra.org/pm
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit fb63ff08cb9d292b1b689ade10fad57a4d485a0d
 (DIR) parent be43a04a0611f495360a0211868eb642224715a9
 (HTM) Author: z3bra <willyatmailoodotorg>
       Date:   Thu, 31 Dec 2015 11:06:20 +0100
       
       Code cleanup
       
       Diffstat:
         M pm.c                                |      64 +++++++++++++++++++------------
       
       1 file changed, 39 insertions(+), 25 deletions(-)
       ---
 (DIR) diff --git a/pm.c b/pm.c
       t@@ -12,12 +12,12 @@
        
        #include "arg.h"
        
       -#define PACKAGE_ROOT  "test/rootfs"
       -#define PACKAGE_DATA  "test/metadata"
       +#define PACKAGE_ROOT  "/ns/distro/rootfs"
       +#define PACKAGE_DATA  "/ns/distro/rootfs/var/lib/pack"
        #define PACKAGE_BUFF_SIZE 8192
        #define PACKAGE_EXTENSION ".tar.bz2"
        
       -struct pkg {
       +struct pack {
                char *path;
                char *name;
                char *version;
       t@@ -42,20 +42,20 @@ enum {
        };
        
        void usage(char *name);
       -int d_empty(char *dir);
       -int p_mkdir(char *dir, mode_t mode);
       +int is_empty(char *dir);
       +int mkdir_parents(char *dir, mode_t mode);
        char *base_name(char *path);
        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, struct pkg *pack);
       +int metadata(char *datadir, struct pack *pack);
        int pack(char *out, char **filename);
        int unpack(char *root, char *in);
        int delete_content(FILE *metafile);
        int delete(const char *datadir, const char *rootfs, const char *name);
       -struct pkg *pack_load_file(char *file);
       -struct pkg *pack_load(char *name);
       +struct pack *pack_load_file(char *file);
       +struct pack *pack_load(char *name);
        
        char *argv0;
        
       t@@ -65,11 +65,12 @@ usage(char *name)
                fprintf(stderr, "usage: %s -adi <pack>\n" , name);
        }
        
       +
        /*
         * returns 0 if a directory is empty, -1 otherwise
         */
        int
       -d_empty(char *dir)
       +is_empty(char *dir)
        {
                DIR *d;
                struct dirent *p;
       t@@ -88,12 +89,13 @@ d_empty(char *dir)
                return 0;
        }
        
       +
        /*
         * recursive mkdir, taken from the ii project
         * http://nion.modprobe.de/blog/archives/357-Recursive-directory-creation.html
         */
        int
       -p_mkdir(char *dir, mode_t mode)
       +mkdir_parents(char *dir, mode_t mode)
        {
                char tmp[PATH_MAX];
                char *p = NULL;
       t@@ -112,6 +114,7 @@ p_mkdir(char *dir, mode_t mode)
                return mkdir(tmp, mode);
        }
        
       +
        /*
         * return a pointer to the basename, or NULL if path ends with '/'
         */
       t@@ -127,7 +130,7 @@ inspect(char *name)
        {
                struct stat st;
        
       -        /* name is NULL, list packages installed */
       +        /* name is NULL, list packs installed */
                if (!name)
                        return list_local(1, PACKAGE_DATA);
        
       t@@ -139,6 +142,7 @@ inspect(char *name)
                return list_archive(1, name);
        }
        
       +
        /*
         * write the content of an archive to a filedes
         */
       t@@ -173,7 +177,7 @@ list_archive(int fd, char *filename)
        
        
        /*
       - * write files installed by a package to a filedes
       + * write files installed by a pack to a filedes
         */
        int
        list_content(int fd, char *name)
       t@@ -195,7 +199,7 @@ list_content(int fd, char *name)
        
        
        /*
       - * write packages installed in PACKAGE_ROOT to a filedes
       + * write packs installed in PACKAGE_ROOT to a filedes
         */
        int
        list_local(int fd, char *datadir)
       t@@ -232,13 +236,13 @@ list_local(int fd, char *datadir)
        
        
        /*
       - * write metadata about a package file
       + * write metadata about a pack file
         *
         * TODO:
         *        + /deps /asdep
         */
        int
       -metadata(char *datadir, struct pkg *pack)
       +metadata(char *datadir, struct pack *pack)
        {
                int fd, r;
                struct stat st;
       t@@ -251,7 +255,7 @@ metadata(char *datadir, struct pkg *pack)
                free(name);
        
                if (stat(tmp, &st) < 0) {
       -                r = p_mkdir(tmp, 0750);
       +                r = mkdir_parents(tmp, 0750);
                        if (r < 0)
                                return r;
                }
       t@@ -278,6 +282,7 @@ metadata(char *datadir, struct pkg *pack)
                return 0;
        }
        
       +
        /*
         * pack mutliple files into an archive
         */
       t@@ -326,6 +331,7 @@ pack(char *out, char **filename)
                return 0;
        }
        
       +
        /*
         * extract files into the given directory
         */
       t@@ -355,7 +361,7 @@ unpack(char *root, char *in)
                if (r != ARCHIVE_OK)
                        return r;
        
       -        /* extract the package at the specified root */
       +        /* extract the pack at the specified root */
                if (chdir(root) < 0) {
                        perror("chdir");
                        return -1;
       t@@ -388,6 +394,7 @@ unpack(char *root, char *in)
                return 0;
        }
        
       +
        /*
         * Delete entries listed in a file recursively
         * This will also remove directories if they are empty
       t@@ -414,7 +421,7 @@ delete_content(FILE *f)
                }
        
                stat(file, &st);
       -        if (S_ISDIR(st.st_mode) && d_empty(file))
       +        if (S_ISDIR(st.st_mode) && is_empty(file))
                        rmdir(file);
                else
                        unlink(file);
       t@@ -422,8 +429,9 @@ delete_content(FILE *f)
                return 0;
        }
        
       +
        /*
       - * Delete all files related to a package. Installed files, but also metadata
       + * Delete all files related to a pack. Installed files, but also metadata
         */
        int
        delete(const char *datadir, const char *rootfs, const char *name)
       t@@ -462,11 +470,12 @@ delete(const char *datadir, const char *rootfs, const char *name)
                return 0;
        }
        
       -struct pkg *
       +
       +struct pack *
        pack_load_file(char *path)
        {
                int fd;
       -        struct pkg *pack = malloc(sizeof(struct pkg));
       +        struct pack *pack = malloc(sizeof(struct pack));
                char tmp[PATH_MAX];
                char *p;
        
       t@@ -502,10 +511,15 @@ pack_load_file(char *path)
                return pack;
        }
        
       -struct pkg *
       +/*
       + * build a package structure from the given argument
       + *
       + * TODO: handle pack names (built from cache/repo file
       + */
       +struct pack *
        pack_load(char *name)
        {
       -        struct pkg *p = NULL;
       +        struct pack *p = NULL;
                struct stat st;
        
                if (stat(name, &st) < 0)
       t@@ -514,7 +528,7 @@ pack_load(char *name)
                p = pack_load_file(name);
        
                if (!p) {
       -                fprintf(stderr, "could not load package %s\n", name);
       +                fprintf(stderr, "could not load pack %s\n", name);
                        return NULL;
                }
        
       t@@ -525,7 +539,7 @@ int
        main (int argc, char **argv)
        {
                char *n = NULL;
       -        struct pkg *p = NULL;
       +        struct pack *p = NULL;
                uint8_t action = ACTION_INVALID;
        
                ARGBEGIN{