aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-04-15 20:57:55 +0530
committersinanmohd <sinan@sinanmohd.com>2024-04-15 21:01:14 +0530
commit42e609321472f3796f010a1d32a39785c4328576 (patch)
treeabc73eddc5463b4a1909189dea8763b0651fe3f4
parent689c7e2204e3e924cf24ddfb9c399215b786ec1b (diff)
libnpass/util: refactor
https://www.kernel.org/doc/html/v4.10/process/coding-style.html - Function return values and names (-Exxx = failure, 0 = success) for imperative commands (0 = failure, non-zero = success) for predicates
-rw-r--r--include/libnpass/util.h2
-rw-r--r--src/libnpass/libnpass.c4
-rw-r--r--src/libnpass/util.c41
3 files changed, 26 insertions, 21 deletions
diff --git a/include/libnpass/util.h b/include/libnpass/util.h
index ebe97b1..c1af1a4 100644
--- a/include/libnpass/util.h
+++ b/include/libnpass/util.h
@@ -1,5 +1,5 @@
#include <stdlib.h>
int r_mkdir(const char *path, mode_t mode);
-int r_rmdir(const char *prefix_path, char *rm_path);
+int r_rmdir_empty(const char *prefix_path, char *rm_path);
void util_strtrim(char *s);
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)