]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Rename t_get_current_dir to t_get_working_dir
authorMartti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
Sun, 4 Dec 2016 10:52:51 +0000 (12:52 +0200)
committerGitLab <gitlab@git.dovecot.net>
Mon, 30 Jan 2017 16:48:15 +0000 (18:48 +0200)
Also add an error_r parameter that cannot be NULL.

src/lib/eacces-error.c
src/lib/nfs-workarounds.c
src/lib/path-util.c
src/lib/path-util.h
src/lib/unlink-directory.c
src/lmtp/main.c

index a7b71ff128a8561b4de3a67d77ab0a4a1008ca10..68230df6270c88a94e2e341ab041e7c350f993bd 100644 (file)
@@ -162,7 +162,11 @@ eacces_error_get_full(const char *func, const char *path, bool creating)
        errmsg = t_str_new(256);
        str_printfa(errmsg, "%s(%s)", func, path);
        if (*path != '/') {
-               if (t_get_current_dir(&dir) == 0) {
+               const char *error;
+               if (t_get_working_dir(&dir, &error) < 0) {
+                       i_error("eacces_error_get_full: %s", error);
+                       str_printfa(errmsg, " in an unknown directory");
+               } else {
                        str_printfa(errmsg, " in directory %s", dir);
                        path = t_strconcat(dir, "/", path, NULL);
                }
index dc65b003087ca1fce26bb60a16f1cd274aba5f07..0d10b74f4d8049f03d10c18c41a6e911f4407bbd 100644 (file)
@@ -322,9 +322,9 @@ nfs_flush_file_handle_cache_dir(const char *path, bool try_parent ATTR_UNUSED)
                        return TRUE;
                }
 
-               if (t_get_current_dir(&cur_path) < 0) {
-                       i_error("nfs_flush_file_handle_cache_dir: "
-                               "getcwd() failed");
+               const char *error;
+               if (t_get_working_dir(&cur_path, &error) < 0) {
+                       i_error("nfs_flush_file_handle_cache_dir: %s", error);
                        i_close_fd(&cur_dir_fd);
                        return TRUE;
                }
index b2eba3b5a0aa9d373f6d3df314fe830ad333b3e0..5a8dbbf63f497b14519af0be172bc17dc55155ed 100644 (file)
@@ -8,14 +8,14 @@
 
 const char *t_abspath(const char *path)
 {
-       const char *dir;
        i_assert(path != NULL);
 
        if (*path == '/')
                return path;
 
-       if (t_get_current_dir(&dir) < 0)
-               i_fatal("getcwd() failed: %m");
+       const char *dir, *error;
+       if (t_get_working_dir(&dir, &error) < 0)
+               i_fatal("Failed to get working directory: %s", error);
        return t_strconcat(dir, "/", path, NULL);
 }
 
@@ -30,16 +30,20 @@ const char *t_abspath_to(const char *path, const char *root)
        return t_strconcat(root, "/", path, NULL);
 }
 
-int t_get_current_dir(const char **dir_r)
+int t_get_working_dir(const char **dir_r, const char **error_r)
 {
+       i_assert(error_r != NULL);
+
        /* @UNSAFE */
        char *dir;
        size_t size = 128;
 
        dir = t_buffer_get(size);
        while (getcwd(dir, size) == NULL) {
-               if (errno != ERANGE)
+               if (errno != ERANGE) {
+                       *error_r = t_strdup_printf("getcwd() failed: %m");
                        return -1;
+               }
                size = nearest_power(size+1);
                dir = t_buffer_get(size);
        }
index af30e23e00aa300b3c5476215316b686ff64fb9e..68c98acfb41c5e6ac705e048eef75d606a21fc79 100644 (file)
@@ -12,8 +12,9 @@ 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);
+/* Get current working directory allocated from data stack. Returns 0 on
+ * success and 1 on failure. error_r is set on failure and cannot be NULL. */
+int t_get_working_dir(const char **dir_r, const char **error_r);
 
 /* Get symlink destination allocated from data stack. Returns 0 on success and
  * -1 on failure. error_r is set on failure and cannot be NULL. */
index 3a466a4bfba5f1c90affcb7d67b7c28f3536de50..6965a266617a49aa14199b7dfad249bbff5f58f9 100644 (file)
@@ -189,11 +189,14 @@ unlink_directory_r(const char *dir, enum unlink_directory_flags flags,
 int unlink_directory(const char *dir, enum unlink_directory_flags flags,
                     const char **error_r)
 {
-       const char *orig_dir;
+       const char *orig_dir, *error;
        int fd, ret, old_errno;
 
-       if (t_get_current_dir(&orig_dir) < 0)
+       if (t_get_working_dir(&orig_dir, &error) < 0) {
+               i_warning("Could not get working directory in unlink_directory(): %s",
+                         error);
                orig_dir = ".";
+       }
 
        fd = open(".", O_RDONLY);
        if (fd == -1) {
index 2c11eb5d7b42e75698c15925819e01b2a9aa1444..5a4f4f9bbdee9a37b2da67664fbdeb05fe2dd398 100644 (file)
@@ -113,8 +113,9 @@ int main(int argc, char *argv[])
                }
        }
 
-       if (t_get_current_dir(&tmp_base_dir) < 0)
-               i_fatal("getcwd() failed: %m");
+       const char *error;
+       if (t_get_working_dir(&tmp_base_dir, &error) < 0)
+               i_fatal("Could not get working directory: %s", error);
        base_dir = i_strdup(tmp_base_dir);
 
        drop_privileges();