[PATCH 1/2] src/ext4_resize.c: Refactor code and ensure accurate errno is returned

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxx>
---
 src/ext4_resize.c | 46 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 32 insertions(+), 14 deletions(-)

diff --git a/src/ext4_resize.c b/src/ext4_resize.c
index 39e16529..78b66432 100644
--- a/src/ext4_resize.c
+++ b/src/ext4_resize.c
@@ -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;
 }
-- 
2.27.0




[Index of Archives]     [Reiser Filesystem Development]     [Ceph FS]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Linux FS]     [Yosemite National Park]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Device Mapper]     [Linux Media]

  Powered by Linux