bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Error not detected in make-directory


From: worley
Subject: Error not detected in make-directory
Date: Sun, 26 Jan 2003 20:11:56 -0500 (EST)

Emacs version: 21.2

Observed problem:

If one invokes make-directory interactively and gives it the name of
an existing, ordinary file, it will return with no error, and causing
no change to the file system.  Similarly, calling '(make-directory
"/exisitng/file/name" t)' in E-Lisp signals no error and causes
nothing to happen.  In either case, the user or E-Lisp proceeds on the
assumption that the given name exists and is a directory, which can
cause errors.

Investigation:

make-directory (lines 2890 et seq. in files.el) does not explicitly
check to see if the name provided as an argument, or any of its
apparent parent directory names, already exists in the filesystem as
non-directory entries.  In most cases, this condition causes an error
in a lower-level file-handling primitive, but in the case that the
name is an ordinary file, no lower-level primitive is called, so the
condition is not detected.

Fix:

This can be corrected by inserting the test indicated below into the
definition of make-directory:

(defun make-directory (dir &optional parents)
  "Create the directory DIR and any nonexistent parent dirs.
Interactively, the default choice of directory to create
is the current default directory for file names.
That is useful when you have visited a file in a nonexistent directory.

Noninteractively, the second (optional) argument PARENTS says whether
to create parent directories if they don't exist."
  (interactive
   (list (read-file-name "Make directory: " default-directory default-directory
                         nil nil)
         t))
  (let ((handler (find-file-name-handler dir 'make-directory)))
    (if handler
        (funcall handler 'make-directory dir parents)
      (if (not parents)
          (make-directory-internal dir)
        (let ((dir (directory-file-name (expand-file-name dir)))
              create-list)
          (while (not (file-exists-p dir))
            (setq create-list (cons dir create-list)
                  dir (directory-file-name (file-name-directory dir))))
>         ;; 'dir' is the lowest parent directory name that already exists.
>         ;; Make sure that it really is a directory.
>         (if (not (car-safe (file-attributes dir)))
>             (error "%s is not a directory" dir))
          (while create-list
            (make-directory-internal (car create-list))
            (setq create-list (cdr create-list))))))))

Dale




reply via email to

[Prev in Thread] Current Thread [Next in Thread]