Merge pull request #852 from RadicalZephyr/owned-files

Don't open a file as root if it is owned by the current user
This commit is contained in:
Bozhidar Batsov 2015-05-17 01:04:29 +03:00
commit 8bd317e2b6

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)