util: Rename virFileHasSuffix() to virStringHasCaseSuffix()

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@redhat.com>
ACKed-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
Andrea Bolognani 2019-03-06 15:07:26 +01:00
parent ab2e90006d
commit 2de7dcba7e
15 changed files with 35 additions and 36 deletions

View File

@ -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)))

View File

@ -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)))

View File

@ -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);

View File

@ -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);

View File

@ -1842,7 +1842,6 @@ virFileGetHugepageSize;
virFileGetMountReverseSubtree;
virFileGetMountSubtree;
virFileGetXAttr;
virFileHasSuffix;
virFileInData;
virFileIsAbsPath;
virFileIsCDROM;
@ -2966,6 +2965,7 @@ virStrdup;
virStringBufferIsPrintable;
virStringEncodeBase64;
virStringFilterChars;
virStringHasCaseSuffix;
virStringHasChars;
virStringHasControlChars;
virStringIsEmpty;

View File

@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -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:

View File

@ -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);

View File

@ -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);

View File

@ -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'"),

View File

@ -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) {

View File

@ -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;

View File

@ -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;