changeset 10502:13be6ac759ee HEAD

Added t_get_current_dir() and use it instead of getcwd().
author Timo Sirainen <tss@iki.fi>
date Wed, 16 Dec 2009 13:43:23 -0500
parents 28a14e2fe5d6
children e7cd1b10f18b
files src/lib/abspath.c src/lib/abspath.h src/lib/eacces-error.c src/lib/nfs-workarounds.c
diffstat 4 files changed, 37 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
 
 	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;
+}
--- 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 @@
 /* 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
--- 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 @@
 	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",
--- 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 @@
 		/* 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 @@
 			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 @@
 	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;
 }