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;
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);
if (n >= sizeof linkval) {
return 0;
}
+ linkval[n] = 0;
file->target = strdup(linkval);
}
}
}
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) {
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);
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->hash, file->configuration, file->target
- );
-
- 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) {
+ 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;
+ }
- printf("%s\n", file->path);
+ 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);
+ }
}
if (opt->recursive && file->type == 'd') {
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;
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;
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 {