Also add an error_r parameter that cannot be NULL.
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);
}
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;
}
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);
}
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);
}
/* 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. */
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) {
}
}
- 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();