From: Luca Boccassi Date: Wed, 23 Mar 2022 21:29:04 +0000 (+0000) Subject: portable: allow reattaching when one image has a version and the other does not X-Git-Tag: v251-rc1~13 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2350712e32c02d77cbae64d362b6614b19542993;p=thirdparty%2Fsystemd.git portable: allow reattaching when one image has a version and the other does not A reattach might go from img.raw to img_0.1.raw or viceversa, but this is not allowed right now as we try to match the full name. Also take into account that running strcspn(a, '/') on an image name, without leading path, will return the length of the full string, but the versions might be different so they won't match, eg: img_0.1.raw -> 12 img_0.1.1.raw -> 14 So adjust the check to take that into account, and skip it if we are not dealing with directories --- diff --git a/src/portable/portable.c b/src/portable/portable.c index 3051760d0b9..155f731936a 100644 --- a/src/portable/portable.c +++ b/src/portable/portable.c @@ -1429,17 +1429,28 @@ static bool marker_matches_images(const char *marker, const char *name_or_path, size_t l; /* We shall match against a path. Let's ignore any prefix here though, as often there are many ways to - * reach the same file. However, in this mode, let's validate any file suffix. */ + * reach the same file. However, in this mode, let's validate any file suffix. + * But also ensure that we don't fail if both components don't have a '/' at all + * (strcspn returns the full length of the string in that case, which might not + * match as the versions might differ). */ l = strcspn(a, "/"); b = last_path_component(*image_name_or_path); - if (strcspn(b, "/") != l) + if ((a[l] != '/') != !strchr(b, '/')) /* One is a directory, the other is not */ + return false; + + if (a[l] != 0 && strcspn(b, "/") != l) return false; underscore = strchr(b, '_'); if (underscore) l = underscore - b; + else { /* Either component could be versioned */ + underscore = strchr(a, '_'); + if (underscore) + l = underscore - a; + } if (!strneq(a, b, l)) return false; diff --git a/test/units/testsuite-29.sh b/test/units/testsuite-29.sh index c4e31b0a2d5..47fd31b9b3a 100755 --- a/test/units/testsuite-29.sh +++ b/test/units/testsuite-29.sh @@ -88,6 +88,14 @@ systemctl is-active app1.service status="$(portablectl is-attached --extension app1 minimal_0)" [[ "${status}" == "running-runtime" ]] +# Ensure that adding or removing a version to the image doesn't break reattaching +cp /usr/share/app1.raw /tmp/app1_2.raw +portablectl "${ARGS[@]}" reattach --now --runtime --extension /tmp/app1_2.raw /usr/share/minimal_1.raw app1 + +systemctl is-active app1.service +status="$(portablectl is-attached --extension app1_2 minimal_1)" +[[ "${status}" == "running-runtime" ]] + portablectl "${ARGS[@]}" reattach --now --runtime --extension /usr/share/app1.raw /usr/share/minimal_1.raw app1 systemctl is-active app1.service