From 6f0c327f3a279b254e5270cff8c401612901811d Mon Sep 17 00:00:00 2001 From: "maia x." Date: Sat, 5 Oct 2024 14:27:32 -0700 Subject: [PATCH] vpick: add path_uses_vpick helper method Add a path_uses_vpick helper method to determine if a path matches the vpick format ('PATH/NAME.SUFFIX.v' or 'PATH.v/NAME___.SUFFIX'). --- src/shared/vpick.c | 35 +++++++++++++++++++++++++++++++++++ src/shared/vpick.h | 2 ++ src/test/test-vpick.c | 23 +++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/src/shared/vpick.c b/src/shared/vpick.c index 4720e7448d8..04b5110c667 100644 --- a/src/shared/vpick.c +++ b/src/shared/vpick.c @@ -681,6 +681,41 @@ int path_pick_update_warn( return 1; } +int path_uses_vpick(const char *path) { + _cleanup_free_ char *dir = NULL, *parent = NULL, *fname = NULL; + int r; + + assert(path); + + r = path_extract_filename(path, &fname); + if (r == -EADDRNOTAVAIL) + return 0; + if (r < 0) + return r; + + /* ...PATH/NAME.SUFFIX.v */ + if (endswith(fname, ".v")) + return 1; + + /* ...PATH.v/NAME___.SUFFIX */ + if (!strrstr(fname, "___")) + return 0; + + r = path_extract_directory(path, &dir); + if (IN_SET(r, -EDESTADDRREQ, -EADDRNOTAVAIL)) /* only filename specified (no dir), or root or "." */ + return 0; + if (r < 0) + return r; + + r = path_extract_filename(dir, &parent); + if (r == -EADDRNOTAVAIL) + return 0; + if (r < 0) + return r; + + return !!endswith(parent, ".v"); +} + const PickFilter pick_filter_image_raw = { .type_mask = (UINT32_C(1) << DT_REG) | (UINT32_C(1) << DT_BLK), .architecture = _ARCHITECTURE_INVALID, diff --git a/src/shared/vpick.h b/src/shared/vpick.h index 38251c84e85..3093f02cb05 100644 --- a/src/shared/vpick.h +++ b/src/shared/vpick.h @@ -56,6 +56,8 @@ int path_pick_update_warn( PickFlags flags, PickResult *ret); +int path_uses_vpick(const char *path); + extern const PickFilter pick_filter_image_raw; extern const PickFilter pick_filter_image_dir; extern const PickFilter pick_filter_image_any; diff --git a/src/test/test-vpick.c b/src/test/test-vpick.c index 88646ec053e..3a91a8973d9 100644 --- a/src/test/test-vpick.c +++ b/src/test/test-vpick.c @@ -168,4 +168,27 @@ TEST(path_pick) { assert_se(result.architecture == ARCHITECTURE_S390); } +TEST(path_uses_vpick) { + assert_se(path_uses_vpick("foo.v") > 0); + assert_se(path_uses_vpick("path/to/foo.v") > 0); + assert_se(path_uses_vpick("./path/to/foo.v") > 0); + assert_se(path_uses_vpick("path/to.v/foo.v") > 0); + assert_se(path_uses_vpick("path/to/foo.raw.v") > 0); + assert_se(path_uses_vpick("/var/lib/machines/mymachine.raw.v/") > 0); + assert_se(path_uses_vpick("path/to.v/foo___.hi/a.v") > 0); + assert_se(!path_uses_vpick("path/to/foo.mp4.vtt")); + assert_se(!path_uses_vpick("path/to/foo.mp4.v.1")); + assert_se(!path_uses_vpick("path/to.v/a")); + + assert_se(path_uses_vpick("to.v/foo___.raw") > 0); + assert_se(path_uses_vpick("path/to.v/foo___.raw") > 0); + assert_se(!path_uses_vpick("path/to/foo___.raw")); + assert_se(!path_uses_vpick("path/to.v/foo__")); + assert_se(!path_uses_vpick("foo___.raw")); + + assert_se(path_uses_vpick("/") < 1); + assert_se(path_uses_vpick(".") < 1); + assert_se(path_uses_vpick("") < 1); +} + DEFINE_TEST_MAIN(LOG_DEBUG);