+
+
+static int is_backup_file(char *fn)
+{
+ int k = strlen(fn) - backup_suffix_len;
+ return k > 0 && strcmp(fn+k, backup_suffix) == 0;
+}
+
+
+/* This function is used to implement per-directory deletion, and
+ * is used by all the --delete-WHEN options. Note that the fbuf
+ * pointer must point to a MAXPATHLEN buffer with the name of the
+ * directory in it (the functions we call will append names onto
+ * the end, but the old dir value will be restored on exit). */
+void delete_in_dir(struct file_list *flist, char *fbuf,
+ struct file_struct *file)
+{
+ static int min_depth = MAXPATHLEN, cur_depth = -1;
+ static void *filt_array[MAXPATHLEN/2+1];
+ struct file_list *dir_list;
+ STRUCT_STAT st;
+ int dlen;
+
+ if (!flist) {
+ while (cur_depth >= min_depth)
+ pop_local_filters(filt_array[cur_depth--]);
+ min_depth = MAXPATHLEN;
+ cur_depth = -1;
+ return;
+ }
+ if (file->dir.depth >= MAXPATHLEN/2+1)
+ return; /* Impossible... */
+
+ if (max_delete && deletion_count >= max_delete)
+ return;
+
+ if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
+ rprintf(FINFO,
+ "IO error encountered -- skipping file deletion\n");
+ max_delete = -1; /* avoid duplicating the above warning */
+ return;
+ }
+
+ while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
+ pop_local_filters(filt_array[cur_depth--]);
+ cur_depth = file->dir.depth;
+ if (min_depth > cur_depth)
+ min_depth = cur_depth;
+ dlen = strlen(fbuf);
+ filt_array[cur_depth] = push_local_filters(fbuf, dlen);
+
+ if (link_stat(fbuf, &st, keep_dirlinks) < 0)
+ return;
+
+ if (one_file_system && file->flags & FLAG_TOP_DIR)
+ filesystem_dev = st.st_dev;
+
+ dir_list = flist_new(WITHOUT_HLINK, "delete_in_dir");
+
+ recurse = 0;
+ send_directory(-1, dir_list, fbuf, dlen);
+ recurse = -1;
+ fbuf[dlen] = '\0';
+
+ if (verbose > 3)
+ output_flist(dir_list, "delete");
+
+ delete_missing(flist, dir_list, fbuf);
+
+ flist_free(dir_list);
+}
+
+
+/* If an item in dir_list is not found in full_list, delete it from the
+ * filesystem. */
+void delete_missing(struct file_list *full_list, struct file_list *dir_list,
+ const char *dirname)
+{
+ int i, j, mode;
+
+ if (max_delete && deletion_count >= max_delete)
+ return;
+
+ if (verbose > 2)
+ rprintf(FINFO, "delete_missing(%s)\n", safe_fname(dirname));
+
+ for (i = dir_list->count; i--; ) {
+ if (!dir_list->files[i]->basename)
+ continue;
+ mode = dir_list->files[i]->mode;
+ if ((j = flist_find(full_list, dir_list->files[i])) < 0
+ || (delete_during && S_ISDIR(mode)
+ && !S_ISDIR(full_list->files[j]->mode))) {
+ char *f = f_name(dir_list->files[i]);
+ if (make_backups && (backup_dir || !is_backup_file(f))
+ && !S_ISDIR(mode)) {
+ make_backup(f);
+ if (verbose) {
+ rprintf(FINFO, "deleting %s\n",
+ safe_fname(f));
+ }
+ } else if (S_ISDIR(mode))
+ delete_file(f, DEL_DIR | DEL_FORCE_RECURSE);
+ else
+ delete_file(f, 0);
+ deletion_count++;
+ if (max_delete && deletion_count >= max_delete)
+ break;
+ }
+ }
+}