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"
37 #include "blkid/blkid.h"
39 #include "../version.h"
40 #include "nls-enable.h"
42 const char * program_name = "get_fstab";
46 static blkid_cache cache = NULL;
65 static void usage(void)
68 _("Usage: %s -r device\n"), program_name);
72 static errcode_t get_file(ext2_filsys fs, const char * filename,
73 struct mem_file *ret_file)
79 struct ext2_inode inode;
86 retval = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
91 retval = ext2fs_read_inode(fs, ino, &inode);
95 if (inode.i_size_high || (inode.i_size > 65536))
98 buf = malloc(inode.i_size + 1);
101 memset(buf, 0, inode.i_size+1);
103 retval = ext2fs_file_open(fs, ino, 0, &e2_file);
107 retval = ext2fs_file_read(e2_file, buf, inode.i_size, &got);
111 retval = ext2fs_file_close(e2_file);
116 ret_file->size = (int) got;
119 ext2fs_file_close(e2_file);
123 static char *get_line(struct mem_file *file)
128 cp = file->buf + file->ptr;
129 while (*cp && *cp != '\n') {
137 memcpy(ret, file->buf + file->ptr, s);
138 while (*cp && (*cp == '\n' || *cp == '\r')) {
146 static int mem_file_eof(struct mem_file *file)
148 return (file->ptr >= file->size);
154 static char *string_copy(const char *s)
160 ret = malloc(strlen(s)+1);
166 static char *skip_over_blank(char *cp)
168 while (*cp && isspace(*cp))
173 static char *skip_over_word(char *cp)
175 while (*cp && !isspace(*cp))
180 static char *parse_word(char **buf)
188 word = skip_over_blank(word);
189 next = skip_over_word(word);
196 static void parse_escape(char *word)
204 for (p = word, q = word; *p; p++, q++) {
223 for (i = 0; i < 3; i++, p++) {
226 ac = (ac * 8) + (*p - '0');
234 static int parse_fstab_line(char *line, struct fs_info *fs)
236 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
238 if ((cp = strchr(line, '#')))
239 *cp = 0; /* Ignore everything after the comment char */
242 device = parse_word(&cp);
243 mntpnt = parse_word(&cp);
244 type = parse_word(&cp);
245 opts = parse_word(&cp);
246 freq = parse_word(&cp);
247 passno = parse_word(&cp);
250 return -1; /* Allow blank lines */
252 if (!mntpnt || !type)
255 parse_escape(device);
256 parse_escape(mntpnt);
260 parse_escape(passno);
262 dev = blkid_get_devname(cache, device, NULL);
266 if (strchr(type, ','))
269 fs->device = string_copy(device);
270 fs->mountpt = string_copy(mntpnt);
271 fs->type = string_copy(type);
272 fs->opts = string_copy(opts ? opts : "");
273 fs->freq = freq ? atoi(freq) : -1;
274 fs->passno = passno ? atoi(passno) : -1;
284 static void free_fstab_line(struct fs_info *fs)
294 memset(fs, 0, sizeof(struct fs_info));
298 static void PRS(int argc, char **argv)
303 setlocale(LC_MESSAGES, "");
304 setlocale(LC_CTYPE, "");
305 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
306 textdomain(NLS_CAT_NAME);
309 while ((c = getopt(argc, argv, "rv")) != EOF) {
316 printf("%s %s (%s)\n", program_name,
317 E2FSPROGS_VERSION, E2FSPROGS_DATE);
323 if (optind < argc - 1 || optind == argc)
325 device_name = blkid_get_devname(NULL, argv[optind], NULL);
327 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
333 static void get_root_type(ext2_filsys fs)
336 struct mem_file file;
338 struct fs_info fs_info;
341 retval = get_file(fs, "/etc/fstab", &file);
343 while (!mem_file_eof(&file)) {
344 buf = get_line(&file);
348 ret = parse_fstab_line(buf, &fs_info);
352 if (!strcmp(fs_info.mountpt, "/"))
353 printf("%s\n", fs_info.type);
355 free_fstab_line(&fs_info);
363 int main (int argc, char ** argv)
369 add_error_table(&et_ext2_error_table);
371 blkid_get_cache(&cache, NULL);
374 #ifdef CONFIG_TESTIO_DEBUG
375 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
376 io_ptr = test_io_manager;
377 test_io_backing_manager = unix_io_manager;
380 io_ptr = unix_io_manager;
381 retval = ext2fs_open (device_name, open_flag, 0, 0, io_ptr, &fs);
388 remove_error_table(&et_ext2_error_table);
389 return (ext2fs_close (fs) ? 1 : 0);