From: Nathan Wagner Date: Wed, 12 Dec 2018 23:55:32 +0000 (+0000) Subject: fix recursive zpm-add X-Git-Tag: v0.5.0~46 X-Git-Url: https://pd.if.org/git/?p=zpackage;a=commitdiff_plain;h=c791f82faa7e8ce6e2d391cedc50b273b42599ef fix recursive zpm-add --- diff --git a/Makefile b/Makefile index 886c8ee..e26de51 100644 --- 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 $@ $< diff --git a/zpm-add.c b/zpm-add.c index d80ac08..8896e80 100644 --- 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;