Compare commits
2 Commits
e89a00c6bd
...
a539bec5f4
Author | SHA1 | Date |
---|---|---|
Maanya Goenka | a539bec5f4 | |
Maanya Goenka | f3a9cb423e |
|
@ -2210,10 +2210,8 @@ static int vl_method_refresh(sd_varlink *link, sd_json_variant *parameters, sd_v
|
|||
}
|
||||
|
||||
static int parse_version(const char *filename, char **ret) {
|
||||
const char *start, *ext_suffix;
|
||||
const char *start, *last_dot_after_underscore;
|
||||
char *version = NULL;
|
||||
/* We are only looking for extension images here, because for directories, we want a no-op*/
|
||||
static const char *valid_extensions[] = {".confext.raw", ".sysext.raw", ".raw"};
|
||||
|
||||
assert(filename);
|
||||
assert(ret);
|
||||
|
@ -2231,37 +2229,41 @@ static int parse_version(const char *filename, char **ret) {
|
|||
}
|
||||
start ++;
|
||||
|
||||
/* Find the extension suffix, if a valid one is associated with it */
|
||||
for (const char **ext = valid_extensions; *ext; ++ext) {
|
||||
ext_suffix = strstr(start, *ext);
|
||||
if (ext_suffix)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!ext_suffix) {
|
||||
/* If no valid extension filename suffix is found, assign "none" and return success. */
|
||||
version = strdup("none");
|
||||
last_dot_after_underscore = strrchr(start, '.');
|
||||
if (!last_dot_after_underscore || (*(last_dot_after_underscore + 1) == '\0')) {
|
||||
/* If no dot found or if dot is the last character, treat everything
|
||||
/ after '_' as the version */
|
||||
version = strdup(start);
|
||||
if (!version)
|
||||
return -ENOMEM;
|
||||
*ret = version;
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t version_length = ext_suffix - start;
|
||||
if (version_length == 0) {
|
||||
version = strdup("none");
|
||||
if (last_dot_after_underscore[1] && isdigit(last_dot_after_underscore[1])) {
|
||||
/* If the last dot is followed by a digit, the version is everything after the underscore
|
||||
* This is for cases where the extension name is like so: image_0.1 */
|
||||
version = strdup(start);
|
||||
if (!version)
|
||||
return -ENOMEM;
|
||||
*ret = version;
|
||||
} else if (last_dot_after_underscore[1] && isalpha(last_dot_after_underscore[1])) {
|
||||
/* If the last dot is followed by a letter, the version is between the first '_' and the last dot
|
||||
* This is for cases where the extension name is like so: image_0.1.raw (version is 0.1)
|
||||
* or image_1.raw (version is 1) */
|
||||
size_t length = last_dot_after_underscore - start;
|
||||
version = strndup(start, length);
|
||||
if (!version)
|
||||
return -ENOMEM;
|
||||
*ret = version;
|
||||
} else {
|
||||
/* In all other cases assign "n/a" to version */
|
||||
version = strdup("n/a");
|
||||
if (!version)
|
||||
return -ENOMEM;
|
||||
*ret = version;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Extract the version substring */
|
||||
version = strndup(start, version_length);
|
||||
if (!version)
|
||||
return -ENOMEM;
|
||||
*ret = version;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue