]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/path: add ul_prefix_fopen(), improve cpuset funcs
authorKarel Zak <kzak@redhat.com>
Fri, 25 May 2018 10:47:06 +0000 (12:47 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 21 Jun 2018 11:07:46 +0000 (13:07 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/path.h
lib/path.c

index 76c18e10310a1ba3813cb62120097a8f08054a5b..f372c75027f7ce3356a0863875c0784154e50125 100644 (file)
@@ -114,6 +114,7 @@ int ul_path_count_dirents(struct path_cxt *pc, const char *path);
 int ul_path_countf_dirents(struct path_cxt *pc, const char *path, ...)
                                __attribute__ ((__format__ (__printf__, 2, 3)));
 
+FILE *ul_prefix_fopen(const char *prefix, const char *path, const char *mode);
 
 
 #ifdef HAVE_CPU_SET_T
index 98455accffd2947c3c6e6a0dcc13718e5f0be9d6..9f351ed05f81e3332e3b4ac9cc6e92d76df90578 100644 (file)
@@ -911,6 +911,25 @@ int ul_path_countf_dirents(struct path_cxt *pc, const char *path, ...)
        return ul_path_count_dirents(pc, p);
 }
 
+/*
+ * Like fopen() but, @path is always prefixed by @prefix. This function is
+ * useful in case when ul_path_* API is overkill.
+ */
+FILE *ul_prefix_fopen(const char *prefix, const char *path, const char *mode)
+{
+       char buf[PATH_MAX];
+
+       if (!path)
+               return NULL;
+       if (!prefix)
+               return fopen(path, mode);
+       if (*path == '/')
+               path++;
+
+       snprintf(buf, sizeof(buf), "%s/%s", prefix, path);
+       return fopen(buf, mode);
+}
+
 #ifdef HAVE_CPU_SET_T
 static int ul_path_cpuparse(struct path_cxt *pc, cpu_set_t **set, int maxcpus, int islist, const char *path, va_list ap)
 {
@@ -918,6 +937,8 @@ static int ul_path_cpuparse(struct path_cxt *pc, cpu_set_t **set, int maxcpus, i
        size_t setsize, len = maxcpus * 7;
        char buf[len];
 
+       *set = NULL;
+
        f = ul_path_vfopenf(pc, "r" UL_CLOEXECSTR, path, ap);
        if (!f)
                return -errno;
@@ -935,11 +956,15 @@ static int ul_path_cpuparse(struct path_cxt *pc, cpu_set_t **set, int maxcpus, i
                return -ENOMEM;
 
        if (islist) {
-               if (cpulist_parse(buf, *set, setsize, 0))
+               if (cpulist_parse(buf, *set, setsize, 0)) {
+                       cpuset_free(*set);
                        return -EINVAL;
+               }
        } else {
-               if (cpumask_parse(buf, *set, setsize))
+               if (cpumask_parse(buf, *set, setsize)) {
+                       cpuset_free(*set);
                        return -EINVAL;
+               }
        }
        return 0;
 }