]> www.infradead.org Git - users/hch/xfstests-dev.git/commitdiff
src/ext4_resize.c: Refactor code and ensure accurate errno is returned
authorOjaswin Mujoo <ojaswin@linux.ibm.com>
Tue, 22 Feb 2022 17:50:52 +0000 (23:20 +0530)
committerEryu Guan <guaneryu@gmail.com>
Sun, 10 Apr 2022 10:28:21 +0000 (18:28 +0800)
The current implementation of ext4_resize returned 1 whenever
there was an error. Modify this to return the correct error code.
This is important for tests that rely on correct error reporting, by
the kernel, for ext4 resize functionality.

Additionaly, perform some code cleanup.

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
Reviewed-by: Ritesh Harjani <riteshh@linux.ibm.com>
Signed-off-by: Eryu Guan <guaneryu@gmail.com>
src/ext4_resize.c

index 39e1652942e50a16aeabb68aa4a6bc50fa5af859..78b6643288f95cf781d20fd3ff11fff3dba7b6c6 100644 (file)
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/ioctl.h>
 #include <sys/mount.h>
 
@@ -19,33 +20,50 @@ typedef unsigned long long __u64;
 #define EXT4_IOC_RESIZE_FS             _IOW('f', 16, __u64)
 #endif
 
+#define pr_error(fmt, ...) do { \
+       fprintf (stderr, "ext4_resize.c: " fmt, ##__VA_ARGS__); \
+} while (0)
+
+static void usage(void)
+{
+       fprintf(stdout, "\nUsage: ext4_resize [mnt_path] [new_size(blocks)]\n");
+}
+
 int main(int argc, char **argv)
 {
        __u64   new_size;
        int     error, fd;
-       char    *tmp = NULL;
+       char    *mnt_dir = NULL, *tmp = NULL;
 
        if (argc != 3) {
-               fputs("insufficient arguments\n", stderr);
-               return 1;
-       }
-       fd = open(argv[1], O_RDONLY);
-       if (!fd) {
-               perror(argv[1]);
-               return 1;
+               error = EINVAL;
+               pr_error("insufficient arguments\n");
+               usage();
+               return error;
        }
 
+       mnt_dir = argv[1];
+
        errno = 0;
        new_size = strtoull(argv[2], &tmp, 10);
        if ((errno) || (*tmp != '\0')) {
-               fprintf(stderr, "%s: invalid new size\n", argv[0]);
-               return 1;
+               error = errno;
+               pr_error("invalid new size\n");
+               return error;
+       }
+
+       fd = open(mnt_dir, O_RDONLY);
+       if (fd < 0) {
+               error = errno;
+               pr_error("open() failed with error: %s\n", strerror(error));
+               return error;
        }
 
-       error = ioctl(fd, EXT4_IOC_RESIZE_FS, &new_size);
-       if (error < 0) {
-               perror("EXT4_IOC_RESIZE_FS");
-               return 1;
+       if(ioctl(fd, EXT4_IOC_RESIZE_FS, &new_size) < 0) {
+               error = errno;
+               pr_error("EXT4_IOC_RESIZE_FS ioctl() failed with error: %s\n", strerror(error));
+               return error;
        }
+
        return 0;
 }