Changes by: antona
Update of /cvsroot/linux-ntfs/linux-ntfs/libntfs
In directory usw-pr-cvs1:/tmp/cvs-serv25870/libntfs
Modified Files:
volume.c
Log Message:
Fixup the force option in mkntfs.c. Change the ntfs_check_if_mounted so it works on system not implementing {set,get,end}mntent, too. Also make it more powerful in telling us not only if something is mounted but also if it is the fs root and if it is read-only.
Index: volume.c
===================================================================
RCS file: /cvsroot/linux-ntfs/linux-ntfs/libntfs/volume.c,v
retrieving revision 1.35
retrieving revision 1.36
diff -U2 -r1.35 -r1.36
--- volume.c 23 Apr 2002 08:37:07 -0000 1.35
+++ volume.c 23 Apr 2002 23:27:33 -0000 1.36
@@ -27,7 +27,6 @@
#include <unistd.h>
#include <errno.h>
-#include <mntent.h>
-#include <sys/types.h>
#include <sys/stat.h>
+#include <limits.h>
#include "volume.h"
@@ -599,37 +598,83 @@
}
+#ifdef HAVE_MNTENT_H
/**
- * ntfs_check_if_mounted - check if an ntfs volume is currently mounted
- * @dev: device to check
- *
- * First ntfs_check_if_mounted() will verify if the device is a valid
- * block device. If it is not, errno will be set to ENODEV and it will return
- * -1 (error).
+ * Internal function: ntfs_check_mntent
+ *
+ * If you are wanting to use this, you actually wanted to use
+ * ntfs_check_if_mounted(), you just didn't realize. (-:
*
- * If the device is currently mounted, ntfs_check_if_mounted() will return
- * +1, otherwise return 0.
+ * See description of ntfs_check_if_mounted(), below.
*/
-int ntfs_check_if_mounted(const char *dev)
+static int ntfs_check_mntent(const char *file, unsigned long *mnt_flags)
{
- FILE *m_f;
- struct stat sbuf;
struct mntent *mnt;
+ FILE *f;
+ char *s;
+ int fd, len;
- /* First verify we are dealing with a valid block device. */
- if (stat(dev, &sbuf) == -1) {
- errno = ENODEV;
- return -1;
- }
-
- if (!(m_f = setmntent(MOUNTED, "r")))
+ if (!(f = setmntent(MOUNTED, "r")))
return -1;
- while ((mnt = getmntent(m_f)))
- if (!strcmp(dev, mnt->mnt_fsname))
+ while ((mnt = getmntent(f)))
+ if (!strcmp(file, mnt->mnt_fsname))
break;
- endmntent(m_f);
-
+ endmntent(f);
if (!mnt)
return 0;
- return 1;
+ *mnt_flags = NTFS_MF_MOUNTED;
+ if (!strcmp(mnt->mnt_dir, "/"))
+ *mnt_flags |= NTFS_MF_ISROOT;
+ len = strlen(mnt->mnt_dir);
+ s = malloc(len + 3);
+ if (!s)
+ return -1;
+ strncpy(s, mnt->mnt_dir, len + 3);
+ if (s[len - 1] != '/')
+ s[len++] = '/';
+ s[len++] = '.';
+ s[len] = '\0';
+ fd = open(s, O_RDWR);
+ if (fd < 0) {
+ if (errno == EROFS)
+ *mnt_flags |= NTFS_MF_READONLY;
+ } else
+ close(fd);
+ return 0;
+}
+#endif
+
+/**
+ * ntfs_check_if_mounted - check if an ntfs volume is currently mounted
+ * @file: device file to check
+ * @mnt_flags: pointer into which to return the ntfs mount flags (see volume.h)
+ *
+ * If the running system does not support the {set,get,end}mntent() calls,
+ * just return 0 and set *mnt_flags to zero.
+ *
+ * When the system does support the calls, ntfs_check_if_mounted() first tries
+ * to find the device @file in /etc/mtab (or wherever this is kept on the
+ * running system). If it is not found, assume the device is not mounted and
+ * return 0.
+ *
+ * If the device @file is found, set the NTFS_MF_MOUNTED flags in @mnt_flags.
+ *
+ * Further if @file is mounted as the file system root ("/"), set the flag
+ * NTFS_MF_ISROOT in @mnt_flags.
+ *
+ * Finally, check if the file system is mounted read-only, and if so set the
+ * NTFS_MF_READONLY flag in @mnt_flags.
+ *
+ * On sucess, return 0 with @mnt_flags set to the ntfs mount flags.
+ *
+ * On error, return -1 with errno set to the error code.
+ */
+int ntfs_check_if_mounted(const char *file, unsigned long *mnt_flags)
+{
+ *mnt_flags = 0;
+#ifdef HAVE_MNTENT_H
+ return ntfs_check_mntent(file, mnt_flags);
+#else
+ return 0;
+#endif
}
|