changeset 311:8a6650589829

Preserve permissions of the destination file when using secure save.
author zas_
date Fri, 11 Apr 2008 16:02:33 +0000
parents 4b25b3b30f35
children 07def8e708e1
files src/secure_save.c src/typedefs.h
diffstat 2 files changed, 28 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/secure_save.c	Fri Apr 11 15:27:08 2008 +0000
+++ b/src/secure_save.c	Fri Apr 11 16:02:33 2008 +0000
@@ -11,6 +11,7 @@
 
 #include <glib/gstdio.h>
 #include <errno.h>
+#include <utime.h>
 
 #include "main.h"
 #include "secure_save.h"
@@ -81,6 +82,7 @@
 	}
 
 	ssi->secure_save = TRUE;
+	ssi->preserve_perms = TRUE;
 
 	ssi->file_name = g_strdup(file_name);
 	if (!ssi->file_name) {
@@ -256,10 +258,34 @@
 	}
 
 	if (ssi->secure_save && ssi->file_name && ssi->tmp_file_name) {
+		struct stat st;
+
 		/* FIXME: Race condition on ssi->file_name. The file
 		 * named ssi->file_name may have changed since
 		 * secure_open() call (where we stat() file and
 		 * more..).  */
+#ifndef NO_UNIX_SOFTLINKS
+		if (g_lstat(ssi->file_name, &st) == 0)
+#else	
+		if (g_stat(ssi->file_name, &st) == 0)
+#endif
+			{
+			/* set the dest file attributes to that of source (ignoring errors) */
+			if (ssi->preserve_perms)
+				{
+				chown(ssi->tmp_file_name, st.st_uid, st.st_gid);
+				chmod(ssi->tmp_file_name, st.st_mode);
+				}
+
+			if (ssi->preserve_mtime)
+				{
+				struct utimbuf tb;
+
+				tb.actime = st.st_atime;
+				tb.modtime = st.st_mtime;
+				utime(ssi->tmp_file_name, &tb);
+				}
+			}
 		if (debug > 2) g_printf("rename %s -> %s", ssi->tmp_file_name, ssi->file_name);
 		if (g_rename(ssi->tmp_file_name, ssi->file_name) == -1) {
 			ret = errno;
--- a/src/typedefs.h	Fri Apr 11 15:27:08 2008 +0000
+++ b/src/typedefs.h	Fri Apr 11 16:02:33 2008 +0000
@@ -723,6 +723,8 @@
 	gchar *tmp_file_name; /**< temporary file name */
 	gint err; /**< set to non-zero value in case of error */
 	gint secure_save; /**< use secure save for this file */
+	gint preserve_perms; /**< whether to preserve perms */
+	gint preserve_mtime; /**< whether to preserve mtime */
 };