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 */
80 t = new + strlen(prefix);
83 /* skip leading curdir */
84 if (*s && *s == '.' && s[1] && s[1] == '/') {
88 /* skip leading slashes */
98 /* skip multiple slashes */
99 if (*s == '/' && s[1] && s[1] == '/') {
102 /* skip trailing slash */
103 if (*s == '/' && !s[1]) {
108 if (*s == '/' && s[1] && s[1] == '.' && s[2] && s[2] == '/') {
113 if (*s == '/' && s[1] && s[1] == '.' && s[2] == 0) {
125 char *getuser(uid_t uid) {
130 return strdup(pw->pw_name);
135 char *getgroup(uid_t uid) {
140 return strdup(pw->pw_name);
145 void free_file(struct zpm_file *file) {
150 file->path = file->owner = file->group = file->target = 0;
153 int stat_file(struct zpm_file *file, char *path, struct opts *opt) {
160 if (opt->followsymlinks) {
161 rv = stat(path, &st);
163 rv = lstat(path, &st);
170 switch (st.st_mode & S_IFMT) {
171 case S_IFBLK: file->type = 'b'; break;
172 case S_IFCHR: file->type = 'c'; break;
173 case S_IFDIR: file->type = 'd'; break;
174 case S_IFIFO: file->type = 'p'; break;
175 case S_IFLNK: file->type = 'l'; break;
176 case S_IFREG: file->type = 'r'; break;
177 case S_IFSOCK: file->type = 's'; break;
178 default: file->type = 0; break;
181 file->configuration = opt->isconfig;
183 if (file->type == 0) {
187 if (file->type == 'l') {
189 file->target = strdup(opt->cltarget);
193 n = readlink(path, linkval, sizeof linkval);
194 if (n >= sizeof linkval) {
197 file->target = strdup(linkval);
201 /* strip and prefix */
202 file->path = cleanpath(path, opt->strip, opt->prefix);
203 if (file->path == 0) {
209 file->mode = opt->clmode;
211 file->mode = st.st_mode;
215 file->owner = strdup(opt->cluser);
217 file->owner = getuser(st.st_uid);
220 if (file->owner == 0) {
226 file->group = strdup(opt->clgroup);
228 file->group = getgroup(st.st_gid);
230 if (file->group == 0) {
235 file->mtime = st.st_mtime;
240 int add_file(struct zpm *zpm, struct zpm_file *file, struct opts *opt);
242 int add_dir(struct zpm *zpm, char *path, struct opts *opt) {
245 struct zpm_file file = { 0 };
250 zpm_seterror(zpm, "can't open dir %s: %s", path, strerror(errno));
254 while ((de = readdir(dir))) {
255 if (!strcmp(de->d_name, ".")) {
258 if (!strcmp(de->d_name, "..")) {
262 if (!stat_file(&file, de->d_name, opt)) {
263 zpm_seterror(zpm, "stat %s failed: %s",
264 de->d_name, strerror(errno));
268 if (!add_file(zpm, &file, opt)) {
277 int add_file(struct zpm *zpm, struct zpm_file *file, struct opts *opt) {
278 char hash[ZPM_HASH_STRLEN+1];
280 if (file->type == 'r') {
281 if (opt->addcontent) {
282 if (zpm_import(zpm, file->data, 0, hash)) {
283 strcpy(file->hash, hash);
288 zpm_hash(file->data, hash, 0);
289 strcpy(file->hash, hash);
293 zpm_db_run(zpm, "insert or replace into packagefiles "
294 "(package,version,release,path,mode,mtime,username,"
295 "groupname,filetype,hash,configuration,target)"
297 "(%Q, %Q, %d, %Q, %o, %d, %Q, %Q, '%c', %Q, %d, %Q)",
298 opt->package, opt->version, opt->release, file->path,
299 file->mode, (int)file->mtime, file->owner, file->group,
301 file->type == 'r' ? file->hash : NULL,
303 file->type == 'l' ? file->target : NULL
310 if (opt->verbose > 1) {
311 printf("%c%o %s:%s %s\n", file->type, file->mode, file->owner, file->group, file->path);
312 } else if (opt->verbose > 0) {
314 printf("%s\n", file->path);
318 if (opt->recursive && file->type == 'd') {
319 if (!add_dir(zpm, file->data, opt)) {
327 int main(int ac, char **av) {
329 struct opts opt = { 0 };
333 char hash[ZPM_HASH_STRLEN+1];
335 char *dbfile = getenv("ZPMDB");
338 dbfile = "/var/lib/zpm/local.db";
343 while ((option = getopt(ac, av, ":f:vr:l:P:S:cu:g:NCm:M:T:H:F:zxh")) != -1) {
345 case 'C': opt.complete = 1; break;
346 case 'F': opt.cltype = optarg; break;
347 case 'H': opt.clhash = optarg; break;
348 case 'M': opt.clmtime = optarg; break;
349 case 'N': opt.addcontent = 0; break;
350 case 'P': opt.prefix = optarg; break;
351 case 'S': opt.strip = optarg; break;
352 case 'T': opt.cltarget = optarg; break;
353 case 'c': opt.isconfig = 1; break;
354 case 'f': dbfile = optarg; break;
355 case 'g': opt.clgroup = optarg; break;
356 case 'h': opt.followsymlinks = 1; break;
357 case 'l': opt.opt_l = 1; break;
358 case 'm': opt.clmode = strtol(optarg, NULL, 8); break;
359 case 'r': opt.recursive = 1; break;
360 case 'u': opt.cluser = optarg; break;
361 case 'v': opt.verbose++; break;
362 case 'x': opt.xargs = 1; dieval = 255; break;
363 case 'z': opt.noclear = 1; break;
374 if (!zpm_open(&zpm, dbfile)) {
375 die("can't open zpm db %s", dbfile);
379 i = sqlite3_config(SQLITE_CONFIG_MMAP_SIZE,98222080);
380 if (i != SQLITE_OK) {
385 /* package is first arg */
388 pkgstr = av[optind++];
389 pkgid = zpm_findpkg(&zpm, pkgstr, NULL);
390 /* TODO if not found, error */
392 die("package %s not found\n", pkgstr);
399 zpm_parse_package(pkgid, package, version, &release);
400 opt.package = package;
401 opt.version = version;
402 opt.release = release;
405 printf("adding to %s %s\n", dbfile, pkgid);
411 struct zpm_file file;
412 for (i=optind; av[i]; i++) {
413 if (!stat_file(&file, av[i], &opt)) {
414 die("can't stat %s: %s", av[i], strerror(errno));
417 if (add_file(&zpm, &file, &opt)) {
420 die("error: %s", zpm.errmsg);
425 zpm_db_run(&zpm, "update packages set build_time = %d where package = %Q and version = %Q and release = %d", time(NULL), package, version, release);
426 zpm_package_hash(&zpm, pkgid, hash);
427 zpm_package_sethash(&zpm, pkgid, hash);
428 } else if (!opt.noclear) {
429 zpm_db_run(&zpm, "update packages set build_time = null, hash = null where package = %Q and version = %Q and release = %d", package, version, release);
431 /* TODO error check */