1 #define _POSIX_C_SOURCE 200809L
16 #include "sqlite/sqlite3.h"
19 char *package, *version;
21 int complete, addcontent, isconfig, opt_l, recursive, verbose;
26 char *cltype, *clhash, *clmtime, *prefix, *strip, *cltarget;
27 char *clgroup, *cluser;
31 static void warn(char *fmt, ...) {
35 vfprintf(stderr, fmt, args);
37 fprintf(stderr, "\n");
41 static int dieval = EXIT_FAILURE;
43 static void die(char *fmt, ...) {
47 vfprintf(stderr, fmt, args);
49 fprintf(stderr, "\n");
53 char *cleanpath(char *path, char *strip, char *prefix) {
61 if (strncmp(path, strip, len)) {
62 /* strip is not a prefix */
79 /* path is only stripped */
85 t = new + strlen(prefix);
88 /* skip leading curdir */
89 if (*s && *s == '.' && s[1] && s[1] == '/') {
93 /* skip leading slashes */
103 /* skip multiple slashes */
104 if (*s == '/' && s[1] && s[1] == '/') {
107 /* skip trailing slash */
108 if (*s == '/' && !s[1]) {
113 if (*s == '/' && s[1] && s[1] == '.' && s[2] && s[2] == '/') {
118 if (*s == '/' && s[1] && s[1] == '.' && s[2] == 0) {
130 char *getuser(uid_t uid) {
135 return strdup(pw->pw_name);
140 char *getgroup(uid_t uid) {
145 return strdup(pw->pw_name);
150 void free_file(struct zpm_file *file) {
155 file->path = file->owner = file->group = file->target = 0;
158 int stat_file(struct zpm_file *file, char *path, struct opts *opt) {
165 if (opt->followsymlinks) {
166 rv = stat(path, &st);
168 rv = lstat(path, &st);
175 switch (st.st_mode & S_IFMT) {
176 case S_IFBLK: file->type = 'b'; break;
177 case S_IFCHR: file->type = 'c'; break;
178 case S_IFDIR: file->type = 'd'; break;
179 case S_IFIFO: file->type = 'p'; break;
180 case S_IFLNK: file->type = 'l'; break;
181 case S_IFREG: file->type = 'r'; break;
182 case S_IFSOCK: file->type = 's'; break;
183 default: file->type = 0; break;
186 file->configuration = opt->isconfig;
188 if (file->type == 0) {
192 if (file->type == 'l') {
194 file->target = strdup(opt->cltarget);
198 n = readlink(path, linkval, sizeof linkval);
199 if (n >= sizeof linkval) {
202 file->target = strdup(linkval);
206 /* strip and prefix */
207 file->path = cleanpath(path, opt->strip, opt->prefix);
208 if (file->path == 0) {
214 file->mode = opt->clmode;
216 file->mode = st.st_mode;
220 file->owner = strdup(opt->cluser);
222 file->owner = getuser(st.st_uid);
225 if (file->owner == 0) {
231 file->group = strdup(opt->clgroup);
233 file->group = getgroup(st.st_gid);
235 if (file->group == 0) {
240 file->mtime = st.st_mtime;
245 int add_file(struct zpm *zpm, struct zpm_file *file, struct opts *opt);
247 char *pathcat(char *dir, char *path) {
248 size_t dirlen = 0, pathlen = 0;
251 /* chop off trailing / on dir */
253 dirlen = strlen(dir);
254 while (dirlen && dir[dirlen-1] == '/') {
260 pathlen = strlen(path);
261 while (*path && *path == '/') {
267 cat = malloc(dirlen + pathlen + 2);
269 strncpy(cat, dir, dirlen);
271 strcpy(cat+dirlen+1, path);
276 int add_dir(struct zpm *zpm, char *path, struct opts *opt) {
279 struct zpm_file file = { 0 };
287 zpm_seterror(zpm, "can't open dir %s: %s", path, strerror(errno));
291 while ((de = readdir(dir))) {
292 if (!strcmp(de->d_name, ".")) {
295 if (!strcmp(de->d_name, "..")) {
299 dpath = pathcat(path, de->d_name);
301 zpm_seterror(zpm, "pathcat failed");
306 if (!stat_file(&file, dpath, opt)) {
307 zpm_seterror(zpm, "stat %s failed: %s",
308 dpath, strerror(errno));
313 if (!add_file(zpm, &file, opt)) {
325 int add_file(struct zpm *zpm, struct zpm_file *file, struct opts *opt) {
326 char hash[ZPM_HASH_STRLEN+1];
328 if (file->path[0] != 0) {
329 if (file->type == 'r') {
330 if (opt->addcontent) {
331 if (zpm_import(zpm, file->data, 0, hash)) {
332 strcpy(file->hash, hash);
337 zpm_hash(file->data, hash, 0);
338 strcpy(file->hash, hash);
342 zpm_db_run(zpm, "insert or replace into packagefiles "
343 "(package,version,release,path,mode,mtime,username,"
344 "groupname,filetype,hash,configuration,target)"
346 "(%Q, %Q, %d, %Q, %o, %d, %Q, %Q, '%c', %Q, %d, %Q)",
347 opt->package, opt->version, opt->release, file->path,
348 file->mode, (int)file->mtime, file->owner, file->group,
350 file->type == 'r' ? file->hash : NULL,
352 file->type == 'l' ? file->target : NULL
359 if (opt->verbose > 1) {
360 printf("%c%o %s:%s %s\n", file->type, file->mode, file->owner, file->group, file->path);
361 } else if (opt->verbose > 0) {
363 printf("%s\n", file->path);
368 if (opt->recursive && file->type == 'd') {
369 if (!add_dir(zpm, file->data, opt)) {
377 int main(int ac, char **av) {
379 struct opts opt = { 0 };
383 char hash[ZPM_HASH_STRLEN+1];
385 char *dbfile = getenv("ZPMDB");
388 dbfile = "/var/lib/zpm/local.db";
393 while ((option = getopt(ac, av, "CF:H:M:NP:S:T:cf:g:hlm:ru:vxz")) != -1) {
395 case 'C': opt.complete = 1; break;
396 case 'F': opt.cltype = optarg; break;
397 case 'H': opt.clhash = optarg; break;
398 case 'M': opt.clmtime = optarg; break;
399 case 'N': opt.addcontent = 0; break;
400 case 'P': opt.prefix = optarg; break;
401 case 'S': opt.strip = optarg; break;
402 case 'T': opt.cltarget = optarg; break;
403 case 'c': opt.isconfig = 1; break;
404 case 'f': dbfile = optarg; break;
405 case 'g': opt.clgroup = optarg; break;
406 case 'h': opt.followsymlinks = 1; break;
407 case 'l': opt.opt_l = 1; break;
408 case 'm': opt.clmode = strtol(optarg, NULL, 8); break;
409 case 'r': opt.recursive = 1; break;
410 case 'u': opt.cluser = optarg; break;
411 case 'v': opt.verbose++; break;
412 case 'x': opt.xargs = 1; dieval = 255; break;
413 case 'z': opt.noclear = 1; break;
424 if (!zpm_open(&zpm, dbfile)) {
425 die("can't open zpm db %s", dbfile);
429 i = sqlite3_config(SQLITE_CONFIG_MMAP_SIZE,98222080);
430 if (i != SQLITE_OK) {
435 /* package is first arg */
438 pkgstr = av[optind++];
439 pkgid = zpm_findpkg(&zpm, pkgstr, NULL);
440 /* TODO if not found, error */
442 die("package %s not found\n", pkgstr);
449 zpm_parse_package(pkgid, package, version, &release);
450 opt.package = package;
451 opt.version = version;
452 opt.release = release;
455 printf("adding to %s %s\n", dbfile, pkgid);
461 struct zpm_file file;
462 for (i=optind; av[i]; i++) {
463 if (!stat_file(&file, av[i], &opt)) {
464 die("can't stat %s: %s", av[i], strerror(errno));
467 if (add_file(&zpm, &file, &opt)) {
470 die("error: %s", zpm.errmsg);
475 zpm_db_run(&zpm, "update packages set build_time = %d where package = %Q and version = %Q and release = %d", time(NULL), package, version, release);
476 zpm_package_hash(&zpm, pkgid, hash);
477 zpm_package_sethash(&zpm, pkgid, hash);
478 } else if (!opt.noclear) {
479 zpm_db_run(&zpm, "update packages set build_time = null, hash = null where package = %Q and version = %Q and release = %d", package, version, release);
481 /* TODO error check */