dovecot-2.2-pigeonhole: lib-sieve: file storage: Restructured st...

pigeonhole at rename-it.nl pigeonhole at rename-it.nl
Fri Jan 16 17:26:02 UTC 2015


details:   http://hg.rename-it.nl/dovecot-2.2-pigeonhole/rev/cd8194a2469e
changeset: 1986:cd8194a2469e
user:      Stephan Bosch <stephan at rename-it.nl>
date:      Fri Jan 16 18:25:51 2015 +0100
description:
lib-sieve: file storage: Restructured storage initialization to address backwards compatibility issues.

diffstat:

 src/lib-sieve/storage/file/sieve-file-script.c         |  105 +-
 src/lib-sieve/storage/file/sieve-file-storage-active.c |    9 +-
 src/lib-sieve/storage/file/sieve-file-storage-list.c   |    1 +
 src/lib-sieve/storage/file/sieve-file-storage-quota.c  |    1 +
 src/lib-sieve/storage/file/sieve-file-storage-save.c   |    1 +
 src/lib-sieve/storage/file/sieve-file-storage.c        |  594 +++++++++++-----
 src/lib-sieve/storage/file/sieve-file-storage.h        |    3 +
 7 files changed, 459 insertions(+), 255 deletions(-)

diffs (truncated from 978 to 300 lines):

diff -r 8f73af8e6c3a -r cd8194a2469e src/lib-sieve/storage/file/sieve-file-script.c
--- a/src/lib-sieve/storage/file/sieve-file-script.c	Fri Jan 16 18:23:49 2015 +0100
+++ b/src/lib-sieve/storage/file/sieve-file-script.c	Fri Jan 16 18:25:51 2015 +0100
@@ -52,7 +52,7 @@
  */
 
 static void sieve_file_script_handle_error
-(struct sieve_file_script *fscript, const char *path,
+(struct sieve_file_script *fscript, const char *op, const char *path,
 	const char *name, enum sieve_error *error_r)
 {
 	struct sieve_script *script = &fscript->script;
@@ -67,12 +67,14 @@
 		break;
 	case EACCES:
 		sieve_script_set_critical(script,
-			"Failed to stat sieve script: %s", eacces_error_get("stat", path));
+			"Failed to %s sieve script: %s",
+			op, eacces_error_get(op, path));
 		*error_r = SIEVE_ERROR_NO_PERMISSION;
 		break;
 	default:
 		sieve_script_set_critical(script,
-			"Failed to stat sieve script: stat(%s) failed: %m", path);
+			"Failed to %s sieve script: %s(%s) failed: %m",
+			op, op, path);
 		*error_r = SIEVE_ERROR_TEMP_FAILURE;
 		break;
 	}
@@ -150,14 +152,15 @@
 	struct sieve_storage *storage = &fstorage->storage;
 	struct sieve_file_script *fscript;
 
-	if (name != NULL) {
+	if (name != NULL && S_ISDIR(fstorage->st.st_mode)) {
 		return sieve_file_script_init_from_filename
 			(fstorage, sieve_script_file_from_name(name), name);
 	}
 
 	fscript = sieve_file_script_alloc();
 	sieve_script_init
-		(&fscript->script, storage, &sieve_file_script,	fstorage->path, NULL);
+		(&fscript->script, storage, &sieve_file_script,
+			fstorage->active_path, name);
 	return fscript;
 }
 
@@ -259,7 +262,7 @@
 	if ( S_ISLNK(st->st_mode) && stat(path, st) < 0 )
 		return -1;
 
-	return 1;
+	return 0;
 }
 
 static const char *
@@ -289,67 +292,67 @@
 	pool_t pool = script->pool;
 	const char *filename, *name, *path;
 	const char *dirpath, *basename, *binpath, *binprefix;
-	struct stat st;
-	struct stat lnk_st;
+	struct stat st, lnk_st;
 	bool success = TRUE;
-	int ret;
+	int ret = 0;
 
 	filename = fscript->filename;
 	basename = NULL;
 	name = script->name;
-	path = fstorage->path;
+	st = fstorage->st;
+	lnk_st = fstorage->lnk_st;
 
 	if (name == NULL)
 		name = storage->script_name;
 
 	T_BEGIN {
-		if ( (ret=sieve_file_script_stat(path, &st, &lnk_st)) > 0 ) {
-			if ( S_ISDIR(st.st_mode) ) {
-				/* Path is directory */
-				if ( (filename == NULL || *filename == '\0') &&
-					name != NULL && *name != '\0' ) {
-					/* Name is used to find actual filename */
-					filename = sieve_script_file_from_name(name);
+		if ( S_ISDIR(st.st_mode) ) {
+			/* Storage is a directory */
+			path = fstorage->path;
+
+			if ( (filename == NULL || *filename == '\0') &&
+				name != NULL && *name != '\0' ) {
+				/* Name is used to find actual filename */
+				filename = sieve_script_file_from_name(name);
+				basename = name;
+			}
+			if ( filename == NULL || *filename == '\0' ) {
+				sieve_script_set_critical(script,
+					"Sieve script file path '%s' is a directory.", path);
+				*error_r = SIEVE_ERROR_TEMP_FAILURE;
+				success = FALSE;
+			}	else {
+				/* Extend storage path with filename */
+				if (name == NULL) {
+					if ( basename == NULL &&
+						(basename=sieve_script_file_get_scriptname(filename)) == NULL )
+						basename = filename;
+					name = basename;
+				} else if (basename == NULL) {
 					basename = name;
 				}
-				if ( filename == NULL || *filename == '\0' ) {
-					sieve_script_set_critical(script,
-						"Sieve script file path '%s' is a directory.", path);
-					*error_r = SIEVE_ERROR_TEMP_FAILURE;
-					success = FALSE;
-				}	else {
-					/* Extend storage path with filename */
-					if (name == NULL) {
-						if ( basename == NULL &&
-							(basename=sieve_script_file_get_scriptname(filename)) == NULL )
-							basename = filename;
-						name = basename;
-					} else if (basename == NULL) {
-						basename = name;
-					}
-					dirpath = path;
+				dirpath = path;
 
-					path = sieve_file_storage_path_extend(fstorage, filename);
-					ret = sieve_file_script_stat(path, &st, &lnk_st);
-				}
+				path = sieve_file_storage_path_extend(fstorage, filename);
+				ret = sieve_file_script_stat(path, &st, &lnk_st);
+			}
 
-			} else {
+		} else {
+			/* Storage is a single file */
+			path = fstorage->active_path;
 
-				/* Extract filename from path */
-				filename = path_split_filename(path, &dirpath);
+			/* Extract filename from path */
+			filename = path_split_filename(path, &dirpath);
 
-				if ( (basename=sieve_script_file_get_scriptname(filename)) == NULL )
-					basename = filename;
+			if ( (basename=sieve_script_file_get_scriptname(filename)) == NULL )
+				basename = filename;
 
-				if ( name == NULL )
-					name = basename;
-			}
-		} else {
-			basename = name;
+			if ( name == NULL )
+				name = basename;
 		}
 
 		if ( success ) {
-			if ( ret <= 0 ) {
+			if ( ret < 0 ) {
 				/* Make sure we have a script name for the error */
 				if ( name == NULL ) {
 					if ( basename == NULL ) {
@@ -361,7 +364,8 @@
 					}
 					name = basename;
 				}
-				sieve_file_script_handle_error(fscript, path, name, error_r);
+				sieve_file_script_handle_error
+					(fscript, "stat", path, name, error_r);
 				success = FALSE;
 
 			} else if ( !S_ISREG(st.st_mode) ) {
@@ -431,7 +435,7 @@
 
 	if ( (fd=open(fscript->path, O_RDONLY)) < 0 ) {
 		sieve_file_script_handle_error
-			(fscript, fscript->path, fscript->script.name, error_r);
+			(fscript, "open", fscript->path, fscript->script.name, error_r);
 		return -1;
 	}
 
@@ -599,6 +603,8 @@
 	if ( ret <= 0 || strcmp(fscript->filename, afile) != 0 )
 		activated = 1;
 
+	i_assert( fstorage->link_path != NULL );
+
 	/* Check the scriptfile we are trying to activate */
 	if ( lstat(fscript->path, &st) != 0 ) {
 		sieve_script_set_critical(script,
@@ -684,6 +690,7 @@
 			/* Is the requested script active? */
 			if ( sieve_script_is_active(script) ) {
 				/* Active; make active link point to the new copy */
+				i_assert( fstorage->link_path != NULL );
 				link_path = t_strconcat
 					( fstorage->link_path, newfile, NULL );
 
diff -r 8f73af8e6c3a -r cd8194a2469e src/lib-sieve/storage/file/sieve-file-storage-active.c
--- a/src/lib-sieve/storage/file/sieve-file-storage-active.c	Fri Jan 16 18:23:49 2015 +0100
+++ b/src/lib-sieve/storage/file/sieve-file-storage-active.c	Fri Jan 16 18:25:51 2015 +0100
@@ -133,6 +133,7 @@
 	}
 
 	/* Check whether the path is any good */
+	i_assert( fstorage->link_path != NULL );
 	if ( _file_path_cmp(scriptpath, fstorage->link_path) != 0 &&
 		_file_path_cmp(scriptpath, fstorage->path) != 0 ) {
 		sieve_storage_sys_warning(storage,
@@ -284,8 +285,12 @@
 			return NULL;
 
 		/* Try to open the active_path as a regular file */
-		fscript = sieve_file_script_open_from_path(fstorage,
-			fstorage->active_path, NULL, NULL);
+		if ( S_ISDIR(fstorage->st.st_mode) ) {
+			fscript = sieve_file_script_open_from_path(fstorage,
+				fstorage->active_path, NULL, NULL);			
+		} else {
+			fscript = sieve_file_script_open_from_name(fstorage, NULL);
+		}
 		if ( fscript == NULL ) {
 			if ( storage->error_code != SIEVE_ERROR_NOT_FOUND ) {
 				sieve_storage_set_critical(storage,
diff -r 8f73af8e6c3a -r cd8194a2469e src/lib-sieve/storage/file/sieve-file-storage-list.c
--- a/src/lib-sieve/storage/file/sieve-file-storage-list.c	Fri Jan 16 18:23:49 2015 +0100
+++ b/src/lib-sieve/storage/file/sieve-file-storage-list.c	Fri Jan 16 18:25:51 2015 +0100
@@ -84,6 +84,7 @@
 			/* Don't list our active sieve script link if the link
 			 * resides in the script dir (generally a bad idea).
 			 */
+			i_assert( fstorage->link_path != NULL );
 			if ( *(fstorage->link_path) == '\0' &&
 				strcmp(fstorage->active_fname, dp->d_name) == 0 )
 				continue;
diff -r 8f73af8e6c3a -r cd8194a2469e src/lib-sieve/storage/file/sieve-file-storage-quota.c
--- a/src/lib-sieve/storage/file/sieve-file-storage-quota.c	Fri Jan 16 18:23:49 2015 +0100
+++ b/src/lib-sieve/storage/file/sieve-file-storage-quota.c	Fri Jan 16 18:25:51 2015 +0100
@@ -60,6 +60,7 @@
 		/* Don't list our active sieve script link if the link
 		 * resides in the script dir (generally a bad idea).
 		 */
+		i_assert( fstorage->link_path != NULL );
 		if ( *(fstorage->link_path) == '\0' &&
 			strcmp(fstorage->active_fname, dp->d_name) == 0 )
 			continue;
diff -r 8f73af8e6c3a -r cd8194a2469e src/lib-sieve/storage/file/sieve-file-storage-save.c
--- a/src/lib-sieve/storage/file/sieve-file-storage-save.c	Fri Jan 16 18:23:49 2015 +0100
+++ b/src/lib-sieve/storage/file/sieve-file-storage-save.c	Fri Jan 16 18:25:51 2015 +0100
@@ -184,6 +184,7 @@
 		/* Prevent overwriting the active script link when it resides in the
 		 * sieve storage directory.
 		 */
+		i_assert( fstorage->link_path != NULL );
 		if ( *(fstorage->link_path) == '\0' ) {
 			const char *svext;
 			size_t namelen;
diff -r 8f73af8e6c3a -r cd8194a2469e src/lib-sieve/storage/file/sieve-file-storage.c
--- a/src/lib-sieve/storage/file/sieve-file-storage.c	Fri Jan 16 18:23:49 2015 +0100
+++ b/src/lib-sieve/storage/file/sieve-file-storage.c	Fri Jan 16 18:25:51 2015 +0100
@@ -2,6 +2,7 @@
  */
 
 #include "lib.h"
+#include "abspath.h"
 #include "home-expand.h"
 #include "ioloop.h"
 #include "mkdir-parents.h"
@@ -46,6 +47,46 @@
  *
  */
 
+static int sieve_file_storage_stat
+(struct sieve_file_storage *fstorage, const char *path,
+	enum sieve_error *error_r)
+{
+	struct sieve_storage *storage = &fstorage->storage;
+	struct stat st;
+
+	if ( lstat(path, &st) == 0 ) {
+		fstorage->lnk_st = st;
+
+		if ( !S_ISLNK(st.st_mode) || stat(path, &st) == 0 ) {
+			fstorage->st = st;
+			return 0;
+		}
+	}
+
+	switch ( errno ) {
+	case ENOENT:
+		sieve_storage_sys_debug(storage,
+			"Storage path `%s' not found", t_abspath(path));
+		sieve_storage_set_internal_error(storage); // should be overriden
+		*error_r = SIEVE_ERROR_NOT_FOUND;
+		break;
+	case EACCES:
+		sieve_storage_set_critical(storage,


More information about the dovecot-cvs mailing list