summaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorGoldwyn Rodrigues <rgoldwyn@gmail.com>2008-04-01 10:36:46 -0400
committerDavid Woodhouse <dwmw2@hera.kernel.org>2008-04-01 10:36:46 -0400
commitc2c5e3e7e533b1b03ba68723ef8ab7274d07148a (patch)
tree1b5527cebdc3ae994e385bd57d5de0019312048c /utils.c
parentad67cd73b7c1ab043f0c27570750f6d2becd7f03 (diff)
check if partition is mounted before mkfs
This saves from the blunder of formatting a live mounted filesystem. This can be extended to get the mount flags of the filesystem mounted. Signed-off-by: Goldwyn Rodrigues <rgoldwyn@gmail.com>
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index 0a067e6b..f3b8fc98 100644
--- a/utils.c
+++ b/utils.c
@@ -30,6 +30,7 @@
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
+#include <mntent.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include "ctree.h"
@@ -525,6 +526,58 @@ error:
return ret;
}
+/*
+ * returns 1 if the device was mounted, < 0 on error or 0 if everything
+ * is safe to continue. TODO, this should also scan multi-device filesystems
+ */
+int check_mounted(char *file)
+{
+ struct mntent *mnt;
+ struct stat st_buf;
+ dev_t file_dev = 0;
+ dev_t file_rdev = 0;
+ ino_t file_ino = 0;
+ FILE *f;
+ int ret = 0;
+
+ if ((f = setmntent ("/proc/mounts", "r")) == NULL)
+ return -errno;
+
+ if (stat(file, &st_buf) < 0) {
+ return -errno;
+ } else {
+ if (S_ISBLK(st_buf.st_mode)) {
+ file_rdev = st_buf.st_rdev;
+ } else {
+ file_dev = st_buf.st_dev;
+ file_ino = st_buf.st_ino;
+ }
+ }
+
+ while ((mnt = getmntent (f)) != NULL) {
+ if (strcmp(file, mnt->mnt_fsname) == 0)
+ break;
+
+ if (stat(mnt->mnt_fsname, &st_buf) == 0) {
+ if (S_ISBLK(st_buf.st_mode)) {
+ if (file_rdev && (file_rdev == st_buf.st_rdev))
+ break;
+ } else if (file_dev && ((file_dev == st_buf.st_dev) &&
+ (file_ino == st_buf.st_ino))) {
+ break;
+ }
+ }
+ }
+
+ if (mnt) {
+ /* found an entry in mnt table */
+ ret = 1;
+ }
+
+ endmntent (f);
+ return ret;
+}
+
struct pending_dir {
struct list_head list;
char name[256];