X Tutup
Skip to content

Commit bcf8fc2

Browse files
committed
blockdev-util: add fd-based APIs for getting backing block device for file
1 parent 1053967 commit bcf8fc2

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/shared/blockdev-util.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,39 @@ int block_get_originating(dev_t dt, dev_t *ret) {
182182
return 1;
183183
}
184184

185-
int get_block_device_harder(const char *path, dev_t *ret) {
185+
int get_block_device_harder_fd(int fd, dev_t *ret) {
186186
int r;
187187

188-
assert(path);
188+
assert(fd >= 0);
189189
assert(ret);
190190

191191
/* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its
192192
* immediate parent, if there is one. */
193193

194-
r = get_block_device(path, ret);
194+
r = get_block_device_fd(fd, ret);
195195
if (r <= 0)
196196
return r;
197197

198198
r = block_get_originating(*ret, ret);
199199
if (r < 0)
200-
log_debug_errno(r, "Failed to chase block device '%s', ignoring: %m", path);
200+
log_debug_errno(r, "Failed to chase block device, ignoring: %m");
201201

202202
return 1;
203203
}
204204

205+
int get_block_device_harder(const char *path, dev_t *ret) {
206+
_cleanup_close_ int fd = -1;
207+
208+
assert(path);
209+
assert(ret);
210+
211+
fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC);
212+
if (fd < 0)
213+
return -errno;
214+
215+
return get_block_device_harder_fd(fd, ret);
216+
}
217+
205218
int lock_whole_block_device(dev_t devt, int operation) {
206219
_cleanup_free_ char *whole_node = NULL;
207220
_cleanup_close_ int lock_fd = -1;

src/shared/blockdev-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ int block_get_originating(dev_t d, dev_t *ret);
1818
int get_block_device_fd(int fd, dev_t *ret);
1919
int get_block_device(const char *path, dev_t *dev);
2020

21+
int get_block_device_harder_fd(int fd, dev_t *dev);
2122
int get_block_device_harder(const char *path, dev_t *dev);
2223

2324
int lock_whole_block_device(dev_t devt, int operation);

0 commit comments

Comments
 (0)
X Tutup