Subject: bug#10489: 24.0.92; dired-do-copy may create infinite directory hierarchy

From: Thierry Volpiatto <[email-masked]>
Date: Tue, 21 Feb 2012 21:58:16 +0100

Stefan Monnier <[email-masked]> writes:

>> so what is the state of this bug and what do you plan for this?
>
>> As a reminder, we needed:
>
>> 1) A function to compare filenames locally.
>> 2) A tramp handler for this function.
>> 3) A function to check if file1 is subdir of file2, locally also.
>> 4) A tramp handler for this one also?
>
>> We have more or less 1 and 3, need tramp handlers for them.
>
>> What else is needed?
>
> Just before we try and solve this problem the hard way:
> I just tried:
>
>    % ln -s erlang-otp erl
>    % cp -r erl/lib erlang-otp/lib/inviso/

We have to check if "erlang-otp/lib/inviso/" is a subdir of "erl/lib"
to resolve this, right?

The following function should work:

--8<---------------cut here---------------start------------->8---
(defun file-subdir-of-p (file1 file2)
  "Check if FILE1 is a subdirectory of FILE2 on current filesystem.
If directory FILE1 is the same than directory FILE2, return non--nil."
  (when (and (not (or (file-remote-p file1)
                      (file-remote-p file2)))
             (not (string= file1 "/"))
             (file-directory-p file1)
             (file-directory-p file2))
    (or (string= file2 "/")
        (loop with f1 = (expand-file-name (file-truename file1))
              with f2 = (expand-file-name (file-truename file2))
              with ls1 = (split-string f1 "/" t)
              with ls2 = (split-string f2 "/" t)
              for p = (string-match "^/" f1)
              for i in ls1
              for j in ls2
              when (string= i j)
              concat (if p (concat "/" i) (concat i "/"))
              into root
              finally return
              (equal (file-attributes (file-truename root))
                     (file-attributes f2))))))
--8<---------------cut here---------------end--------------->8---

Can you try?

(file-subdir-of-p "erlang-otp/lib/inviso/" "erl/lib")

> on my Debian system, and it told me "cp: impossible de créer un
> répertoire (« erl/lib ») dans lui-même (« erlang-otp/lib/inviso/lib »)",
> but note that it only told me so *after* performing the copy.
> I.e. it only detected the problem when trying to make
> erlang-otp/lib/inviso/lib/inviso/lib
>
> So it seems that the coreutils guys have found it sufficient to detect
> the inf-loop after the fact and interrupt the operation at that point
> rather than to try and predict that the cp will loop and don't perform
> it at all.
Do we have to strictly follow this?

> It might be easier to get a solution that catches all cases that way:
> remember the name and identity (inode/file-attributes/younameit) of
> the top directory we create,
"erl/lib"?

> and whenever we're about to copy a directory of the same name,
Not sure to fully understand this, do you mean
"and whenever we're about to copy the CONTENTS of a directory of the same
name?"

> check whether it happens to have the same identity, in which case
> we've hit an inf-loop.

-- 
  Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997