Whamcloud - gitweb
e2fsck: add support for dirdata feature
[tools/e2fsprogs.git] / misc / e2initrd_helper.c
index eaf9ce6..b39fe15 100644 (file)
@@ -9,6 +9,7 @@
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <unistd.h>
 #ifdef HAVE_STDLIB_H
@@ -34,12 +35,13 @@ extern char *optarg;
 #include "ext2fs/ext2_fs.h"
 #include "ext2fs/ext2fs.h"
 #include "blkid/blkid.h"
+#include "support/nls-enable.h"
+#include "support/devname.h"
 
 #include "../version.h"
-#include "nls-enable.h"
 
-const char * program_name = "get_fstab";
-char * device_name;
+static const char * program_name = "e2initrd_helper";
+static char * device_name;
 static int open_flag;
 static int root_type;
 static blkid_cache cache = NULL;
@@ -73,7 +75,7 @@ static errcode_t get_file(ext2_filsys fs, const char * filename,
 {
        errcode_t       retval;
        char            *buf;
-       ext2_file_t     e2_file;
+       ext2_file_t     e2_file = NULL;
        unsigned int    got;
        struct ext2_inode inode;
        ext2_ino_t      ino;
@@ -101,7 +103,7 @@ static errcode_t get_file(ext2_filsys fs, const char * filename,
 
        retval = ext2fs_file_open(fs, ino, 0, &e2_file);
        if (retval)
-               return retval;
+               goto errout;
 
        retval = ext2fs_file_read(e2_file, buf, inode.i_size, &got);
        if (retval)
@@ -109,13 +111,16 @@ static errcode_t get_file(ext2_filsys fs, const char * filename,
 
        retval = ext2fs_file_close(e2_file);
        if (retval)
-               return retval;
+               goto errout;
 
        ret_file->buf = buf;
        ret_file->size = (int) got;
+       return 0;
 
 errout:
-       ext2fs_file_close(e2_file);
+       free(buf);
+       if (e2_file)
+               ext2fs_file_close(e2_file);
        return retval;
 }
 
@@ -258,7 +263,7 @@ static int parse_fstab_line(char *line, struct fs_info *fs)
        parse_escape(freq);
        parse_escape(passno);
 
-       dev = blkid_get_devname(cache, device, NULL);
+       dev = get_devname(cache, device, NULL);
        if (dev)
                device = dev;
 
@@ -302,6 +307,7 @@ static void PRS(int argc, char **argv)
        setlocale(LC_CTYPE, "");
        bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
        textdomain(NLS_CAT_NAME);
+       set_com_err_gettext(gettext);
 #endif
 
        while ((c = getopt(argc, argv, "rv")) != EOF) {
@@ -320,9 +326,9 @@ static void PRS(int argc, char **argv)
        }
        if (optind < argc - 1 || optind == argc)
                usage();
-       device_name = blkid_get_devname(NULL, argv[optind], NULL);
+       device_name = get_devname(NULL, argv[optind], NULL);
        if (!device_name) {
-               com_err("tune2fs", 0, _("Unable to resolve '%s'"),
+               com_err(program_name, 0, _("Unable to resolve '%s'"),
                        argv[optind]);
                exit(1);
        }
@@ -337,6 +343,10 @@ static void get_root_type(ext2_filsys fs)
        int             ret;
 
        retval = get_file(fs, "/etc/fstab", &file);
+       if (retval) {
+               com_err(program_name, retval, "couldn't open /etc/fstab");
+               exit(1);
+       }
 
        while (!mem_file_eof(&file)) {
                buf = get_line(&file);