Subject: bug#3911: get-free-disk-space when default-directory doesn't exist

From: Stefan Monnier <[email-masked]>
Date: Fri, 21 Aug 2009 23:57:20 -0400

How 'bout this patch instead (which could even work on Tramp
directories)?

        Stefan

--- files.el.~1.1069.~	2009-08-21 23:47:30.000000000 -0400
+++ files.el	2009-08-21 23:56:57.000000000 -0400
@@ -782,9 +782,12 @@
 ;;       nil)))
 
 (defun locate-dominating-file (file name)
-  "Look up the directory hierarchy from FILE for a file named NAME.
+  "Look up the directory hierarchy from FILE for a directory containing NAME.
 Stop at the first parent directory containing a file NAME,
-and return the directory.  Return nil if not found."
+and return the directory.  Return nil if not found.
+Instead of a string, NAME can also be a predicate taking one argument
+\(a directory) and returning a non-nil value if that directory is the one for
+which we're looking."
   ;; We used to use the above locate-dominating-files code, but the
   ;; directory-files call is very costly, so we're much better off doing
   ;; multiple calls using the code in here.
@@ -812,13 +815,15 @@
                     ;;   (setq user (nth 2 (file-attributes file)))
                     ;;   (and prev-user (not (equal user prev-user))))
                     (string-match locate-dominating-stop-dir-regexp file)))
-      (setq try (file-exists-p (expand-file-name name file)))
+      (setq try (if (stringp name)
+                    (file-exists-p (expand-file-name name file))
+                  (funcall name file)))
       (cond (try (setq root file))
             ((equal file (setq prev-file file
                                file (file-name-directory
                                      (directory-file-name file))))
              (setq file nil))))
-    root))
+    (file-name-as-directory root)))

 (defun executable-find (command)
@@ -5398,21 +5403,18 @@
 	(let ((fsinfo (file-system-info dir)))
 	  (if fsinfo
 	      (format "%.0f" (/ (nth 2 fsinfo) 1024))))
+      (setq dir (expand-file-name dir))
       (save-match-data
 	(with-temp-buffer
 	  (when (and directory-free-space-program
 		     ;; Avoid failure if the default directory does
 		     ;; not exist (Bug#2631, Bug#3911).
-		     (let ((default-directory default-directory))
-		       (setq dir (expand-file-name dir))
-		       (unless (and (not (file-remote-p default-directory))
-				    (file-directory-p default-directory)
-				    (file-readable-p default-directory))
-			 (setq default-directory "/"))
+		     (let ((default-directory
+                             (locate-dominating-file dir 'file-directory-p)))
 		       (eq (call-process directory-free-space-program
 					 nil t nil
 					 directory-free-space-args
-					 dir)
+					 (file-relative-name dir))
 			   0)))
 	    ;; Usual format is a header line followed by a line of
 	    ;; numbers.