]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/path: consolidate ul_path_mkpath() usage
authorKarel Zak <kzak@redhat.com>
Wed, 15 May 2019 13:47:29 +0000 (15:47 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 15 May 2019 13:47:29 +0000 (15:47 +0200)
* always use errno from ul_path_mkpath()
* keep code simple and short as possible

Signed-off-by: Karel Zak <kzak@redhat.com>
lib/path.c

index b3d66a45de4ec28791a16e3b292f9421ff9ba2f4..968ce9114dfb6c22586964f07a7ca1df7cefd080 100644 (file)
@@ -230,10 +230,16 @@ int ul_path_isopen_dirfd(struct path_cxt *pc)
 
 static const char *ul_path_mkpath(struct path_cxt *pc, const char *path, va_list ap)
 {
-       int rc = vsnprintf(pc->path_buffer, sizeof(pc->path_buffer), path, ap);
+       int rc;
 
-       if (rc < 0)
+       errno = 0;
+
+       rc = vsnprintf(pc->path_buffer, sizeof(pc->path_buffer), path, ap);
+       if (rc < 0) {
+               if (!errno)
+                       errno = EINVAL;
                return NULL;
+       }
 
        if ((size_t)rc >= sizeof(pc->path_buffer)) {
                errno = ENAMETOOLONG;
@@ -308,10 +314,7 @@ int ul_path_accessf(struct path_cxt *pc, int mode, const char *path, ...)
        p = ul_path_mkpath(pc, path, ap);
        va_end(ap);
 
-       if (!p)
-               return -errno;
-
-       return ul_path_access(pc, mode, p);
+       return !p ? -errno : ul_path_access(pc, mode, p);
 }
 
 int ul_path_open(struct path_cxt *pc, int flags, const char *path)
@@ -341,13 +344,9 @@ int ul_path_open(struct path_cxt *pc, int flags, const char *path)
 
 int ul_path_vopenf(struct path_cxt *pc, int flags, const char *path, va_list ap)
 {
-       const char *p;
+       const char *p = ul_path_mkpath(pc, path, ap);
 
-       p = ul_path_mkpath(pc, path, ap);
-       if (!p)
-               return -errno;
-
-       return ul_path_open(pc, flags, p);
+       return !p ? -errno : ul_path_open(pc, flags, p);
 }
 
 int ul_path_openf(struct path_cxt *pc, int flags, const char *path, ...)
@@ -408,13 +407,9 @@ FILE *ul_path_fopen(struct path_cxt *pc, const char *mode, const char *path)
 
 FILE *ul_path_vfopenf(struct path_cxt *pc, const char *mode, const char *path, va_list ap)
 {
-       const char *p;
+       const char *p = ul_path_mkpath(pc, path, ap);
 
-       p = ul_path_mkpath(pc, path, ap);
-       if (!p)
-               return NULL;
-
-       return ul_path_fopen(pc, mode, p);
+       return !p ? NULL : ul_path_fopen(pc, mode, p);
 }
 
 FILE *ul_path_fopenf(struct path_cxt *pc, const char *mode, const char *path, ...)
@@ -465,13 +460,9 @@ DIR *ul_path_opendir(struct path_cxt *pc, const char *path)
  */
 DIR *ul_path_vopendirf(struct path_cxt *pc, const char *path, va_list ap)
 {
-       const char *p;
+       const char *p = ul_path_mkpath(pc, path, ap);
 
-       p = ul_path_mkpath(pc, path, ap);
-       if (!p)
-               return NULL;
-
-       return ul_path_opendir(pc, p);
+       return !p ? NULL : ul_path_opendir(pc, p);
 }
 
 /*
@@ -523,10 +514,7 @@ ssize_t ul_path_readlinkf(struct path_cxt *pc, char *buf, size_t bufsiz, const c
        p = ul_path_mkpath(pc, path, ap);
        va_end(ap);
 
-       if (!p)
-               return -errno;
-
-       return ul_path_readlink(pc, buf, bufsiz, p);
+       return !p ? -errno : ul_path_readlink(pc, buf, bufsiz, p);
 }
 
 int ul_path_read(struct path_cxt *pc, char *buf, size_t len, const char *path)
@@ -549,13 +537,9 @@ int ul_path_read(struct path_cxt *pc, char *buf, size_t len, const char *path)
 
 int ul_path_vreadf(struct path_cxt *pc, char *buf, size_t len, const char *path, va_list ap)
 {
-       const char *p;
-
-       p = ul_path_mkpath(pc, path, ap);
-       if (!p)
-               return -EINVAL;
+       const char *p = ul_path_mkpath(pc, path, ap);
 
-       return ul_path_read(pc, buf, len, p);
+       return !p ? -errno : ul_path_read(pc, buf, len, p);
 }
 
 int ul_path_readf(struct path_cxt *pc, char *buf, size_t len, const char *path, ...)
@@ -609,10 +593,7 @@ int ul_path_readf_string(struct path_cxt *pc, char **str, const char *path, ...)
        p = ul_path_mkpath(pc, path, ap);
        va_end(ap);
 
-       if (!p)
-               return -EINVAL;
-
-       return ul_path_read_string(pc, str, p);
+       return !p ? -errno : ul_path_read_string(pc, str, p);
 }
 
 int ul_path_read_buffer(struct path_cxt *pc, char *buf, size_t bufsz, const char *path)
@@ -638,10 +619,7 @@ int ul_path_readf_buffer(struct path_cxt *pc, char *buf, size_t bufsz, const cha
        p = ul_path_mkpath(pc, path, ap);
        va_end(ap);
 
-       if (!p)
-               return -EINVAL;
-
-       return ul_path_read_buffer(pc, buf, bufsz, p);
+       return !p ? -errno : ul_path_read_buffer(pc, buf, bufsz, p);
 }
 
 int ul_path_scanf(struct path_cxt *pc, const char *path, const char *fmt, ...)
@@ -705,10 +683,7 @@ int ul_path_readf_s64(struct path_cxt *pc, int64_t *res, const char *path, ...)
        p = ul_path_mkpath(pc, path, ap);
        va_end(ap);
 
-       if (!p)
-               return -EINVAL;
-
-       return ul_path_read_s64(pc, res, p);
+       return !p ? -errno : ul_path_read_s64(pc, res, p);
 }
 
 int ul_path_read_u64(struct path_cxt *pc, uint64_t *res, const char *path)
@@ -733,10 +708,7 @@ int ul_path_readf_u64(struct path_cxt *pc, uint64_t *res, const char *path, ...)
        p = ul_path_mkpath(pc, path, ap);
        va_end(ap);
 
-       if (!p)
-               return -EINVAL;
-
-       return ul_path_read_u64(pc, res, p);
+       return !p ? -errno : ul_path_read_u64(pc, res, p);
 }
 
 int ul_path_read_s32(struct path_cxt *pc, int *res, const char *path)
@@ -760,10 +732,7 @@ int ul_path_readf_s32(struct path_cxt *pc, int *res, const char *path, ...)
        p = ul_path_mkpath(pc, path, ap);
        va_end(ap);
 
-       if (!p)
-               return -EINVAL;
-
-       return ul_path_read_s32(pc, res, p);
+       return !p ? -errno : ul_path_read_s32(pc, res, p);
 }
 
 int ul_path_read_u32(struct path_cxt *pc, unsigned int *res, const char *path)
@@ -788,10 +757,7 @@ int ul_path_readf_u32(struct path_cxt *pc, unsigned int *res, const char *path,
        p = ul_path_mkpath(pc, path, ap);
        va_end(ap);
 
-       if (!p)
-               return -EINVAL;
-
-       return ul_path_read_u32(pc, res, p);
+       return !p ? -errno : ul_path_read_u32(pc, res, p);
 }
 
 int ul_path_read_majmin(struct path_cxt *pc, dev_t *res, const char *path)
@@ -815,10 +781,7 @@ int ul_path_readf_majmin(struct path_cxt *pc, dev_t *res, const char *path, ...)
        p = ul_path_mkpath(pc, path, ap);
        va_end(ap);
 
-       if (!p)
-               return -EINVAL;
-
-       return ul_path_read_majmin(pc, res, p);
+       return !p ? -errno : ul_path_read_majmin(pc, res, p);
 }
 
 int ul_path_write_string(struct path_cxt *pc, const char *str, const char *path)
@@ -847,10 +810,7 @@ int ul_path_writef_string(struct path_cxt *pc, const char *str, const char *path
        p = ul_path_mkpath(pc, path, ap);
        va_end(ap);
 
-       if (!p)
-               return -EINVAL;
-
-       return ul_path_write_string(pc, str, p);
+       return !p ? -errno : ul_path_write_string(pc, str, p);
 }
 
 int ul_path_write_s64(struct path_cxt *pc, int64_t num, const char *path)
@@ -906,10 +866,7 @@ int ul_path_writef_u64(struct path_cxt *pc, uint64_t num, const char *path, ...)
        p = ul_path_mkpath(pc, path, ap);
        va_end(ap);
 
-       if (!p)
-               return -EINVAL;
-
-       return ul_path_write_u64(pc, num, p);
+       return !p ? -errno : ul_path_write_u64(pc, num, p);
 
 }
 
@@ -937,10 +894,7 @@ int ul_path_countf_dirents(struct path_cxt *pc, const char *path, ...)
        p = ul_path_mkpath(pc, path, ap);
        va_end(ap);
 
-       if (!p)
-               return 0;
-
-       return ul_path_count_dirents(pc, p);
+       return !p ? -errno : ul_path_count_dirents(pc, p);
 }
 
 /*