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) {
203 file->target = strdup(linkval);
207 /* strip and prefix */
208 file->path = cleanpath(path, opt->strip, opt->prefix);
209 if (file->path == 0) {
215 file->mode = opt->clmode;
217 file->mode = st.st_mode;
221 file->owner = strdup(opt->cluser);
223 file->owner = getuser(st.st_uid);
226 if (file->owner == 0) {
232 file->group = strdup(opt->clgroup);
234 file->group = getgroup(st.st_gid);
236 if (file->group == 0) {
241 file->mtime = st.st_mtime;
246 int add_file(struct zpm *zpm, struct zpm_file *file, struct opts *opt);
248 char *pathcat(char *dir, char *path) {
249 size_t dirlen = 0, pathlen = 0;
252 /* chop off trailing / on dir */
254 dirlen = strlen(dir);
255 while (dirlen && dir[dirlen-1] == '/') {
261 pathlen = strlen(path);
262 while (*path && *path == '/') {
268 cat = malloc(dirlen + pathlen + 2);
270 strncpy(cat, dir, dirlen);
272 strcpy(cat+dirlen+1, path);
277 int add_dir(struct zpm *zpm, char *path, struct opts *opt) {
280 struct zpm_file file = { 0 };
288 zpm_seterror(zpm, "can't open dir %s: %s", path, strerror(errno));
292 while ((de = readdir(dir))) {
293 if (!strcmp(de->d_name, ".")) {
296 if (!strcmp(de->d_name, "..")) {
300 dpath = pathcat(path, de->d_name);
302 zpm_seterror(zpm, "pathcat failed");
307 if (!stat_file(&file, dpath, opt)) {
308 zpm_seterror(zpm, "stat %s failed: %s",
309 dpath, strerror(errno));
314 if (!add_file(zpm, &file, opt)) {
326 int add_file(struct zpm *zpm, struct zpm_file *file, struct opts *opt) {
327 char hash[ZPM_HASH_STRLEN+1];
329 if (file->path[0] != 0) {
330 if (file->type == 'r') {
331 if (opt->addcontent) {
332 if (zpm_import(zpm, file->data, 0, hash)) {
333 strcpy(file->hash, hash);
338 zpm_hash(file->data, hash, 0);
339 strcpy(file->hash, hash);
343 zpm_db_run(zpm, "insert or replace into packagefiles "
344 "(package,version,release,path,mode,mtime,username,"
345 "groupname,filetype,hash,configuration,target)"
347 "(%Q, %Q, %d, %Q, %o, %d, %Q, %Q, '%c', %Q, %d, %Q)",
348 opt->package, opt->version, opt->release, file->path,
349 file->mode, (int)file->mtime, file->owner, file->group,
351 file->type == 'r' ? file->hash : NULL,
353 file->type == 'l' ? file->target : NULL
360 if (opt->verbose > 1) {
361 printf("%c%o %s:%s %s\n", file->type, file->mode, file->owner, file->group, file->path);
362 } else if (opt->verbose > 0) {
364 printf("%s\n", file->path);
369 if (opt->recursive && file->type == 'd') {
370 if (!add_dir(zpm, file->data, opt)) {
378 int main(int ac, char **av) {
380 struct opts opt = { 0 };
384 char hash[ZPM_HASH_STRLEN+1];
386 char *dbfile = getenv("ZPMDB");
389 dbfile = "/var/lib/zpm/local.db";
394 while ((option = getopt(ac, av, "CF:H:M:NP:S:T:cf:g:hlm:ru:vxz")) != -1) {
396 case 'C': opt.complete = 1; break;
397 case 'F': opt.cltype = optarg; break;
398 case 'H': opt.clhash = optarg; break;
399 case 'M': opt.clmtime = optarg; break;
400 case 'N': opt.addcontent = 0; break;
401 case 'P': opt.prefix = optarg; break;
402 case 'S': opt.strip = optarg; break;
403 case 'T': opt.cltarget = optarg; break;
404 case 'c': opt.isconfig = 1; break;
405 case 'f': dbfile = optarg; break;
406 case 'g': opt.clgroup = optarg; break;
407 case 'h': opt.followsymlinks = 1; break;
408 case 'l': opt.opt_l = 1; break;
409 case 'm': opt.clmode = strtol(optarg, NULL, 8); break;
410 case 'r': opt.recursive = 1; break;
411 case 'u': opt.cluser = optarg; break;
412 case 'v': opt.verbose++; break;
413 case 'x': opt.xargs = 1; dieval = 255; break;
414 case 'z': opt.noclear = 1; break;
425 if (!zpm_open(&zpm, dbfile)) {
426 die("can't open zpm db %s", dbfile);
430 i = sqlite3_config(SQLITE_CONFIG_MMAP_SIZE,98222080);
431 if (i != SQLITE_OK) {
436 /* package is first arg */
439 pkgstr = av[optind++];
440 pkgid = zpm_findpkg(&zpm, pkgstr, NULL);
441 /* TODO if not found, error */
443 die("package %s not found\n", pkgstr);
450 zpm_parse_package(pkgid, package, version, &release);
451 opt.package = package;
452 opt.version = version;
453 opt.release = release;
456 printf("adding to %s %s\n", dbfile, pkgid);
462 struct zpm_file file;
463 for (i=optind; av[i]; i++) {
464 if (!stat_file(&file, av[i], &opt)) {
465 die("can't stat %s: %s", av[i], strerror(errno));
468 if (add_file(&zpm, &file, &opt)) {
471 die("error: %s", zpm.errmsg);
476 zpm_db_run(&zpm, "update packages set build_time = %d where package = %Q and version = %Q and release = %d", time(NULL), package, version, release);
477 zpm_package_hash(&zpm, pkgid, hash);
478 zpm_package_sethash(&zpm, pkgid, hash);
479 } else if (!opt.noclear) {
480 zpm_db_run(&zpm, "update packages set build_time = null, hash = null where package = %Q and version = %Q and release = %d", package, version, release);
482 /* TODO error check */