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

From: Thierry Volpiatto <[email-masked]>
Date: Tue, 17 Jan 2012 07:06:47 +0100

Thierry Volpiatto <[email-masked]> writes:

> Andreas Schwab <[email-masked]> writes:
>
>> You also need to check whether the target is a subdirectory of the
>> source.
> Indeed yes, is there already a function to check this?
> If not what about something like this:

BTW this work also for equality between two directories.
(both GNU/Linux and Windows)

#+BEGIN_SRC lisp
(defun file-subdir-of-p (file1 file2)
  "Check if FILE1 is a subdirectory of FILE2."
  (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 file1)
              with f2 = (expand-file-name 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
              ;; Use here `file-equal-p' when ready to work.
              (string= (file-truename (directory-file-name root))
                       (file-truename(directory-file-name f2)))))))

#+END_SRC

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