aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libnpass/libnpass.c4
-rw-r--r--src/libnpass/util.c41
2 files changed, 25 insertions, 20 deletions
diff --git a/src/libnpass/libnpass.c b/src/libnpass/libnpass.c
index 6c66a79..94a0647 100644
--- a/src/libnpass/libnpass.c
+++ b/src/libnpass/libnpass.c
@@ -269,7 +269,7 @@ int pass_init(const char *fpr)
err_ret(1, "key not usable, try gpg --full-generate-key");
r = r_mkdir(pass_dir, S_IRWXU);
- if (r)
+ if (r < 0)
err_ret(1, "%s %s", pass_dir, strerror(errno));
r = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir,
@@ -436,5 +436,5 @@ int pass_rm(const char *path)
if (r)
err_ret(1, "%s %s", abs_path, strerror(errno));
- return r_rmdir(pass_dir, dirname(gpg_path));
+ return r_rmdir_empty(pass_dir, dirname(gpg_path));
}
diff --git a/src/libnpass/util.c b/src/libnpass/util.c
index 4e34c26..7f2f488 100644
--- a/src/libnpass/util.c
+++ b/src/libnpass/util.c
@@ -12,48 +12,53 @@
int r_mkdir(const char *path, mode_t mode)
{
- int r;
+ char path_cp[PATH_MAX];
size_t len;
char *p;
- char tmp[NAME_MAX];
+ int ret;
- strncpy(tmp, path, sizeof(tmp) - 1);
- len = strlen(tmp);
- if (tmp[len - 1] == '/')
- tmp[len - 1] = '\0';
+ len = strlen(path);
+ if (len >= PATH_MAX)
+ err_ret(-EINVAL, "%s", "Path exceeded PATH_MAX");
- for (p = tmp + 1; *p; ++p) {
+ strcpy(path_cp, path);
+ for (p = path_cp + 1; *p; ++p) {
if (*p == '/') {
*p = '\0';
- r = mkdir(tmp, mode);
- if (r && !(errno & EEXIST))
- return r;
+ ret = mkdir(path_cp, mode);
+ if (ret < 0 && !(errno & EEXIST))
+ err_ret(-errno, "%s", strerror(errno));
*p = '/';
}
}
- return mkdir(path, mode);
+ ret = mkdir(path, mode);
+ if (ret < 0 && !(errno & EEXIST))
+ err_ret(-errno, "%s", strerror(errno));
+
+ return 0;
}
-int r_rmdir(const char *prefix_path, char *rm_path)
+int r_rmdir_empty(const char *prefix_path, char *rm_path)
{
- int r;
char abs_path[PATH_MAX];
+ int r;
- if (!strcmp(rm_path, "."))
+ r = strcmp(rm_path, ".");
+ if (r == 0)
return 0;
r = snprintf(abs_path, sizeof(abs_path), "%s/%s", prefix_path, rm_path);
if (r < 0 || (size_t)r >= sizeof(abs_path))
- err_ret(1, "failed to build path");
+ err_ret(-EINVAL, "%s", "Path exceeded PATH_MAX");
r = rmdir(abs_path);
- if (r && errno != EEXIST && errno != ENOTEMPTY)
- err_ret(1, "%s", strerror(errno));
+ if (r < 0 && errno != EEXIST && errno != ENOTEMPTY)
+ err_ret(-errno, "%s", strerror(errno));
- return r_rmdir(prefix_path, dirname(rm_path));
+ return r_rmdir_empty(prefix_path, dirname(rm_path));
}
void util_strtrim(char *s)