Skip to content

Commit

Permalink
lib: Rename t_get_current_dir to t_get_working_dir
Browse files Browse the repository at this point in the history
Also add an error_r parameter that cannot be NULL.
  • Loading branch information
mrannanj authored and GitLab committed Jan 30, 2017
1 parent dd64535 commit f0913ba
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/lib/eacces-error.c
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/nfs-workarounds.c
Expand Up @@ -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;
}
Expand Down
14 changes: 9 additions & 5 deletions src/lib/path-util.c
Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/path-util.h
Expand Up @@ -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. */
Expand Down
7 changes: 5 additions & 2 deletions src/lib/unlink-directory.c
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/lmtp/main.c
Expand Up @@ -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();
Expand Down

0 comments on commit f0913ba

Please sign in to comment.