lib/path: add ul_prefix_fopen(), improve cpuset funcs

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2018-05-25 12:47:06 +02:00
parent c87b53ca5f
commit 9023b20123
2 changed files with 28 additions and 2 deletions

View 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

View 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;
}