aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlenn Morris2017-11-21 12:27:36 -0500
committerGlenn Morris2017-11-21 12:27:36 -0500
commit92f0c4cd56d4dc1a92c116172404e65996c5884d (patch)
treeeb506e142f9ebace0013b3d5e25f4f4dfaad958f
parent8d450453fae4518f79f7f951d8c70e11f887a934 (diff)
downloademacs-92f0c4cd56d4dc1a92c116172404e65996c5884d.tar.gz
emacs-92f0c4cd56d4dc1a92c116172404e65996c5884d.zip
Avoid bogus abbreviated file names if HOME changes
* lisp/files.el (abbreviate-file-name): If HOME changes, ignore `abbreviated-home-dir'. (Bug#19657#20) * test/lisp/files-tests.el (files-test-abbreviated-home-dir): New.
-rw-r--r--lisp/files.el58
-rw-r--r--test/lisp/files-tests.el14
2 files changed, 48 insertions, 24 deletions
diff --git a/lisp/files.el b/lisp/files.el
index 96d7ae7cf16..d8b38a9f169 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -1859,9 +1859,9 @@ home directory is a root directory) and removes automounter prefixes
1859 1859
1860When this function is first called, it caches the user's home 1860When this function is first called, it caches the user's home
1861directory as a regexp in `abbreviated-home-dir', and reuses it 1861directory as a regexp in `abbreviated-home-dir', and reuses it
1862afterwards. Lisp programs that temporarily set the home directory 1862afterwards (so long as the home directory does not change;
1863to a different value should let-bind `abbreviated-home-dir' for 1863if you want to permanently change your home directory after having
1864the modified home directory to take effect." 1864started Emacs, set `abbreviated-home-dir' to nil so it will be recalculated)."
1865 ;; Get rid of the prefixes added by the automounter. 1865 ;; Get rid of the prefixes added by the automounter.
1866 (save-match-data 1866 (save-match-data
1867 (if (and automount-dir-prefix 1867 (if (and automount-dir-prefix
@@ -1883,29 +1883,37 @@ the modified home directory to take effect."
1883 ;; give time for directory-abbrev-alist to be set properly. 1883 ;; give time for directory-abbrev-alist to be set properly.
1884 ;; We include a slash at the end, to avoid spurious matches 1884 ;; We include a slash at the end, to avoid spurious matches
1885 ;; such as `/usr/foobar' when the home dir is `/usr/foo'. 1885 ;; such as `/usr/foobar' when the home dir is `/usr/foo'.
1886 (or abbreviated-home-dir 1886 (unless abbreviated-home-dir
1887 (setq abbreviated-home-dir 1887 (put 'abbreviated-home-dir 'home (expand-file-name "~"))
1888 (let ((abbreviated-home-dir "$foo")) 1888 (setq abbreviated-home-dir
1889 (setq abbreviated-home-dir 1889 (let ((abbreviated-home-dir "$foo"))
1890 (concat "\\`" 1890 (setq abbreviated-home-dir
1891 (abbreviate-file-name (expand-file-name "~")) 1891 (concat "\\`"
1892 "\\(/\\|\\'\\)")) 1892 (abbreviate-file-name
1893 ;; Depending on whether default-directory does or 1893 (get 'abbreviated-home-dir 'home))
1894 ;; doesn't include non-ASCII characters, the value 1894 "\\(/\\|\\'\\)"))
1895 ;; of abbreviated-home-dir could be multibyte or 1895 ;; Depending on whether default-directory does or
1896 ;; unibyte. In the latter case, we need to decode 1896 ;; doesn't include non-ASCII characters, the value
1897 ;; it. Note that this function is called for the 1897 ;; of abbreviated-home-dir could be multibyte or
1898 ;; first time (from startup.el) when 1898 ;; unibyte. In the latter case, we need to decode
1899 ;; locale-coding-system is already set up. 1899 ;; it. Note that this function is called for the
1900 (if (multibyte-string-p abbreviated-home-dir) 1900 ;; first time (from startup.el) when
1901 abbreviated-home-dir 1901 ;; locale-coding-system is already set up.
1902 (decode-coding-string abbreviated-home-dir 1902 (if (multibyte-string-p abbreviated-home-dir)
1903 (if (eq system-type 'windows-nt) 1903 abbreviated-home-dir
1904 'utf-8 1904 (decode-coding-string abbreviated-home-dir
1905 locale-coding-system)))))) 1905 (if (eq system-type 'windows-nt)
1906 'utf-8
1907 locale-coding-system))))))
1906 1908
1907 ;; If FILENAME starts with the abbreviated homedir, 1909 ;; If FILENAME starts with the abbreviated homedir,
1910 ;; and ~ hasn't changed since abbreviated-home-dir was set,
1908 ;; make it start with `~' instead. 1911 ;; make it start with `~' instead.
1912 ;; If ~ has changed, we ignore abbreviated-home-dir rather than
1913 ;; invalidating it, on the assumption that a change in HOME
1914 ;; is likely temporary (eg for testing).
1915 ;; FIXME Is it even worth caching abbreviated-home-dir?
1916 ;; Ref: https://debbugs.gnu.org/19657#20
1909 (if (and (string-match abbreviated-home-dir filename) 1917 (if (and (string-match abbreviated-home-dir filename)
1910 ;; If the home dir is just /, don't change it. 1918 ;; If the home dir is just /, don't change it.
1911 (not (and (= (match-end 0) 1) 1919 (not (and (= (match-end 0) 1)
@@ -1914,7 +1922,9 @@ the modified home directory to take effect."
1914 ;; Novell Netware allows drive letters beyond `Z:'. 1922 ;; Novell Netware allows drive letters beyond `Z:'.
1915 (not (and (memq system-type '(ms-dos windows-nt cygwin)) 1923 (not (and (memq system-type '(ms-dos windows-nt cygwin))
1916 (save-match-data 1924 (save-match-data
1917 (string-match "^[a-zA-`]:/$" filename))))) 1925 (string-match "^[a-zA-`]:/$" filename))))
1926 (equal (get 'abbreviated-home-dir 'home)
1927 (expand-file-name "~")))
1918 (setq filename 1928 (setq filename
1919 (concat "~" 1929 (concat "~"
1920 (match-string 1 filename) 1930 (match-string 1 filename)
diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el
index 285a884b695..732b3c02379 100644
--- a/test/lisp/files-tests.el
+++ b/test/lisp/files-tests.el
@@ -411,5 +411,19 @@ name (Bug#28412)."
411 (should (file-directory-p (concat (file-name-as-directory dest2) "a"))) 411 (should (file-directory-p (concat (file-name-as-directory dest2) "a")))
412 (delete-directory dir 'recursive))) 412 (delete-directory dir 'recursive)))
413 413
414(ert-deftest files-test-abbreviated-home-dir ()
415 "Test that changing HOME does not confuse `abbreviate-file-name'.
416See <https://debbugs.gnu.org/19657#20>."
417 (let* ((homedir temporary-file-directory)
418 (process-environment (cons (format "HOME=%s" homedir)
419 process-environment))
420 (abbreviated-home-dir nil)
421 (testfile (expand-file-name "foo" homedir))
422 (old (file-truename (abbreviate-file-name testfile)))
423 (process-environment (cons (format "HOME=%s"
424 (expand-file-name "bar" homedir))
425 process-environment)))
426 (should (equal old (file-truename (abbreviate-file-name testfile))))))
427
414(provide 'files-tests) 428(provide 'files-tests)
415;;; files-tests.el ends here 429;;; files-tests.el ends here