2 * e2initrd_helper.c - Get the filesystem table
4 * Copyright 2004 by Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
23 #include <sys/types.h>
34 #include "ext2fs/ext2_fs.h"
35 #include "ext2fs/ext2fs.h"
36 #include "blkid/blkid.h"
38 #include "../version.h"
39 #include "nls-enable.h"
41 const char * program_name = "get_fstab";
45 static blkid_cache cache = NULL;
64 static void usage(void)
67 _("Usage: %s -r device\n"), program_name);
71 static errcode_t get_file(ext2_filsys fs, const char * filename,
72 struct mem_file *ret_file)
78 struct ext2_inode inode;
85 retval = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
90 retval = ext2fs_read_inode(fs, ino, &inode);
94 if (inode.i_size_high || (inode.i_size > 65536))
97 buf = malloc(inode.i_size + 1);
100 memset(buf, 0, inode.i_size+1);
102 retval = ext2fs_file_open(fs, ino, 0, &e2_file);
106 retval = ext2fs_file_read(e2_file, buf, inode.i_size, &got);
110 retval = ext2fs_file_close(e2_file);
115 ret_file->size = (int) got;
118 ext2fs_file_close(e2_file);
122 static char *get_line(struct mem_file *file)
127 cp = file->buf + file->ptr;
128 while (*cp && *cp != '\n') {
136 memcpy(ret, file->buf + file->ptr, s);
137 while (*cp && (*cp == '\n' || *cp == '\r')) {
145 static int mem_file_eof(struct mem_file *file)
147 return (file->ptr >= file->size);
153 static char *string_copy(const char *s)
159 ret = malloc(strlen(s)+1);
165 static char *skip_over_blank(char *cp)
167 while (*cp && isspace(*cp))
172 static char *skip_over_word(char *cp)
174 while (*cp && !isspace(*cp))
179 static char *parse_word(char **buf)
187 word = skip_over_blank(word);
188 next = skip_over_word(word);
195 static void parse_escape(char *word)
203 for (p = word, q = word; *p; p++, q++) {
222 for (i = 0; i < 3; i++, p++) {
225 ac = (ac * 8) + (*p - '0');
233 static int parse_fstab_line(char *line, struct fs_info *fs)
235 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
237 if ((cp = strchr(line, '#')))
238 *cp = 0; /* Ignore everything after the comment char */
241 device = parse_word(&cp);
242 mntpnt = parse_word(&cp);
243 type = parse_word(&cp);
244 opts = parse_word(&cp);
245 freq = parse_word(&cp);
246 passno = parse_word(&cp);
249 return -1; /* Allow blank lines */
251 if (!mntpnt || !type)
254 parse_escape(device);
255 parse_escape(mntpnt);
259 parse_escape(passno);
261 dev = blkid_get_devname(cache, device, NULL);
265 if (strchr(type, ','))
268 fs->device = string_copy(device);
269 fs->mountpt = string_copy(mntpnt);
270 fs->type = string_copy(type);
271 fs->opts = string_copy(opts ? opts : "");
272 fs->freq = freq ? atoi(freq) : -1;
273 fs->passno = passno ? atoi(passno) : -1;
283 static void free_fstab_line(struct fs_info *fs)
293 memset(fs, 0, sizeof(struct fs_info));
297 static void PRS(int argc, char **argv)
302 setlocale(LC_MESSAGES, "");
303 setlocale(LC_CTYPE, "");
304 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
305 textdomain(NLS_CAT_NAME);
308 while ((c = getopt(argc, argv, "rv")) != EOF) {
315 printf("%s %s (%s)\n", program_name,
316 E2FSPROGS_VERSION, E2FSPROGS_DATE);
322 if (optind < argc - 1 || optind == argc)
324 device_name = blkid_get_devname(NULL, argv[optind], NULL);
326 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
332 static void get_root_type(ext2_filsys fs)
335 struct mem_file file;
337 struct fs_info fs_info;
340 retval = get_file(fs, "/etc/fstab", &file);
342 while (!mem_file_eof(&file)) {
343 buf = get_line(&file);
347 ret = parse_fstab_line(buf, &fs_info);
351 if (!strcmp(fs_info.mountpt, "/"))
352 printf("%s\n", fs_info.type);
354 free_fstab_line(&fs_info);
362 int main (int argc, char ** argv)
368 add_error_table(&et_ext2_error_table);
370 blkid_get_cache(&cache, NULL);
373 #ifdef CONFIG_TESTIO_DEBUG
374 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
375 io_ptr = test_io_manager;
376 test_io_backing_manager = unix_io_manager;
379 io_ptr = unix_io_manager;
380 retval = ext2fs_open (device_name, open_flag, 0, 0, io_ptr, &fs);
387 remove_error_table(&et_ext2_error_table);
388 return (ext2fs_close (fs) ? 1 : 0);