;; Compute target name.
(setq directory (directory-file-name (expand-file-name directory))
newname (directory-file-name (expand-file-name newname)))
- (if (not (file-directory-p newname)) (make-directory newname parents))
+
+ (if (not (file-directory-p newname))
+ ;; If NEWNAME is not an existing directory, create it; that
+ ;; is where we will copy the files of DIRECTORY.
+ (make-directory newname parents)
+ ;; If NEWNAME is an existing directory, we will copy into
+ ;; NEWNAME/[DIRECTORY-BASENAME].
+ (setq newname (expand-file-name
+ (file-name-nondirectory
+ (directory-file-name directory))
+ newname))
+ (and (file-exists-p newname)
+ (not (file-directory-p newname))
+ (error "Cannot overwrite non-directory %s with a directory"
+ newname))
+ (make-directory newname t))
;; Copy recursively.
- (mapc
- (lambda (file)
- (let ((target (expand-file-name
- (file-name-nondirectory file) newname))
- (attrs (file-attributes file)))
- (cond ((file-directory-p file)
- (copy-directory file target keep-time parents))
- ((stringp (car attrs)) ; Symbolic link
- (make-symbolic-link (car attrs) target t))
- (t
- (copy-file file target t keep-time)))))
- ;; We do not want to copy "." and "..".
- (directory-files directory 'full directory-files-no-dot-files-regexp))
+ (dolist (file
+ ;; We do not want to copy "." and "..".
+ (directory-files directory 'full
+ directory-files-no-dot-files-regexp))
+ (if (file-directory-p file)
+ (copy-directory file newname keep-time parents)
+ (let ((target (expand-file-name (file-name-nondirectory file) newname))
+ (attrs (file-attributes file)))
+ (if (stringp (car attrs)) ; Symbolic link
+ (make-symbolic-link (car attrs) target t)
+ (copy-file file target t keep-time)))))
;; Set directory attributes.
(set-file-modes newname (file-modes directory))