From mboxrd@z Thu Jan  1 00:00:00 1970
Path: main.gmane.org!not-for-mail
From: Jeff Sheinberg <jeffsh@erols.com>
Newsgroups: gmane.emacs.bugs
Subject: Re: yes I'm root but want 444 files found readonly
Date: 01 Jul 2002 21:31:51 -0400
Organization: Jeff at home
Sender: bug-gnu-emacs-admin@gnu.org
Message-ID: <86znxaq5iw.fsf@eden-hda7.my.local>
References: <87ptyd99ml.fsf@jidanni.org>
NNTP-Posting-Host: localhost.gmane.org
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: main.gmane.org 1025573558 14610 127.0.0.1 (2 Jul 2002 01:32:38 GMT)
X-Complaints-To: usenet@main.gmane.org
NNTP-Posting-Date: Tue, 2 Jul 2002 01:32:38 +0000 (UTC)
Return-path: <bug-gnu-emacs-admin@gnu.org>
Original-Received: from fencepost.gnu.org ([199.232.76.164])
	by main.gmane.org with esmtp (Exim 3.33 #1 (Debian))
	id 17PCWz-0003nX-00
	for <gnu-bug-gnu-emacs@m.gmane.org>; Tue, 02 Jul 2002 03:32:38 +0200
Original-Received: from localhost ([127.0.0.1] helo=fencepost.gnu.org)
	by fencepost.gnu.org with esmtp (Exim 3.34 #1 (Debian))
	id 17PCXP-00078y-00; Mon, 01 Jul 2002 21:33:03 -0400
Original-Received: from mailbox1.ucsd.edu ([132.239.1.53])
	by fencepost.gnu.org with esmtp (Exim 3.34 #1 (Debian))
	id 17PCWJ-00076g-00
	for <bug-gnu-emacs@prep.ai.mit.edu>; Mon, 01 Jul 2002 21:31:55 -0400
Original-Received: from bob.news.rcn.net (bob.news.rcn.net [207.172.3.45])
	by mailbox1.ucsd.edu (8.12.1/8.12.1) with ESMTP id g621VsPY015794
	for <gnu-emacs-bug@moderators.isc.org>; Mon, 1 Jul 2002 18:31:54 -0700 (PDT)
Original-Received: from news by bob.news.rcn.net with local (Exim 2.01 #2)
	for gnu-emacs-bug@moderators.isc.org
	id 17PCWH-0007JX-00; Mon, 1 Jul 2002 21:31:53 -0400
Original-To: gnu-emacs-bug@moderators.isc.org
Original-Path: not-for-mail
Original-Newsgroups: gnu.emacs.bug
Original-Lines: 75
Original-X-Trace: UmFuZG9tSVZeHkVA+MEmY8pGSkABCzu0TRPPLOFkFgvCkctoLT1zblnrgvvf60KC
Original-X-Complaints-To: abuse@rcn.com
Original-NNTP-Posting-Date: 2 Jul 2002 01:31:53 GMT
User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2
Errors-To: bug-gnu-emacs-admin@gnu.org
X-BeenThere: bug-gnu-emacs@gnu.org
X-Mailman-Version: 2.0.11
Precedence: bulk
List-Help: <mailto:bug-gnu-emacs-request@gnu.org?subject=help>
List-Post: <mailto:bug-gnu-emacs@gnu.org>
List-Subscribe: <http://mail.gnu.org/mailman/listinfo/bug-gnu-emacs>,
	<mailto:bug-gnu-emacs-request@gnu.org?subject=subscribe>
List-Id: Bug reports for GNU Emacs,
	the Swiss army knife of text editors <bug-gnu-emacs.gnu.org>
List-Unsubscribe: <http://mail.gnu.org/mailman/listinfo/bug-gnu-emacs>,
	<mailto:bug-gnu-emacs-request@gnu.org?subject=unsubscribe>
List-Archive: <http://mail.gnu.org/pipermail/bug-gnu-emacs/>
Xref: main.gmane.org gmane.emacs.bugs:2384
X-Report-Spam: http://spam.gmane.org/gmane.emacs.bugs:2384

jidanni@dman.ddts.net (Dan Jacobson) writes:
 > and the files are found without the "%%" readonly in the mode line.
 > 
 > OK, I'm root with the royal blood.  But that doesn't mean I don't want
 > to enjoy the same protection against altering my files all over the
 > place just like the other guy.  Sure wish there was an option in emacs
 > that would respect the hints from the file modes even if I'm root.  Or
 > what, do you expect me to do C-x C-q on each file?  Unfair.
 > 
 > Yes, for my mode 0 file the option could have a third etc. choice, not
 > only find-readonly, but also ask the user if he really wants to see it
 > etc.

Greetings Zen Master "jidanni",

See below for a sample implementation that finds such files in
read-only mode.  IMO, there is no need for prompting, since %%
shows in the mode line, and C-x C-q will easily toggle read-only
off when and if desired.

 > Also there's the case of 444 but not owned by root.  Here also I would
 > find-readonly. 

My version of Emacs (GNU Emacs 21.2.1) already finds files with
mode 444 read-only, just like versions 18, 19, and 20 did.


;;----------------------------------------------------------------------
;; Put buffer in read-only mode if there are no write bits set in the
;; file's mode.  This can occur when uid=0 edits a non-writable file.

(defun my-set-read-only-if-no-writeable-mode-bits ()
  "This function is used to warn the super-user that the current buffer
has been set read-only because none of the file's write mode bits
are set."
  (interactive)
  (let* ((file (buffer-file-name))
	 (mode (file-modes file)))
    (if mode
	(progn
	  (if (and (not buffer-read-only)
		   (file-writable-p file)
		   (= (logand mode 146) 0)) ; 146 decimal is 222 octal.
	      (progn
		(setq buffer-read-only t)
		(ding)
		(message "File's mode is %o, buffer set read-only, use `toggle-read-only' wisely!" mode)))))))

(add-hook 'find-file-hooks 'my-set-read-only-if-no-writeable-mode-bits)

;; Put all buffers in `/usr/share/*' hierarchy in read-only mode.
(defun my-set-read-only-usr-share-files ()
  "This function is used to warn the user that the current buffer has
been set read-only because the file being visited is in the
`/usr/share/*' hierarchy."
  (interactive)
  (let* ((dir "/usr/share/")
	 (len (- (length dir) 1)))
    (if (and (not buffer-read-only)
	     (stringp buffer-file-truename)
	     (file-writable-p buffer-file-truename)
	     (fboundp 'compare-strings) ; keep emacs-19 happy
	     (eq t
	      (compare-strings dir 0 len buffer-file-truename 0 len)))
	(progn
	  (setq buffer-read-only t)
	  (ding)
	  (message "Buffer %s set read-only, use `toggle-read-only' wisely!" (buffer-name))))))

(add-hook 'find-file-hooks 'my-set-read-only-usr-share-files)
;;----------------------------------------------------------------------

HTH,
-- 
Jeff Sheinberg  <jeffsh@erols.com>