[dovecot-cvs] dovecot/src/lib-storage/index/maildir maildir-copy.c,1.14,1.15 maildir-expunge.c,1.7,1.8 maildir-list.c,1.4,1.5 maildir-save.c,1.11,1.12 maildir-storage.c,1.16,1.17

cras at procontrol.fi cras at procontrol.fi
Thu Dec 19 03:02:38 EET 2002


Update of /home/cvs/dovecot/src/lib-storage/index/maildir
In directory danu:/tmp/cvs-serv11467/lib-storage/index/maildir

Modified Files:
	maildir-copy.c maildir-expunge.c maildir-list.c maildir-save.c 
	maildir-storage.c 
Log Message:
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Added str_path() and str_ppath() functions. i_snprintf() now returns only -1
or 0 depending on if buffer got full. dec2str() returns the string allocated
from data stack. Instead of just casting to (long) or (int), we now use
dec2str() with printf-like functions. Added o_stream_send_str(). Added
strocpy() and replaced all strcpy()s and strncpy()s with it.

Pretty much untested, hope it doesn't break too badly :)



Index: maildir-copy.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/maildir/maildir-copy.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- maildir-copy.c	25 Nov 2002 19:02:50 -0000	1.14
+++ maildir-copy.c	19 Dec 2002 01:02:35 -0000	1.15
@@ -23,7 +23,7 @@
 	CopyHardContext *ctx = context;
 	MailFlags flags;
 	const char *fname;
-	char src[1024], dest[1024];
+	char src[PATH_MAX], dest[PATH_MAX];
 
 	flags = rec->msg_flags;
 	if (!index_mailbox_fix_custom_flags(ctx->dest, &flags,
@@ -32,12 +32,20 @@
 
 	/* link the file */
 	fname = index->lookup_field(index, rec, DATA_FIELD_LOCATION);
-	i_snprintf(src, sizeof(src), "%s/cur/%s", index->dir, fname);
+	if (str_ppath(src, sizeof(src), index->dir, "cur/", fname) < 0) {
+		mail_storage_set_critical(ctx->storage, "Filename too long: %s",
+					  fname);
+		return FALSE;
+	}
 
 	fname = maildir_filename_set_flags(maildir_generate_tmp_filename(),
 					   flags);
-	i_snprintf(dest, sizeof(dest), "%s/new/%s",
-		   ctx->dest->index->dir, fname);
+	if (str_ppath(dest, sizeof(dest),
+		      ctx->dest->index->dir, "new/", fname) < 0) {
+		mail_storage_set_critical(ctx->storage, "Filename too long: %s",
+					  fname);
+		return FALSE;
+	}
 
 	if (link(src, dest) == 0)
 		return TRUE;

Index: maildir-expunge.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/maildir/maildir-expunge.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- maildir-expunge.c	27 Oct 2002 06:37:18 -0000	1.7
+++ maildir-expunge.c	19 Dec 2002 01:02:35 -0000	1.8
@@ -8,13 +8,19 @@
 static int expunge_msg(IndexMailbox *ibox, MailIndexRecord *rec)
 {
 	const char *fname;
-	char path[1024];
+	char path[PATH_MAX];
 
 	fname = ibox->index->lookup_field(ibox->index, rec,
 					  DATA_FIELD_LOCATION);
 	if (fname != NULL) {
-		i_snprintf(path, sizeof(path), "%s/cur/%s",
-			   ibox->index->dir, fname);
+		if (str_ppath(path, sizeof(path),
+			      ibox->index->dir, "cur/", fname) < 0) {
+			mail_storage_set_critical(ibox->box.storage,
+						  "Filename too long: %s",
+						  fname);
+			return FALSE;
+		}
+
 		if (unlink(path) < 0) {
 			/* if it didn't exist, someone just had either
 			   deleted it or changed it's flags */
@@ -26,7 +32,6 @@
 	}
 
 	return TRUE;
-
 }
 
 int maildir_expunge_locked(IndexMailbox *ibox, int notify)

Index: maildir-list.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/maildir/maildir-list.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- maildir-list.c	3 Dec 2002 22:44:39 -0000	1.4
+++ maildir-list.c	19 Dec 2002 01:02:35 -0000	1.5
@@ -20,11 +20,11 @@
 maildir_get_marked_flags_from(const char *dir, time_t index_stamp)
 {
 	struct stat st;
-	char path[1024];
+	char path[PATH_MAX];
 	time_t cur_stamp;
 
-	i_snprintf(path, sizeof(path), "%s/cur", dir);
-	if (stat(path, &st) == -1) {
+	if (str_path(path, sizeof(path), dir, "cur") < 0 ||
+	    stat(path, &st) < 0) {
 		/* no cur/ directory - broken */
 		return 0;
 	}
@@ -35,8 +35,8 @@
 		return MAILBOX_MARKED;
 	}
 
-	i_snprintf(path, sizeof(path), "%s/new", dir);
-	if (stat(path, &st) == -1) {
+	if (str_path(path, sizeof(path), dir, "new") < 0 ||
+	    stat(path, &st) < 0) {
 		/* no new/ directory - broken */
 		return 0;
 	}
@@ -79,7 +79,7 @@
 	struct dirent *d;
 	struct stat st;
         MailboxFlags flags;
-	char path[1024];
+	char path[PATH_MAX];
 	int failed, found_inbox;
 
 	mail_storage_clear_error(storage);
@@ -109,8 +109,10 @@
 		if (fname[1] == '.' || imap_match(glob, fname+1) <= 0)
 			continue;
 
+		if (str_path(path, sizeof(path), storage->dir, fname) < 0)
+			continue;
+
 		/* make sure it's a directory */
-		i_snprintf(path, sizeof(path), "%s/%s", storage->dir, fname);
 		if (stat(path, &st) != 0) {
 			if (errno == ENOENT)
 				continue; /* just deleted, ignore */
@@ -138,8 +140,10 @@
 		if (strcasecmp(fname+1, "INBOX") == 0)
 			found_inbox = TRUE;
 
-                flags = maildir_get_marked_flags(storage, path);
+		t_push();
+		flags = maildir_get_marked_flags(storage, path);
 		func(storage, fname+1, flags, context);
+		t_pop();
 	}
 
 	if (!failed && !found_inbox && imap_match(glob, "INBOX") > 0) {
@@ -157,14 +161,16 @@
 	FindSubscribedContext *ctx = context;
 	MailboxFlags flags;
 	struct stat st;
-	char path[1024];
-
-	i_snprintf(path, sizeof(path), "%s/.%s", storage->dir, name);
+	char path[PATH_MAX];
 
-	if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
-		flags = maildir_get_marked_flags(storage, path);
-	else
+	if (str_ppath(path, sizeof(path), storage->dir, ".", name) < 0)
 		flags = MAILBOX_NOSELECT;
+	else {
+		if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
+			flags = maildir_get_marked_flags(storage, path);
+		else
+			flags = MAILBOX_NOSELECT;
+	}
 
 	ctx->func(storage, name, flags, ctx->context);
 	return TRUE;

Index: maildir-save.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/maildir/maildir-save.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- maildir-save.c	6 Dec 2002 01:09:23 -0000	1.11
+++ maildir-save.c	19 Dec 2002 01:02:35 -0000	1.12
@@ -18,7 +18,7 @@
 
 	hostpid_init();
 
-	return t_strdup_printf("%lu.%s_%u.%s", (unsigned long) ioloop_time,
+	return t_strdup_printf("%s.%s_%u.%s", dec2str(ioloop_time),
 			       my_pid, create_count++, my_hostname);
 }
 

Index: maildir-storage.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/maildir/maildir-storage.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- maildir-storage.c	25 Nov 2002 19:02:50 -0000	1.16
+++ maildir-storage.c	19 Dec 2002 01:02:35 -0000	1.17
@@ -85,13 +85,14 @@
 static int create_maildir(const char *dir, int verify)
 {
 	const char **tmp;
-	char path[1024];
+	char path[PATH_MAX];
 
 	if (mkdir(dir, CREATE_MODE) == -1 && (errno != EEXIST || !verify))
 		return FALSE;
 
 	for (tmp = maildirs; *tmp != NULL; tmp++) {
-		i_snprintf(path, sizeof(path), "%s/%s", dir, *tmp);
+		if (str_path(path, sizeof(path), dir, *tmp) < 0)
+			return FALSE;
 
 		if (mkdir(path, CREATE_MODE) == -1 &&
 		    (errno != EEXIST || !verify))
@@ -104,13 +105,17 @@
 static int verify_inbox(MailStorage *storage, const char *dir)
 {
 	const char **tmp;
-	char src[1024], dest[1024];
+	char src[PATH_MAX], dest[PATH_MAX];
 
 	/* first make sure the cur/ new/ and tmp/ dirs exist in root dir */
 	(void)create_maildir(dir, TRUE);
 
 	/* create the .INBOX directory */
-	i_snprintf(dest, sizeof(dest), "%s/.INBOX", dir);
+	if (str_path(dest, sizeof(dest), dir, ".INBOX") < 0) {
+		mail_storage_set_critical(storage, "Path too long: %s", dir);
+		return FALSE;
+	}
+
 	if (mkdir(dest, CREATE_MODE) == -1 && errno != EEXIST) {
 		mail_storage_set_critical(storage, "Can't create directory "
 					  "%s: %m", dest);
@@ -119,8 +124,16 @@
 
 	/* then symlink the cur/ new/ and tmp/ into the .INBOX/ directory */
 	for (tmp = maildirs; *tmp != NULL; tmp++) {
-		i_snprintf(src, sizeof(src), "../%s", *tmp);
-		i_snprintf(dest, sizeof(dest), "%s/.INBOX/%s", dir, *tmp);
+		if (str_path(src, sizeof(src), "..", *tmp) < 0) {
+			mail_storage_set_critical(storage, "Path too long: %s",
+						  *tmp);
+			return FALSE;
+		}
+		if (str_ppath(dest, sizeof(dest), dir, ".INBOX/", *tmp) < 0) {
+			mail_storage_set_critical(storage, "Path too long: %s",
+						  dir);
+			return FALSE;
+		}
 
 		if (symlink(src, dest) == -1 && errno != EEXIST) {
 			mail_storage_set_critical(storage, "symlink(%s, %s) "
@@ -172,7 +185,7 @@
 				     int readonly, int fast)
 {
 	struct stat st;
-	char path[1024];
+	char path[PATH_MAX];
 
 	mail_storage_clear_error(storage);
 
@@ -188,7 +201,12 @@
 		return FALSE;
 	}
 
-	i_snprintf(path, sizeof(path), "%s/.%s", storage->dir, name);
+	if (str_ppath(path, sizeof(path), storage->dir, ".", name) < 0) {
+		mail_storage_set_critical(storage, "Mailbox name too long: %s",
+					  name);
+		return FALSE;
+	}
+
 	if (stat(path, &st) == 0) {
 		/* exists - make sure the required directories are also there */
 		(void)create_maildir(path, TRUE);
@@ -207,7 +225,7 @@
 
 static int maildir_create_mailbox(MailStorage *storage, const char *name)
 {
-	char path[1024];
+	char path[PATH_MAX];
 
 	mail_storage_clear_error(storage);
 
@@ -217,7 +235,12 @@
 		return FALSE;
 	}
 
-	i_snprintf(path, sizeof(path), "%s/.%s", storage->dir, name);
+	if (str_ppath(path, sizeof(path), storage->dir, ".", name) < 0) {
+		mail_storage_set_critical(storage, "Mailbox name too long: %s",
+					  name);
+		return FALSE;
+	}
+
 	if (create_maildir(path, FALSE))
 		return TRUE;
 	else if (errno == EEXIST) {
@@ -233,7 +256,7 @@
 static int maildir_delete_mailbox(MailStorage *storage, const char *name)
 {
 	struct stat st;
-	char src[1024], dest[1024];
+	char src[PATH_MAX], dest[PATH_MAX];
 	int count;
 
 	mail_storage_clear_error(storage);
@@ -251,8 +274,17 @@
 
 	/* rename the .maildir into ..maildir which marks it as being
 	   deleted. this way we never see partially deleted maildirs. */
-	i_snprintf(src, sizeof(src), "%s/.%s", storage->dir, name);
-	i_snprintf(dest, sizeof(dest), "%s/..%s", storage->dir, name);
+	if (str_ppath(src, sizeof(src), storage->dir, ".", name) < 0) {
+		mail_storage_set_critical(storage, "Mailbox name too long: %s",
+					  name);
+		return FALSE;
+	}
+
+	if (str_ppath(dest, sizeof(dest), storage->dir, "..", name) < 0) {
+		mail_storage_set_critical(storage, "Mailbox name too long: %s",
+					  name);
+		return FALSE;
+	}
 
 	if (stat(src, &st) != 0 && errno == ENOENT) {
 		mail_storage_set_error(storage, "Mailbox doesn't exist: %s",
@@ -290,13 +322,17 @@
 static int move_inbox_data(MailStorage *storage, const char *newdir)
 {
 	const char **tmp;
-	char oldpath[1024], newpath[1024];
+	char oldpath[PATH_MAX], newpath[PATH_MAX];
 
 	/* newpath points to the destination folder directory, which contains
 	   symlinks to real INBOX directories. unlink() the symlinks and
 	   move the real cur/ directory here. */
 	for (tmp = maildirs; *tmp != NULL; tmp++) {
-		i_snprintf(newpath, sizeof(newpath), "%s/%s", newdir, *tmp);
+		if (str_path(newpath, sizeof(newpath), newdir, *tmp) < 0) {
+			mail_storage_set_critical(storage, "Path too long: %s",
+						  newdir);
+			return FALSE;
+		}
 
 		if (unlink(newpath) == -1 && errno != EEXIST) {
 			mail_storage_set_critical(storage,
@@ -306,8 +342,16 @@
 		}
 	}
 
-	i_snprintf(oldpath, sizeof(oldpath), "%s/cur", storage->dir);
-	i_snprintf(newpath, sizeof(newpath), "%s/cur", newdir);
+	if (str_path(oldpath, sizeof(oldpath), storage->dir, "cur") < 0) {
+		mail_storage_set_critical(storage, "Path too long: %s",
+					  storage->dir);
+		return FALSE;
+	}
+	if (str_path(newpath, sizeof(newpath), newdir, "cur") < 0) {
+		mail_storage_set_critical(storage, "Path too long: %s", newdir);
+		return FALSE;
+	}
+
 	if (rename(oldpath, newpath) != 0) {
 		mail_storage_set_critical(storage, "rename(%s, %s) failed: %m",
 					  oldpath, newpath);
@@ -323,13 +367,22 @@
 			     MailboxFlags flags __attr_unused__, void *context)
 {
 	RenameContext *ctx = context;
-	char oldpath[1024], newpath[1024];
+	char oldpath[PATH_MAX], newpath[PATH_MAX];
 
 	i_assert(ctx->oldnamelen <= strlen(name));
 
-	i_snprintf(oldpath, sizeof(oldpath), "%s/.%s", storage->dir, name);
-	i_snprintf(newpath, sizeof(newpath), "%s/.%s.%s",
-		   storage->dir, ctx->newname, name + ctx->oldnamelen);
+	if (str_ppath(oldpath, sizeof(oldpath), storage->dir, ".", name) < 0) {
+		mail_storage_set_critical(storage, "Mailbox name too long: %s",
+					  name);
+		return;
+	}
+
+	if (i_snprintf(newpath, sizeof(newpath), "%s/.%s.%s", storage->dir,
+		       ctx->newname, name + ctx->oldnamelen) < 0) {
+		mail_storage_set_critical(storage, "Mailbox name too long: %s",
+					  newpath);
+		return;
+	}
 
 	/* FIXME: it's possible to merge two folders if either one of them
 	   doesn't have existing root folder. We could check this but I'm not
@@ -350,7 +403,7 @@
 				  const char *newname)
 {
 	RenameContext ctx;
-	char oldpath[1024], newpath[1024];
+	char oldpath[PATH_MAX], newpath[PATH_MAX];
 	int ret;
 
 	mail_storage_clear_error(storage);
@@ -368,8 +421,19 @@
 
 	   NOTE: it's possible to rename a nonexisting folder which has
 	   subfolders. In that case we should ignore the rename() error. */
-	i_snprintf(oldpath, sizeof(oldpath), "%s/.%s", storage->dir, oldname);
-	i_snprintf(newpath, sizeof(newpath), "%s/.%s", storage->dir, newname);
+	if (str_ppath(oldpath, sizeof(oldpath),
+		      storage->dir, ".", oldname) < 0) {
+		mail_storage_set_critical(storage, "Mailbox name too long: %s",
+					  oldname);
+		return FALSE;
+	}
+
+	if (str_ppath(newpath, sizeof(newpath),
+		      storage->dir, ".", newname) < 0) {
+		mail_storage_set_critical(storage, "Mailbox name too long: %s",
+					  newname);
+		return FALSE;
+	}
 
 	ret = rename(oldpath, newpath);
 	if (ret == 0 || (errno == ENOENT && strcmp(oldname, "INBOX") != 0)) {
@@ -408,7 +472,7 @@
 					   MailboxNameStatus *status)
 {
 	struct stat st;
-	char path[1024];
+	char path[PATH_MAX];
 
 	mail_storage_clear_error(storage);
 
@@ -418,8 +482,8 @@
 		return TRUE;
 	}
 
-	i_snprintf(path, sizeof(path), "%s/.%s", storage->dir, name);
-	if (stat(path, &st) == 0) {
+	if (str_ppath(path, sizeof(path), storage->dir, ".", name) == 0 &&
+	    stat(path, &st) == 0) {
 		*status = MAILBOX_NAME_EXISTS;
 		return TRUE;
 	} else if (errno == ENOENT) {




More information about the dovecot-cvs mailing list