From: Lennart Poettering Date: Mon, 8 Mar 2021 22:48:21 +0000 (+0100) Subject: blockdev-util: add fd-based APIs for getting backing block device for file X-Git-Tag: v250-rc1~975 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bcf8fc267f90cede1ab9206624bc0cc783b39565;p=thirdparty%2Fsystemd.git blockdev-util: add fd-based APIs for getting backing block device for file --- diff --git a/src/shared/blockdev-util.c b/src/shared/blockdev-util.c index 53df7d2f0d2..db50505c9ae 100644 --- a/src/shared/blockdev-util.c +++ b/src/shared/blockdev-util.c @@ -182,26 +182,39 @@ int block_get_originating(dev_t dt, dev_t *ret) { return 1; } -int get_block_device_harder(const char *path, dev_t *ret) { +int get_block_device_harder_fd(int fd, dev_t *ret) { int r; - assert(path); + assert(fd >= 0); assert(ret); /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its * immediate parent, if there is one. */ - r = get_block_device(path, ret); + r = get_block_device_fd(fd, ret); if (r <= 0) return r; r = block_get_originating(*ret, ret); if (r < 0) - log_debug_errno(r, "Failed to chase block device '%s', ignoring: %m", path); + log_debug_errno(r, "Failed to chase block device, ignoring: %m"); return 1; } +int get_block_device_harder(const char *path, dev_t *ret) { + _cleanup_close_ int fd = -1; + + assert(path); + assert(ret); + + fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC); + if (fd < 0) + return -errno; + + return get_block_device_harder_fd(fd, ret); +} + int lock_whole_block_device(dev_t devt, int operation) { _cleanup_free_ char *whole_node = NULL; _cleanup_close_ int lock_fd = -1; diff --git a/src/shared/blockdev-util.h b/src/shared/blockdev-util.h index d36707cf632..05501f26570 100644 --- a/src/shared/blockdev-util.h +++ b/src/shared/blockdev-util.h @@ -18,6 +18,7 @@ int block_get_originating(dev_t d, dev_t *ret); int get_block_device_fd(int fd, dev_t *ret); int get_block_device(const char *path, dev_t *dev); +int get_block_device_harder_fd(int fd, dev_t *dev); int get_block_device_harder(const char *path, dev_t *dev); int lock_whole_block_device(dev_t devt, int operation);