]> wimlib.net Git - wimlib/blobdiff - programs/imagex.c
NTFS apply updates
[wimlib] / programs / imagex.c
index 7bb553700541015cf42dca2efbc22385b085a889..1fce776bcd5b1684cfd1174768a858496ba7d8cd 100644 (file)
@@ -133,8 +133,6 @@ static const struct option apply_options[] = {
        {"hardlink", no_argument,       NULL, 'h'},
        {"symlink",  no_argument,       NULL, 's'},
        {"verbose",  no_argument,       NULL, 'v'},
-       {"ntfs",     no_argument,       NULL, 'N'},
-       {"mkntfs-args", required_argument, NULL, 'm'},
        {NULL, 0, NULL, 0},
 };
 static const struct option capture_options[] = {
@@ -143,7 +141,6 @@ static const struct option capture_options[] = {
        {"compress", optional_argument, NULL, 'x'},
        {"flags",    required_argument, NULL, 'f'},
        {"verbose",  no_argument,       NULL,'v'},
-       {"ntfs",     no_argument,       NULL, 'N'},
        {"dereference", no_argument, NULL, 'L'},
        {NULL, 0, NULL, 0},
 };
@@ -281,7 +278,7 @@ static int verify_image_exists_and_is_single(int image)
 static int get_compression_type(const char *optarg)
 {
        if (!optarg)
-               return WIM_COMPRESSION_TYPE_LZX;
+               return WIM_COMPRESSION_TYPE_XPRESS;
        if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
                return WIM_COMPRESSION_TYPE_LZX;
        else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
@@ -366,7 +363,6 @@ static int imagex_apply(int argc, const char **argv)
        const char *wimfile;
        const char *dir;
        const char *image_num_or_name;
-       const char *mkntfs_args = "";
        int extract_flags = 0;
 
        for_opt(c, apply_options) {
@@ -383,12 +379,6 @@ static int imagex_apply(int argc, const char **argv)
                case 'v':
                        extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
                        break;
-               case 'N':
-                       extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
-                       break;
-               case 'm':
-                       mkntfs_args = optarg;
-                       break;
                default:
                        usage(APPLY);
                        return -1;
@@ -410,73 +400,14 @@ static int imagex_apply(int argc, const char **argv)
                dir = argv[2];
        }
 
-#ifdef WITH_NTFS_3G
-       char tmpdir[strlen(dir) + 50];
-       tmpdir[0] = '\0';
-
-       /* Check to see if a block device file was specified.  If so, 
-        * create a NTFS filesystem on it. */
-       struct stat stbuf;
-
-       ret = stat(dir, &stbuf);
-       if (ret != 0)
-               imagex_error_with_errno("Failed to stat `%s'", dir);
-       if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
-               extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
-
-               const char *dev = dir;
-               printf("Making NTFS filesystem on `%s'\n", dev);
-
-               char mkntfs_cmdline[sizeof("mkntfs ") + strlen(mkntfs_args) +
-                                   sizeof(" ") + strlen(dev)];
-               sprintf(mkntfs_cmdline, "mkntfs %s %s", mkntfs_args, dev);
-               puts(mkntfs_cmdline);
-               ret = system(mkntfs_cmdline);
-               if (ret == -1) {
-                       imagex_error_with_errno("Failed to execute the "
-                                               "`mkntfs' program");
-                       return -1;
-               } else if (ret > 0) {
-                       imagex_error("`mkntfs' exited with failure status");
-                       imagex_error("Note: You can pass additional arguments "
-                                    "to `mkntfs' using the --mkntfs-args "
-                                    "argument");
-                       return -1;
-               }
-               sprintf(tmpdir, "/tmp/imagex-%d-ntfsmount-%s-XXXXXX",
-                               getpid(), dev);
-               dir = mkdtemp(tmpdir);
-               if (!dir) {
-                       imagex_error_with_errno("Failed to create "
-                                               "temporary directory "
-                                               "`%s'", tmpdir);
-               }
-               char ntfs_3g_cmdline[sizeof("ntfs-3g ") + strlen(dev) +
-                                    sizeof(" ") + strlen(dir)];
-               sprintf(ntfs_3g_cmdline, "ntfs-3g %s %s", dev, dir);
-               puts(ntfs_3g_cmdline);
-               ret = system(ntfs_3g_cmdline);
-               if (ret == -1) {
-                       imagex_error_with_errno("Failed to execute the "
-                                               "`ntfs-3g' program");
-                       ret = -1;
-                       goto out_rm_tmpdir;
-               } else if (ret > 0) {
-                       imagex_error("`ntfs-3g' exited with failure status");
-                       ret = -1;
-                       goto out_rm_tmpdir;
-               }
-       }
-#endif
-
        ret = wimlib_open_wim(wimfile, open_flags, &w);
        if (ret != 0)
-               goto done;
+               goto out;
 
        image = wimlib_resolve_image(w, image_num_or_name);
        ret = verify_image_exists(image);
        if (ret != 0)
-               goto done;
+               goto out;
 
        num_images = wimlib_get_num_images(w);
        if (argc == 2 && num_images != 1) {
@@ -484,46 +415,32 @@ static int imagex_apply(int argc, const char **argv)
                             "(or all)", wimfile, num_images);
                usage(APPLY);
                ret = -1;
-               goto done;
+               goto out;
        }
-       ret = wimlib_extract_image(w, image, dir, extract_flags);
-done:
-       wimlib_free(w);
-#ifdef WITH_NTFS_3G
-out_rm_tmpdir:
-       if (tmpdir[0] != '\0') {
-               /* Unmount and remove the NTFS-3g mounted directory */
-
-               int pid = fork();
-               int status;
-               if (pid == -1) {
-                       imagex_error_with_errno("Failed to fork()");
-                       return -1;
-               }
-               if (pid == 0) {
-                       execlp("fusermount", "fusermount", "-u", dir, NULL);
-                       imagex_error_with_errno("Failed to execute `fusermount'");
-                       return -1;
-               }
 
-               if (waitpid(pid, &status, 0) == -1) {
-                       imagex_error_with_errno("Failed to wait for "
-                                               "fusermount process to "
-                                               "terminate");
-                       return -1;
-               }
+#ifdef WITH_NTFS_3G
+       struct stat stbuf;
 
-               if (status != 0) {
-                       imagex_error("fusermount exited with status %d",
-                                    status);
-                       return -1;
-               }
-               if (rmdir(tmpdir) != 0) {
-                       imagex_error_with_errno("Failed to remove temporary "
-                                               "directory `%s'", tmpdir);
+       ret = stat(dir, &stbuf);
+       if (ret == 0) {
+               if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
+                       const char *ntfs_device = dir;
+                       printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
+                              image, wimfile, ntfs_device);
+                       ret = wimlib_apply_image_to_ntfs_volume(w, image,
+                                                               ntfs_device,
+                                                               extract_flags);
+                       goto out;
                }
+       } else {
+               if (errno != -ENOENT)
+                       imagex_error_with_errno("Failed to stat `%s'", dir);
        }
 #endif
+
+       ret = wimlib_extract_image(w, image, dir, extract_flags);
+out:
+       wimlib_free(w);
        return ret;
 }
 
@@ -534,7 +451,7 @@ static int imagex_capture(int argc, const char **argv)
        int c;
        int add_image_flags = 0;
        int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
-       int compression_type = WIM_COMPRESSION_TYPE_NONE;
+       int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
        const char *flags_element = NULL;
        const char *dir;
        const char *wimfile;
@@ -561,9 +478,7 @@ static int imagex_capture(int argc, const char **argv)
                        break;
                case 'v':
                        add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
-                       break;
-               case 'N':
-                       add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
+                       write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
                        break;
                case 'L':
                        add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
@@ -735,7 +650,7 @@ static int imagex_export(int argc, const char **argv)
        int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
        int export_flags = 0;
        int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
-       int compression_type = WIM_COMPRESSION_TYPE_NONE;
+       int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
        bool compression_type_specified = false;
        const char *src_wimfile;
        const char *src_image_num_or_name;
@@ -790,7 +705,7 @@ static int imagex_export(int argc, const char **argv)
        if (stat(dest_wimfile, &stbuf) == 0) {
                wim_is_new = false;
                /* Destination file exists. */
-               if (!S_ISREG(stbuf.st_mode)) {
+               if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
                        imagex_error("`%s' is not a regular file",
                                        dest_wimfile);
                        goto done;