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
24 #include <sys/types.h>
35 #include "ext2fs/ext2_fs.h"
36 #include "ext2fs/ext2fs.h"
37 #include "blkid/blkid.h"
38 #include "support/nls-enable.h"
40 #include "../version.h"
42 static const char * program_name
= "e2initrd_helper";
43 static char * device_name
;
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
)
77 ext2_file_t e2_file
= NULL
;
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
;
122 ext2fs_file_close(e2_file
);
126 static char *get_line(struct mem_file
*file
)
131 cp
= file
->buf
+ file
->ptr
;
132 while (*cp
&& *cp
!= '\n') {
140 memcpy(ret
, file
->buf
+ file
->ptr
, s
);
141 while (*cp
&& (*cp
== '\n' || *cp
== '\r')) {
149 static int mem_file_eof(struct mem_file
*file
)
151 return (file
->ptr
>= file
->size
);
157 static char *string_copy(const char *s
)
163 ret
= malloc(strlen(s
)+1);
169 static char *skip_over_blank(char *cp
)
171 while (*cp
&& isspace(*cp
))
176 static char *skip_over_word(char *cp
)
178 while (*cp
&& !isspace(*cp
))
183 static char *parse_word(char **buf
)
191 word
= skip_over_blank(word
);
192 next
= skip_over_word(word
);
199 static void parse_escape(char *word
)
207 for (p
= word
, q
= word
; *p
; p
++, q
++) {
226 for (i
= 0; i
< 3; i
++, p
++) {
229 ac
= (ac
* 8) + (*p
- '0');
237 static int parse_fstab_line(char *line
, struct fs_info
*fs
)
239 char *dev
, *device
, *mntpnt
, *type
, *opts
, *freq
, *passno
, *cp
;
241 if ((cp
= strchr(line
, '#')))
242 *cp
= 0; /* Ignore everything after the comment char */
245 device
= parse_word(&cp
);
246 mntpnt
= parse_word(&cp
);
247 type
= parse_word(&cp
);
248 opts
= parse_word(&cp
);
249 freq
= parse_word(&cp
);
250 passno
= parse_word(&cp
);
253 return -1; /* Allow blank lines */
255 if (!mntpnt
|| !type
)
258 parse_escape(device
);
259 parse_escape(mntpnt
);
263 parse_escape(passno
);
265 dev
= blkid_get_devname(cache
, device
, NULL
);
269 if (strchr(type
, ','))
272 fs
->device
= string_copy(device
);
273 fs
->mountpt
= string_copy(mntpnt
);
274 fs
->type
= string_copy(type
);
275 fs
->opts
= string_copy(opts
? opts
: "");
276 fs
->freq
= freq
? atoi(freq
) : -1;
277 fs
->passno
= passno
? atoi(passno
) : -1;
286 static void free_fstab_line(struct fs_info
*fs
)
296 memset(fs
, 0, sizeof(struct fs_info
));
300 static void PRS(int argc
, char **argv
)
305 setlocale(LC_MESSAGES
, "");
306 setlocale(LC_CTYPE
, "");
307 bindtextdomain(NLS_CAT_NAME
, LOCALEDIR
);
308 textdomain(NLS_CAT_NAME
);
309 set_com_err_gettext(gettext
);
312 while ((c
= getopt(argc
, argv
, "rv")) != EOF
) {
319 printf("%s %s (%s)\n", program_name
,
320 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
326 if (optind
< argc
- 1 || optind
== argc
)
328 device_name
= blkid_get_devname(NULL
, argv
[optind
], NULL
);
330 com_err(program_name
, 0, _("Unable to resolve '%s'"),
336 static void get_root_type(ext2_filsys fs
)
339 struct mem_file file
;
341 struct fs_info fs_info
;
344 retval
= get_file(fs
, "/etc/fstab", &file
);
346 com_err(program_name
, retval
, "couldn't open /etc/fstab");
350 while (!mem_file_eof(&file
)) {
351 buf
= get_line(&file
);
355 ret
= parse_fstab_line(buf
, &fs_info
);
359 if (!strcmp(fs_info
.mountpt
, "/"))
360 printf("%s\n", fs_info
.type
);
362 free_fstab_line(&fs_info
);
370 int main (int argc
, char ** argv
)
376 add_error_table(&et_ext2_error_table
);
378 blkid_get_cache(&cache
, NULL
);
381 #ifdef CONFIG_TESTIO_DEBUG
382 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
383 io_ptr
= test_io_manager
;
384 test_io_backing_manager
= unix_io_manager
;
387 io_ptr
= unix_io_manager
;
388 retval
= ext2fs_open (device_name
, open_flag
, 0, 0, io_ptr
, &fs
);
395 remove_error_table(&et_ext2_error_table
);
396 return (ext2fs_close (fs
) ? 1 : 0);