dovecot-2.0: dict-file: When replacing the dict file, preserve i...

dovecot at dovecot.org dovecot at dovecot.org
Fri May 1 03:23:04 EEST 2009


details:   http://hg.dovecot.org/dovecot-2.0/rev/cc280979ea8d
changeset: 9190:cc280979ea8d
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Apr 30 20:22:59 2009 -0400
description:
dict-file: When replacing the dict file, preserve its permissions.

diffstat:

1 file changed, 37 insertions(+)
src/lib-dict/dict-file.c |   37 +++++++++++++++++++++++++++++++++++++

diffs (54 lines):

diff -r c0c4e6e75366 -r cc280979ea8d src/lib-dict/dict-file.c
--- a/src/lib-dict/dict-file.c	Thu Apr 30 20:00:17 2009 -0400
+++ b/src/lib-dict/dict-file.c	Thu Apr 30 20:22:59 2009 -0400
@@ -280,6 +280,38 @@ static void file_dict_apply_changes(stru
 	}
 }
 
+static int fd_copy_permissions(int src_fd, const char *src_path,
+			       int dest_fd, const char *dest_path)
+{
+	struct stat src_st, dest_st;
+
+	if (fstat(src_fd, &src_st) < 0) {
+		i_error("fstat(%s) failed: %m", src_path);
+		return -1;
+	}
+	if (fstat(dest_fd, &dest_st) < 0) {
+		i_error("fstat(%s) failed: %m", dest_path);
+		return -1;
+	}
+
+	if (src_st.st_gid != dest_st.st_gid) {
+		if (fchown(dest_fd, (uid_t)-1, src_st.st_gid) < 0) {
+			i_error("fchown(%s, -1, %s) failed: %m",
+				dest_path, dec2str(src_st.st_gid));
+			return -1;
+		}
+	}
+
+	if ((src_st.st_mode & 07777) != (dest_st.st_mode & 07777)) {
+		if (fchmod(dest_fd, src_st.st_mode & 07777) < 0) {
+			i_error("fchmod(%s, %o) failed: %m",
+				dest_path, (int)(src_st.st_mode & 0777));
+			return -1;
+		}
+	}
+	return 0;
+}
+
 static int file_dict_write_changes(struct file_dict_transaction_context *ctx)
 {
 	struct file_dict *dict = (struct file_dict *)ctx->ctx.dict;
@@ -300,6 +332,11 @@ static int file_dict_write_changes(struc
 	if (file_dict_refresh(dict) < 0) {
 		file_dotlock_delete(&dotlock);
 		return -1;
+	}
+	if (dict->fd != -1) {
+		/* preserve the permissions */
+		(void)fd_copy_permissions(dict->fd, dict->path, fd,
+					  file_dotlock_get_lock_path(dotlock));
 	}
 	file_dict_apply_changes(ctx);
 


More information about the dovecot-cvs mailing list