Skip to contentSkip to conversation

e2fsprogs: check for read-only devices before starting fsck

PATCH New Eric Sandeen 1 participant 1 message Oct 10, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 e2fsprogs: check for read-only devices before starting fsck

When we open a device on linux, test whether it is writable right away, rather than trying to proceed and clean up when writes start failing.

---

lib/ext2fs/unix_io.c +16 -0
Index: e2fsprogs/lib/ext2fs/unix_io.c
===================================================================
@@ -30,6 +30,7 @@
 #include <time.h>
 #ifdef __linux__
 #include <sys/utsname.h>
+#include <linux/fs.h>
 #endif
 #if HAVE_SYS_STAT_H
 #include <sys/stat.h>
@@ -454,6 +455,21 @@ static errcode_t unix_open(const char *n
 	}
 
 #ifdef __linux__
+	if (flags & IO_FLAG_RW) {
+		int error;
+		int readonly = 0;
+
+		/* Is the block device actually writable? */
+		error = ioctl(data->dev, BLKROGET, &readonly);
+		if (!error && readonly) {
+			close(data->dev);
+			retval = EPERM;
+			goto cleanup;
+		}
+	}
+#endif
+
+#ifdef __linux__
 #undef RLIM_INFINITY
 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
 #define RLIM_INFINITY	((unsigned long)(~0UL>>1))