Add wimapply.c (not compiled by default)
authorEric Biggers <ebiggers3@gmail.com>
Sat, 24 Nov 2012 19:10:30 +0000 (13:10 -0600)
committerEric Biggers <ebiggers3@gmail.com>
Sat, 24 Nov 2012 19:10:30 +0000 (13:10 -0600)
Makefile.am
programs/wimapply.c [new file with mode: 0644]

index 60c3af3..da603f2 100644 (file)
@@ -103,6 +103,7 @@ EXTRA_DIST =                                        \
        debian                                  \
        rpm                                     \
        programs/install.cmd                    \
+       programs/wimapply.c                     \
        tests/common_tests.sh                   \
        tests/tests-common.sh                   \
        tests/security_descriptor_1.base64      \
diff --git a/programs/wimapply.c b/programs/wimapply.c
new file mode 100644 (file)
index 0000000..1271d33
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * wimapply.c
+ *
+ * This is a "minimal" program to apply an image from a stand-alone WIM file.
+ * It's intended to be statically linked to the WIM library to create a small
+ * executable containing only the functions needed to apply a WIM file.
+ *
+ * This is not installed by default since the 'apply' subcommand of 'imagex'
+ * covers all this functionality and more.
+ *
+ * Compile with something like:
+ *     $ cd wimlib-1.2.0
+ *     $ ./configure --without-fuse --disable-error-messages \
+ *       --disable-assertions --disable-custom-memory-allocator
+ *     $ cd programs
+ *     $ gcc -O2 -fwhole-program -flto -s wimapply.c -o wimapply \
+ *       ../src/*.c -I/usr/include/libxml2 -I.. -D_FILE_OFFSET_BITS=64 \
+ *       -D_GNU_SOURCE -std=gnu99 -lxml2 -lcrypto -lpthread -lntfs-3g
+ *     $ stat -c %s wimapply
+ *     48592
+ *
+ * Compare this to:
+ *     $ stat -c %s /usr/lib/libwim.so.1.0.0
+ *     196720
+ *     $ stat -c %s /usr/bin/imagex
+ *     35384
+ *
+ * Use with:
+ *     $ wimapply install.wim 5 /dev/sda2
+ */
+
+#include "wimlib.h"
+#include <stdio.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+int main(int argc, char **argv)
+{
+       const char *wimfile;
+       const char *image_num;
+       const char *target;
+       int image;
+       int ret;
+       WIMStruct *w;
+       struct stat stbuf;
+       int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
+
+       if (argc != 4) {
+               fprintf(stderr, "Usage: wimapply WIMFILE IMAGE_NUM TARGET\n");
+               return 2;
+       }
+
+       wimfile = argv[1];
+       image_num = argv[2];
+       target = argv[3];
+
+       image = atoi(image_num);
+
+       ret = stat(target, &stbuf);
+       if (ret != 0) {
+               fprintf(stderr, "Cannot stat `%s': %s\n",
+                       target, strerror(errno));
+               return -1;
+       }
+
+       if (!S_ISDIR(stbuf.st_mode))
+               extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
+
+       ret = wimlib_open_wim(wimfile, 0, &w, NULL);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to open `%s'!\n", wimfile);
+               fprintf(stderr, "Error code: %s\n", wimlib_get_error_string(ret));
+               return ret;
+       }
+
+       ret = wimlib_extract_image(w, image, target,
+                                  WIMLIB_EXTRACT_FLAG_SEQUENTIAL,
+                                  NULL, 0, NULL);
+       if (ret != 0) {
+               fputs("Failed to apply WIM image\n", stderr);
+               fprintf(stderr, "Error code: %s\n", wimlib_get_error_string(ret));
+       }
+
+       /* Not calling wimlib_free() because the process is ending anyway. */
+
+       return ret;
+}