+ DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
+ return ret;
+}
+
+/*
+ * We can not use rsync locally if the local user is different from the remote
+ * user or if the src dir is not on the local host (or both).
+ */
+static int use_rsync_locally(char *logname)
+{
+ char *h = conf.remote_host_arg;
+
+ if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
+ return 0;
+ if (conf.remote_user_given && strcmp(conf.remote_user_arg, logname))
+ return 0;
+ return 1;
+}
+
+static int rename_resume_snap(int64_t creation_time)
+{
+ struct snapshot_list sl;
+ struct snapshot *s = NULL;
+ char *new_name = incomplete_name(creation_time);
+ int ret;
+ const char *why;
+
+ sl.num_snapshots = 0;
+
+ ret = 0;
+ if (conf.no_resume_given)
+ goto out;
+ dss_get_snapshot_list(&sl);
+ /*
+ * Snapshot recycling: We first look at the newest snapshot. If this
+ * snapshot happens to be incomplete, the last rsync process was
+ * aborted and we reuse this one. Otherwise we look at snapshots which
+ * could be removed (outdated and redundant snapshots) as candidates
+ * for recycling. If no outdated/redundant snapshot exists, we check if
+ * there is an orphaned snapshot, which likely is useless anyway.
+ *
+ * Only if no existing snapshot is suitable for recycling, we bite the
+ * bullet and create a new one.
+ */
+ s = get_newest_snapshot(&sl);
+ if (!s) /* no snapshots at all */
+ goto out;
+ /* re-use last snapshot if it is incomplete */
+ why = "aborted";
+ if ((s->flags & SS_COMPLETE) == 0)
+ goto out;
+ why = "outdated";
+ s = find_outdated_snapshot(&sl);
+ if (s)
+ goto out;
+ why = "redundant";
+ s = find_redundant_snapshot(&sl);
+ if (s)
+ goto out;
+ why = "orphaned";
+ s = find_orphaned_snapshot(&sl);
+out:
+ if (s) {
+ DSS_INFO_LOG("reusing %s snapshot %s\n", why, s->name);
+ ret = dss_rename(s->name, new_name);
+ }
+ if (ret >= 0)
+ DSS_NOTICE_LOG("creating new snapshot %s\n", new_name);
+ free(new_name);
+ free_snapshot_list(&sl);
+ return ret;
+}
+
+static void create_rsync_argv(char ***argv, int64_t *num)
+{
+ char *logname;
+ int i = 0, j;
+ struct snapshot_list sl;
+
+ dss_get_snapshot_list(&sl);
+ assert(!name_of_reference_snapshot);
+ name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
+ free_snapshot_list(&sl);
+
+ *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
+ (*argv)[i++] = dss_strdup("rsync");
+ (*argv)[i++] = dss_strdup("-aq");
+ (*argv)[i++] = dss_strdup("--delete");
+ for (j = 0; j < conf.rsync_option_given; j++)
+ (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
+ if (name_of_reference_snapshot) {
+ DSS_INFO_LOG("using %s as reference\n", name_of_reference_snapshot);
+ (*argv)[i++] = make_message("--link-dest=../%s",
+ name_of_reference_snapshot);
+ } else
+ DSS_INFO_LOG("no suitable reference snapshot found\n");
+ logname = dss_logname();
+ if (use_rsync_locally(logname))
+ (*argv)[i++] = dss_strdup(conf.source_dir_arg);
+ else
+ (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
+ conf.remote_user_arg : logname,
+ conf.remote_host_arg, conf.source_dir_arg);
+ free(logname);
+ *num = get_current_time();
+ (*argv)[i++] = incomplete_name(*num);
+ (*argv)[i++] = NULL;
+ for (j = 0; j < i; j++)
+ DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
+}
+
+static void free_rsync_argv(char **argv)
+{
+ int i;
+
+ if (!argv)
+ return;
+ for (i = 0; argv[i]; i++)
+ free(argv[i]);
+ free(argv);
+}
+
+static int create_snapshot(char **argv)
+{
+ int ret;
+
+ ret = rename_resume_snap(current_snapshot_creation_time);