2 * capturewim.c - A program to capture a directory tree into a WIM file.
4 * The author dedicates this file to the public domain.
5 * You can do whatever you want with this file.
11 #define TO_PERCENT(numerator, denominator) \
12 ((float)(((denominator) == 0) ? 0 : ((numerator) * 100 / (float)(denominator))))
14 static enum wimlib_progress_status
15 write_progress(enum wimlib_progress_msg msg,
16 union wimlib_progress_info *info, void *progctx)
19 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
20 printf("Writing WIM: %.2f%% complete\n",
21 TO_PERCENT(info->write_streams.completed_bytes,
22 info->write_streams.total_bytes));
27 return WIMLIB_PROGRESS_STATUS_CONTINUE;
30 int main(int argc, char **argv)
33 WIMStruct *wim = NULL;
37 /* Check for the correct number of arguments. */
39 fprintf(stderr, "Usage: capturewim DIR WIM\n");
46 /* Create a WIMStruct for a WIM. */
47 ret = wimlib_create_new_wim(WIMLIB_COMPRESSION_TYPE_LZX, &wim);
48 if (ret != 0) /* Always should check the error codes. */
51 /* Register our progress function. */
52 wimlib_register_progress_function(wim, write_progress, NULL);
54 /* Add the directory tree to the WIMStruct as an image. */
56 ret = wimlib_add_image(wim, /* WIMStruct to which to add the image */
57 srcdir, /* Directory from which to add the image */
58 NULL, /* Name to give the image (NULL means none) */
59 NULL, /* Capture configuration structure (NULL means none) */
60 0); /* WIMLIB_ADD_FLAG_* flags (0 means all defaults) */
64 /* Write the WIM file. */
66 ret = wimlib_write(wim, /* WIMStruct from which to write a WIM */
67 wimpath, /* Path to write the WIM to */
68 WIMLIB_ALL_IMAGES, /* Image(s) in the WIM to write */
69 0, /* WIMLIB_WRITE_FLAG_* flags (0 means all defaults) */
70 0); /* Number of compressor threads (0 means default) */
73 /* Free the WIMStruct. Has no effect if the pointer to it is NULL. */
76 /* Check for error status. */
78 fprintf(stderr, "wimlib error %d: %s\n",
79 ret, wimlib_get_error_string(ret));
82 /* Free global memory (optional). */
83 wimlib_global_cleanup();