hammer utility - Add force support to cleanup
[dragonfly.git] / sbin / hammer / cmd_cleanup.c
index e5d9e2d..8342684 100644 (file)
@@ -81,6 +81,7 @@ static void save_period(const char *snapshots_path, const char *cmd,
 static int check_softlinks(int fd, int new_config, const char *snapshots_path);
 static void cleanup_softlinks(int fd, int new_config,
                        const char *snapshots_path, int arg2, char *arg3);
+static void delete_snapshots(int fd, struct hammer_ioc_snapshot *dsnapshot);
 static int check_expired(const char *fpath, int arg2);
 
 static int create_snapshot(const char *path, const char *snapshots_path);
@@ -616,7 +617,11 @@ migrate_one_snapshot(int fd, const char *fpath,
                t = (time_t)-1;
                tid = (hammer_tid_t)(int64_t)-1;
 
-               ptr = fpath;
+               /* fpath may contain directory components */
+               if ((ptr = strrchr(fpath, '/')) != NULL)
+                       ++ptr;
+               else
+                       ptr = fpath;
                while (*ptr && *ptr != '-' && *ptr != '.')
                        ++ptr;
                if (*ptr)
@@ -648,6 +653,9 @@ migrate_one_snapshot(int fd, const char *fpath,
                        snprintf(snap->label, sizeof(snap->label),
                                 "migrated");
                        ++snapshot->count;
+               } else {
+                       printf("    non-canonical snapshot softlink: %s->%s\n",
+                              fpath, linkbuf);
                }
        }
 
@@ -716,6 +724,8 @@ dividing_slash(const char *path)
  * Periods in minutes, hours, or days are assumed to have been crossed
  * if the local time crosses a minute, hour, or day boundary regardless
  * of how close the last operation actually was.
+ *
+ * If ForceOpt is set always return true.
  */
 static int
 check_period(const char *snapshots_path, const char *cmd, int arg1,
@@ -731,6 +741,12 @@ check_period(const char *snapshots_path, const char *cmd, int arg1,
        time(savep);
        localtime_r(savep, &tp1);
 
+       /*
+        * Force run if -F
+        */
+       if (ForceOpt)
+               return(1);
+
        /*
         * Retrieve the start time of the last successful operation.
         */
@@ -898,6 +914,7 @@ cleanup_softlinks(int fd, int new_config,
                struct hammer_snapshot_data *snap;
                struct tm *tp;
                time_t t;
+               time_t dt;
                char snapts[32];
                u_int32_t i;
 
@@ -911,12 +928,13 @@ cleanup_softlinks(int fd, int new_config,
                        }
                        for (i = 0; i < snapshot.count; ++i) {
                                snap = &snapshot.snaps[i];
-                               t = time(NULL) - snap->ts / 1000000ULL;
-                               if ((int)t > arg2 && snap->tid != 0) {
+                               t = snap->ts / 1000000ULL;
+                               dt = time(NULL) - t;
+                               if ((int)dt > arg2 || snap->tid == 0) {
                                        dsnapshot.snaps[dsnapshot.count++] =
                                                *snap;
                                }
-                               if ((int)t > arg2 && VerboseOpt) {
+                               if ((int)dt > arg2 && VerboseOpt) {
                                        tp = localtime(&t);
                                        strftime(snapts, sizeof(snapts),
                                                 "%Y-%m-%d %H:%M:%S %Z", tp);
@@ -925,31 +943,38 @@ cleanup_softlinks(int fd, int new_config,
                                               snapts,
                                               snap->label);
                                }
-                               if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL) {
-                                       if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, &dsnapshot) < 0) {
-                                               printf("    Ioctl to delete snapshots failed: %s index %d\n", strerror(errno), dsnapshot.index);
-                                       } else if (dsnapshot.head.error) {
-                                               printf("    Ioctl to delete snapshots failed: %s\n", strerror(dsnapshot.head.error));
-                                               exit(1);
-                                       }
-                                       dsnapshot.index = 0;
-                                       dsnapshot.count = 0;
-                                       dsnapshot.head.error = 0;
-                               }
+                               if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL)
+                                       delete_snapshots(fd, &dsnapshot);
                        }
                } while (snapshot.head.error == 0 && snapshot.count);
 
-               if (dsnapshot.count) {
-                       if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, &dsnapshot) < 0) {
-                               printf("    Ioctl to delete snapshots failed: %s\n", strerror(errno));
-                       } else if (dsnapshot.head.error) {
-                               printf("    Ioctl to delete snapshots failed: %s\n", strerror(dsnapshot.head.error));
-                       }
-                       dsnapshot.count = 0;
-                       dsnapshot.index = 0;
-                       dsnapshot.head.error = 0;
+               if (dsnapshot.count)
+                       delete_snapshots(fd, &dsnapshot);
+       }
+}
+
+static void
+delete_snapshots(int fd, struct hammer_ioc_snapshot *dsnapshot)
+{
+       for (;;) {
+               if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, dsnapshot) < 0) {
+                       printf("    Ioctl to delete snapshots failed: %s\n",
+                              strerror(errno));
+                       break;
                }
+               if (dsnapshot->head.error) {
+                       printf("    Ioctl to delete snapshots failed at "
+                              "snap=%016jx: %s\n",
+                              dsnapshot->snaps[dsnapshot->index].tid,
+                              strerror(dsnapshot->head.error));
+                       if (++dsnapshot->index < dsnapshot->count)
+                               continue;
+               }
+               break;
        }
+       dsnapshot->index = 0;
+       dsnapshot->count = 0;
+       dsnapshot->head.error = 0;
 }
 
 /*