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

From: Thierry Volpiatto <[email-masked]>
Date: Mon, 16 Jan 2012 20:14:47 +0100

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:

#+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)))
             (file-directory-p file1)
             (file-directory-p 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= (directory-file-name root)
                   (directory-file-name f2)))))

#+END_SRC

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