X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=zpm-add.c;h=86b289d9748d836aff391c7ef4f32f6f3d20bb4c;hb=62f6ff407bc4f2cf03d1fa7cf3dc9a3f4026624a;hp=d80ac08e6faf1309d9911ba958e59ae530b5a219;hpb=9a61c64a01cbe0d297509d222cef22a8dbc28214;p=zpackage diff --git a/zpm-add.c b/zpm-add.c index d80ac08..86b289d 100644 --- a/zpm-add.c +++ b/zpm-add.c @@ -21,6 +21,7 @@ struct opts { int complete, addcontent, isconfig, opt_l, recursive, verbose; int followsymlinks; int xargs, noclear; + int striparg; mode_t clmode; char *cltype, *clhash, *clmtime, *prefix, *strip, *cltarget; @@ -68,13 +69,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); @@ -194,6 +200,7 @@ int stat_file(struct zpm_file *file, char *path, struct opts *opt) { if (n >= sizeof linkval) { return 0; } + linkval[n] = 0; file->target = strdup(linkval); } } @@ -206,9 +213,9 @@ int stat_file(struct zpm_file *file, char *path, struct opts *opt) { } if (opt->clmode) { - file->mode = opt->clmode; + file->mode = opt->clmode & 07777; } else { - file->mode = st.st_mode; + file->mode = st.st_mode & 07777; } if (opt->cluser) { @@ -239,10 +246,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 +297,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 +392,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; @@ -349,6 +401,7 @@ int main(int ac, char **av) { case 'N': opt.addcontent = 0; break; case 'P': opt.prefix = optarg; break; case 'S': opt.strip = optarg; break; + case 's': opt.striparg = 1; break; case 'T': opt.cltarget = optarg; break; case 'c': opt.isconfig = 1; break; case 'f': dbfile = optarg; break; @@ -387,7 +440,6 @@ int main(int ac, char **av) { char *pkgid; pkgstr = av[optind++]; pkgid = zpm_findpkg(&zpm, pkgstr, NULL); - /* TODO if not found, error */ if (!pkgid) { die("package %s not found\n", pkgstr); } @@ -414,6 +466,10 @@ int main(int ac, char **av) { die("can't stat %s: %s", av[i], strerror(errno)); } file.data = av[i]; + if (opt.striparg) { + opt.strip = av[i]; + } + if (add_file(&zpm, &file, &opt)) { free_file(&file); } else {