Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * getsize.c --- get the size of a partition. |
| 3 | * |
| 4 | * Copyright (C) 1995 Theodore Ts'o. This file may be |
| 5 | * redistributed under the terms of the GNU Public License. |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #if HAVE_UNISTD_H |
| 10 | #include <unistd.h> |
| 11 | #endif |
| 12 | #if HAVE_STDLIB_H |
| 13 | #include <stdlib.h> |
| 14 | #endif |
| 15 | #ifdef HAVE_ERRNO_H |
| 16 | #include <errno.h> |
| 17 | #endif |
| 18 | #include <fcntl.h> |
| 19 | #ifdef HAVE_LINUX_FS_H |
| 20 | #include <linux/fs.h> |
| 21 | #endif |
| 22 | #ifdef HAVE_LINUX_FD_H |
| 23 | #include <linux/fd.h> |
| 24 | #endif |
| 25 | #ifdef HAVE_MNTENT_H |
| 26 | #include <mntent.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_GETMNTINFO |
| 29 | #include <paths.h> |
| 30 | #include <sys/param.h> |
| 31 | #include <sys/mount.h> |
| 32 | #endif /* HAVE_GETMNTINFO */ |
| 33 | |
| 34 | #include <linux/ext2_fs.h> |
| 35 | #include "ext2fs.h" |
| 36 | |
| 37 | #ifdef HAVE_MNTENT_H |
| 38 | /* |
| 39 | * XXX we only check to see if the mount is readonly when it's the |
Theodore Ts'o | 297f47a | 1997-04-26 14:25:20 +0000 | [diff] [blame^] | 40 | * root filesystem. |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 41 | */ |
| 42 | static errcode_t check_mntent(const char *file, int *mount_flags) |
| 43 | { |
| 44 | FILE * f; |
| 45 | struct mntent * mnt; |
| 46 | int fd; |
| 47 | |
| 48 | *mount_flags = 0; |
| 49 | if ((f = setmntent (MOUNTED, "r")) == NULL) |
| 50 | return errno; |
| 51 | while ((mnt = getmntent (f)) != NULL) |
| 52 | if (strcmp(file, mnt->mnt_fsname) == 0) |
| 53 | break; |
| 54 | endmntent (f); |
| 55 | if (mnt == 0) |
| 56 | return 0; |
| 57 | *mount_flags = EXT2_MF_MOUNTED; |
| 58 | |
| 59 | if (!strcmp(mnt->mnt_dir, "/")) { |
| 60 | *mount_flags |= EXT2_MF_ISROOT; |
| 61 | fd = open(MOUNTED, O_RDWR); |
| 62 | if (fd < 0) { |
| 63 | if (errno == EROFS) |
| 64 | *mount_flags |= EXT2_MF_READONLY; |
| 65 | } else |
| 66 | close(fd); |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | #ifdef HAVE_GETMNTINFO |
| 73 | static errcode_t check_getmntinfo(const char *file, int *mount_flags) |
| 74 | { |
| 75 | struct statfs *mp; |
| 76 | int len, n; |
| 77 | const char *s1; |
| 78 | char *s2; |
| 79 | |
| 80 | n = getmntinfo(&mp, MNT_NOWAIT); |
| 81 | if (n == 0) |
| 82 | return errno; |
| 83 | |
| 84 | len = sizeof(_PATH_DEV) - 1; |
| 85 | s1 = file; |
| 86 | if (strncmp(_PATH_DEV, s1, len) == 0) |
| 87 | s1 += len; |
| 88 | |
| 89 | *mount_flags = 0; |
| 90 | while (--n >= 0) { |
| 91 | s2 = mp->f_mntfromname; |
| 92 | if (strncmp(_PATH_DEV, s2, len) == 0) { |
| 93 | s2 += len - 1; |
| 94 | *s2 = 'r'; |
| 95 | } |
| 96 | if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) { |
| 97 | *mount_flags = EXT2_MF_MOUNTED; |
| 98 | break; |
| 99 | } |
| 100 | ++mp; |
| 101 | } |
| 102 | return 0; |
| 103 | } |
| 104 | #endif /* HAVE_GETMNTINFO */ |
| 105 | |
| 106 | /* |
| 107 | * Is_mounted is set to 1 if the device is mounted, 0 otherwise |
| 108 | */ |
| 109 | errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags) |
| 110 | { |
| 111 | #ifdef HAVE_MNTENT_H |
| 112 | return check_mntent(file, mount_flags); |
| 113 | #else |
| 114 | #ifdef HAVE_GETMNTINFO |
| 115 | return check_getmntinfo(file, mount_flags); |
| 116 | #else |
| 117 | *mount_flags = 0; |
| 118 | return 0; |
| 119 | #endif /* HAVE_GETMNTINFO */ |
| 120 | #endif /* HAVE_MNTENT_H */ |
| 121 | } |