Whamcloud - gitweb
fstype.c (identify_fs): New file which looks at the superblock
authorTheodore Ts'o <tytso@mit.edu>
Fri, 20 Jul 2001 00:38:53 +0000 (20:38 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 20 Jul 2001 00:38:53 +0000 (20:38 -0400)
of the filesystem to determines its type.

fsck.c (fsck_device, ignore, interpret_type): Call identify_fs
if the type specified in /etc/fstab is "auto".

misc/ChangeLog
misc/Makefile.in
misc/fsck.c
misc/fsck.h
misc/fstype.c [new file with mode: 0644]

index dd6f113..9919f38 100644 (file)
@@ -1,3 +1,11 @@
+2001-07-19  Theodore Tso  <tytso@valinux.com>
+
+       * fstype.c (identify_fs): New file which looks at the superblock
+               of the filesystem to determines its type.
+
+       * fsck.c (fsck_device, ignore, interpret_type): Call identify_fs
+               if the type specified in /etc/fstab is "auto".
+
 2001-06-23  Theodore Tso  <tytso@valinux.com>
 
        * Release of E2fsprogs 1.22
index 971ee95..d4421fc 100644 (file)
@@ -31,12 +31,12 @@ UUIDGEN_OBJS=       uuidgen.o
 DUMPE2FS_OBJS= dumpe2fs.o
 BADBLOCKS_OBJS=        badblocks.o
 E2IMAGE_OBJS=  e2image.o
-FSCK_OBJS=     fsck.o get_device_by_label.o base_device.o
+FSCK_OBJS=     fsck.o get_device_by_label.o base_device.o fstype.o
 
 SRCS=  $(srcdir)/tune2fs.c $(srcdir)/mklost+found.c $(srcdir)/mke2fs.c \
                $(srcdir)/chattr.c $(srcdir)/lsattr.c $(srcdir)/dumpe2fs.c \
                $(srcdir)/badblocks.c $(srcdir)/fsck.c $(srcdir)/util.c \
-               $(srcdir)/uuidgen.c
+               $(srcdir)/uuidgen.c $(srcdir)/fstype.c
 
 LIBS= $(LIBEXT2FS) $(LIBCOM_ERR) 
 DEPLIBS= $(LIBEXT2FS) $(LIBCOM_ERR) 
index 17ac42d..6eefd55 100644 (file)
@@ -252,6 +252,26 @@ static char *interpret_device(char *spec)
 }
 
 /*
+ * Interpret filesystem auto type if necessary
+ */
+static void interpret_type(struct fs_info *fs)
+{
+       const char      *type;
+       
+       if (strcmp(fs->type, "auto") == 0) {
+               type = identify_fs(fs->device);
+               if (type) {
+                       free(fs->type);
+                       fs->type = string_copy(type);
+               } else
+                       fprintf(stderr, _("Could not determine "
+                                         "filesystem type for %s\n"),
+                               fs->device);
+       }
+}
+
+
+/*
  * Load the filesystem database from /etc/fstab
  */
 static void load_fs_info(const char *filename)
@@ -598,6 +618,7 @@ static void fsck_device(char *device, int interactive)
 
        if ((fsent = lookup(device))) {
                device = fsent->device;
+               interpret_type(fsent);
                if (!type)
                        type = fsent->type;
        }
@@ -764,6 +785,8 @@ static int ignore(struct fs_info *fs)
        if (fs->passno == 0)
                return 1;
 
+       interpret_type(fs);
+
        /*
         * If a specific fstype is specified, and it doesn't match,
         * ignore it.
index 9d9f6ea..71eb011 100644 (file)
@@ -61,3 +61,4 @@ struct fsck_instance {
 
 extern char *base_device(char *device);
 extern char *string_copy(const char *s);
+extern const char *identify_fs(const char *fs_name);
diff --git a/misc/fstype.c b/misc/fstype.c
new file mode 100644 (file)
index 0000000..31f3027
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * fstype.c
+ * 
+ * Copyright (C) 2001 Theodore Ts'o.
+ *
+ * %Begin-Header%
+ * This file may be redistributed under the terms of the GNU Public
+ * License.
+ * %End-Header%
+ */
+
+#include <stdio.h>
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <fcntl.h>
+#include <sys/types.h>
+
+struct fs_magic {
+       const char *fs_name;
+       int     offset;
+       int     len;
+       char    *magic;
+};
+
+struct fs_magic type_array[] = {
+       { "ext2", 1024+56, 2, "\123\357" },
+       { "minix", 1040, 2, "\177\023" },
+       { "minix", 1040, 2, "\217\023" },
+       { "minix", 1040, 2, "\150\044" },
+       { "minix", 1040, 2, "\170\044" },
+       { "xfs", 0, 4, "XFSB" },
+       { 0, 0, 0, 0 }
+};
+
+const char *identify_fs(const char *fs_name)
+{
+       char    buf[2048];
+       struct fs_magic *p;
+       int     fd;
+
+       fd = open(fs_name, O_RDONLY);
+       if (fd < 0)
+               return NULL;
+       if (lseek(fd, 0, SEEK_SET) < 0)
+               return NULL;
+       if (read(fd, buf, sizeof(buf)) != sizeof(buf))
+               return NULL;
+       for (p = type_array; p->fs_name; p++) {
+               if (memcmp(p->magic, buf+p->offset, p->len) == 0)
+                       return p->fs_name;
+       }
+       return NULL;
+}
+
+#ifdef TEST_PROGRAM
+int main(int argc, char **argv)
+{
+       const char      *type;
+       
+       if (argc != 2) {
+               fprintf(stderr, "Usage: %s device\n", argv[0]);
+               exit(1);
+       }
+       type = identify_fs(argv[1]);
+       printf("%s is a %s filesystem\n", argv[1], type);
+       return (0);
+}
+#endif
+
+       
+