tAdd ability to load a pack from the metadata directory - pm - barely a pack manager
 (HTM) git clone git://z3bra.org/pm
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 77b61b8c42ad62ed77124cf43cb68632ad9d5005
 (DIR) parent 1be3eff999d9f349dbc430701429058d30152778
 (HTM) Author: z3bra <willyatmailoodotorg>
       Date:   Thu, 16 Jun 2016 13:17:13 +0200
       
       Add ability to load a pack from the metadata directory
       
       Diffstat:
         M pm.c                                |      61 ++++++++++++++++++++++++++-----
       
       1 file changed, 52 insertions(+), 9 deletions(-)
       ---
 (DIR) diff --git a/pm.c b/pm.c
       t@@ -51,7 +51,9 @@ int is_empty(char *dir);
        int mkdir_parents(char *dir, mode_t mode);
        char *base_name(char *path);
        
       -struct pack *pack_load(char *path);
       +struct pack *pack_load_tarball(char *path);
       +struct pack *pack_load_directory(const char *datadir, char *path);
       +struct pack *pack_load(const char *datadir, char *path);
        void pack_free(struct pack *p);
        int pack_install(const char *rootfs, const char *datadir, struct pack *p);
        
       t@@ -148,7 +150,6 @@ int
        inspect_version(const char *datadir, const char *name, char version[LINE_MAX])
        {
                FILE *stream;
       -        size_t len = 0;
                char tmp[PATH_MAX] = "", *lf = NULL;
        
                snprintf(tmp, PATH_MAX, "%s/%s/version", datadir, name);
       t@@ -239,9 +240,7 @@ inspect_system(int fd, const char *datadir)
        {
                DIR *d;
                struct dirent *p;
       -        char tmp[PATH_MAX] = "", ver[LINE_MAX];
       -        int meta;
       -        size_t len;
       +        char ver[LINE_MAX];
        
                if (!(d = opendir(datadir))) {
                        perror(datadir);
       t@@ -287,7 +286,7 @@ write_metadata(const char *datadir, struct pack *p)
                char tmp[PATH_MAX];
        
                snprintf(tmp, PATH_MAX, "%s/%s", datadir, p->name);
       -        if (stat(tmp, &st) < 0) {
       +        if (stat(tmp, &st) < 0 && errno == ENOENT) {
                        if ((r = mkdir_parents(tmp, 0755)) < 0)
                                return r;
                }
       t@@ -603,7 +602,7 @@ install(const char *rootfs, const char *datadir, char *path)
                int r = 0;
                struct pack *p = NULL;
        
       -        if ((p = pack_load(path)) == NULL)
       +        if ((p = pack_load(datadir, path)) == NULL)
                        return ERR_PACK_LOAD;
        
                r += pack_install(rootfs, datadir, p);
       t@@ -623,7 +622,7 @@ update(const char *rootfs, const char *datadir, char *path)
                int r = 0;
                struct pack *p = NULL;
        
       -        if ((p = pack_load(path)) == NULL)
       +        if ((p = pack_load(datadir, path)) == NULL)
                        return ERR_PACK_LOAD;
        
                if (delete(rootfs, datadir, p->name) != 0)
       t@@ -640,7 +639,7 @@ update(const char *rootfs, const char *datadir, char *path)
         * Load a pack from a tarball and return a pack structure
         */
        struct pack *
       -pack_load(char *path)
       +pack_load_tarball(char *path)
        {
                int fd;
                struct pack *pack = NULL;
       t@@ -687,6 +686,50 @@ pack_load(char *path)
                return pack;
        }
        
       +/*
       + * Load a pack from a metadata directory and return a pack structure
       + */
       +struct pack *
       +pack_load_directory(const char *datadir, char *path)
       +{
       +        struct pack *pack = NULL;
       +
       +        if (!(pack = malloc(sizeof(struct pack)))) {
       +                perror(path);
       +                return NULL;
       +        }
       +
       +        pack->path = NULL;
       +        pack->name = NULL;
       +        pack->version = malloc(LINE_MAX);
       +
       +        pack->path = strdup(path);
       +        pack->name = strdup(base_name(path));
       +        inspect_version(datadir, pack->name, pack->version);
       +
       +        return pack;
       +}
       +
       +/*
       + * Guess how a pack should be loaded depending on the path given
       + */
       +struct pack *
       +pack_load(const char *datadir, char *path)
       +{
       +        struct pack *p = NULL;
       +        struct stat st;
       +
       +        if (stat(path, &st) < 0) {
       +                perror(path);
       +        } else if (S_ISDIR(st.st_mode)) {
       +                p = pack_load_directory(datadir, path);
       +        } else if (S_ISREG(st.st_mode)) {
       +                p = pack_load_tarball(path);
       +        }
       +
       +        return p;
       +}
       +
        
        /*
         * Free a pack structure