]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/blkdev: set errno in more cases
authorSamanta Navarro <ferivoz@riseup.net>
Fri, 13 Jan 2023 11:51:52 +0000 (11:51 +0000)
committerSamanta Navarro <ferivoz@riseup.net>
Mon, 16 Jan 2023 12:05:24 +0000 (12:05 +0000)
Applications evaluate errno in case of a -1 return value. Examples
within util-linux are fsck.cramfs and mkfs.minix. Set errno more often
to offer reasonable error codes to the application and user.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
lib/blkdev.c

index 954de454e6d6f0ff79df5003580f0c5292414d7e..757f3e818b0283af9a493971d804b256709463de 100644 (file)
@@ -8,6 +8,7 @@
 #include <sys/stat.h>
 #include <sys/file.h>
 #include <sys/ioctl.h>
+#include <errno.h>
 #include <unistd.h>
 #include <stdint.h>
 
@@ -60,8 +61,10 @@ blkdev_find_size (int fd) {
        uintmax_t high, low = 0;
 
        for (high = 1024; blkdev_valid_offset (fd, high); ) {
-               if (high == UINTMAX_MAX)
+               if (high == UINTMAX_MAX) {
+                       errno = EFBIG;
                        return -1;
+               }
 
                low = high;
 
@@ -167,8 +170,10 @@ blkdev_get_size(int fd, unsigned long long *bytes)
                        *bytes = st.st_size;
                        return 0;
                }
-               if (!S_ISBLK(st.st_mode))
+               if (!S_ISBLK(st.st_mode)) {
+                       errno = ENOTBLK;
                        return -1;
+               }
        }
 
        *bytes = blkdev_find_size(fd);