b2754445df323a5867bce8f228fccc28b50c2ad0
[wimlib] / include / wimlib / progress.h
1 #ifndef _WIMLIB_PROGRESS_H
2 #define _WIMLIB_PROGRESS_H
3
4 #include "wimlib.h"
5 #include "wimlib/types.h"
6
7 /* If specified, call the user-provided progress function and check its result.
8  */
9 static inline int
10 call_progress(wimlib_progress_func_t progfunc,
11               enum wimlib_progress_msg msg,
12               union wimlib_progress_info *info,
13               void *progctx)
14 {
15         if (progfunc) {
16                 enum wimlib_progress_status status;
17
18                 status = (*progfunc)(msg, info, progctx);
19
20                 switch (status) {
21                 case WIMLIB_PROGRESS_STATUS_CONTINUE:
22                         return 0;
23                 case WIMLIB_PROGRESS_STATUS_ABORT:
24                         return WIMLIB_ERR_ABORTED_BY_PROGRESS;
25                 default:
26                         return WIMLIB_ERR_UNKNOWN_PROGRESS_STATUS;
27                 }
28         }
29         return 0;
30 }
31
32 extern int
33 report_error(wimlib_progress_func_t progfunc,
34              void *progctx, int error_code, const tchar *path);
35
36 /* Rate-limiting of byte-count based progress messages: update *next_progress_p
37  * to the value that completed_bytes needs to reach before the next progress
38  * message will be sent.  */
39 static inline void
40 set_next_progress(u64 completed_bytes, u64 total_bytes, u64 *next_progress_p)
41 {
42         if (*next_progress_p < total_bytes) {
43                 /*
44                  * Send the next message as soon as:
45                  *      - another 1/128 of the total has been processed;
46                  *      - OR another 5000000 bytes have been processed;
47                  *      - OR all bytes have been processed.
48                  */
49                 *next_progress_p = min(min(completed_bytes + total_bytes / 128,
50                                            completed_bytes + 5000000),
51                                        total_bytes);
52         } else {
53                 /* Last message has been sent.  */
54                 *next_progress_p = ~0;
55         }
56 }
57
58 #endif /* _WIMLIB_PROGRESS_H */