lib/path: add ul_prefix_fopen(), improve cpuset funcs
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
c87b53ca5f
commit
9023b20123
|
@ -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, ...)
|
int ul_path_countf_dirents(struct path_cxt *pc, const char *path, ...)
|
||||||
__attribute__ ((__format__ (__printf__, 2, 3)));
|
__attribute__ ((__format__ (__printf__, 2, 3)));
|
||||||
|
|
||||||
|
FILE *ul_prefix_fopen(const char *prefix, const char *path, const char *mode);
|
||||||
|
|
||||||
|
|
||||||
#ifdef HAVE_CPU_SET_T
|
#ifdef HAVE_CPU_SET_T
|
||||||
|
|
29
lib/path.c
29
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);
|
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
|
#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)
|
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;
|
size_t setsize, len = maxcpus * 7;
|
||||||
char buf[len];
|
char buf[len];
|
||||||
|
|
||||||
|
*set = NULL;
|
||||||
|
|
||||||
f = ul_path_vfopenf(pc, "r" UL_CLOEXECSTR, path, ap);
|
f = ul_path_vfopenf(pc, "r" UL_CLOEXECSTR, path, ap);
|
||||||
if (!f)
|
if (!f)
|
||||||
return -errno;
|
return -errno;
|
||||||
|
@ -935,11 +956,15 @@ static int ul_path_cpuparse(struct path_cxt *pc, cpu_set_t **set, int maxcpus, i
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
if (islist) {
|
if (islist) {
|
||||||
if (cpulist_parse(buf, *set, setsize, 0))
|
if (cpulist_parse(buf, *set, setsize, 0)) {
|
||||||
|
cpuset_free(*set);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (cpumask_parse(buf, *set, setsize))
|
if (cpumask_parse(buf, *set, setsize)) {
|
||||||
|
cpuset_free(*set);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue