dovecot-2.0: Added t_get_current_dir() and use it instead of get...

dovecot at dovecot.org dovecot at dovecot.org
Wed Dec 16 20:48:22 EET 2009


details:   http://hg.dovecot.org/dovecot-2.0/rev/13be6ac759ee
changeset: 10502:13be6ac759ee
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Dec 16 13:43:23 2009 -0500
description:
Added t_get_current_dir() and use it instead of getcwd().

diffstat:

4 files changed, 37 insertions(+), 17 deletions(-)
src/lib/abspath.c         |   21 +++++++++++++++++++--
src/lib/abspath.h         |    3 +++
src/lib/eacces-error.c    |    5 ++---
src/lib/nfs-workarounds.c |   25 +++++++++++++------------

diffs (140 lines):

diff -r 28a14e2fe5d6 -r 13be6ac759ee src/lib/abspath.c
--- a/src/lib/abspath.c	Wed Dec 16 13:42:45 2009 -0500
+++ b/src/lib/abspath.c	Wed Dec 16 13:43:23 2009 -0500
@@ -7,12 +7,12 @@
 
 const char *t_abspath(const char *path)
 {
-	char dir[PATH_MAX];
+	const char *dir;
 
 	if (*path == '/')
 		return path;
 
-	if (getcwd(dir, sizeof(dir)) == NULL)
+	if (t_get_current_dir(&dir) < 0)
 		i_fatal("getcwd() failed: %m");
 	return t_strconcat(dir, "/", path, NULL);
 }
@@ -24,3 +24,20 @@ const char *t_abspath_to(const char *pat
 
 	return t_strconcat(root, "/", path, NULL);
 }
+
+int t_get_current_dir(const char **dir_r)
+{
+	/* @UNSAFE */
+	char *dir;
+	size_t size = 128;
+
+	dir = t_buffer_get(size);
+	while (getcwd(dir, size) == NULL) {
+		if (errno != ERANGE)
+			return -1;
+		size = nearest_power(size+1);
+	}
+	t_buffer_alloc(strlen(dir) + 1);
+	*dir_r = dir;
+	return 0;
+}
diff -r 28a14e2fe5d6 -r 13be6ac759ee src/lib/abspath.h
--- a/src/lib/abspath.h	Wed Dec 16 13:42:45 2009 -0500
+++ b/src/lib/abspath.h	Wed Dec 16 13:43:23 2009 -0500
@@ -7,4 +7,7 @@ const char *t_abspath(const char *path);
 /* Like t_abspath(), but path is relative to given root. */
 const char *t_abspath_to(const char *path, const char *root);
 
+/* Returns current directory, allocated from data stack. */
+int t_get_current_dir(const char **dir_r);
+
 #endif
diff -r 28a14e2fe5d6 -r 13be6ac759ee src/lib/eacces-error.c
--- a/src/lib/eacces-error.c	Wed Dec 16 13:42:45 2009 -0500
+++ b/src/lib/eacces-error.c	Wed Dec 16 13:43:23 2009 -0500
@@ -2,6 +2,7 @@
 
 #include "lib.h"
 #include "str.h"
+#include "abspath.h"
 #include "restrict-access.h"
 #include "eacces-error.h"
 
@@ -88,15 +89,13 @@ eacces_error_get_full(const char *func, 
 	const struct group *group;
 	string_t *errmsg;
 	struct stat st, dir_st;
-	char cwd[PATH_MAX];
 	int orig_errno, ret = -1;
 
 	orig_errno = errno;
 	errmsg = t_str_new(256);
 	str_printfa(errmsg, "%s(%s)", func, path);
 	if (*path != '/') {
-		dir = getcwd(cwd, sizeof(cwd));
-		if (dir != NULL)
+		if (t_get_current_dir(&dir) == 0)
 			str_printfa(errmsg, " in directory %s", dir);
 	}
 	str_printfa(errmsg, " failed: Permission denied (euid=%s",
diff -r 28a14e2fe5d6 -r 13be6ac759ee src/lib/nfs-workarounds.c
--- a/src/lib/nfs-workarounds.c	Wed Dec 16 13:42:45 2009 -0500
+++ b/src/lib/nfs-workarounds.c	Wed Dec 16 13:43:23 2009 -0500
@@ -28,6 +28,7 @@
 */
 
 #include "lib.h"
+#include "abspath.h"
 #include "nfs-workarounds.h"
 
 #include <fcntl.h>
@@ -313,7 +314,7 @@ nfs_flush_file_handle_cache_dir(const ch
 		/* Solaris gives this if we're trying to rmdir() the current
 		   directory. Work around this by temporarily changing the
 		   current directory to the parent directory. */
-		char cur_path[PATH_MAX], *p;
+		const char *cur_path, *p;
 		int cur_dir_fd;
 		bool ret;
 
@@ -323,19 +324,17 @@ nfs_flush_file_handle_cache_dir(const ch
 			return TRUE;
 		}
 
-		if (getcwd(cur_path, sizeof(cur_path)) == NULL) {
+		if (t_get_current_dir(&cur_path) < 0) {
 			i_error("nfs_flush_file_handle_cache_dir: "
 				"getcwd() failed");
 			(void)close(cur_dir_fd);
 			return TRUE;
 		}
 		p = strrchr(cur_path, '/');
-		if (p != NULL)
-			*p = '\0';
-		else {
-			p[0] = '/';
-			p[1] = '\0';
-		}
+		if (p == NULL)
+			cur_path = "/";
+		else
+			cur_path = t_strdup_until(cur_path, p);
 		if (chdir(cur_path) < 0) {
 			i_error("nfs_flush_file_handle_cache_dir: "
 				"chdir() failed");
@@ -358,10 +357,12 @@ static void nfs_flush_file_handle_cache_
 	const char *p;
 
 	p = strrchr(path, '/');
-	if (p == NULL)
-		nfs_flush_file_handle_cache_dir(".", TRUE);
-	else T_BEGIN {
-		nfs_flush_file_handle_cache_dir(t_strdup_until(path, p), TRUE);
+	T_BEGIN {
+		if (p == NULL)
+			nfs_flush_file_handle_cache_dir(".", TRUE);
+		else
+			nfs_flush_file_handle_cache_dir(t_strdup_until(path, p),
+							TRUE);
 	} T_END;
 }
 


More information about the dovecot-cvs mailing list