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 <[EMAIL PROTECTED]>
--- 

 mkfs.c  |    8 +++++++-
 utils.c |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 utils.h |    1 +
 3 files changed, 60 insertions(+), 1 deletion(-)

diff -r 13295e4517d1 mkfs.c
--- a/mkfs.c    Tue Mar 04 13:07:22 2008 -0500
+++ b/mkfs.c    Thu Mar 20 12:15:27 2008 +0530
@@ -173,7 +173,7 @@ int main(int ac, char **av)
        u64 block_count = 0;
        int fd;
        struct stat st;
-       int ret;
+       int ret, mounted = 0;
        int i;
        u32 leafsize = 16 * 1024;
        u32 sectorsize = 4096;
@@ -232,6 +232,12 @@ int main(int ac, char **av)
                fprintf(stderr, "unable to stat %s\n", file);
                exit(1);
        }
+       if (check_mounted(file, &mounted)==0)
+               if (mounted) {
+                       fprintf(stderr, "%s is mounted\n", file);
+                       exit(1);
+               }
+
        if (block_count == 0) {
                block_count = device_size(fd, &st);
                if (block_count == 0) {
diff -r 13295e4517d1 utils.c
--- a/utils.c   Tue Mar 04 13:07:22 2008 -0500
+++ b/utils.c   Thu Mar 20 12:15:27 2008 +0530
@@ -25,6 +25,7 @@
 #include <uuid/uuid.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <mntent.h>
 #include "kerncompat.h"
 #include "radix-tree.h"
 #include "ctree.h"
@@ -230,3 +231,54 @@ error:
 error:
        return ret;
 }
+
+
+int check_mounted(char *file, int *flag)
+{
+       struct mntent   *mnt;
+       struct stat     st_buf;
+       dev_t           file_dev=0, file_rdev=0;
+       ino_t           file_ino=0;
+       FILE            *f;
+
+       *flag = 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 */
+               *flag = 1;
+       }
+
+       endmntent (f);
+       return 0;
+}
+
diff -r 13295e4517d1 utils.h
--- a/utils.h   Tue Mar 04 13:07:22 2008 -0500
+++ b/utils.h   Thu Mar 20 12:15:27 2008 +0530
@@ -22,4 +22,5 @@ int make_btrfs(int fd, u64 new_blocks[4]
                u32 leafsize, u32 sectorsize, u32 stripesize);
 int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
                        struct btrfs_root *root, u64 objectid);
+int check_mounted(char *devicename, int *mounted);
 #endif



_______________________________________________
Btrfs-devel mailing list
[email protected]
http://oss.oracle.com/mailman/listinfo/btrfs-devel

Reply via email to