stat-util: also introduce stat_verify_block() and use it everywhere

This commit is contained in:
Lennart Poettering
2026-04-13 13:20:43 +02:00
parent 7f2410deb1
commit bda7e91337
12 changed files with 68 additions and 37 deletions
+9 -6
View File
@@ -378,9 +378,9 @@ static int loop_configure(
}
static int fd_get_max_discard(int fd, uint64_t *ret) {
struct stat st;
char sysfs_path[STRLEN("/sys/dev/block/" ":" "/queue/discard_max_bytes") + DECIMAL_STR_MAX(dev_t) * 2 + 1];
_cleanup_free_ char *buffer = NULL;
struct stat st;
int r;
assert(ret);
@@ -388,8 +388,9 @@ static int fd_get_max_discard(int fd, uint64_t *ret) {
if (fstat(ASSERT_FD(fd), &st) < 0)
return -errno;
if (!S_ISBLK(st.st_mode))
return -ENOTBLK;
r = stat_verify_block(&st);
if (r < 0)
return r;
xsprintf(sysfs_path, "/sys/dev/block/" DEVNUM_FORMAT_STR "/queue/discard_max_bytes", DEVNUM_FORMAT_VAL(st.st_rdev));
@@ -401,14 +402,16 @@ static int fd_get_max_discard(int fd, uint64_t *ret) {
}
static int fd_set_max_discard(int fd, uint64_t max_discard) {
struct stat st;
char sysfs_path[STRLEN("/sys/dev/block/" ":" "/queue/discard_max_bytes") + DECIMAL_STR_MAX(dev_t) * 2 + 1];
struct stat st;
int r;
if (fstat(ASSERT_FD(fd), &st) < 0)
return -errno;
if (!S_ISBLK(st.st_mode))
return -ENOTBLK;
r = stat_verify_block(&st);
if (r < 0)
return r;
xsprintf(sysfs_path, "/sys/dev/block/" DEVNUM_FORMAT_STR "/queue/discard_max_bytes", DEVNUM_FORMAT_VAL(st.st_rdev));