]> pd.if.org Git - zpackage/commitdiff
fix recursive zpm-add
authorNathan Wagner <nw@hydaspes.if.org>
Wed, 12 Dec 2018 23:55:32 +0000 (23:55 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Wed, 12 Dec 2018 23:55:32 +0000 (23:55 +0000)
Makefile
zpm-add.c

index 886c8eee5e7a1012bb1a072d5fcaee8428c8c867..e26de514cc4351f9445e336ea71c77533f764d4b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -158,8 +158,8 @@ zpm-syncfs: zpm-syncfs.o libzpm.a libelf.a
 zpm-parse: zpm-parse.o lib/parse.o
        $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $+
 
-zpm-add: zpm-add.o lib/parse.o libzpm.a
-       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $+ -lzpm -lelf
+zpm-add: zpm-add.o libzpm.a libelf.a
+       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< -lzpm -lelf
 
 zpm-quote: zpm-quote.o
        $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<
index d80ac08e6faf1309d9911ba958e59ae530b5a219..8896e800511856af65b30740ccdda540967e9628 100644 (file)
--- a/zpm-add.c
+++ b/zpm-add.c
@@ -68,13 +68,18 @@ char *cleanpath(char *path, char *strip, char *prefix) {
        len += strlen(s)+1;
 
        new = malloc(len);
-       memset(new, 0, len);
-
        if (!new) {
                return NULL;
        }
+       memset(new, 0, len);
+
        t = new;
 
+       if (*s == 0) {
+               /* path is only stripped */
+               return new;
+       }
+
        if (prefix) {
                strcpy(new, prefix);
                t = new + strlen(prefix);
@@ -239,10 +244,42 @@ int stat_file(struct zpm_file *file, char *path, struct opts *opt) {
 
 int add_file(struct zpm *zpm, struct zpm_file *file, struct opts *opt);
 
+char *pathcat(char *dir, char *path) {
+       size_t dirlen = 0, pathlen = 0;
+       char *cat;
+
+       /* chop off trailing / on dir */
+       if (dir) {
+               dirlen = strlen(dir);
+               while (dirlen && dir[dirlen-1] == '/') {
+                       dirlen--;
+               }
+       }
+
+       if (path) {
+               pathlen = strlen(path);
+               while (*path && *path == '/') {
+                       path++;
+                       pathlen--;
+               }
+       }
+
+       cat = malloc(dirlen + pathlen + 2);
+       if (cat) {
+               strncpy(cat, dir, dirlen);
+               cat[dirlen] = '/';
+               strcpy(cat+dirlen+1, path);
+       }
+       return cat;
+}
+
 int add_dir(struct zpm *zpm, char *path, struct opts *opt) {
        DIR *dir;
        struct dirent *de;
        struct zpm_file file = { 0 };
+       int fail = 0;
+
+       char *dpath = 0;
 
        dir = opendir(path);
 
@@ -258,61 +295,74 @@ int add_dir(struct zpm *zpm, char *path, struct opts *opt) {
                if (!strcmp(de->d_name, "..")) {
                        continue;
                }
+
+               dpath = pathcat(path, de->d_name);
+               if (!dpath) {
+                       zpm_seterror(zpm, "pathcat failed");
+                       fail = 1;
+                       break;
+               }
                
-               if (!stat_file(&file, de->d_name, opt)) {
+               if (!stat_file(&file, dpath, opt)) {
                        zpm_seterror(zpm, "stat %s failed: %s",
-                                       de->d_name, strerror(errno));
-                       return 0;
+                                       dpath, strerror(errno));
+                       fail = 1;
+                       break;
                }
 
                if (!add_file(zpm, &file, opt)) {
                        free_file(&file);
-                       return 0;
+                       fail = 1;
+                       break;
                }
                free_file(&file);
+               free(dpath);
        }
-       return 1;
+       closedir(dir);
+       return fail ? 0 : 1;
 }
 
 int add_file(struct zpm *zpm, struct zpm_file *file, struct opts *opt) {
        char hash[ZPM_HASH_STRLEN+1];
 
-       if (file->type == 'r') {
-               if (opt->addcontent) {
-                       if (zpm_import(zpm, file->data, 0, hash)) {
-                               strcpy(file->hash, hash);
+       if (file->path[0] != 0) {
+               if (file->type == 'r') {
+                       if (opt->addcontent) {
+                               if (zpm_import(zpm, file->data, 0, hash)) {
+                                       strcpy(file->hash, hash);
+                               } else {
+                                       return 0;
+                               }
                        } else {
-                               return 0;
+                               zpm_hash(file->data, hash, 0);
+                               strcpy(file->hash, hash);
                        }
-               } else {
-                       zpm_hash(file->data, hash, 0);
-                       strcpy(file->hash, hash);
                }
-       }
 
-       zpm_db_run(zpm, "insert or replace into packagefiles "
-                       "(package,version,release,path,mode,mtime,username,"
-                       "groupname,filetype,hash,configuration,target)"
-                      " values "
-                      "(%Q, %Q, %d, %Q, %o, %d, %Q, %Q, '%c', %Q, %d, %Q)",
-                       opt->package, opt->version, opt->release, file->path,
-                       file->mode, (int)file->mtime, file->owner, file->group,
-                       file->type,
-                       file->type == 'r' ? file->hash : NULL,
-                       file->configuration,
-                       file->type == 'l' ? file->target : NULL
-                 );
-
-       if (zpm->error) {
-               return 0;
-       }
+               zpm_db_run(zpm, "insert or replace into packagefiles "
+                               "(package,version,release,path,mode,mtime,username,"
+                               "groupname,filetype,hash,configuration,target)"
+                               " values "
+                               "(%Q, %Q, %d, %Q, %o, %d, %Q, %Q, '%c', %Q, %d, %Q)",
+                               opt->package, opt->version, opt->release, file->path,
+                               file->mode, (int)file->mtime, file->owner, file->group,
+                               file->type,
+                               file->type == 'r' ? file->hash : NULL,
+                               file->configuration,
+                               file->type == 'l' ? file->target : NULL
+                         );
+
+               if (zpm->error) {
+                       return 0;
+               }
 
-       if (opt->verbose > 1) {
-               printf("%c%o %s:%s %s\n", file->type, file->mode, file->owner, file->group, file->path);
-       } else if (opt->verbose > 0) {
+               if (opt->verbose > 1) {
+                       printf("%c%o %s:%s %s\n", file->type, file->mode, file->owner, file->group, file->path);
+               } else if (opt->verbose > 0) {
 
-               printf("%s\n", file->path);
-               fflush(stdout);
+                       printf("%s\n", file->path);
+                       fflush(stdout);
+               }
        }
 
        if (opt->recursive && file->type == 'd') {
@@ -340,7 +390,7 @@ int main(int ac, char **av) {
 
        opt.addcontent = 1;
 
-       while ((option = getopt(ac, av, ":f:vr:l:P:S:cu:g:NCm:M:T:H:F:zxh")) != -1) {
+       while ((option = getopt(ac, av, "CF:H:M:NP:S:T:cf:g:hlm:ru:vxz")) != -1) {
                switch (option) {
                        case 'C': opt.complete = 1; break;
                        case 'F': opt.cltype = optarg; break;