summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKarel Zak2018-05-25 12:47:06 +0200
committerKarel Zak2018-06-21 13:07:46 +0200
commit9023b2012340c76340337bce71eafaa90ac3c612 (patch)
tree35b87006b76ec46db79608b668b84d0c1c9d3c6c /lib
parentlib/path: don't use extra '/' (diff)
downloadkernel-qcow2-util-linux-9023b2012340c76340337bce71eafaa90ac3c612.tar.gz
kernel-qcow2-util-linux-9023b2012340c76340337bce71eafaa90ac3c612.tar.xz
kernel-qcow2-util-linux-9023b2012340c76340337bce71eafaa90ac3c612.zip
lib/path: add ul_prefix_fopen(), improve cpuset funcs
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/path.c29
1 files changed, 27 insertions, 2 deletions
diff --git a/lib/path.c b/lib/path.c
index 98455accf..9f351ed05 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -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;
}