]> pd.if.org Git - zpackage/blob - zpm-syncfs.c
ignore missing files to unlink
[zpackage] / zpm-syncfs.c
1 #define _POSIX_C_SOURCE 200809L
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include <errno.h>
10 #include <ctype.h>
11 #include <pwd.h>
12 #include <grp.h>
13 #include <math.h>
14 #include <stdarg.h>
15 #include <time.h>
16
17 /* needed for S_IFMT and AT_FDCWD */
18 #include <fcntl.h>
19
20 #include <string.h>
21
22 #include "sqlite3.h"
23 #include "zpm.h"
24
25 struct config {
26         struct zpm *log; /* logging db will be attached as "log" */
27         struct zpm *src;
28         char *dbfile;
29         char *rootdir;
30         int errabort, errors, verbose, dryrun, conflicts;
31         int setuser, setgroup;
32         int reverse, exitonerror;
33         int overwrite, accept, acceptdir, ignoredirmd;
34         int ops_total, ops_completed;
35         int ops_remove, ops_remove_completed;
36         int ops_update, ops_update_completed;
37         int ops_install, ops_install_completed;
38         int progress; /* type of progress meter */
39 };
40
41 struct nitem {
42         int op;
43         char *opstr;
44         uid_t uid;
45         gid_t gid;
46         char *dest;
47         char *path;
48         char *hash, *ohash;
49         char *mds, *omds;
50         char *target;
51         char *pkglist; /* space separated */
52         time_t mtime;
53         mode_t mode;
54         int ftype;
55         int configuration, oldwasconf;
56         struct timespec times[2];
57 };
58
59 static void usage() {
60         printf("usage: zpm $scriptname [-fncC] args ...\n");
61 }
62
63 static void pdots(int len, int ch, int was, int now, int total) {
64         was = len * was / total;
65         if (now > total) {
66                 now = total;
67         }
68         now = len * now / total;
69         while (was++ < now) {
70                 putchar(ch);
71         }
72         fflush(stdout);
73 }
74
75 static int seterror(struct config *conf, char *msgfmt, ...) {
76         char msg[1024];
77         va_list ap;
78
79         conf->errors++;
80
81         va_start(ap, msgfmt);
82         vsnprintf(msg, sizeof msg, msgfmt, ap);
83         va_end(ap);
84
85         msg[1023] = 0;
86         if (conf->log->errmsg) {
87                 free(conf->log->errmsg);
88         }
89
90         conf->log->errmsg = strdup(msg);
91
92         if (conf->verbose) {
93                 fprintf(stderr, "%s\n", msg);
94         }
95
96         return conf->errabort;
97 }
98
99 static int setsyserr(struct config *conf, char *msgfmt, ...) {
100         char msg[1024];
101         va_list ap;
102         int printed;
103
104         conf->errors++;
105
106         va_start(ap, msgfmt);
107         printed = vsnprintf(msg, sizeof msg, msgfmt, ap);
108         va_end(ap);
109
110         if (printed < 1) {
111                 /* nothing we can really do */
112                 return conf->errabort;
113         }
114
115         if ((size_t)printed < sizeof msg) {
116                 snprintf(msg+printed, sizeof msg - printed, ": %s",
117                                 strerror(errno));
118         }
119
120         msg[1023] = 0;
121         if (conf->log->errmsg) {
122                 free(conf->log->errmsg);
123         }
124
125         conf->log->errmsg = strdup(msg);
126
127         if (conf->verbose) {
128                 fprintf(stderr, "%s\n", msg);
129         }
130
131         return conf->errabort;
132 }
133
134 static int exists(char *path, mode_t *mode) {
135         struct stat st;
136
137         if (lstat(path, &st) == -1) {
138                 return 0;
139         }
140         if (mode) *mode = st.st_mode;
141         return 1;
142 }
143
144 /* TODO maintain a list of already created directories */
145 static int create_leading_dirs(char *path) {
146         char *delim, *s;
147         int ch = 0;
148         char pcopy[ZPM_PATH_MAX];
149         struct stat st;
150         
151         strcpy(pcopy, path);
152
153         delim = strrchr(pcopy, '/');
154         if (!delim) return 1; /* not an error, but no leading dirs */
155
156         /* cut off last component */
157         *delim = 0;
158
159         s = pcopy;
160         do {
161                 while (*s == '/') {
162                         s++;
163                 }
164
165                 delim = strchr(s, '/');
166                 if (delim) {
167                         ch = *delim;
168                         *delim = 0;
169                 }
170
171                 /* try to create the directory, if it exists
172                  * and is a directory or a symlink, that's ok
173                  * should be (eventually) a symlink to a directory
174                  * so we want stat here, not lstat
175                  */
176                 if (mkdir(pcopy, 0755) == -1) {
177                         switch (errno) {
178                                 case EEXIST:
179                                         if (stat(pcopy, &st) == -1) {
180                                                 /* can't stat? */
181                                                 return 0;
182                                         }
183                                         switch (st.st_mode & S_IFMT) {
184                                                 case S_IFDIR:
185                                                         break;
186                                                 default:
187                                                         return 0;
188                                         }
189                                         break;
190                                 default:
191                                         return 0;
192                         }
193                 }
194                 if (delim) {
195                         *delim = ch;
196                 }
197                 s = delim;
198         } while (delim);
199         
200         return 1;
201 }
202
203 static char *column(char *col, int ncols, char **vals, char **cols) {
204         int i = 0;
205         char *val = NULL;
206
207         for (i=0; i < ncols; i++) {
208 //              fprintf(stderr, "checking '%s' = '%s'\n", cols[i], vals[i]);
209                 
210                 if (!strcmp(col, cols[i])) {
211                         val = vals[i];
212                         break;
213                 }
214         }
215         return val;
216 }
217
218 #define COL(x) column(x, ncols, vals, cols)
219 #define SYSERR(x) do { conf->log->error = 2; return conf->errabort; } while (0)
220
221
222 static char *ops[] = { "new", "remove", "update", 0 };
223
224 enum op {
225         OP_NEW = 1,
226         OP_REMOVE = 2,
227         OP_UPDATE = 3
228 };
229
230 static int getop(char *opstr) {
231         int i;
232
233         if (!opstr) return 0;
234         for (i=0;ops[i];i++) {
235                 if (!strcmp(opstr, ops[i])) {
236                         return i+1;
237                 }
238         }
239         return 0;
240 }
241
242 static int report_conflicts(void *f, int ncols, char **vals, char **cols) {
243         struct config *conf = f;
244         char *path, *hash, *pkg, *conflict_type, *mds;
245
246         pkg = COL("pkgid");
247         path = COL("path");
248         conflict_type = COL("conflict");
249         if (!strcmp(conflict_type, "hash")) {
250                 hash = COL("hash");
251                 fprintf(stderr, "hash conflict: package %s path %s hash %.8s\n",
252                                 pkg, path, hash);
253         } else
254         if (!strcmp(conflict_type, "md")) {
255                 mds = COL("mds");
256                 fprintf(stderr, "md conflict: package %s path %s md %s\n",
257                                 pkg, path, mds);
258         } else {
259                 fprintf(stderr, "%s conflict: package %s path %s\n",
260                                 conflict_type, pkg, path);
261         }
262
263         conf->conflicts++;
264         return 0;
265 }
266
267 static int read_item(struct config *conf, int ncols, char **vals, char **cols,
268                 struct nitem *n) {
269         char *val;
270         long lval;
271         struct passwd *pw;
272         struct group *gr;
273         struct nitem zero = { 0 };
274
275         *n = zero;
276
277         val = COL("op");
278         if (!val) {
279                 seterror(conf, "can't determine op");
280                 return 0;
281         }
282         n->opstr = val;
283         n->op = getop(val);
284         if (!n->op) {
285                 seterror(conf, "can't determine op");
286                 return 0;
287         }
288
289         n->path = COL("path");
290         if (!n->path) {
291                 seterror(conf, "no file path");
292                 return 0;
293         }
294         if (strlen(n->path) == 0) {
295                 seterror(conf, "zero length path not allowed");
296                 return 0;
297         }
298
299         /* TODO config to dishonor setuid/setgid */
300         n->dest = COL("dest");
301         if (!n->dest) {
302                 seterror(conf, "no file dest");
303                 return 0;
304         }
305
306         if (strlen(n->dest) == 0) {
307                 seterror(conf, "zero length dest not allowed");
308                 return 0;
309         }
310
311         val = COL("mode");
312
313         if (!val) {
314                 seterror(conf, "can't determine mode");
315                 return 0;
316         }
317
318         n->mode = strtoul(val, NULL, 8);
319
320         val = COL("configuration");
321         if (!val) {
322                 seterror(conf, "can't determine config status");
323                 return 0;
324         }
325         lval = strtol(val, NULL, 10);
326
327         n->configuration = ((lval & 1) != 0);
328         n->oldwasconf = ((lval & 2) != 0);
329
330         val = COL("filetype");
331         if (!val || strlen(val) == 0) {
332                 seterror(conf, "can't determine file type");
333                 return 0;
334         }
335         n->ftype = *val;
336
337         /* these can be null */
338         n->ohash = COL("ohash");
339         n->mds = COL("mds");
340         n->omds = COL("omds");
341         n->pkglist = COL("pkglist");
342
343         if (n->ftype == 'r') {
344                 n->hash = COL("hash");
345                 if (!n->hash) {
346                         seterror(conf, "can't get hash");
347                         return 0;
348                 }
349         } else if (n->ftype == 'l') {
350                 n->target = COL("target");
351                 if (!n->target) {
352                         seterror(conf, "can't get target");
353                         return 0;
354                 }
355                 if (strlen(n->target) == 0) {
356                         seterror(conf, "zero length target not allowed");
357                         return 0;
358                 }
359                 n->hash = n->target;
360         }
361
362         if (conf->setuser) {
363                 val = COL("username");
364                 if (!val) {
365                         seterror(conf, "no username");
366                         return 0;
367                 }
368                 pw = getpwnam(val);
369                 if (!pw) {
370                         seterror(conf, "no passwd entry");
371                         return 0;
372                 }
373                 n->uid = pw->pw_uid;
374         } else {
375                 n->uid = geteuid();
376         }
377
378         if (conf->setgroup) {
379                 val = COL("groupname");
380                 if (!val) {
381                         seterror(conf, "no groupname");
382                         return 0;
383                 }
384                 gr = getgrnam(val);
385                 if (!gr) {
386                         seterror(conf, "no group entry for %s", val);
387                         return 0;
388                 }
389                 n->gid = gr->gr_gid;
390         } else {
391                 n->gid = getegid();
392         }
393
394         errno = 0;
395         double mtime = strtod(COL("mtime"),NULL);
396         if (errno) {
397                 mtime = (double)time(NULL);
398         }
399
400         n->mtime = (time_t)mtime;
401
402         n->times[0].tv_sec = 0;
403         n->times[0].tv_nsec = UTIME_OMIT;
404         n->times[1].tv_sec = (time_t)llrint(floor(mtime));
405         n->times[1].tv_nsec = lrint(floor(fmod(mtime,1.0)*1000000000));
406
407         return 1;
408 }
409
410 /* file does not exist */
411 #define D_NOEXIST 0x1
412 /* files are different types */
413 #define D_TYPE 0x2
414 /* metadata is different */
415 #define D_MD 0x4
416 /* content or link target is different */
417 #define D_HASH 0x8
418 /* file to be installed is a directory */
419 #define D_ISDIR  0x10
420 /* path on disk is a directory */
421 #define D_EISDIR 0x20
422 /* usernames different */
423 #define D_UID 0x40
424 /* group names different */
425 #define D_GID 0x80
426 /* file mode is different */
427 #define D_MODE 0x100
428 /* mtimes are different */
429 #define D_MTIME 0x200
430 /* the hash of the file we are supposedly replacing is different than
431  * the the hash of the file on disk
432  */
433 #define D_OHASH 0x400
434 /* an error occurred trying to compare the file (other than it doesn't exist */
435 #define D_ERROR 0x1000
436 /* there was a stat error */
437 #define D_STATERROR 0x2000
438 /* there was an error calling readlink */
439 #define D_RLERROR 0x4000
440
441 /* 1 = file doesn't exist, 2 = file is a directory, target isn't */
442 /* 4 == ftype different */
443 /* 8 = hash different when both are regular files */
444 static unsigned int file_compare(struct nitem *n, struct stat *st) {
445         int etype = 0, stat_type;
446         char ehash[ZPM_HASH_STRLEN+1];
447         unsigned int diff = 0;
448         char link[1024];
449         ssize_t lsize;
450
451         switch (n->ftype) {
452                 case 'd': etype = S_IFDIR; diff |= D_ISDIR ; break;
453                 case 'r': etype = S_IFREG; break;
454                 case 'l': etype = S_IFLNK; break;
455                 default: etype = 0; break;
456         }
457
458         errno = 0;
459         /* new file, so check type, hash, etc */
460         if (lstat(n->dest, st) == 0) {
461                 stat_type = st->st_mode & S_IFMT;
462                 if (stat_type != etype) {
463                         diff |= D_TYPE;
464                 }
465                 if (stat_type == S_IFDIR) {
466                         diff |= D_EISDIR;
467                 }
468
469                 if (n->hash && etype == S_IFREG && stat_type == S_IFREG) {
470                         zpm_hash(n->dest, ehash, 0);
471                         if (strcmp(n->hash, ehash) != 0) {
472                                 diff |= D_HASH;
473                         }
474                         if (n->ohash && strcmp(n->ohash, ehash) != 0) {
475                                 diff |= D_OHASH;
476                         }
477                 }
478                 if (n->hash && etype == S_IFLNK && stat_type == S_IFLNK) {
479                         lsize = readlink(n->dest, link, sizeof link);
480
481                         if (lsize == -1 || lsize == sizeof link) {
482                                 diff |= D_RLERROR;
483                                 diff |= D_ERROR;
484                         } else {
485                                 link[lsize] = 0;
486                                 if (strcmp(n->target, link) != 0) {
487                                         diff |= D_HASH;
488                                 }
489                         }
490                 }
491                 if (n->uid != st->st_uid) {
492                         diff |= D_UID;
493                         diff |= D_MD;
494                 }
495                 if (n->gid != st->st_gid) {
496                         diff |= D_GID;
497                         diff |= D_MD;
498                 }
499                 if (n->mode != (st->st_mode & 07777)) {
500                         diff |= D_MODE;
501                         diff |= D_MD;
502                 }
503         } else {
504                 switch(errno) {
505                         case ENOENT: diff |= D_NOEXIST; break;
506                         default: diff |= (D_STATERROR|D_ERROR); break;
507                 }
508         }
509
510         return diff;
511 }
512
513
514 /* 0 = not acceptable
515  * 1 = accept and create/update/remove
516  * 2 = accept as is
517  * 3 = remove and overwrite
518  * 4 = update metadata
519  */
520 static int acceptable(struct config *conf, unsigned int diffs, int op) {
521         int exist = (!(diffs & D_NOEXIST));
522         int sametype = (!(diffs & D_TYPE));
523         int mdsame = (!(diffs & D_MD));
524         int hashsame = (!(diffs & D_HASH));
525         int isdir = (diffs & D_ISDIR);
526
527         if (!exist) {
528                 return op == OP_REMOVE ? 2 : 1;
529         }
530
531         if (op == OP_UPDATE) {
532                 return sametype ? 4 : 3;
533         }
534
535         if (op == OP_REMOVE) {
536                 return 1;
537         }
538
539         /* the hard cases, should be installing new, but already exists */
540
541         if (!sametype) {
542                 return conf->overwrite ? 3 : 0;
543         }
544
545         if (mdsame && (conf->accept || conf->overwrite)) {
546                 return 1;
547         }
548
549         if (isdir) {
550                 if (mdsame || conf->ignoredirmd) {
551                         return conf->acceptdir ? 2 : 0;
552                 }
553                 if (conf->overwrite) {
554                         return 4;
555                 }
556         }
557
558         if (hashsame && (conf->accept || conf->overwrite)) {
559                 return 1;
560         }
561
562         return conf->overwrite ? 3 : 0;
563 }
564
565 static int check_existing(void *f, int ncols, char **vals, char **cols) {
566         struct config *conf = f;
567         struct stat st;
568         struct nitem nitem;
569
570         if (!read_item(conf, ncols, vals, cols, &nitem)) {
571                 fprintf(stderr, "can't read item\n");
572                 return conf->errabort;
573         }
574
575         if (conf->verbose > 1) {
576                 fprintf(stderr, "check for existing %s\n", nitem.path);
577         }
578
579         if (lstat(nitem.path, &st) == -1) {
580                 switch(errno) {
581                         /* not an error, file shouldn't exist*/
582                         case ENOENT: break;
583                         default:
584                                 fprintf(stderr, "unable to check %s: %s\n",
585                                                 nitem.path, strerror(errno));
586                                 conf->errors++;
587                                 break;
588                 }
589                 return 0;
590         }
591
592         unsigned int diffs = file_compare(&nitem, &st);
593         if (diffs >= D_ERROR) {
594                 return seterror(conf, "can't check %s", nitem.dest);
595         }
596
597         int action = acceptable(conf, diffs, nitem.op);
598         if (!action) {
599                 if (conf->accept) {
600                         fprintf(stderr, "%s exists and is not acceptable\n", nitem.path);
601                 } else {
602                         fprintf(stderr, "%s exists\n", nitem.path);
603                 }
604                 conf->errors++;
605         }
606
607         return 0;
608 }
609
610 static int remove_files(void *f, int ncols, char **vals, char **cols) {
611         struct config *conf = f;
612         char *dest;
613         struct stat st;
614         int flags = 0;
615
616         dest = COL("dest");
617         if (!dest) return seterror(conf,"no file dest");
618
619         if (conf->dryrun) {
620                 char *ftype = COL("filetype");
621                 int t = *ftype;
622
623                 switch(t) {
624                         case 'd': printf("rmdir %s\n", dest); break;
625                         default: printf("unlink %s\n", dest); break;
626                 }
627                 fflush(stdout);
628                 return 0;
629         }
630
631         errno = 0;
632
633         if (lstat(dest, &st) == -1) {
634                 switch (errno) {
635                         case ENOENT:
636                                 /* TODO chatter if verbose */
637                                 break;
638                         default:
639                                 return seterror(conf, "can't stat %s: %s", dest, strerror(errno));
640                 }
641                 return 0;
642         }
643
644         if (S_ISDIR(st.st_mode)) {
645                 flags = AT_REMOVEDIR;
646         }
647         /* TODO check that expected filetype matches actual filetype */
648
649         if (conf->verbose) {
650                 if (conf->progress == 2) {
651                         fprintf(stderr, "%s(%s)\n", flags ? "rmdir" : "unlink", dest);
652                 } else if (conf->progress == 1) {
653                         /* overwrite */
654                         pdots(50, '.', conf->ops_completed, conf->ops_completed + 1, conf->ops_total);
655                         conf->ops_completed++;
656                         conf->ops_completed++;
657                 } else {
658                         pdots(50, '.', conf->ops_completed, conf->ops_completed + 1, conf->ops_total);
659                         conf->ops_completed++;
660                 }
661         }
662
663         errno = 0;
664
665         if (unlinkat(AT_FDCWD, dest, flags) == -1) {
666                 switch (errno) {
667                         case ENOENT:
668                                 break;
669                         default:
670                                 return seterror(conf, "can't unlink %s: %s", dest, strerror(errno));
671                 }
672         }
673         
674         return 0;
675 }
676
677 #define MARK fprintf(stderr, "%s %d: mark\n", __func__, __LINE__)
678
679
680 static int remove_dir(struct config *conf, char *path) {
681         int rv;
682
683         rv = rmdir(path);
684         if (rv == -1) {
685                 setsyserr(conf, "can't rmdir %s", path);
686                 return 0;
687         }
688         return 1;
689 }
690
691 static int remove_existing(struct config *conf, char *path) {
692         int rv;
693
694         rv = unlink(path);
695         if (rv == -1) {
696                 setsyserr(conf, "can't unlink %s", path);
697                 return 0;
698         }
699         return 1;
700 }
701
702 static int set_md(struct config *conf, struct nitem *item) {
703         int rv;
704         int success = 0;
705
706         if (conf->dryrun) {
707                 if (item->ftype != 'l') {
708                         printf("chmod %o %s\n", item->mode, item->dest);
709                 }
710                 if (conf->setuser && conf->setgroup) {
711                         printf("lchown %d:%d %s\n", item->uid, item->gid,
712                                         item->dest);
713                 }
714                 printf("mtime %.0f %s\n", (double)item->mtime, item->dest);
715                 fflush(stdout);
716                 return success;
717         }
718
719         /* can't chmod a symlink */
720         if (item->ftype != 'l') {
721                 rv = chmod(item->dest, item->mode);
722
723                 if (rv == -1) {
724                         setsyserr(conf, "can't chmod %o %s", item->mode, item->dest);
725                         return conf->errabort;
726                 }
727         }
728
729         if (conf->setuser && conf->setgroup) {
730                 rv = lchown(item->dest, item->uid, item->gid);
731                 if (rv == -1) {
732                         setsyserr(conf, "can't lchown %s", item->dest);
733                         return conf->errabort;
734                 }
735         }
736
737         rv = utimensat(AT_FDCWD, item->dest, item->times, AT_SYMLINK_NOFOLLOW);
738         if (rv == -1) {
739                 setsyserr(conf, "can't set mtime %.0f %s", (double)item->mtime,
740                                 item->dest);
741                 return conf->errabort;
742         }
743         return 0;
744 }
745
746 /* install a file or create a directory or symlink.  path should not exist
747  * at this point.
748  */
749 /* flags: 1 = set md, 2 = create leading dirs, 4 = unlink existing file,
750  * 8 = rmdir existing dir, 16 = return true/false
751  */
752 #define INS_MD 0x1
753 #define INS_CLD 0x2
754 #define INS_UNLINK 0x4
755 #define INS_RMDIR 0x8
756 #define INS_RTF 0x10
757 #define INS_ZPMNEW 0x20
758 static int install(struct config *conf, struct nitem *item, unsigned int flags) {
759         int rv = 1;
760         struct zpm *source;
761
762         int mkleading = (flags & 2);
763         int setmd = (flags & 1);
764         int unlink_file = (flags & 4);
765         int rm_dir = (flags & 8);
766         int failure = conf->errabort;
767         int success = 0;
768
769         if (flags & 16) {
770                 failure = 0;
771                 success = 1;
772         }
773
774         if (conf->dryrun) {
775                 if (unlink_file) {
776                         printf("unlink %s\n", item->dest);
777                 } else if (rm_dir) {
778                         printf("rmdir %s\n", item->dest);
779                 }
780
781                 printf("install %c%o %d:%d %s", item->ftype,
782                                 item->mode, item->uid, item->gid,
783                                 item->dest);
784                 if (item->ftype == 'l') {
785                         printf(" -> %s", item->target);
786                 }
787                 printf("\n");
788                 fflush(stdout);
789                 return success;
790         }
791
792         source = conf->src ? conf->src : conf->log;
793
794         if (unlink_file) {
795                 rv = remove_existing(conf, item->dest);
796         } else if (rm_dir) {
797                 rv = remove_dir(conf, item->dest);
798         }
799
800         if (rv != 1) {
801                 return failure;
802         }
803
804         if (mkleading) {
805                 rv = create_leading_dirs(item->dest);
806                 if (!rv) {
807                         setsyserr(conf, "can't create leading dirs for %s", item->dest);
808                         return failure;
809                 }
810         }
811
812         if (item->ftype == 'r') {
813                 rv = zpm_extract(source, item->hash, item->dest, item->mode);
814                 if (rv == 0) {
815                         seterror(conf, "can't extract %s", item->dest);
816                         return failure;
817                 }
818                 return success;
819         }
820
821         switch (item->ftype) {
822                 case 'd': rv = mkdir(item->dest, item->mode);
823                           break;
824                 case 'l': rv = symlink(item->target, item->dest);
825                           break;
826                 default: /* error */
827                           break;
828         }
829
830         if (rv == -1) {
831                 setsyserr(conf, "installing %s failed", item->dest);
832                 return failure;
833         }
834
835         if (setmd) {
836                 return set_md(conf, item) == 0 ? success : failure;
837         }
838
839         return success;
840 }
841
842 /*
843  * figure out what the difference is for a config file, only called
844  * for an update of a configuration file
845  * return -1 on an error
846  * return 1 if the new file should not be installed
847  * return 0 if the new file should be installed
848  */
849 static int adjust_for_config(struct config *conf, struct nitem *n, unsigned int
850                 diffs) {
851
852         if (!n->oldwasconf) {
853                 return 0;
854         }
855         
856         /* TODO what if old was a directory? */
857         if (!n->configuration) {
858                 /* replacing conf with non-conf */
859                 /* absorb file, mark todo */
860                 char hash[ZPM_HASH_STRLEN+1];
861                 if (!conf->dryrun) {
862                         if (conf->verbose) {
863                                 fprintf(stderr, "importing old conf file\n");
864                         }
865                         if (zpm_import(conf->log, n->dest, 0, hash)) {
866                                 zpm_note_add(conf->log, n->pkglist, n->dest, hash,
867                                                 "replaced config file with non-config.  zpm-cat %.8s", hash);
868                         } else {
869                                 fprintf(stderr, "unable to import existing config file %s\n", n->dest);
870                                 return -1;
871                         }
872                 } else {
873                         fprintf(stderr, "dry-run: would replace config file %s with non-config file\n", n->dest);
874                 }
875                 return 0;
876         }
877
878         int sametype = (!(diffs & D_TYPE));
879         int isdir = (diffs & D_ISDIR);
880         int eisdir = (diffs & D_EISDIR);
881
882         /* both old and new are config files */
883         if (isdir && sametype) {
884                 /* both config directories, can only be changing
885                  * metadata, so no adjustment needed
886                  */
887                         if (conf->verbose) {
888                                 fprintf(stderr, "both config dirs, ok to update\n");
889                         }
890                 return 0;
891         }
892
893         if (isdir) {
894                 char hash[ZPM_HASH_STRLEN+1];
895
896                 /* replacing old file with new directory */
897                 /* absorb, make note */
898                 if (!conf->dryrun) {
899                         if (zpm_import(conf->log, n->dest, 0, hash)) {
900                                 zpm_note_add(conf->log, n->pkglist, n->dest, hash,
901                                                 "replaced config file with config directory.  zpm-cat %.8s", hash);
902                         } else {
903                                 fprintf(stderr, "unable to import existing config file %s\n", n->dest);
904                                 return -1;
905                         }
906                 } else {
907                         fprintf(stderr, "dry-run: would replace config file %s with config directory\n", n->dest);
908                 }
909                         return 0;
910         }
911
912         if (eisdir) {
913                 /* replacing old conf directory with a conf file.
914                  * nothing needs to be done, if the directory
915                  * is empty, it's ok to remove.  if it's not empty,
916                  * the install will fail
917                  */
918                 return 0;
919         }
920         
921         /* replacing old file with new file */
922         /* new is same as on disk */
923         if (!(diffs & D_HASH)) {
924                         if (conf->verbose) {
925                                 fprintf(stderr, "new config file is already on disk, probably shouldn't happen\n");
926                         }
927                 return 0;
928         }
929
930         /* new is different than on disk, but on disk is same as old */
931         if (!(diffs & D_OHASH)) {
932                         if (conf->verbose) {
933                                 fprintf(stderr, "old config file not changed from default, replacing with new default\n");
934                         }
935                 /* ok to do the update, since same as default */
936                 fprintf(stderr, "updating default config %s\n", n->dest);
937                 return 0;
938         }
939
940         /* new is different than on disk, and disk different than old */
941         /* log */
942         if (conf->verbose) {
943                 fprintf(stderr, "new default config file is different than on disk, and old default was changed, should keep on-disk config\n");
944         }
945         if (!conf->dryrun) {
946                 zpm_note_add(conf->log, n->pkglist, n->dest, n->hash,
947                                 "default config file update.  zpm-cat %.8s", n->hash);
948                 /* TODO check for note error */
949         } else {
950                 fprintf(stderr, "dry-run: default config file %s update\n",
951                                 n->dest);
952         }
953         return 1;
954
955 }
956
957 static int install_files(void *f, int ncols, char **vals, char **cols) {
958         struct config *conf = f;
959         struct nitem nitem;
960         struct stat existing;
961         int update = 0;
962
963         /* TODO put the result row in a hash table.  May not actually
964          * be faster
965          */
966         if (!read_item(conf, ncols, vals, cols, &nitem)) {
967                 fprintf(stderr, "can't read item\n");
968                 return conf->errabort;
969         }
970
971 #if 0
972         int64_t used, high;
973         used = sqlite3_memory_used()/1024/1024;
974         high = sqlite3_memory_highwater(0)/1024/1024;
975         fprintf(stderr, "memory = %ld MB / %ld MB\n", used, high);
976 #endif
977         if (conf->verbose && !conf->dryrun) {
978                 if (conf->progress == 2) {
979                         fprintf(stderr, "%s '%c' %s\n", nitem.opstr, nitem.ftype,
980                                         nitem.dest);
981                 } else if (conf->progress == 1) {
982                         /* overwrite */
983                         pdots(50, '.', conf->ops_completed, conf->ops_completed + 1, conf->ops_total);
984                         conf->ops_completed++;
985                         conf->ops_completed++;
986                 } else {
987                         pdots(50, '.', conf->ops_completed, conf->ops_completed + 1, conf->ops_total);
988                         conf->ops_completed++;
989                 }
990         }
991
992         unsigned int diffs = file_compare(&nitem, &existing);
993         if (diffs >= D_ERROR) {
994                 return seterror(conf, "can't check %s", nitem.dest);
995         }
996
997         /* updates:
998          * exist & same type & md same & hash same: do nothing, but warn bug
999          * exist & same type & md diff & hash same: fix md
1000          * exist & same type & md same & hash diff: replace
1001          * exist & same type & md diff & hash diff: replace & fix
1002          * no exist: install and warn
1003          * dir & not dir : remove, mkdir
1004          * not dir & not dir & diff type: remove, install
1005          * not dir & dir : remove dir if empty, error if not empty, install
1006          *
1007          * installs:
1008          * no exist: create leading dirs, install
1009          *
1010          * exist & same type & md same & hash same & accept or over: do nothing
1011          * exist & same & md diff or hash diff & overwrite : update
1012          * exist & same & md diff or hash diff & accept : error, can't accept
1013          * exist & same & md diff or hash diff & not accept : error
1014          *
1015          * exist & different type & not overwrite : error
1016          * not dir & not dir & overwrite : remove and install
1017          * not dir & dir & overwrite: remove empty or error, install
1018          * dir & dir & overwrite: fix md
1019          * dir & not dir & overwrite: remove and mkdir
1020          */
1021         int exist = (!(diffs & D_NOEXIST));
1022         int sametype = (!(diffs & D_TYPE));
1023         int mdsame = (!(diffs & D_MD));
1024         int hashsame = (!(diffs & D_HASH));
1025         int isdir = (diffs & D_ISDIR);
1026         int eisdir = (diffs & D_EISDIR);
1027         int accept = conf->accept;
1028         int overwrite = conf->overwrite;
1029         int installing = (nitem.op == OP_NEW);
1030         update = (nitem.op == OP_UPDATE);
1031
1032         if (update) {
1033                 if (!exist) {
1034                         /* warn, it should exist */
1035                         fprintf(stderr, "%s missing, installing", nitem.dest);
1036                         return install(conf, &nitem, 3);
1037                 }
1038
1039                 switch (adjust_for_config(conf, &nitem, diffs)) {
1040                         case -1: return conf->errabort; break;
1041                         case 1:
1042                         fprintf(stderr, "skipping changed default config file: %s\n", nitem.dest);
1043                         return 0; break;
1044                         default: break;
1045                 }
1046
1047                 /* file exists in filesystem */
1048                 if (sametype) {
1049                         if (mdsame && hashsame) {
1050                                 /* warn, bug in logic.  This shouldn't occur,
1051                                  * because if there is nothing to do, it
1052                                  * shouldn't be listed as an update
1053                                  */
1054                                 /* could be an update.  We're checking against
1055                                  * what's actually on disk, not what was
1056                                  * expected to have been on disk.  So, if
1057                                  * the admin has modified the file, or if
1058                                  * it had been installed ignoring the user
1059                                  * and group, it might be correct on disk
1060                                  * but not as in the local database
1061                                  */
1062                                 /* TODO detect whether this a logic bug or
1063                                  * an on-disk difference
1064                                  */
1065 #if 0
1066                                 fprintf(stderr, "%s should not be an update\n", nitem.dest);
1067                                 fprintf(stderr, "old hash: %s\n", nitem.ohash);
1068                                 fprintf(stderr, "new hash: %s\n", nitem.hash);
1069                                 fprintf(stderr, "old mds: %s\n", nitem.omds);
1070                                 fprintf(stderr, "new mds: %s\n", nitem.mds);
1071 #endif
1072                                 /* do nothing */
1073                                 return 0;
1074                         }
1075                         if (!mdsame && hashsame) {
1076                                 /* fix md */
1077                                 return set_md(conf, &nitem);
1078                         }
1079                         if (mdsame && !hashsame) {
1080                                 /* install */
1081                                 return install(conf, &nitem, 3);
1082                         }
1083                         if (!mdsame && !hashsame) {
1084                                 /* install */
1085                                 return install(conf, &nitem, 3);
1086                         }
1087                 }
1088
1089                 /* file exists, and is not the same type */
1090
1091                 if (isdir && !eisdir) {
1092                         /* remove existing */
1093                         /* mkdir */
1094                         return install(conf, &nitem, 7);
1095                 }
1096                 if (!isdir && eisdir) {
1097                         /* remove dir, or error */
1098                         /* install */
1099                         return install(conf, &nitem, 11);
1100                 }
1101                 if (!isdir && !isdir) {
1102                         /* necessarily !sametype, sametype handled above */
1103                         /* remove existing */
1104                         /* install */
1105                         return install(conf, &nitem, 7);
1106                 }
1107                 /* error, should not be possible, assert(0)? */
1108                 fprintf(stderr,"impossible state: %s:%d\n", __func__, __LINE__);
1109         }
1110
1111         if (installing) {
1112                 if (!exist) {
1113                         return install(conf, &nitem, 3);
1114                 }
1115
1116                 /* file exists in filesystem */
1117                 if (sametype) {
1118                         if (mdsame && hashsame && (accept || overwrite)) {
1119                                 /* do nothing */
1120                                 if (conf->dryrun || conf->verbose) {
1121                                         fprintf(stderr, "accept %s: %s\n",
1122                                                         eisdir ? "directory" : "file", nitem.dest);
1123                                 }
1124                                 return 0;
1125                         }
1126
1127                         if (mdsame && isdir && conf->acceptdir) {
1128                                 return 0;
1129                         }
1130
1131                         if (!mdsame && isdir && conf->ignoredirmd) {
1132                                 /* TODO warn ignoring dir md */
1133                                 return 0;
1134                         }
1135
1136                         if (mdsame && hashsame && !(accept || overwrite)) {
1137                                 /* error */
1138                                 return seterror(conf, "file exists: %s", nitem.dest);
1139                         }
1140
1141                         if (mdsame && !hashsame && overwrite) {
1142                                 /* install */
1143                                 return install(conf, &nitem, eisdir ? 11 : 7);
1144                         }
1145
1146                         if (nitem.configuration && accept) {
1147                                 /* accept a changed config file */
1148                                 if (conf->dryrun || conf->verbose) {
1149                                         fprintf(stderr, "accept %smodified config %s: %s\n", (!mdsame || !hashsame) ? "" : "un", 
1150                                                         eisdir ? "directory" : "file", nitem.dest);
1151                                 }
1152                                 return 0;
1153                         }
1154
1155                         if (mdsame && !hashsame && !overwrite) {
1156                                 /* accept doesn't matter, since it's
1157                                  * not an acceptable file */
1158                                 /* error */
1159                                 if (nitem.ftype == 'l') {
1160                                         char link[1024];
1161                                         ssize_t lsize;
1162                                         lsize = readlink(nitem.dest, link, sizeof link);
1163                                         if (lsize == -1 || (size_t)lsize >= sizeof link) {
1164                                                 return seterror(conf, "%s (linkdiff): expecting %s -> %s, unable to read link", accept ? "existing file not acceptable" : "file exists", nitem.dest, nitem.target, link);
1165                                         } else {
1166                                                 link[lsize] = 0;
1167                                                 /* links must be different */
1168                                                 return seterror(conf, "%s (linkdiff): expecting %s -> %s, have -> %s", accept ? "existing file not acceptable" : "file exists", nitem.dest, nitem.target, link);
1169                                         }
1170                                 } else {
1171                                         return seterror(conf, "%s (hashdiff): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1172                                 }
1173                         }
1174                         if (!mdsame && hashsame && overwrite) {
1175                                 /* fix md */
1176                                 return set_md(conf, &nitem);
1177                         }
1178                         if (!mdsame && hashsame && !overwrite) {
1179                                 /* accept doesn't matter, since it's
1180                                  * not an acceptable file */
1181                                 /* error */
1182                                 return seterror(conf, "%s (mddiff): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1183                         }
1184                         if (!mdsame && !hashsame && overwrite) {
1185                                 /* install */
1186                                 return install(conf, &nitem, eisdir ? 11 : 7);
1187                         }
1188                         if (!mdsame && !hashsame && !overwrite) {
1189                                 /* accept doesn't matter, since it's
1190                                  * not an acceptable file */
1191                                 /* error */
1192                                 return seterror(conf, "%s (md+hash): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1193                         }
1194                         /* TODO error, should be impossible */
1195                         return seterror(conf, "impossible state reached");
1196                 }
1197
1198                 /* file exists, and is not the same type */
1199                 if (!overwrite) {
1200                         /* error */
1201                                 return seterror(conf, "%s (difftype): %s", accept ? "existing file not acceptable" : "file exists", nitem.dest);
1202                 }
1203
1204                 /* not the same type, but ok to overwrite */
1205                 if (!eisdir) {
1206                         /* remove existing */
1207                         return install(conf, &nitem, 7);
1208                 }
1209
1210                 /* existing path is a directory */
1211                 if (isdir) {
1212                         /* fix md */
1213                         /* impossible, if isdir and eisdir, would
1214                          * be same type
1215                          * TODO
1216                          */
1217                         return set_md(conf, &nitem);
1218                 } else {
1219                         /* remove empty dir or error */
1220                         /* install */
1221                         return install(conf, &nitem, 11);
1222                 }
1223                 /* if we get here, we missed a case */
1224                 /* TODO error */
1225                 return seterror(conf, "impossible state 2 reached");
1226         }
1227
1228         /* TODO extra verbose print perms, mtime, etc, probably ls -l
1229          * format
1230          */ 
1231         if (conf->verbose) {
1232                 printf("%s\n", nitem.path);
1233         }
1234
1235         return 0;
1236 }
1237
1238 static void check_conflicts(struct config *conf, char *conflict_type,
1239                 int (callback)(void *, int, char **, char **)) {
1240         int rv;
1241         char *errmsg;
1242         sqlite3_str *s;
1243         char *sql;
1244
1245         s = sqlite3_str_new(conf->log->db);
1246         sqlite3_str_appendall(s, "select *, ");
1247         if (conf->rootdir) {
1248                 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
1249         } else {
1250                 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
1251         }
1252         sqlite3_str_appendall(s, " as dest from syncconflicts");
1253
1254         if (conflict_type) {
1255                 sqlite3_str_appendf(s," where conflict = %Q", conflict_type);
1256         }
1257         if (conf->reverse) {
1258                 sqlite3_str_appendall(s," order by length(path) desc, path desc,pkgid collate vercmp desc, conflict desc");
1259         } else {
1260                 sqlite3_str_appendall(s," order by length(path), path, pkgid collate vercmp, conflict");
1261
1262         }
1263
1264         sql = sqlite3_str_value(s);
1265
1266         rv = zpm_exec(conf->log, sql, callback, conf, &errmsg);
1267
1268         sqlite3_str_finish(s);
1269
1270         if (rv) {
1271                 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
1272                 if (errmsg) {
1273                         fprintf(stderr, "database error: %s\n", errmsg);
1274                         conf->errors++;
1275                 }
1276                 if (conf->log->error == 1) {
1277                         fprintf(stderr, "unable to allocate memory\n");
1278                 }
1279                 fprintf(stderr, "zpm_exec failure: %s\n",
1280                                 conf->log->errmsg ? conf->log->errmsg : "unknown");
1281                 conf->errors++;
1282         }
1283         if (conf->log->errmsg) {
1284                 fprintf(stderr, "error: %s\n", conf->log->errmsg);
1285         }
1286         if (conf->errors && conf->exitonerror) {
1287                 zpm_close(conf->log);
1288                 zpm_close(conf->src);
1289                 exit(EXIT_FAILURE);
1290         }
1291         /* TODO final report function in conf var */
1292 }
1293
1294 static void runstage(struct config *conf, char *stage,
1295                 int (callback)(void *, int, char **, char **)) {
1296         int rv;
1297         char *errmsg;
1298         sqlite3_str *s;
1299         char *sql;
1300
1301         s = sqlite3_str_new(conf->log->db);
1302         sqlite3_str_appendall(s, "select *, ");
1303         if (conf->rootdir) {
1304                 sqlite3_str_appendf(s, "printf('%%s/%%s',rtrim(%Q,'/'),ltrim(path,'/'))", conf->rootdir);
1305         } else {
1306                 sqlite3_str_appendf(s, "printf('/%%s', trim(path, '/'))");
1307         }
1308         sqlite3_str_appendall(s, " as dest from syncinfo");
1309
1310         if (stage) {
1311                 sqlite3_str_appendf(s," where op = %Q", stage);
1312         }
1313         if (conf->reverse) {
1314                 sqlite3_str_appendall(s," order by length(path) desc, path desc");
1315         }
1316
1317         sql = sqlite3_str_value(s);
1318
1319         rv = zpm_exec(conf->log, sql, callback, conf, &errmsg);
1320
1321         sqlite3_str_finish(s);
1322
1323         if (rv) {
1324                 fprintf(stderr, "exec fail: %s\n", sqlite3_errstr(rv));
1325                 if (errmsg) {
1326                         fprintf(stderr, "database error: %s\n", errmsg);
1327                         conf->errors++;
1328                 }
1329                 if (conf->log->error == 1) {
1330                         fprintf(stderr, "unable to allocate memory\n");
1331                 }
1332                 fprintf(stderr, "zpm_exec failure: %s\n",
1333                                 conf->log->errmsg ? conf->log->errmsg : "unknown");
1334                 conf->errors++;
1335         }
1336 #if 0
1337         if (conf->log->errmsg) {
1338                 fprintf(stderr, "error: %s\n", conf->log->errmsg);
1339         }
1340 #endif
1341         if (conf->errors && conf->exitonerror) {
1342                 zpm_close(conf->log);
1343                 zpm_close(conf->src);
1344                 exit(EXIT_FAILURE);
1345         }
1346         /* TODO final report function in conf var */
1347 }
1348
1349 static void count_ops(struct config *conf) {
1350         conf->ops_remove = zpm_db_int(conf->log, "select count(*) from syncinfo where op = 'remove'");
1351         conf->ops_update = zpm_db_int(conf->log, "select count(*) from syncinfo where op = 'update'");
1352         conf->ops_install = zpm_db_int(conf->log, "select count(*) from syncinfo where op = 'new'");
1353         conf->ops_total = conf->ops_remove + conf->ops_update + conf->ops_install;
1354 }
1355
1356 int main(int ac, char **av) {
1357         struct zpm localdb;
1358         struct zpm pkgdb;
1359         int opt;
1360         char *pkgdbfile = 0, *localdbfile = 0;
1361         char *s;
1362
1363         struct config conf = { 0 };
1364
1365         conf.errabort = 1;
1366         conf.errors = 0;
1367         conf.conflicts = 0;
1368         conf.verbose = 0;
1369         conf.dryrun = 0;
1370         conf.setuser = 1;
1371         conf.setgroup = 1;
1372         conf.log = 0;
1373         conf.src = 0;
1374         conf.rootdir = 0;
1375         conf.reverse = 0;
1376         conf.overwrite = 0;
1377         conf.accept = 0;
1378         conf.acceptdir = 1;
1379
1380         if (geteuid() != 0) {
1381                 conf.setuser = 0;
1382                 conf.setgroup = 0;
1383         }
1384
1385         localdbfile = ZPM_LOCAL_DB;
1386         if ((s = getenv("ZPMDB"))) {
1387                 /* TODO does this need to be copied ? */
1388                 localdbfile = s;
1389         }
1390
1391         if ((s = getenv("ZPM_ROOT_DIR"))) {
1392                 /* TODO does this need to be copied ? */
1393                 conf.rootdir = s;
1394         }
1395
1396         /*
1397          * -d localdb or ZPMDB * or /var/lib/zpm/zpm.db, or die
1398          * -f 'package database', otherwise regular default of env
1399          *  ZPM_PACKAGE_FILE, or use pkgdb if otherwise not found
1400          * -R root of pkg, will just chdir there
1401          *
1402          *  args are pkgid triple, but will do a pkg find on the pkgdb
1403          */
1404
1405         while ((opt = getopt(ac, av, "f:d:c:nCR:vOAMDp")) != -1) {
1406                 switch (opt) {
1407                         case 'd': localdbfile = optarg; break;
1408                         case 'f': pkgdbfile = optarg; break;
1409                         case 'n': conf.dryrun = 1; break;
1410                         case 'v': conf.verbose++; break;
1411                         case 'C': conf.errabort = 0; break;
1412                         case 'R': conf.rootdir = optarg; break;
1413                         case 'N': conf.setuser = 0; conf.setgroup = 0; break;
1414                         case 'O': conf.overwrite = 1; break;
1415                         case 'A': conf.accept = 1; break;
1416                         case 'M': conf.ignoredirmd = 1;
1417                         case 'D': conf.acceptdir = 0;
1418                         case 'p': conf.progress++;
1419                         default:
1420                                   usage();
1421                                   exit(EXIT_FAILURE);
1422                                   break;
1423                 }
1424         }
1425
1426         /* verify root dir exists */
1427         if (conf.rootdir && !exists(conf.rootdir, NULL)) {
1428                 fprintf(stderr, "rootdir %s does not exist\n", conf.rootdir);
1429         }
1430
1431         if (!zpm_open(&localdb, localdbfile)) {
1432                 fprintf(stderr, "can't open zpm db %s\n", localdbfile);
1433                 exit(EXIT_FAILURE);
1434         }
1435         conf.log = &localdb;
1436
1437         if (pkgdbfile) {
1438                 /* TODO open read-only */
1439                 if (!zpm_open(&pkgdb, pkgdbfile)) {
1440                         fprintf(stderr, "can't open src db %s\n", localdbfile);
1441                         exit(EXIT_FAILURE);
1442                 } else {
1443                         conf.src = &pkgdb;
1444                 }
1445         }
1446
1447         /* TODO find pkgid from arg */
1448
1449         /* TODO set conf var to finalize error reporting */
1450         if (conf.verbose) {
1451                 fprintf(stderr, "syncing filesystem %s (ldb %s) from %s\n",
1452                                 conf.rootdir ? conf.rootdir : "/",
1453                                 localdbfile, pkgdbfile);
1454         }
1455
1456         conf.errors = 0;
1457         conf.exitonerror = 0;
1458         check_conflicts(&conf, NULL, report_conflicts);
1459
1460         if (conf.conflicts) {
1461                 fprintf(stderr, "%d conflicts reported, aborting sync\n",
1462                                 conf.conflicts);
1463                 conf.errors++;
1464         } else {
1465                 /* no point in running it if we're just going to
1466                  * overwrite everything
1467                  */
1468                 if (!conf.overwrite && !conf.accept && !conf.dryrun) {
1469                         runstage(&conf, "new", check_existing);
1470                 }
1471
1472                 if (conf.verbose) {
1473                         fprintf(stderr, "beginning %ssync\n", conf.dryrun ?
1474                                         "dryrun " : "");
1475                 }
1476                 /* have to do the removes first otherwise
1477                  * old files may conflict with update file
1478                  * type changes
1479                  */
1480                 if (!conf.errors) {
1481                         conf.exitonerror = conf.dryrun ? 0 : 1;
1482                         conf.errabort = conf.dryrun ? 0 : 1;
1483                         count_ops(&conf);
1484                         fprintf(stderr, "file ops: %d\n", conf.ops_total);
1485                         if (conf.ops_remove > 0) {
1486                                 if (conf.verbose) {
1487                                         fprintf(stderr, "removing %d file%s\n", conf.ops_remove, conf.ops_remove > 0 ? "s" : "");
1488                                 }
1489                                 conf.reverse = 1;
1490                                 conf.ops_completed = 0;
1491                                 conf.ops_total = conf.ops_remove;
1492                                 runstage(&conf, "remove", remove_files);
1493                                 if (conf.verbose && conf.progress < 2) {
1494                                         fprintf(stderr, " done\n");
1495                                         fflush(stderr);
1496                                 }
1497                         }
1498
1499                         if (conf.ops_update > 0) {
1500                                 if (conf.verbose) {
1501                                         fprintf(stderr, "updating %d file%s\n", conf.ops_update, conf.ops_update > 0 ? "s" : "");
1502                                 }
1503                                 conf.reverse = 0;
1504                                 conf.ops_completed = 0;
1505                                 conf.ops_total = conf.ops_update;
1506                                 runstage(&conf, "update", install_files);
1507                                 if (conf.verbose && conf.progress < 2) {
1508                                         fprintf(stderr, " done\n");
1509                                         fflush(stderr);
1510                                 }
1511                         }
1512
1513                         if (conf.ops_install > 0) {
1514                                 if (conf.verbose) {
1515                                         fprintf(stderr, "installing %d file%s\n", conf.ops_install, conf.ops_install > 0 ? "s" : "");
1516                                 }
1517                                 conf.reverse = 0;
1518                                 conf.ops_completed = 0;
1519                                 conf.ops_total = conf.ops_install;
1520                                 runstage(&conf, "new", install_files);
1521                                 if (conf.verbose && conf.progress < 2) {
1522                                         fprintf(stderr, " done\n");
1523                                         fflush(stderr);
1524                                 }
1525                         }
1526                 }
1527         }
1528
1529         zpm_close(&localdb);
1530         zpm_close(conf.src);
1531         return conf.errors ? 1 : 0;
1532 }