]> pd.if.org Git - zpackage/blobdiff - zpm-syncfs.c
ignore non-empty directories at unlink
[zpackage] / zpm-syncfs.c
index 4aa792d887a89b941efeb476bd59cbf0beca14bf..66e5aaa4b3eb685aa60d6744239efe940f03b879 100644 (file)
@@ -31,6 +31,11 @@ struct config {
        int setuser, setgroup;
        int reverse, exitonerror;
        int overwrite, accept, acceptdir, ignoredirmd;
+       int ops_total, ops_completed;
+       int ops_remove, ops_remove_completed;
+       int ops_update, ops_update_completed;
+       int ops_install, ops_install_completed;
+       int progress; /* type of progress meter */
 };
 
 struct nitem {
@@ -55,6 +60,18 @@ static void usage() {
        printf("usage: zpm $scriptname [-fncC] args ...\n");
 }
 
+static void pdots(int len, int ch, int was, int now, int total) {
+       was = len * was / total;
+       if (now > total) {
+               now = total;
+       }
+       now = len * now / total;
+       while (was++ < now) {
+               putchar(ch);
+       }
+       fflush(stdout);
+}
+
 static int seterror(struct config *conf, char *msgfmt, ...) {
        char msg[1024];
        va_list ap;
@@ -611,8 +628,21 @@ static int remove_files(void *f, int ncols, char **vals, char **cols) {
                return 0;
        }
 
+       errno = 0;
+
        if (lstat(dest, &st) == -1) {
-               return seterror(conf,"can't stat");
+               switch (errno) {
+                       case ENOENT:
+                               /* TODO chatter if verbose */
+                               break;
+                       case ENOTEMPTY: /* fall through */
+                       case EEXIST:
+                               /* TODO chatter, or possibly require */
+                               break;
+                       default:
+                               return seterror(conf, "can't stat %s: %s", dest, strerror(errno));
+               }
+               return 0;
        }
 
        if (S_ISDIR(st.st_mode)) {
@@ -621,7 +651,17 @@ static int remove_files(void *f, int ncols, char **vals, char **cols) {
        /* TODO check that expected filetype matches actual filetype */
 
        if (conf->verbose) {
-               fprintf(stderr, "%s(%s)\n", flags ? "rmdir" : "unlink", dest);
+               if (conf->progress == 2) {
+                       fprintf(stderr, "%s(%s)\n", flags ? "rmdir" : "unlink", dest);
+               } else if (conf->progress == 1) {
+                       /* overwrite */
+                       pdots(50, '.', conf->ops_completed, conf->ops_completed + 1, conf->ops_total);
+                       conf->ops_completed++;
+                       conf->ops_completed++;
+               } else {
+                       pdots(50, '.', conf->ops_completed, conf->ops_completed + 1, conf->ops_total);
+                       conf->ops_completed++;
+               }
        }
 
        errno = 0;
@@ -631,7 +671,7 @@ static int remove_files(void *f, int ncols, char **vals, char **cols) {
                        case ENOENT:
                                break;
                        default:
-                               return seterror(conf, "can't unlink");
+                               return seterror(conf, "can't unlink %s: %s", dest, strerror(errno));
                }
        }
        
@@ -939,8 +979,18 @@ static int install_files(void *f, int ncols, char **vals, char **cols) {
        fprintf(stderr, "memory = %ld MB / %ld MB\n", used, high);
 #endif
        if (conf->verbose && !conf->dryrun) {
-               fprintf(stderr, "%s '%c' %s\n", nitem.opstr, nitem.ftype,
-                               nitem.dest);
+               if (conf->progress == 2) {
+                       fprintf(stderr, "%s '%c' %s\n", nitem.opstr, nitem.ftype,
+                                       nitem.dest);
+               } else if (conf->progress == 1) {
+                       /* overwrite */
+                       pdots(50, '.', conf->ops_completed, conf->ops_completed + 1, conf->ops_total);
+                       conf->ops_completed++;
+                       conf->ops_completed++;
+               } else {
+                       pdots(50, '.', conf->ops_completed, conf->ops_completed + 1, conf->ops_total);
+                       conf->ops_completed++;
+               }
        }
 
        unsigned int diffs = file_compare(&nitem, &existing);
@@ -1300,14 +1350,21 @@ static void runstage(struct config *conf, char *stage,
        /* TODO final report function in conf var */
 }
 
-int main(int ac, char **av){
+static void count_ops(struct config *conf) {
+       conf->ops_remove = zpm_db_int(conf->log, "select count(*) from syncinfo where op = 'remove'");
+       conf->ops_update = zpm_db_int(conf->log, "select count(*) from syncinfo where op = 'update'");
+       conf->ops_install = zpm_db_int(conf->log, "select count(*) from syncinfo where op = 'new'");
+       conf->ops_total = conf->ops_remove + conf->ops_update + conf->ops_install;
+}
+
+int main(int ac, char **av) {
        struct zpm localdb;
        struct zpm pkgdb;
        int opt;
        char *pkgdbfile = 0, *localdbfile = 0;
        char *s;
 
-       struct config conf;
+       struct config conf = { 0 };
 
        conf.errabort = 1;
        conf.errors = 0;
@@ -1349,7 +1406,7 @@ int main(int ac, char **av){
         *  args are pkgid triple, but will do a pkg find on the pkgdb
         */
 
-       while ((opt = getopt(ac, av, "f:d:c:nCR:vOAMD")) != -1) {
+       while ((opt = getopt(ac, av, "f:d:c:nCR:vOAMDp")) != -1) {
                switch (opt) {
                        case 'd': localdbfile = optarg; break;
                        case 'f': pkgdbfile = optarg; break;
@@ -1362,6 +1419,7 @@ int main(int ac, char **av){
                        case 'A': conf.accept = 1; break;
                        case 'M': conf.ignoredirmd = 1;
                        case 'D': conf.acceptdir = 0;
+                       case 'p': conf.progress++;
                        default:
                                  usage();
                                  exit(EXIT_FAILURE);
@@ -1426,20 +1484,49 @@ int main(int ac, char **av){
                if (!conf.errors) {
                        conf.exitonerror = conf.dryrun ? 0 : 1;
                        conf.errabort = conf.dryrun ? 0 : 1;
-                       conf.reverse = 1;
-                       if (conf.verbose) {
-                               fprintf(stderr, "removing old files\n");
+                       count_ops(&conf);
+                       fprintf(stderr, "file ops: %d\n", conf.ops_total);
+                       if (conf.ops_remove > 0) {
+                               if (conf.verbose) {
+                                       fprintf(stderr, "removing %d file%s\n", conf.ops_remove, conf.ops_remove > 0 ? "s" : "");
+                               }
+                               conf.reverse = 1;
+                               conf.ops_completed = 0;
+                               conf.ops_total = conf.ops_remove;
+                               runstage(&conf, "remove", remove_files);
+                               if (conf.verbose && conf.progress < 2) {
+                                       fprintf(stderr, " done\n");
+                                       fflush(stderr);
+                               }
                        }
-                       runstage(&conf, "remove", remove_files);
-                       conf.reverse = 0;
-                       if (conf.verbose) {
-                               fprintf(stderr, "updating files\n");
+
+                       if (conf.ops_update > 0) {
+                               if (conf.verbose) {
+                                       fprintf(stderr, "updating %d file%s\n", conf.ops_update, conf.ops_update > 0 ? "s" : "");
+                               }
+                               conf.reverse = 0;
+                               conf.ops_completed = 0;
+                               conf.ops_total = conf.ops_update;
+                               runstage(&conf, "update", install_files);
+                               if (conf.verbose && conf.progress < 2) {
+                                       fprintf(stderr, " done\n");
+                                       fflush(stderr);
+                               }
                        }
-                       runstage(&conf, "update", install_files);
-                       if (conf.verbose) {
-                               fprintf(stderr, "installing files\n");
+
+                       if (conf.ops_install > 0) {
+                               if (conf.verbose) {
+                                       fprintf(stderr, "installing %d file%s\n", conf.ops_install, conf.ops_install > 0 ? "s" : "");
+                               }
+                               conf.reverse = 0;
+                               conf.ops_completed = 0;
+                               conf.ops_total = conf.ops_install;
+                               runstage(&conf, "new", install_files);
+                               if (conf.verbose && conf.progress < 2) {
+                                       fprintf(stderr, " done\n");
+                                       fflush(stderr);
+                               }
                        }
-                       runstage(&conf, "new", install_files);
                }
        }