Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH]: restore permissions and mode on HISTFILE when renaming it



On Fri, Dec 16, 2005 at 09:42:29AM +0100, Arkadiusz Miskiewicz wrote:
> rename(".historyz.new", ".historyz") [loses] all permissions and
> user/owner of the original .historyz file.

I think that having rsync maintain the current permissions and group is
a desirable feature.  I also like Bart's idea that zsh should refuse to
re-write the history file (with a warning) if it is currently owned by
some other user.  The attached patch does this.  (If folks like the
idea for the upcoming release, I'll go ahead and commit it.)

As for the HISTFILE behavior when using sudo, I have code in my .zshrc
that makes sure that root writes out its history into its own history
file.  You can do this by conditionally redefining HISTFILE based on the
current user (among other possibilities).

..wayne..
--- configure.ac	9 Dec 2005 19:20:02 -0000	1.46
+++ configure.ac	16 Dec 2005 18:29:59 -0000
@@ -1098,7 +1098,7 @@ dnl AC_FUNC_STRFTIME
 AC_CHECK_FUNCS(strftime difftime gettimeofday \
 	       select poll \
 	       readlink faccessx fchdir ftruncate \
-	       fstat lstat lchown \
+	       fstat lstat lchown fchown fchmod \
 	       fseeko ftello \
 	       mkfifo _mktemp mkstemp \
 	       waitpid wait3 \
--- Src/hist.c	4 Nov 2005 16:20:34 -0000	1.61
+++ Src/hist.c	16 Dec 2005 18:29:59 -0000
@@ -2080,8 +2080,25 @@ savehistfile(char *fn, int err, int writ
 	tmpfile = bicat(unmeta(fn), ".new");
 	if (unlink(tmpfile) < 0 && errno != ENOENT)
 	    out = NULL;
-	else
-	    out = fdopen(open(tmpfile, O_CREAT | O_WRONLY | O_EXCL, 0600), "w");
+	else {
+	    struct stat sb;
+	    int old_exists = stat(unmeta(fn), &sb) == 0;
+
+	    if (old_exists && sb.st_uid != geteuid()) {
+		tmpfile = NULL; /* Avoid an error about HISTFILE.new */
+		out = NULL;
+	    } else
+		out = fdopen(open(tmpfile, O_CREAT | O_WRONLY | O_EXCL, 0600), "w");
+
+#ifdef HAVE_FCHMOD
+	    if (old_exists && out) {
+#ifdef HAVE_FCHOWN
+		fchown(fileno(out), sb.st_uid, sb.st_gid);
+#endif
+		fchmod(fileno(out), sb.st_mode);
+	    }
+#endif
+	}
     }
     if (out) {
 	for (; he && he->histnum <= xcurhist; he = down_histent(he)) {


Messages sorted by: Reverse Date, Date, Thread, Author