zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH]: restore permissions and mode on HISTFILE when renaming it
@ 2005-12-16  8:42 Arkadiusz Miskiewicz
  2005-12-16 15:48 ` Bart Schaefer
  2005-12-16 18:39 ` Wayne Davison
  0 siblings, 2 replies; 4+ messages in thread
From: Arkadiusz Miskiewicz @ 2005-12-16  8:42 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 859 bytes --]

Hi,

There is one annoying thing in handling history file in zsh. When it uses 
tmpfile (not append mode) and just does rename(".historyz.new", ".history") 
it looses all permissions and user/owner of the original .historyz file.

Exaple, I'm working as user arekm and .historyz has arekm:users user/group but 
if I do sudo zsh; ls; exit then it's renamed and I end up with .historyz 
being root:root and since I've exited sudo and I'm back as arekm:users my 
history is no longer saved.

Attached patch makes zsh restore original permissions/owner/group of HISTFILE 
after sucessful rename. It's against HEAD (4.3) cvs version.

Any races here (between stat and chown/chmod) shouldn't be unsafe I guess.

Thanks!
-- 
Arkadiusz Miśkiewicz                    PLD/Linux Team
http://www.t17.ds.pwr.wroc.pl/~misiek/  http://ftp.pld-linux.org/

[-- Attachment #2: zsh-restore-histfile.patch --]
[-- Type: text/x-diff, Size: 667 bytes --]

--- zsh.org/Src/hist.c.org	2005-12-16 10:51:24.680963000 +0100
+++ zsh/Src/hist.c	2005-12-16 10:59:14.100963000 +0100
@@ -2127,8 +2127,18 @@
 	}
 	fclose(out);
 	if (tmpfile) {
+	    struct stat sb;
+	    int restore = 0;
+	    if (stat(unmeta(fn), &sb) == 0)
+		    restore = 1;
 	    if (rename(tmpfile, unmeta(fn)) < 0)
 		zerr("can't rename %s.new to $HISTFILE", fn, 0);
+	    else if (restore) {
+		    if (chown(unmeta(fn), sb.st_uid, sb.st_gid) < 0)
+			    zerr("can't restore user/group on $HISTFILE", NULL, 0);
+		    if (chmod(unmeta(fn), sb.st_mode) < 0)
+			    zerr("can't restore permissions on $HISTFILE", NULL, 0);
+	    }
 	    free(tmpfile);
 	}
 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-12-19 11:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-16  8:42 [PATCH]: restore permissions and mode on HISTFILE when renaming it Arkadiusz Miskiewicz
2005-12-16 15:48 ` Bart Schaefer
2005-12-16 18:39 ` Wayne Davison
2005-12-19 11:56   ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).