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)
76 ext2_file_t e2_file = NULL;
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;
121 ext2fs_file_close(e2_file);
125 static char *get_line(struct mem_file *file)
130 cp = file->buf + file->ptr;
131 while (*cp && *cp != '\n') {
139 memcpy(ret, file->buf + file->ptr, s);
140 while (*cp && (*cp == '\n' || *cp == '\r')) {
148 static int mem_file_eof(struct mem_file *file)
150 return (file->ptr >= file->size);
156 static char *string_copy(const char *s)
162 ret = malloc(strlen(s)+1);
168 static char *skip_over_blank(char *cp)
170 while (*cp && isspace(*cp))
175 static char *skip_over_word(char *cp)
177 while (*cp && !isspace(*cp))
182 static char *parse_word(char **buf)
190 word = skip_over_blank(word);
191 next = skip_over_word(word);
198 static void parse_escape(char *word)
206 for (p = word, q = word; *p; p++, q++) {
225 for (i = 0; i < 3; i++, p++) {
228 ac = (ac * 8) + (*p - '0');
236 static int parse_fstab_line(char *line, struct fs_info *fs)
238 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
240 if ((cp = strchr(line, '#')))
241 *cp = 0; /* Ignore everything after the comment char */
244 device = parse_word(&cp);
245 mntpnt = parse_word(&cp);
246 type = parse_word(&cp);
247 opts = parse_word(&cp);
248 freq = parse_word(&cp);
249 passno = parse_word(&cp);
252 return -1; /* Allow blank lines */
254 if (!mntpnt || !type)
257 parse_escape(device);
258 parse_escape(mntpnt);
262 parse_escape(passno);
264 dev = blkid_get_devname(cache, device, NULL);
268 if (strchr(type, ','))
271 fs->device = string_copy(device);
272 fs->mountpt = string_copy(mntpnt);
273 fs->type = string_copy(type);
274 fs->opts = string_copy(opts ? opts : "");
275 fs->freq = freq ? atoi(freq) : -1;
276 fs->passno = passno ? atoi(passno) : -1;
285 static void free_fstab_line(struct fs_info *fs)
295 memset(fs, 0, sizeof(struct fs_info));
299 static void PRS(int argc, char **argv)
304 setlocale(LC_MESSAGES, "");
305 setlocale(LC_CTYPE, "");
306 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
307 textdomain(NLS_CAT_NAME);
310 while ((c = getopt(argc, argv, "rv")) != EOF) {
317 printf("%s %s (%s)\n", program_name,
318 E2FSPROGS_VERSION, E2FSPROGS_DATE);
324 if (optind < argc - 1 || optind == argc)
326 device_name = blkid_get_devname(NULL, argv[optind], NULL);
328 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
334 static void get_root_type(ext2_filsys fs)
337 struct mem_file file;
339 struct fs_info fs_info;
342 retval = get_file(fs, "/etc/fstab", &file);
344 while (!mem_file_eof(&file)) {
345 buf = get_line(&file);
349 ret = parse_fstab_line(buf, &fs_info);
353 if (!strcmp(fs_info.mountpt, "/"))
354 printf("%s\n", fs_info.type);
356 free_fstab_line(&fs_info);
364 int main (int argc, char ** argv)
370 add_error_table(&et_ext2_error_table);
372 blkid_get_cache(&cache, NULL);
375 #ifdef CONFIG_TESTIO_DEBUG
376 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
377 io_ptr = test_io_manager;
378 test_io_backing_manager = unix_io_manager;
381 io_ptr = unix_io_manager;
382 retval = ext2fs_open (device_name, open_flag, 0, 0, io_ptr, &fs);
389 remove_error_table(&et_ext2_error_table);
390 return (ext2fs_close (fs) ? 1 : 0);