]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
mke2fs: create a regular file if necessary
authorTheodore Ts'o <tytso@mit.edu>
Sat, 26 Apr 2014 20:17:38 +0000 (16:17 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Sat, 26 Apr 2014 20:17:38 +0000 (16:17 -0400)
This is useful when creating a filesystem for use with a VM, for
example.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
misc/mke2fs.c
misc/util.c
misc/util.h

index fa61e7b80640dad73e4a9ebef8ae2f7462d61e9e..a2b1f6578fe09cf1976665447cf224f126cf808b 100644 (file)
@@ -1749,7 +1749,8 @@ profile_error:
        if (optind < argc)
                usage();
 
-       if (!check_plausibility(device_name, 0, &is_device) && !force)
+       if (!check_plausibility(device_name, CREATE_FILE,
+                               &is_device) && !force)
                proceed_question();
 
        check_mount(device_name, force, _("filesystem"));
index 08ec761e609407829f23ae5247d4e9b3fae0ac4b..f85942e3ae9407d4f4a7e28e6e87bcbaab5ed854 100644 (file)
@@ -13,6 +13,7 @@
 #define _LARGEFILE64_SOURCE
 
 #include "config.h"
+#include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
 #ifdef HAVE_ERRNO_H
@@ -21,6 +22,7 @@
 #ifdef HAVE_LINUX_MAJOR_H
 #include <linux/major.h>
 #endif
+#include <sys/types.h>
 #ifdef HAVE_SYS_STAT_H
 #include <sys/stat.h>
 #endif
@@ -85,19 +87,29 @@ void proceed_question(void)
  */
 int check_plausibility(const char *device, int flags, int *ret_is_dev)
 {
-       int val, is_dev = 0;
+       int fd, is_dev = 0;
        ext2fs_struct_stat s;
+       int fl = O_RDONLY;
 
-       val = ext2fs_stat(device, &s);
+       if (flags & CREATE_FILE)
+               fl |= O_CREAT;
 
-       if(val == -1) {
-               fprintf(stderr, _("Could not stat %s --- %s\n"),
+       fd = open(device, fl, 0666);
+       if (fd < 0) {
+               fprintf(stderr, _("Could not open %s: %s\n"),
                        device, error_message(errno));
                if (errno == ENOENT)
                        fputs(_("\nThe device apparently does not exist; "
                                "did you specify it correctly?\n"), stderr);
                exit(1);
        }
+
+       if (ext2fs_fstat(fd, &s) < 0) {
+               perror("stat");
+               exit(1);
+       }
+       close(fd);
+
        if (S_ISBLK(s.st_mode))
                is_dev = 1;
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
index 867f4b0a48a88453f59a62641442f612bc28e4d5..b80d4895b38ddb84d17dc841c67d9e58efa0127c 100644 (file)
@@ -19,6 +19,7 @@ extern char   *journal_location_string;
  * Flags for check_plausibility()
  */
 #define CHECK_BLOCK_DEV        0x0001
+#define CREATE_FILE    0x0002
 
 #ifndef HAVE_STRCASECMP
 extern int strcasecmp (char *s1, char *s2);