GNU bug report logs - #58721
28.2; dired with delete-by-moving-to-trash can't trash directory twice

Previous Next

Package: emacs;

Reported by: Gustavo Barros <gusbrs.2016 <at> gmail.com>

Date: Sat, 22 Oct 2022 18:24:01 UTC

Severity: normal

Found in version 28.2

Done: Eli Zaretskii <eliz <at> gnu.org>

Bug is archived. No further changes may be made.

Full log


Message #80 received at 58721 <at> debbugs.gnu.org (full text, mbox):

From: Mike Kupfer <mkupfer <at> alum.berkeley.edu>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 58721 <at> debbugs.gnu.org, gusbrs.2016 <at> gmail.com
Subject: Re: bug#58721: 28.2;
 dired with delete-by-moving-to-trash can't trash directory twice
Date: Sat, 29 Oct 2022 17:09:06 -0700
[Message part 1 (text/plain, inline)]
Eli Zaretskii wrote:

> Yet another possibility is to refrain from calling rename-file when
> the moved file is a directory, and instead to do what rename-file
> does, with a twist, "by hand".  That is what I actually prefer, as
> nothing is really wrong with rename-file.

I'm afraid I don't understand your suggestion.

I've attached the proof-of-concept patch that I came up with, which just
modifies move-file-to-trash.  I've tested it with files and directories,
both same-filesystem and crossing filesystems.  Did you have in mind
something similar?

cheers,
mike
[bug58721.diffs (text/x-diff, inline)]
diff --git a/lisp/files.el b/lisp/files.el
index 1e1ec6127d..63786ec103 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -8565,10 +8565,29 @@ move-file-to-trash
 		    (setq files-base (substring (file-name-nondirectory info-fn)
                                                 0 (- (length ".trashinfo"))))
 		    (write-region nil nil info-fn nil 'quiet info-fn)))
-		 ;; Finally, try to move the file to the trashcan.
+		 ;; Finally, try to move the item to the trashcan.  If
+                 ;; it's a file, just move it.  If it's a directory,
+                 ;; there's no way to invoke rename-file to replace
+                 ;; new-fn with fn, so move everything in fn and then
+                 ;; delete it.
 		 (let ((delete-by-moving-to-trash nil)
 		       (new-fn (file-name-concat trash-files-dir files-base)))
-		   (rename-file fn new-fn overwrite)))))))))
+                   (if (not (file-directory-p fn))
+                       (rename-file fn new-fn overwrite)
+                     (make-directory new-fn)
+                     (mapc  
+                      (lambda (f1)
+                        (let ((src (file-name-concat fn f1))
+	                      (targ (file-name-concat new-fn f1)))
+                          (cond
+                           ((or (string= f1 ".")
+	                        (string= f1 ".."))
+                            t)
+                           (t
+                            (rename-file src targ)))))
+                      (directory-files fn))
+                     (delete-directory fn))))))))))
+
 
 
 (defsubst file-attribute-type (attributes)

This bug report was last modified 2 years and 182 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.