From 898851a391ba076ddfef94113f222274f89f74c8 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Sat, 24 Nov 2012 13:10:30 -0600 Subject: [PATCH 1/1] Add wimapply.c (not compiled by default) --- Makefile.am | 1 + programs/wimapply.c | 89 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 programs/wimapply.c diff --git a/Makefile.am b/Makefile.am index 60c3af3c..da603f21 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 00000000..1271d332 --- /dev/null +++ b/programs/wimapply.c @@ -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 +#include +#include +#include +#include + +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; +} -- 2.43.0