Skip to content

Commit 48af03b

Browse files
committed
Refactor: libcrmcommon: Use pcmk__compare_versions() for transforms
Seems simpler, even if it's heavier-weight and more than what we need for this particular use. Why reinvent the wheel? Signed-off-by: Reid Wahl <[email protected]>
1 parent 42933bf commit 48af03b

File tree

1 file changed

+8
-33
lines changed

1 file changed

+8
-33
lines changed

lib/common/schemas.c

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,6 @@ transform_filter(const struct dirent *entry)
326326
* \internal
327327
* \brief Compare transform files based on the version strings in their names
328328
*
329-
* This is a crude version comparison that relies on the specific structure of
330-
* these filenames.
331-
*
332329
* \retval -1 if \p entry1 sorts before \p entry2
333330
* \retval 0 if \p entry1 sorts equal to \p entry2
334331
* \retval 1 if \p entry1 sorts after \p entry2
@@ -339,38 +336,16 @@ transform_filter(const struct dirent *entry)
339336
static int
340337
compare_transforms(const struct dirent **entry1, const struct dirent **entry2)
341338
{
342-
unsigned char major1 = 0;
343-
unsigned char major2 = 0;
344-
unsigned char minor1 = 0;
345-
unsigned char minor2 = 0;
346-
unsigned char order1 = 0;
347-
unsigned char order2 = 0;
348-
349-
// If these made it through the filter, they should be of the right format
350-
CRM_LOG_ASSERT(sscanf((*entry1)->d_name, "upgrade-%hhu.%hhu-%hhu.xsl",
351-
&major1, &minor1, &order1) == 3);
352-
CRM_LOG_ASSERT(sscanf((*entry2)->d_name, "upgrade-%hhu.%hhu-%hhu.xsl",
353-
&major2, &minor2, &order2) == 3);
354-
355-
if (major1 < major2) {
356-
return -1;
357-
} else if (major1 > major2) {
358-
return 1;
359-
}
339+
// We already validated the format of each filename in transform_filter()
340+
static const size_t offset = sizeof("upgrade-") - 1;
360341

361-
if (minor1 < minor2) {
362-
return -1;
363-
} else if (minor1 > minor2) {
364-
return 1;
365-
}
342+
gchar *ver1 = g_strdelimit(g_strdup((*entry1)->d_name + offset), "-", '.');
343+
gchar *ver2 = g_strdelimit(g_strdup((*entry2)->d_name + offset), "-", '.');
344+
int rc = pcmk__compare_versions(ver1, ver2);
366345

367-
if (order1 < order2) {
368-
return -1;
369-
} else if (order1 > order2) {
370-
return 1;
371-
}
372-
373-
return 0;
346+
g_free(ver1);
347+
g_free(ver2);
348+
return rc;
374349
}
375350

376351
/*!

0 commit comments

Comments
 (0)