diff --git a/src/lib/probe.c b/src/lib/probe.c index 6025d97..01ac26a 100644 --- a/src/lib/probe.c +++ b/src/lib/probe.c @@ -24,20 +24,9 @@ #include "blkid_stub.h" #include "log.h" #include "probe.h" +#include "system_stub.h" #include "util.h" -/** - * Factory function to convert a dev_t to the full device path - */ -static char *cbm_devnode_to_devpath(dev_t dev) -{ - autofree(char) *c = NULL; - if (asprintf(&c, "/dev/block/%u:%u", major(dev), minor(dev)) < 0) { - return NULL; - } - return realpath(c, NULL); -} - /** * Convert a sysfs dev file to the target device path */ @@ -65,7 +54,7 @@ static char *cbm_dev_file_to_devpath(const char *devfile) return NULL; } - return cbm_devnode_to_devpath(makedev(dev_major, dev_minor)); + return cbm_system_devnode_to_devpath(makedev(dev_major, dev_minor)); } static char *cbm_get_luks_uuid(const char *part) @@ -150,13 +139,9 @@ CbmDeviceProbe *cbm_probe_path(const char *path) LOG_ERROR("Path does not exist: %s", path); return NULL; } - if (major(st.st_dev) == 0) { - LOG_ERROR("Invalid block device: %s", path); - return NULL; - } probe.dev = st.st_dev; - devnode = cbm_devnode_to_devpath(probe.dev); + devnode = cbm_system_devnode_to_devpath(probe.dev); if (!devnode) { DECLARE_OOM(); return NULL; diff --git a/src/lib/system_stub.c b/src/lib/system_stub.c index f349d2d..bfbb795 100644 --- a/src/lib/system_stub.c +++ b/src/lib/system_stub.c @@ -16,16 +16,37 @@ #include #include "files.h" +#include "log.h" + +/** + * Factory function to convert a dev_t to the full device path + * This is the default internal function + */ +static char *cbm_devnode_to_devpath(dev_t dev) +{ + if (major(dev) == 0) { + LOG_ERROR("Invalid block device: %u:%u", major(dev), minor(dev)); + return NULL; + } + + autofree(char) *c = NULL; + if (asprintf(&c, "/dev/block/%u:%u", major(dev), minor(dev)) < 0) { + return NULL; + } + return realpath(c, NULL); +} /** * Default vtable for system call passthrough */ -static CbmSystemOps default_system_ops = {.mount = mount, - .umount = umount, - .system = system, - .is_mounted = cbm_is_mounted, - .get_mountpoint_for_device = - cbm_get_mountpoint_for_device }; +static CbmSystemOps default_system_ops = { + .mount = mount, + .umount = umount, + .system = system, + .is_mounted = cbm_is_mounted, + .get_mountpoint_for_device = cbm_get_mountpoint_for_device, + .devnode_to_devpath = cbm_devnode_to_devpath, +}; /** * Pointer to the currently active vtable @@ -50,6 +71,7 @@ void cbm_system_set_vtable(CbmSystemOps *ops) assert(system_ops->is_mounted != NULL); assert(system_ops->get_mountpoint_for_device != NULL); assert(system_ops->system != NULL); + assert(system_ops->devnode_to_devpath != NULL); } int cbm_system_mount(const char *source, const char *target, const char *filesystemtype, @@ -78,6 +100,11 @@ char *cbm_system_get_mountpoint_for_device(const char *device) return system_ops->get_mountpoint_for_device(device); } +char *cbm_system_devnode_to_devpath(dev_t d) +{ + return system_ops->devnode_to_devpath(d); +} + /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/src/lib/system_stub.h b/src/lib/system_stub.h index 3550ca8..558abab 100644 --- a/src/lib/system_stub.h +++ b/src/lib/system_stub.h @@ -14,6 +14,7 @@ #define _GNU_SOURCE #include +#include /** * Defines the vtable used for all systen operations within clr-boot-manager. @@ -32,6 +33,9 @@ typedef struct CbmSystemOps { /* exec family */ int (*system)(const char *command); + + /* dev utility */ + char *(*devnode_to_devpath)(dev_t t); } CbmSystemOps; /** @@ -74,6 +78,11 @@ int cbm_system_umount(const char *target); */ int cbm_system_system(const char *command); +/** + * Resolve the path for a given dev_t + */ +char *cbm_system_devnode_to_devpath(dev_t d); + /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/tests/system-harness.h b/tests/system-harness.h index c5e72d7..23bc4d7 100644 --- a/tests/system-harness.h +++ b/tests/system-harness.h @@ -41,6 +41,15 @@ static inline char *test_get_mountpoint_for_device(__cbm_unused__ const char *de return NULL; } +static inline char *test_devnode_to_devpath(__cbm_unused__ dev_t d) +{ + char *a = NULL; + if (asprintf(&a, "%s/dev/testRoot", TOP_BUILD_DIR "/tests/update_playground") < 0) { + abort(); + } + return a; +} + /** * Default vtable for testing. Copy into a local struct and override specific * fields. @@ -49,7 +58,8 @@ CbmSystemOps SystemTestOps = {.mount = test_mount, .umount = test_umount, .system = test_system, .is_mounted = test_is_mounted, - .get_mountpoint_for_device = test_get_mountpoint_for_device }; + .get_mountpoint_for_device = test_get_mountpoint_for_device, + .devnode_to_devpath = test_devnode_to_devpath }; /* * Editor modelines - https://www.wireshark.org/tools/modelines.html