-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudo-save.el
97 lines (68 loc) · 2.82 KB
/
sudo-save.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
;;; sudo-save.el ---
;; Copyright (C) 2003 Free Software Foundation, Inc.
;; Author: Kevin A. Burton ([email protected])
;; Maintainer: Kevin A. Burton ([email protected])
;; Location: http://www.peerfear.org
;; Keywords:
;; Version: 1.0
;; This file is [not yet] part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free Software
;; Foundation; either version 2 of the License, or any later version.
;;
;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
;; details.
;;
;; You should have received a copy of the GNU General Public License along with
;; this program; if not, write to the Free Software Foundation, Inc., 59 Temple
;; Place - Suite 330, Boston, MA 02111-1307, USA.
;;; Commentary:
;; Use `write-file-hooks' and `after-save-hook' to run "sudo chown ... " before
;; AND after file save. This allows the Emacs process to grant ownership to the
;; user and then restore ownership just after save.
;; TODO:
;;
;; - Actually what we SHOULD do is actually do a chown this way and NOT a chmod.
;;
;; - Can sudo cache passwords? I think it can.
;;
;; - Ability to chmod a+r a file JUST prior to reading it ... and then restoring
;; permissions RIGHT after it.
;;
;; - Ability to s
;;
;;; History:
;;; Code:
(defvar sudo-save-file-uid nil "")
(defun sudo-save--after-save-hook()
"If we've chown'd this file then we should restore it's ownership."
(when sudo-save-file-uid
;;restore original file access.
(sudo-save--chown (number-to-string sudo-save-file-uid) (buffer-file-name))
(setq sudo-save-file-uid nil)
(message "Wrote (with sudo) %s" (buffer-file-name))))
(defun sudo-save--chown(user file-name)
(message "sudo chown %s %s" user file-name)
(call-process "sudo" nil nil nil
"chown"
user
file-name))
(defun sudo-save--write-file-hook()
"Take ownership of this file and later restore it."
;;take a snapshow of the owner of the file.
;;call sudo to change the file's modes
(when (not (file-writable-p (buffer-file-name)))
;;preserve uid of file
(setq sudo-save-file-uid (nth 2 (file-attributes (buffer-file-name))))
(sudo-save--chown user-login-name (buffer-file-name)))
nil)
(defun sudo-save--find-file-hook()
"Disable read-only support since this is no obsolete for this file."
(setq buffer-read-only nil))
(add-hook 'write-file-hooks 'sudo-save--write-file-hook)
(add-hook 'after-save-hook 'sudo-save--after-save-hook)
(add-hook 'find-file-hooks 'sudo-save--find-file-hook)
(provide 'sudo-save)
;;; sudo-save.el ends here