]> wimlib.net Git - wimlib/blob - programs/wimapply.c
Source list mode
[wimlib] / programs / wimapply.c
1 /*
2  * wimapply.c
3  *
4  * This is a "minimal" program to apply an image from a stand-alone WIM file.
5  * It's intended to be statically linked to the WIM library to create a small
6  * executable containing only the functions needed to apply a WIM file.
7  *
8  * This is not installed by default since the 'apply' subcommand of 'imagex'
9  * covers all this functionality and more.
10  *
11  * Compile with something like:
12  *      $ cd wimlib-1.2.0
13  *      $ ./configure --without-fuse --disable-error-messages \
14  *        --disable-assertions --disable-custom-memory-allocator
15  *      $ cd programs
16  *      $ gcc -O2 -fwhole-program -flto -s wimapply.c -o wimapply \
17  *        ../src/*.c -I/usr/include/libxml2 -I.. -D_FILE_OFFSET_BITS=64 \
18  *        -D_GNU_SOURCE -std=gnu99 -lxml2 -lcrypto -lpthread -lntfs-3g
19  *      $ stat -c %s wimapply
20  *      48880
21  *
22  * Compare this to:
23  *      $ stat -c %s /usr/lib/libwim.so.1.0.0
24  *      196720
25  *      $ stat -c %s /usr/bin/imagex
26  *      35384
27  *
28  * Use with:
29  *      $ wimapply install.wim 5 /dev/sda2
30  */
31
32 #include "wimlib.h"
33 #include <stdio.h>
34 #include <sys/stat.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38
39 int main(int argc, char **argv)
40 {
41         const char *wimfile;
42         const char *image_num;
43         const char *target;
44         int image;
45         int ret;
46         WIMStruct *w;
47         struct stat stbuf;
48         int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
49
50         if (argc != 4) {
51                 fprintf(stderr, "Usage: wimapply WIMFILE IMAGE_NUM TARGET\n");
52                 return 2;
53         }
54
55         wimfile = argv[1];
56         image_num = argv[2];
57         target = argv[3];
58
59         image = atoi(image_num);
60
61         ret = stat(target, &stbuf);
62         if (ret != 0) {
63                 fprintf(stderr, "Cannot stat `%s': %s\n",
64                         target, strerror(errno));
65                 return -1;
66         }
67
68         if (!S_ISDIR(stbuf.st_mode))
69                 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
70
71         ret = wimlib_open_wim(wimfile, 0, &w, NULL);
72         if (ret != 0) {
73                 fprintf(stderr, "Failed to open `%s'!\n", wimfile);
74                 fprintf(stderr, "Error code: %s\n", wimlib_get_error_string(ret));
75                 return ret;
76         }
77
78         ret = wimlib_extract_image(w, image, target, extract_flags,
79                                    NULL, 0, NULL);
80         if (ret != 0) {
81                 fputs("Failed to apply WIM image\n", stderr);
82                 fprintf(stderr, "Error code: %s\n", wimlib_get_error_string(ret));
83         }
84
85         /* Not calling wimlib_free() because the process is ending anyway. */
86
87         return ret;
88 }