Despite its name, this is really just a general-purpose string
manipulation function, so it should be moved to the virstring
module and renamed accordingly.
In addition to the obvious s/File/String/, also tweak the name
to make it clear that the presence of the suffix is verified
using case-insensitive comparison.
A few trivial whitespace changes are squashed in.
Signed-off-by: Andrea Bolognani <abologna(a)redhat.com>
---
src/conf/virsecretobj.c | 2 +-
src/conf/virstorageobj.c | 2 +-
src/esx/esx_driver.c | 2 +-
src/esx/esx_storage_backend_vmfs.c | 4 ++--
src/libvirt_private.syms | 2 +-
src/util/virfile.c | 13 -------------
src/util/virfile.h | 3 ---
src/util/virstoragefile.c | 2 +-
src/util/virstring.c | 12 ++++++++++++
src/util/virstring.h | 3 +++
src/vmware/vmware_conf.c | 2 +-
src/vmx/vmx.c | 12 ++++++------
tests/testutils.c | 4 ++--
tests/virschematest.c | 4 ++--
tools/nss/libvirt_nss.c | 4 ++--
15 files changed, 35 insertions(+), 36 deletions(-)
diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c
index 8b418d5e66..5cd36b8a40 100644
--- a/src/conf/virsecretobj.c
+++ b/src/conf/virsecretobj.c
@@ -949,7 +949,7 @@ virSecretLoadAllConfigs(virSecretObjListPtr secrets,
char *path;
virSecretObjPtr obj;
- if (!virFileHasSuffix(de->d_name, ".xml"))
+ if (!virStringHasCaseSuffix(de->d_name, ".xml"))
continue;
if (!(path = virFileBuildPath(configDir, de->d_name, NULL)))
diff --git a/src/conf/virstorageobj.c b/src/conf/virstorageobj.c
index 2286857acf..68c89bd9a9 100644
--- a/src/conf/virstorageobj.c
+++ b/src/conf/virstorageobj.c
@@ -1719,7 +1719,7 @@ virStoragePoolObjLoadAllConfigs(virStoragePoolObjListPtr pools,
char *autostartLink;
virStoragePoolObjPtr obj;
- if (!virFileHasSuffix(entry->d_name, ".xml"))
+ if (!virStringHasCaseSuffix(entry->d_name, ".xml"))
continue;
if (!(path = virFileBuildPath(configDir, entry->d_name, NULL)))
diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
index 379c2bae73..afebd78566 100644
--- a/src/esx/esx_driver.c
+++ b/src/esx/esx_driver.c
@@ -3078,7 +3078,7 @@ esxDomainDefineXMLFlags(virConnectPtr conn, const char *xml,
unsigned int flags)
goto cleanup;
}
- if (! virFileHasSuffix(src, ".vmdk")) {
+ if (!virStringHasCaseSuffix(src, ".vmdk")) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Expecting source '%s' of first file-based harddisk
to "
"be a VMDK image"), src);
diff --git a/src/esx/esx_storage_backend_vmfs.c b/src/esx/esx_storage_backend_vmfs.c
index 6d6bd1a6ce..c56f887c25 100644
--- a/src/esx/esx_storage_backend_vmfs.c
+++ b/src/esx/esx_storage_backend_vmfs.c
@@ -884,7 +884,7 @@ esxStorageVolCreateXML(virStoragePoolPtr pool,
goto cleanup;
}
- if (! virFileHasSuffix(def->name, ".vmdk")) {
+ if (!virStringHasCaseSuffix(def->name, ".vmdk")) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Volume name '%s' has unsupported suffix, "
"expecting '.vmdk'"), def->name);
@@ -1104,7 +1104,7 @@ esxStorageVolCreateXMLFrom(virStoragePoolPtr pool,
goto cleanup;
}
- if (! virFileHasSuffix(def->name, ".vmdk")) {
+ if (!virStringHasCaseSuffix(def->name, ".vmdk")) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Volume name '%s' has unsupported suffix, "
"expecting '.vmdk'"), def->name);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 69643732e0..e4ed1def33 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1834,7 +1834,6 @@ virFileGetHugepageSize;
virFileGetMountReverseSubtree;
virFileGetMountSubtree;
virFileGetXAttr;
-virFileHasSuffix;
virFileInData;
virFileIsAbsPath;
virFileIsCDROM;
@@ -2958,6 +2957,7 @@ virStrdup;
virStringBufferIsPrintable;
virStringEncodeBase64;
virStringFilterChars;
+virStringHasCaseSuffix;
virStringHasChars;
virStringHasControlChars;
virStringIsEmpty;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index 9208523c47..21f7dc1ac3 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1543,19 +1543,6 @@ virFileMatchesNameSuffix(const char *file,
return 0;
}
-int
-virFileHasSuffix(const char *str,
- const char *suffix)
-{
- int len = strlen(str);
- int suffixlen = strlen(suffix);
-
- if (len < suffixlen)
- return 0;
-
- return STRCASEEQ(str + len - suffixlen, suffix);
-}
-
#define SAME_INODE(Stat_buf_1, Stat_buf_2) \
((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
&& (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
diff --git a/src/util/virfile.h b/src/util/virfile.h
index 0b946fe1ca..5a43d57181 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -166,9 +166,6 @@ int virFileMatchesNameSuffix(const char *file,
const char *name,
const char *suffix);
-int virFileHasSuffix(const char *str,
- const char *suffix);
-
int virFileStripSuffix(char *str,
const char *suffix) ATTRIBUTE_RETURN_CHECK;
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 233278e879..50ee9b574c 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -725,7 +725,7 @@ virStorageFileMatchesExtension(const char *extension,
if (extension == NULL)
return false;
- if (virFileHasSuffix(path, extension))
+ if (virStringHasCaseSuffix(path, extension))
return true;
return false;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 8a791f96d4..399b9468e6 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -1235,6 +1235,18 @@ virStringReplace(const char *haystack,
return virBufferContentAndReset(&buf);
}
+int
+virStringHasCaseSuffix(const char *str,
+ const char *suffix)
+{
+ int len = strlen(str);
+ int suffixlen = strlen(suffix);
+
+ if (len < suffixlen)
+ return 0;
+
+ return STRCASEEQ(str + len - suffixlen, suffix);
+}
/**
* virStringStripIPv6Brackets:
diff --git a/src/util/virstring.h b/src/util/virstring.h
index f2e72936c8..b2ba97a8d1 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -288,6 +288,9 @@ char *virStringReplace(const char *haystack,
const char *newneedle)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
+int virStringHasCaseSuffix(const char *str,
+ const char *suffix);
+
void virStringStripIPv6Brackets(char *str);
bool virStringHasChars(const char *str,
const char *chars);
diff --git a/src/vmware/vmware_conf.c b/src/vmware/vmware_conf.c
index 21559da4a4..0c1b1f9550 100644
--- a/src/vmware/vmware_conf.c
+++ b/src/vmware/vmware_conf.c
@@ -434,7 +434,7 @@ vmwareVmxPath(virDomainDefPtr vmdef, char **vmxPath)
if (vmwareParsePath(src, &directoryName, &fileName) < 0)
goto cleanup;
- if (!virFileHasSuffix(fileName, ".vmdk")) {
+ if (!virStringHasCaseSuffix(fileName, ".vmdk")) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Expecting source '%s' of first file-based harddisk
"
"to be a VMDK image"), src);
diff --git a/src/vmx/vmx.c b/src/vmx/vmx.c
index 75cdea9067..bbf8e55c3f 100644
--- a/src/vmx/vmx.c
+++ b/src/vmx/vmx.c
@@ -1600,7 +1600,7 @@ virVMXParseConfig(virVMXContext *ctx,
if (virVMXGetConfigString(conf, "guestOS", &guestOS, true) < 0)
goto cleanup;
- if (guestOS != NULL && virFileHasSuffix(guestOS, "-64")) {
+ if (guestOS != NULL && virStringHasCaseSuffix(guestOS, "-64")) {
def->os.arch = VIR_ARCH_X86_64;
} else {
def->os.arch = VIR_ARCH_I686;
@@ -2218,7 +2218,7 @@ virVMXParseDisk(virVMXContext *ctx, virDomainXMLOptionPtr xmlopt,
virConfPtr con
/* Setup virDomainDiskDef */
if (device == VIR_DOMAIN_DISK_DEVICE_DISK) {
- if (virFileHasSuffix(fileName, ".vmdk")) {
+ if (virStringHasCaseSuffix(fileName, ".vmdk")) {
char *tmp;
if (deviceType != NULL) {
@@ -2254,7 +2254,7 @@ virVMXParseDisk(virVMXContext *ctx, virDomainXMLOptionPtr xmlopt,
virConfPtr con
if (mode)
(*def)->transient = STRCASEEQ(mode,
"independent-nonpersistent");
- } else if (virFileHasSuffix(fileName, ".iso") ||
+ } else if (virStringHasCaseSuffix(fileName, ".iso") ||
STREQ(fileName, "emptyBackingString") ||
(deviceType &&
(STRCASEEQ(deviceType, "atapi-cdrom") ||
@@ -2277,7 +2277,7 @@ virVMXParseDisk(virVMXContext *ctx, virDomainXMLOptionPtr xmlopt,
virConfPtr con
goto cleanup;
}
} else if (device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
- if (virFileHasSuffix(fileName, ".iso")) {
+ if (virStringHasCaseSuffix(fileName, ".iso")) {
char *tmp;
if (deviceType && STRCASENEQ(deviceType, "cdrom-image")) {
@@ -2295,7 +2295,7 @@ virVMXParseDisk(virVMXContext *ctx, virDomainXMLOptionPtr xmlopt,
virConfPtr con
goto cleanup;
}
VIR_FREE(tmp);
- } else if (virFileHasSuffix(fileName, ".vmdk")) {
+ } else if (virStringHasCaseSuffix(fileName, ".vmdk")) {
/*
* This function was called in order to parse a CDROM device, but
* .vmdk files are for harddisk devices only. Just ignore it,
@@ -3585,7 +3585,7 @@ virVMXFormatDisk(virVMXContext *ctx, virDomainDiskDefPtr def,
const char *src = virDomainDiskGetSource(def);
if (src) {
- if (!virFileHasSuffix(src, fileExt)) {
+ if (!virStringHasCaseSuffix(src, fileExt)) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Image file for %s %s '%s' has "
"unsupported suffix, expecting
'%s'"),
diff --git a/tests/testutils.c b/tests/testutils.c
index 13bb9630df..cfdf122ae0 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -525,8 +525,8 @@ virTestRewrapFile(const char *filename)
char *script = NULL;
virCommandPtr cmd = NULL;
- if (!(virFileHasSuffix(filename, ".args") ||
- virFileHasSuffix(filename, ".ldargs")))
+ if (!(virStringHasCaseSuffix(filename, ".args") ||
+ virStringHasCaseSuffix(filename, ".ldargs")))
return 0;
if (!perl) {
diff --git a/tests/virschematest.c b/tests/virschematest.c
index d1bcdeac9c..46c67760a7 100644
--- a/tests/virschematest.c
+++ b/tests/virschematest.c
@@ -41,7 +41,7 @@ static int
testSchemaFile(const void *args)
{
const struct testSchemaData *data = args;
- bool shouldFail = virFileHasSuffix(data->xml_path, "-invalid.xml");
+ bool shouldFail = virStringHasCaseSuffix(data->xml_path,
"-invalid.xml");
xmlDocPtr xml = NULL;
int ret = -1;
@@ -82,7 +82,7 @@ testSchemaDir(const char *schema,
return -1;
while ((rc = virDirRead(dir, &ent, dir_path)) > 0) {
- if (!virFileHasSuffix(ent->d_name, ".xml"))
+ if (!virStringHasCaseSuffix(ent->d_name, ".xml"))
continue;
if (ent->d_name[0] == '.')
continue;
diff --git a/tools/nss/libvirt_nss.c b/tools/nss/libvirt_nss.c
index 46d051c08c..4d86f8e6ce 100644
--- a/tools/nss/libvirt_nss.c
+++ b/tools/nss/libvirt_nss.c
@@ -274,7 +274,7 @@ findLease(const char *name,
while ((ret = virDirRead(dir, &entry, leaseDir)) > 0) {
char *path;
- if (virFileHasSuffix(entry->d_name, ".status")) {
+ if (virStringHasCaseSuffix(entry->d_name, ".status")) {
if (!(path = virFileBuildPath(leaseDir, entry->d_name, NULL)))
goto cleanup;
@@ -285,7 +285,7 @@ findLease(const char *name,
goto cleanup;
}
VIR_FREE(path);
- } else if (virFileHasSuffix(entry->d_name, ".macs")) {
+ } else if (virStringHasCaseSuffix(entry->d_name, ".macs")) {
if (!(path = virFileBuildPath(leaseDir, entry->d_name, NULL)))
goto cleanup;
--
2.20.1