summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorOjaswin Mujoo <ojaswin@linux.ibm.com>2022-02-22 23:20:52 +0530
committerEryu Guan <guaneryu@gmail.com>2022-04-10 18:28:21 +0800
commit50490c851139599d18a60897ffd32e8dc16e1328 (patch)
tree19d2850e0bbbf5f529b5c9334a79e06484ea7049 /src
parent6c28307640211b57d4667ba8aafdaa686069cc46 (diff)
src/ext4_resize.c: Refactor code and ensure accurate errno is returned
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>
Diffstat (limited to 'src')
-rw-r--r--src/ext4_resize.c46
1 files 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;
}