Don't open a file as root if it is owned by the current user

Fixes #839.

The reasoning behind this is that if we own a file, we can change it's
permissions with `chmod` so there's probably a good reason it's not
writable.
This commit is contained in:
Geoff Shannon 2015-05-16 12:39:31 -05:00
parent 600c53ff7b
commit 67cf26cbbd

View file

@ -285,6 +285,17 @@ there's a region, all lines that region covers will be duplicated."
(interactive) (interactive)
(byte-recompile-directory prelude-dir 0)) (byte-recompile-directory prelude-dir 0))
(defun prelude-file-owner-uid (filename)
"Return the UID of the FILENAME as an integer.
See `file-attributes' for more info."
(nth 2 (file-attributes filename 'integer)))
(defun prelude-file-owned-by-user-p (filename)
"Return t if file FILENAME is owned by the currently logged in user."
(equal (prelude-file-owner-uid filename)
(user-uid)))
(defun prelude-find-alternate-file-as-root (filename) (defun prelude-find-alternate-file-as-root (filename)
"Wraps `find-alternate-file' with opening a file as root." "Wraps `find-alternate-file' with opening a file as root."
(find-alternate-file (concat "/sudo:root@localhost:" filename))) (find-alternate-file (concat "/sudo:root@localhost:" filename)))
@ -307,7 +318,8 @@ buffer is not visiting a file."
(unless (or (tramp-tramp-file-p buffer-file-name) (unless (or (tramp-tramp-file-p buffer-file-name)
(equal major-mode 'dired-mode) (equal major-mode 'dired-mode)
(not (file-exists-p (file-name-directory buffer-file-name))) (not (file-exists-p (file-name-directory buffer-file-name)))
(file-writable-p buffer-file-name)) (file-writable-p buffer-file-name)
(prelude-file-owned-by-user-p buffer-file-name))
(prelude-find-alternate-file-as-root buffer-file-name))) (prelude-find-alternate-file-as-root buffer-file-name)))
(add-hook 'find-file-hook 'prelude-reopen-as-root) (add-hook 'find-file-hook 'prelude-reopen-as-root)