tCreate metadata files recursively - pm - barely a pack manager
 (HTM) git clone git://z3bra.org/pm
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 45faba361e445fe72b0266ae50c899d310efd65c
 (DIR) parent 247e62ef65fa6934b4952002822c4ec5085cb286
 (HTM) Author: z3bra <willyatmailoodotorg>
       Date:   Mon, 28 Dec 2015 15:15:32 +0100
       
       Create metadata files recursively
       
       `pm` will now create all parent directories for metadata, as well
       as a sub-directory named after the package. Installation of packages
       using relative paths is handled.
       
       Package names must be formatted as follow:
       
               > ${NAME}:${VERSION}.${EXTENSION}
       
       Diffstat:
         M pm.c                                |      46 +++++++++++++++++++++++--------
       
       1 file changed, 35 insertions(+), 11 deletions(-)
       ---
 (DIR) diff --git a/pm.c b/pm.c
       t@@ -1,4 +1,5 @@
        #include <fcntl.h>
       +#include <libgen.h>
        #include <limits.h>
        #include <stdio.h>
        #include <stdlib.h>
       t@@ -53,6 +54,29 @@ usage(char *name)
        }
        
        /*
       + * recursive mkdir, taken from the ii project
       + * http://nion.modprobe.de/blog/archives/357-Recursive-directory-creation.html
       + */
       +int
       +p_mkdir(const char *dir, mode_t mode) {
       +        char tmp[PATH_MAX];
       +        char *p = NULL;
       +        size_t len;
       +
       +        snprintf(tmp, sizeof(tmp),"%s",dir);
       +        len = strlen(tmp);
       +        if(tmp[len - 1] == '/')
       +                tmp[len - 1] = 0;
       +        for(p = tmp + 1; *p; p++)
       +                if(*p == '/') {
       +                        *p = 0;
       +                        mkdir(tmp, mode);
       +                        *p = '/';
       +                }
       +        return mkdir(tmp, mode);
       +}
       +
       +/*
         * write the content of an archive to the given file descriptor
         */
        int
       t@@ -88,8 +112,6 @@ inspect(int fd, const char *filename)
         * write metadata about a package file
         *
         * TODO:
       - *        + subdir for package name
       - *        + recursive mkdir
         *        + /deps /asdep /version
         */
        int
       t@@ -97,19 +119,22 @@ metadata(const char *datadir, const char *filename)
        {
                int fd, r;
                struct stat st;
       -        char tmp[PATH_MAX];
       +        char tmp[PATH_MAX], *name;
        
       -        strncpy(tmp, datadir, PATH_MAX);
       -        strncat(tmp, "/files", PATH_MAX);
       +        name = strdup(filename);
       +        name = strtok(name, ":");
       +        sprintf(tmp, "%s/%s", datadir, basename(name));
        
       -        if (stat(datadir, &st) < 0) {
       -                r = mkdir(datadir, 0750);
       +        free(name);
       +
       +        if (stat(tmp, &st) < 0) {
       +                r = p_mkdir(tmp, 0750);
                        if (r < 0)
                                return r;
                }
        
       +        strncat(tmp, "/files", PATH_MAX);
                fd = open(tmp, O_CREAT|O_WRONLY|O_TRUNC, 0644);
       -
                if (fd > 0) {
                        inspect(fd, filename);
                        close(fd);
       t@@ -233,7 +258,7 @@ int
        main (int argc, char **argv)
        {
                const char *packname;
       -        uint8_t action;
       +        uint8_t action = ACTION_INVALID;
        
                ARGBEGIN{
                case 'c':
       t@@ -251,10 +276,9 @@ main (int argc, char **argv)
                        packname = EARGF(usage(argv0));
                        break;
                case 'h':
       +        default:
                        usage(argv0);
                        return 0;
       -        default:
       -                action = ACTION_INVALID;
                }ARGEND;
        
                switch (action) {