Subject: bug#3353: rename-file being called on directories for cross-device rename, mostly during trashing...

From: David De La Harpe Golden <[email-masked]>
Date: Sat, 23 May 2009 00:10:48 +0100

Sorry about timing, first time I had to investigate - not sure this is 
release-critical - haven't proved it can really cause data loss,  but 
it's at least surprising.  I don't really use trashing myself, but 
noticed something was acting funny a while back:

On a gnu+linux or other unix-like system:
To replicate, you'll need two distinct mounted filesystems - I'll assume 
/home and /tmp  as they're often separate filesystems

Set delete-by-moving-to-trash non-nil
Now both delete-file and delete-directory will call move-file-to-trash 
to do their deletion.

Point your trash-directory to a subdir on one filesystem - the default 
~/.Trash/  may well be fine.

Make a directory on another filesystem, say
mkdir /tmp/uhoh/

Now try to
M-x delete-directory /tmp/uhoh/

Get an error:
Non-regular file: is a directory, /tmp/uhoh

... But of course you know it's a directory, and you are now puzzled
as to why it didn't delete.

Do the same with delete-by-moving-to-trash turned off, and the directory
will be deleted as one would expect.

This is happening because move-file-to-trash is calling rename-file
with the directory name, and then rename-file is blindly trying to 
copy-file the directory if the C-level rename() fails with EXDEV (around 
line 2269 in fileio.c) as it tries to emulate a rename for the regular
file case.

Some possible paths to addressing this issue (non-exhaustive...):

1. have move-file-to-trash check if the file is a directory before
trying to rename-file it.  The issue here is rename-file actually works
fine on directories as-is so long as you don't try to rename
them across filesystems.

2. make rename-file not blindly copy-file directories but throw
an error about renaming directories cross-device not being supported
on the platform. That might be "good enough" as a placeholder for the 
23.1 release, would at least be less confusing than the "Non-regular 
file: is a directory" error.

3. make rename-file not blindly copy-file directories but work
more like shell "mv".  Right now it acts a bit like mv for
regular files (emulates move with the problematic copy and delete), but 
like C-level rename() for directories (bombs out...)
(3a. Or make a separate rename-directory that move-file-to-trash can 
call for directories...)

4. make copy-file do something for directories other than simply 
noticing it's a directory and giving up, like shell "cp -r".
(4a. Or make a separate copy-directory that rename-file (or 3a's 
rename-directory) could call underneath)