X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=examples%2Fapplywim.c;h=a92b26376a8b50ab29df13e56ba5519c730ba417;hp=ba62a9571f42b3dcc0884e490d033131d8f1e840;hb=c3a1bd96026fd5a1d43c5f6178f9f0165afdd098;hpb=16b6e19468c93a370ff2347031a62130c18ad7d7 diff --git a/examples/applywim.c b/examples/applywim.c index ba62a957..a92b2637 100644 --- a/examples/applywim.c +++ b/examples/applywim.c @@ -1,42 +1,72 @@ /* - * applywim.c - A simple program to extract all images from a WIM file to a - * directory. + * applywim.c - A program to extract the first image from a WIM file. */ #include #include +#define TO_PERCENT(numerator, denominator) \ + ((float)(((denominator) == 0) ? 0 : ((numerator) * 100 / (float)(denominator)))) + +static int +extract_progress(enum wimlib_progress_msg msg, + const union wimlib_progress_info *info) +{ + switch (msg) { + case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS: + printf("Extracting files: %.2f%% complete\n", + TO_PERCENT(info->extract.completed_bytes, + info->extract.total_bytes)); + break; + default: + break; + } + return 0; +} + int main(int argc, char **argv) { int ret; - WIMStruct *wim; + WIMStruct *wim = NULL; + const char *wimpath; + const char *destdir; + /* Check for the correct number of arguments. */ if (argc != 3) { fprintf(stderr, "Usage: applywim WIM DIR\n"); - ret = 2; - goto out; + return 2; } - /* Initialize the library. */ - ret = wimlib_global_init(0); - if (ret) - goto out; + wimpath = argv[1]; + destdir = argv[2]; - /* Open the WIM file. */ - ret = wimlib_open_wim(argv[1], 0, &wim, NULL); - if (ret) - goto out_wimlib_global_cleanup; + /* Open the WIM file as a WIMStruct. */ + ret = wimlib_open_wim(wimpath, /* Path of WIM file to open */ + 0, /* WIMLIB_OPEN_FLAG_* flags (0 means all defaults) */ + &wim, /* Return the WIMStruct pointer in this location */ + NULL); /* Progress function (NULL means none) */ + if (ret != 0) /* Always should check the error codes. */ + goto out; - /* Extract all the images. */ - ret = wimlib_extract_image(wim, WIMLIB_ALL_IMAGES, argv[2], 0, - NULL, 0, NULL); + /* Extract the first image. */ + ret = wimlib_extract_image(wim, /* WIMStruct from which to extract the image */ + 1, /* Image to extract */ + destdir, /* Directory to extract the image to */ + 0, /* WIMLIB_EXTRACT_FLAG_* flags (0 means all defaults) */ + extract_progress); /* Progress function */ - /* Free the WIM file */ +out: + /* Free the WIMStruct. Has no effect if the pointer to it is NULL. */ wimlib_free(wim); -out_wimlib_global_cleanup: - /* Finalize the library */ + /* Check for error status. */ + if (ret != 0) { + fprintf(stderr, "wimlib error %d: %s\n", + ret, wimlib_get_error_string(ret)); + } + + /* Free global memory (optional). */ wimlib_global_cleanup(); -out: + return ret; }