util: Remove virsysfs and instead enhance virFileReadValue* functions

It is no longer needed thanks to the great virfilewrapper.c.  And this
way we don't have to add a new set of functions for each prefixed
path.

While on that, add two functions that weren't there before, string and
scaled integer reading ones.  Also increase the length of the string
being read by one to accompany for the optional newline at the
end (i.e. change INT_STRLEN_BOUND to INT_BUFSIZE_BOUND).

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Martin Kletzander 2017-04-05 16:13:52 +02:00
parent ae60ea48bc
commit 7008e10869
13 changed files with 228 additions and 417 deletions

View File

@ -177,7 +177,6 @@ UTIL_SOURCES = \
util/virstorageencryption.c util/virstorageencryption.h \ util/virstorageencryption.c util/virstorageencryption.h \
util/virstoragefile.c util/virstoragefile.h \ util/virstoragefile.c util/virstoragefile.h \
util/virstring.h util/virstring.c \ util/virstring.h util/virstring.c \
util/virsysfs.c util/virsysfs.h util/virsysfspriv.h \
util/virsysinfo.c util/virsysinfo.h util/virsysinfopriv.h \ util/virsysinfo.c util/virsysinfo.h util/virsysinfopriv.h \
util/virsystemd.c util/virsystemd.h util/virsystemdpriv.h \ util/virsystemd.c util/virsystemd.h util/virsystemdpriv.h \
util/virthread.c util/virthread.h \ util/virthread.c util/virthread.h \
@ -2651,7 +2650,6 @@ libvirt_setuid_rpc_client_la_SOURCES = \
util/virrandom.c \ util/virrandom.c \
util/virsocketaddr.c \ util/virsocketaddr.c \
util/virstring.c \ util/virstring.c \
util/virsysfs.c \
util/virsystemd.c \ util/virsystemd.c \
util/virtime.c \ util/virtime.c \
util/virthread.c \ util/virthread.c \

View File

@ -45,7 +45,6 @@
#include "virlog.h" #include "virlog.h"
#include "virnuma.h" #include "virnuma.h"
#include "virstring.h" #include "virstring.h"
#include "virsysfs.h"
#include "virtypedparam.h" #include "virtypedparam.h"
#include "viruuid.h" #include "viruuid.h"

View File

@ -1654,6 +1654,8 @@ virFileReadLimFD;
virFileReadLink; virFileReadLink;
virFileReadValueBitmap; virFileReadValueBitmap;
virFileReadValueInt; virFileReadValueInt;
virFileReadValueScaledInt;
virFileReadValueString;
virFileReadValueUint; virFileReadValueUint;
virFileRelLinkPointsTo; virFileRelLinkPointsTo;
virFileRemove; virFileRemove;
@ -2643,20 +2645,6 @@ virTrimSpaces;
virVasprintfInternal; virVasprintfInternal;
# util/virsysfs.h
virSysfsGetCpuValueBitmap;
virSysfsGetCpuValueInt;
virSysfsGetCpuValueString;
virSysfsGetCpuValueUint;
virSysfsGetNodeValueBitmap;
virSysfsGetNodeValueString;
virSysfsGetSystemPath;
virSysfsGetValueBitmap;
virSysfsGetValueInt;
virSysfsGetValueString;
virSysfsSetSystemPath;
# util/virsysinfo.h # util/virsysinfo.h
virSysinfoBaseBoardDefClear; virSysinfoBaseBoardDefClear;
virSysinfoBIOSDefFree; virSysinfoBIOSDefFree;

View File

@ -3799,24 +3799,36 @@ virFileComparePaths(const char *p1, const char *p2)
/** /**
* virFileReadValueInt: * virFileReadValueInt:
* @path: file to read from
* @value: pointer to int to be filled in with the value * @value: pointer to int to be filled in with the value
* @format, ...: file to read from
* *
* Read int from @path and put it into @value. * Read int from @format and put it into @value.
* *
* Return -2 for non-existing file, -1 on other errors and 0 if everything went * Return -2 for non-existing file, -1 on other errors and 0 if everything went
* fine. * fine.
*/ */
int int
virFileReadValueInt(const char *path, int *value) virFileReadValueInt(int *value, const char *format, ...)
{ {
int ret = -1;
char *str = NULL; char *str = NULL;
char *path = NULL;
va_list ap;
if (!virFileExists(path)) va_start(ap, format);
return -2; if (virVasprintf(&path, format, ap) < 0) {
va_end(ap);
goto cleanup;
}
va_end(ap);
if (virFileReadAll(path, INT_STRLEN_BOUND(*value), &str) < 0) if (!virFileExists(path)) {
return -1; ret = -2;
goto cleanup;
}
if (virFileReadAll(path, INT_BUFSIZE_BOUND(*value), &str) < 0)
goto cleanup;
virStringTrimOptionalNewline(str); virStringTrimOptionalNewline(str);
@ -3824,83 +3836,205 @@ virFileReadValueInt(const char *path, int *value)
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Invalid integer value '%s' in file '%s'"), _("Invalid integer value '%s' in file '%s'"),
str, path); str, path);
return -1; goto cleanup;
} }
ret = 0;
cleanup:
VIR_FREE(path);
VIR_FREE(str); VIR_FREE(str);
return ret;
return 0;
} }
/** /**
* virFileReadValueUint: * virFileReadValueUint:
* @path: file to read from * @value: pointer to int to be filled in with the value
* @value: pointer to unsigned int to be filled in with the value * @format, ...: file to read from
* *
* Read int from @path and put it into @value. * Read unsigned int from @format and put it into @value.
* *
* Return -2 for non-existing file, -1 on other errors and 0 if everything went * Return -2 for non-existing file, -1 on other errors and 0 if everything went
* fine. * fine.
*/ */
int int
virFileReadValueUint(const char *path, unsigned int *value) virFileReadValueUint(unsigned int *value, const char *format, ...)
{ {
int ret = -1;
char *str = NULL; char *str = NULL;
char *path = NULL;
va_list ap;
if (!virFileExists(path)) va_start(ap, format);
return -2; if (virVasprintf(&path, format, ap) < 0) {
va_end(ap);
goto cleanup;
}
va_end(ap);
if (virFileReadAll(path, INT_STRLEN_BOUND(*value), &str) < 0) if (!virFileExists(path)) {
return -1; ret = -2;
goto cleanup;
}
if (virFileReadAll(path, INT_BUFSIZE_BOUND(*value), &str) < 0)
goto cleanup;
virStringTrimOptionalNewline(str); virStringTrimOptionalNewline(str);
if (virStrToLong_uip(str, NULL, 10, value)) { if (virStrToLong_uip(str, NULL, 10, value) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Invalid unsigned integer value '%s' in file '%s'"), _("Invalid unsigned integer value '%s' in file '%s'"),
str, path); str, path);
return -1; goto cleanup;
} }
ret = 0;
cleanup:
VIR_FREE(path);
VIR_FREE(str); VIR_FREE(str);
return ret;
return 0;
} }
/** /**
* virFileReadValueBitmap: * virFileReadValueScaledInt:
* @path: file to read from * @value: pointer to unsigned long long int to be filled in with the value
* @value: double pointer to virBitmap to be allocated and filled in with the * @format, ...: file to read from
* value
* *
* Read int from @path and put it into @value. * Read unsigned scaled int from @format and put it into @value.
* *
* Return -2 for non-existing file, -1 on other errors and 0 if everything went * Return -2 for non-existing file, -1 on other errors and 0 if everything went
* fine. * fine.
*/ */
int int
virFileReadValueBitmap(const char *path, virFileReadValueScaledInt(unsigned long long *value, const char *format, ...)
int maxlen,
virBitmapPtr *value)
{ {
char *buf = NULL;
int ret = -1; int ret = -1;
char *str = NULL;
char *endp = NULL;
char *path = NULL;
va_list ap;
if (!virFileExists(path)) va_start(ap, format);
return -2; if (virVasprintf(&path, format, ap) < 0) {
va_end(ap);
goto cleanup;
}
va_end(ap);
if (virFileReadAll(path, maxlen, &buf) < 0) if (!virFileExists(path)) {
ret = -2;
goto cleanup;
}
if (virFileReadAll(path, INT_BUFSIZE_BOUND(*value), &str) < 0)
goto cleanup; goto cleanup;
virStringTrimOptionalNewline(buf); virStringTrimOptionalNewline(str);
*value = virBitmapParseUnlimited(buf); if (virStrToLong_ullp(str, &endp, 10, value) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Invalid unsigned scaled integer value '%s' in file '%s'"),
str, path);
goto cleanup;
}
ret = virScaleInteger(value, endp, 1024, ULLONG_MAX);
cleanup:
VIR_FREE(path);
VIR_FREE(str);
return ret;
}
/* Arbitrarily sized number, feel free to change, but the function should be
* used for small, interface-like files, so it should not be huge (subjective) */
#define VIR_FILE_READ_VALUE_STRING_MAX 4096
/**
* virFileReadValueBitmap:
* @value: pointer to virBitmapPtr to be allocated and filled in with the value
* @format, ...: file to read from
*
* Read int from @format and put it into @value.
*
* Return -2 for non-existing file, -1 on other errors and 0 if everything went
* fine.
*/
int
virFileReadValueBitmap(virBitmapPtr *value, const char *format, ...)
{
int ret = -1;
char *str = NULL;
char *path = NULL;
va_list ap;
va_start(ap, format);
if (virVasprintf(&path, format, ap) < 0) {
va_end(ap);
goto cleanup;
}
va_end(ap);
if (!virFileExists(path)) {
ret = -2;
goto cleanup;
}
if (virFileReadAll(path, VIR_FILE_READ_VALUE_STRING_MAX, &str) < 0)
goto cleanup;
virStringTrimOptionalNewline(str);
*value = virBitmapParseUnlimited(str);
if (!*value) if (!*value)
goto cleanup; goto cleanup;
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(buf); VIR_FREE(path);
VIR_FREE(str);
return ret;
}
/**
* virFileReadValueString:
* @value: pointer to char * to be allocated and filled in with the value
* @format, ...: file to read from
*
* Read string from @format and put it into @value. Don't get this mixed with
* virFileReadAll(). This function is a wrapper over it with the behaviour
* aligned to other virFileReadValue* functions
*
* Return -2 for non-existing file, -1 on other errors and 0 if everything went
* fine.
*/
int
virFileReadValueString(char **value, const char *format, ...)
{
int ret = -1;
char *str = NULL;
char *path = NULL;
va_list ap;
va_start(ap, format);
if (virVasprintf(&path, format, ap) < 0) {
va_end(ap);
goto cleanup;
}
va_end(ap);
if (!virFileExists(path)) {
ret = -2;
goto cleanup;
}
ret = virFileReadAll(path, VIR_FILE_READ_VALUE_STRING_MAX, value);
if (*value)
virStringTrimOptionalNewline(*value);
cleanup:
VIR_FREE(path);
VIR_FREE(str);
return ret; return ret;
} }

View File

@ -336,8 +336,16 @@ int virFileCopyACLs(const char *src,
int virFileComparePaths(const char *p1, const char *p2); int virFileComparePaths(const char *p1, const char *p2);
int virFileReadValueInt(const char *path, int *value); int virFileReadValueInt(int *value, const char *format, ...)
int virFileReadValueUint(const char *path, unsigned int *value); ATTRIBUTE_FMT_PRINTF(2, 3);
int virFileReadValueBitmap(const char *path, int maxlen, virBitmapPtr *value); int virFileReadValueUint(unsigned int *value, const char *format, ...)
ATTRIBUTE_FMT_PRINTF(2, 3);
int virFileReadValueBitmap(virBitmapPtr *value, const char *format, ...)
ATTRIBUTE_FMT_PRINTF(2, 3);
int virFileReadValueScaledInt(unsigned long long *value, const char *format, ...)
ATTRIBUTE_FMT_PRINTF(2, 3);
int virFileReadValueString(char **value, const char *format, ...)
ATTRIBUTE_FMT_PRINTF(2, 3);
#endif /* __VIR_FILE_H */ #endif /* __VIR_FILE_H */

View File

@ -56,7 +56,6 @@
#include "virfile.h" #include "virfile.h"
#include "virtypedparam.h" #include "virtypedparam.h"
#include "virstring.h" #include "virstring.h"
#include "virsysfs.h"
#include "virnuma.h" #include "virnuma.h"
#include "virlog.h" #include "virlog.h"
@ -192,6 +191,7 @@ virHostCPUGetStatsFreeBSD(int cpuNum,
#ifdef __linux__ #ifdef __linux__
# define CPUINFO_PATH "/proc/cpuinfo" # define CPUINFO_PATH "/proc/cpuinfo"
# define PROCSTAT_PATH "/proc/stat" # define PROCSTAT_PATH "/proc/stat"
# define SYSFS_SYSTEM_PATH "/sys/devices/system"
# define VIR_HOST_CPU_MASK_LEN 1024 # define VIR_HOST_CPU_MASK_LEN 1024
# define LINUX_NB_CPU_STATS 4 # define LINUX_NB_CPU_STATS 4
@ -205,7 +205,9 @@ virHostCPUCountThreadSiblings(unsigned int cpu)
char *str = NULL; char *str = NULL;
size_t i; size_t i;
rv = virSysfsGetCpuValueString(cpu, "topology/thread_siblings", &str); rv = virFileReadValueString(&str,
"%s/cpu/cpu%u/topology/thread_siblings",
SYSFS_SYSTEM_PATH, cpu);
if (rv == -2) { if (rv == -2) {
ret = 1; ret = 1;
goto cleanup; goto cleanup;
@ -227,9 +229,9 @@ int
virHostCPUGetSocket(unsigned int cpu, unsigned int *socket) virHostCPUGetSocket(unsigned int cpu, unsigned int *socket)
{ {
int tmp; int tmp;
int ret = virSysfsGetCpuValueInt(cpu, int ret = virFileReadValueInt(&tmp,
"topology/physical_package_id", "%s/cpu/cpu%u/topology/physical_package_id",
&tmp); SYSFS_SYSTEM_PATH, cpu);
/* If the file is not there, it's 0 */ /* If the file is not there, it's 0 */
if (ret == -2) if (ret == -2)
@ -251,7 +253,9 @@ virHostCPUGetSocket(unsigned int cpu, unsigned int *socket)
int int
virHostCPUGetCore(unsigned int cpu, unsigned int *core) virHostCPUGetCore(unsigned int cpu, unsigned int *core)
{ {
int ret = virSysfsGetCpuValueUint(cpu, "topology/core_id", core); int ret = virFileReadValueUint(core,
"%s/cpu/cpu%u/topology/core_id",
SYSFS_SYSTEM_PATH, cpu);
/* If the file is not there, it's 0 */ /* If the file is not there, it's 0 */
if (ret == -2) if (ret == -2)
@ -268,7 +272,9 @@ virHostCPUGetSiblingsList(unsigned int cpu)
virBitmapPtr ret = NULL; virBitmapPtr ret = NULL;
int rv = -1; int rv = -1;
rv = virSysfsGetCpuValueBitmap(cpu, "topology/thread_siblings_list", &ret); rv = virFileReadValueBitmap(&ret,
"%s/cpu/cpu%u/topology/thread_siblings_list",
SYSFS_SYSTEM_PATH, cpu);
if (rv == -2) { if (rv == -2) {
/* If the file doesn't exist, the threadis its only sibling */ /* If the file doesn't exist, the threadis its only sibling */
ret = virBitmapNew(cpu + 1); ret = virBitmapNew(cpu + 1);
@ -615,7 +621,7 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo,
/* OK, we've parsed clock speed out of /proc/cpuinfo. Get the /* OK, we've parsed clock speed out of /proc/cpuinfo. Get the
* core, node, socket, thread and topology information from /sys * core, node, socket, thread and topology information from /sys
*/ */
if (virAsprintf(&sysfs_nodedir, "%s/node", virSysfsGetSystemPath()) < 0) if (virAsprintf(&sysfs_nodedir, "%s/node", SYSFS_SYSTEM_PATH) < 0)
goto cleanup; goto cleanup;
if (virDirOpenQuiet(&nodedir, sysfs_nodedir) < 0) { if (virDirOpenQuiet(&nodedir, sysfs_nodedir) < 0) {
@ -659,8 +665,8 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo,
(*nodes)++; (*nodes)++;
if (virAsprintf(&sysfs_cpudir, "%s/node/%s", if (virAsprintf(&sysfs_cpudir, "%s/node/%s", SYSFS_SYSTEM_PATH,
virSysfsGetSystemPath(), nodedirent->d_name) < 0) nodedirent->d_name) < 0)
goto cleanup; goto cleanup;
if ((nodecpus = virHostCPUParseNode(sysfs_cpudir, arch, if ((nodecpus = virHostCPUParseNode(sysfs_cpudir, arch,
@ -694,7 +700,7 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo,
fallback: fallback:
VIR_FREE(sysfs_cpudir); VIR_FREE(sysfs_cpudir);
if (virAsprintf(&sysfs_cpudir, "%s/cpu", virSysfsGetSystemPath()) < 0) if (virAsprintf(&sysfs_cpudir, "%s/cpu", SYSFS_SYSTEM_PATH) < 0)
goto cleanup; goto cleanup;
if ((nodecpus = virHostCPUParseNode(sysfs_cpudir, arch, if ((nodecpus = virHostCPUParseNode(sysfs_cpudir, arch,
@ -841,7 +847,7 @@ virHostCPUParseCountLinux(void)
char *tmp; char *tmp;
int ret = -1; int ret = -1;
if (virSysfsGetValueString("cpu/present", &str) < 0) if (virFileReadValueString(&str, "%s/cpu/present", SYSFS_SYSTEM_PATH) < 0)
return -1; return -1;
tmp = str; tmp = str;
@ -866,8 +872,9 @@ int
virHostCPUGetOnline(unsigned int cpu, bool *online) virHostCPUGetOnline(unsigned int cpu, bool *online)
{ {
unsigned int tmp = 0; unsigned int tmp = 0;
int ret = virSysfsGetCpuValueUint(cpu, "online", &tmp); int ret = virFileReadValueUint(&tmp,
"%s/cpu/cpu%u/online",
SYSFS_SYSTEM_PATH, cpu);
/* If the file is not there, it's online (doesn't support offlining) */ /* If the file is not there, it's online (doesn't support offlining) */
if (ret == -2) if (ret == -2)
@ -1032,7 +1039,7 @@ virHostCPUGetPresentBitmap(void)
#ifdef __linux__ #ifdef __linux__
virBitmapPtr ret = NULL; virBitmapPtr ret = NULL;
virSysfsGetValueBitmap("cpu/present", &ret); virFileReadValueBitmap(&ret, "%s/cpu/present", SYSFS_SYSTEM_PATH);
return ret; return ret;
#else #else
@ -1048,7 +1055,7 @@ virHostCPUGetOnlineBitmap(void)
#ifdef __linux__ #ifdef __linux__
virBitmapPtr ret = NULL; virBitmapPtr ret = NULL;
virSysfsGetValueBitmap("cpu/online", &ret); virFileReadValueBitmap(&ret, "%s/cpu/online", SYSFS_SYSTEM_PATH);
return ret; return ret;
#else #else

View File

@ -1,231 +0,0 @@
/*
* virsysfs.c: Helper functions for manipulating sysfs files
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
* Author: Martin Kletzander <mkletzan@redhat.com>
*/
#include <config.h>
#include "internal.h"
#include "virsysfspriv.h"
#include "viralloc.h"
#include "virfile.h"
#include "virlog.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
VIR_LOG_INIT("util.sysfs");
#define VIR_SYSFS_VALUE_MAXLEN 8192
#define SYSFS_SYSTEM_PATH "/sys/devices/system"
static const char *sysfs_system_path = SYSFS_SYSTEM_PATH;
void virSysfsSetSystemPath(const char *path)
{
if (path)
sysfs_system_path = path;
else
sysfs_system_path = SYSFS_SYSTEM_PATH;
}
const char *
virSysfsGetSystemPath(void)
{
return sysfs_system_path;
}
int
virSysfsGetValueInt(const char *file,
int *value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/%s", sysfs_system_path, file) < 0)
return -1;
ret = virFileReadValueInt(path, value);
VIR_FREE(path);
return ret;
}
int
virSysfsGetValueString(const char *file,
char **value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/%s", sysfs_system_path, file) < 0)
return -1;
if (!virFileExists(path)) {
ret = -2;
goto cleanup;
}
if (virFileReadAll(path, VIR_SYSFS_VALUE_MAXLEN, value) < 0)
goto cleanup;
virStringTrimOptionalNewline(*value);
ret = 0;
cleanup:
VIR_FREE(path);
return ret;
}
int
virSysfsGetValueBitmap(const char *file,
virBitmapPtr *value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/%s", sysfs_system_path, file) < 0)
return -1;
ret = virFileReadValueBitmap(path, VIR_SYSFS_VALUE_MAXLEN, value);
VIR_FREE(path);
return ret;
}
int
virSysfsGetCpuValueInt(unsigned int cpu,
const char *file,
int *value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/cpu/cpu%u/%s", sysfs_system_path, cpu, file) < 0)
return -1;
ret = virFileReadValueInt(path, value);
VIR_FREE(path);
return ret;
}
int
virSysfsGetCpuValueUint(unsigned int cpu,
const char *file,
unsigned int *value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/cpu/cpu%u/%s", sysfs_system_path, cpu, file) < 0)
return -1;
ret = virFileReadValueUint(path, value);
VIR_FREE(path);
return ret;
}
int
virSysfsGetCpuValueString(unsigned int cpu,
const char *file,
char **value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/cpu/cpu%u/%s", sysfs_system_path, cpu, file) < 0)
return -1;
if (!virFileExists(path)) {
ret = -2;
goto cleanup;
}
if (virFileReadAll(path, VIR_SYSFS_VALUE_MAXLEN, value) < 0)
goto cleanup;
ret = 0;
cleanup:
VIR_FREE(path);
return ret;
}
int
virSysfsGetCpuValueBitmap(unsigned int cpu,
const char *file,
virBitmapPtr *value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/cpu/cpu%u/%s", sysfs_system_path, cpu, file) < 0)
return -1;
ret = virFileReadValueBitmap(path, VIR_SYSFS_VALUE_MAXLEN, value);
VIR_FREE(path);
return ret;
}
int
virSysfsGetNodeValueString(unsigned int node,
const char *file,
char **value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/node/node%u/%s", sysfs_system_path, node, file) < 0)
return -1;
if (!virFileExists(path)) {
ret = -2;
goto cleanup;
}
if (virFileReadAll(path, VIR_SYSFS_VALUE_MAXLEN, value) < 0)
goto cleanup;
ret = 0;
cleanup:
VIR_FREE(path);
return ret;
}
int
virSysfsGetNodeValueBitmap(unsigned int node,
const char *file,
virBitmapPtr *value)
{
char *path = NULL;
int ret = -1;
if (virAsprintf(&path, "%s/node/node%u/%s", sysfs_system_path, node, file) < 0)
return -1;
ret = virFileReadValueBitmap(path, VIR_SYSFS_VALUE_MAXLEN, value);
VIR_FREE(path);
return ret;
}

View File

@ -1,70 +0,0 @@
/*
* virsysfs.h: Helper functions for manipulating sysfs files
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
* Author: Martin Kletzander <mkletzan@redhat.com>
*/
#ifndef __VIR_SYSFS_H__
# define __VIR_SYSFS_H__
# include "internal.h"
# include "virbitmap.h"
const char * virSysfsGetSystemPath(void);
int
virSysfsGetValueInt(const char *file,
int *value);
int
virSysfsGetValueString(const char *file,
char **value);
int
virSysfsGetValueBitmap(const char *file,
virBitmapPtr *value);
int
virSysfsGetCpuValueInt(unsigned int cpu,
const char *file,
int *value);
int
virSysfsGetCpuValueUint(unsigned int cpu,
const char *file,
unsigned int *value);
int
virSysfsGetCpuValueString(unsigned int cpu,
const char *file,
char **value);
int
virSysfsGetCpuValueBitmap(unsigned int cpu,
const char *file,
virBitmapPtr *value);
int
virSysfsGetNodeValueString(unsigned int node,
const char *file,
char **value);
int
virSysfsGetNodeValueBitmap(unsigned int cpu,
const char *file,
virBitmapPtr *value);
#endif /* __VIR_SYSFS_H__*/

View File

@ -1,28 +0,0 @@
/*
* virsysfspriv.h: Helper functions for manipulating sysfs files
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
* Author: Martin Kletzander <mkletzan@redhat.com>
*/
#ifndef __VIR_SYSFS_PRIV_H__
# define __VIR_SYSFS_PRIV_H__
# include "virsysfs.h"
void virSysfsSetSystemPath(const char *path);
#endif /* __VIR_SYSFS_PRIV_H__*/

View File

@ -930,7 +930,7 @@ virconftest_SOURCES = \
virconftest_LDADD = $(LDADDS) virconftest_LDADD = $(LDADDS)
virhostcputest_SOURCES = \ virhostcputest_SOURCES = \
virhostcputest.c testutils.h testutils.c virhostcputest.c testutils.h testutils.c virfilewrapper.c
virhostcputest_LDADD = $(LDADDS) virhostcputest_LDADD = $(LDADDS)
commandtest_SOURCES = \ commandtest_SOURCES = \
@ -1147,7 +1147,7 @@ virhostcpumock_la_LIBADD = $(MOCKLIBS_LIBS)
if WITH_LINUX if WITH_LINUX
vircaps2xmltest_SOURCES = \ vircaps2xmltest_SOURCES = \
vircaps2xmltest.c testutils.h testutils.c vircaps2xmltest.c testutils.h testutils.c virfilewrapper.c
vircaps2xmltest_LDADD = $(LDADDS) vircaps2xmltest_LDADD = $(LDADDS)
virnumamock_la_SOURCES = \ virnumamock_la_SOURCES = \
@ -1155,6 +1155,7 @@ virnumamock_la_SOURCES = \
virnumamock_la_CFLAGS = $(AM_CFLAGS) virnumamock_la_CFLAGS = $(AM_CFLAGS)
virnumamock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS) virnumamock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
virnumamock_la_LIBADD = $(MOCKLIBS_LIBS) virnumamock_la_LIBADD = $(MOCKLIBS_LIBS)
else ! WITH_LINUX else ! WITH_LINUX
EXTRA_DIST += vircaps2xmltest.c virnumamock.c virfilewrapper.c virfilewrapper.h EXTRA_DIST += vircaps2xmltest.c virnumamock.c virfilewrapper.c virfilewrapper.h
endif ! WITH_LINUX endif ! WITH_LINUX

View File

@ -25,7 +25,7 @@
#include "testutils.h" #include "testutils.h"
#include "capabilities.h" #include "capabilities.h"
#include "virbitmap.h" #include "virbitmap.h"
#include "virsysfspriv.h" #include "virfilewrapper.h"
#define VIR_FROM_THIS VIR_FROM_NONE #define VIR_FROM_THIS VIR_FROM_NONE
@ -52,7 +52,7 @@ test_virCapabilities(const void *opaque)
abs_srcdir, data->filename) < 0) abs_srcdir, data->filename) < 0)
goto cleanup; goto cleanup;
virSysfsSetSystemPath(dir); virFileWrapperAddPrefix("/sys/devices/system", dir);
caps = virCapabilitiesNew(data->arch, data->offlineMigrate, data->liveMigrate); caps = virCapabilitiesNew(data->arch, data->offlineMigrate, data->liveMigrate);
if (!caps) if (!caps)
@ -61,7 +61,7 @@ test_virCapabilities(const void *opaque)
if (virCapabilitiesInitNUMA(caps) < 0) if (virCapabilitiesInitNUMA(caps) < 0)
goto cleanup; goto cleanup;
virSysfsSetSystemPath(NULL); virFileWrapperClearPrefixes();
if (!(capsXML = virCapabilitiesFormatXML(caps))) if (!(capsXML = virCapabilitiesFormatXML(caps)))
goto cleanup; goto cleanup;

View File

@ -8,12 +8,14 @@
#include "testutils.h" #include "testutils.h"
#include "internal.h" #include "internal.h"
#include "virhostcpupriv.h" #include "virhostcpupriv.h"
#include "virsysfspriv.h"
#include "virfile.h" #include "virfile.h"
#include "virstring.h" #include "virstring.h"
#include "virfilewrapper.h"
#define VIR_FROM_THIS VIR_FROM_NONE #define VIR_FROM_THIS VIR_FROM_NONE
#define SYSFS_SYSTEM_PATH "/sys/devices/system"
#if !(defined __linux__) #if !(defined __linux__)
int int
@ -178,9 +180,9 @@ linuxTestHostCPU(const void *opaque)
goto cleanup; goto cleanup;
} }
virSysfsSetSystemPath(sysfs_prefix); virFileWrapperAddPrefix(SYSFS_SYSTEM_PATH, sysfs_prefix);
result = linuxTestCompareFiles(cpuinfo, data->arch, output); result = linuxTestCompareFiles(cpuinfo, data->arch, output);
virSysfsSetSystemPath(NULL); virFileWrapperRemovePrefix(SYSFS_SYSTEM_PATH);
cleanup: cleanup:
VIR_FREE(cpuinfo); VIR_FREE(cpuinfo);

View File

@ -1,5 +1,5 @@
/* /*
* virnumamock.c: Mock some virNuma functions using virsysfs * virnumamock.c: Mock some virNuma functions using sysfs
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -24,10 +24,11 @@
#include "virfile.h" #include "virfile.h"
#include "viralloc.h" #include "viralloc.h"
#include "virstring.h" #include "virstring.h"
#include "virsysfspriv.h"
#define VIR_FROM_THIS VIR_FROM_NONE #define VIR_FROM_THIS VIR_FROM_NONE
#define SYSFS_SYSTEM_PATH "/sys/devices/system"
static int numa_avail = -1; static int numa_avail = -1;
@ -42,7 +43,7 @@ virNumaIsAvailable(void)
if (numa_avail < 0) { if (numa_avail < 0) {
char *sysfs_node_path = NULL; char *sysfs_node_path = NULL;
if (virAsprintfQuiet(&sysfs_node_path, "%s/node", virSysfsGetSystemPath()) < 0) if (virAsprintfQuiet(&sysfs_node_path, "%s/node", SYSFS_SYSTEM_PATH) < 0)
return false; return false;
numa_avail = virFileExists(sysfs_node_path); numa_avail = virFileExists(sysfs_node_path);
@ -68,7 +69,7 @@ virNumaGetMaxNode(void)
int ret = -1; int ret = -1;
virBitmapPtr map = NULL; virBitmapPtr map = NULL;
if (virSysfsGetValueBitmap("node/online", &map) < 0) if (virFileReadValueBitmap(&map, "%s/node/online", SYSFS_SYSTEM_PATH) < 0)
return -1; return -1;
ret = virBitmapLastSetBit(map); ret = virBitmapLastSetBit(map);
@ -82,7 +83,7 @@ virNumaNodeIsAvailable(int node)
bool ret = false; bool ret = false;
virBitmapPtr map = NULL; virBitmapPtr map = NULL;
if (virSysfsGetValueBitmap("node/online", &map) < 0) if (virFileReadValueBitmap(&map, "%s/node/online", SYSFS_SYSTEM_PATH) < 0)
return false; return false;
ret = virBitmapIsBitSet(map, node); ret = virBitmapIsBitSet(map, node);
@ -117,7 +118,7 @@ virNumaGetDistances(int node ATTRIBUTE_UNUSED,
} }
/* /*
* TODO: Adapt virNumaGetHugePageInfo{Path,Dir} to use virsysfs so that the * TODO: Adapt virNumaGetHugePageInfo{Path,Dir} to use sysfs so that the
* paths can be modified and this function can be thrown away and instead we'd * paths can be modified and this function can be thrown away and instead we'd
* have copied info from /sys (as we do with /sys/devices/system). * have copied info from /sys (as we do with /sys/devices/system).
*/ */
@ -177,7 +178,9 @@ virNumaGetNodeCPUs(int node, virBitmapPtr *cpus)
int ret = -1; int ret = -1;
char *cpulist = NULL; char *cpulist = NULL;
if (virSysfsGetNodeValueString(node, "cpulist", &cpulist) < 0) if (virFileReadValueString(&cpulist,
"%s/node/node%u/cpulist",
SYSFS_SYSTEM_PATH, node) < 0)
return -1; return -1;
*cpus = virBitmapParseUnlimited(cpulist); *cpus = virBitmapParseUnlimited(cpulist);