]> wimlib.net Git - wimlib/blob - include/wimlib/apply.h
v1.14.4
[wimlib] / include / wimlib / apply.h
1 #ifndef _WIMLIB_APPLY_H
2 #define _WIMLIB_APPLY_H
3
4 #include "wimlib/compiler.h"
5 #include "wimlib/file_io.h"
6 #include "wimlib/list.h"
7 #include "wimlib/progress.h"
8 #include "wimlib/types.h"
9 #include "wimlib.h"
10
11 /* These can be treated as counts (for required_features) or booleans (for
12  * supported_features).  */
13 struct wim_features {
14         unsigned long readonly_files;
15         unsigned long hidden_files;
16         unsigned long system_files;
17         unsigned long archive_files;
18         unsigned long compressed_files;
19         unsigned long encrypted_files;
20         unsigned long encrypted_directories;
21         unsigned long not_context_indexed_files;
22         unsigned long sparse_files;
23         unsigned long named_data_streams;
24         unsigned long hard_links;
25         unsigned long reparse_points;
26         unsigned long symlink_reparse_points;
27         unsigned long other_reparse_points;
28         unsigned long security_descriptors;
29         unsigned long short_names;
30         unsigned long unix_data;
31         unsigned long object_ids;
32         unsigned long timestamps;
33         unsigned long case_sensitive_filenames;
34         unsigned long xattrs;
35 };
36
37 struct blob_descriptor;
38 struct read_blob_callbacks;
39 struct apply_operations;
40 struct wim_dentry;
41
42 struct apply_ctx {
43         /* The WIMStruct from which files are being extracted from the currently
44          * selected image.  */
45         WIMStruct *wim;
46
47         /* The target of the extraction, usually the path to a directory.  */
48         const tchar *target;
49
50         /* Length of @target in tchars.  */
51         size_t target_nchars;
52
53         /* Extraction flags (WIMLIB_EXTRACT_FLAG_*)  */
54         int extract_flags;
55
56         /* User-provided progress function, or NULL if not specified.  */
57         wimlib_progress_func_t progfunc;
58         void *progctx;
59
60         /* Progress data buffer, with progress.extract initialized.  */
61         union wimlib_progress_info progress;
62
63         /* Features required to extract the files (with counts)  */
64         struct wim_features required_features;
65
66         /* Features supported by the extraction mode (with booleans)  */
67         struct wim_features supported_features;
68
69         /* The members below should not be used outside of extract.c  */
70         const struct apply_operations *apply_ops;
71         u64 next_progress;
72         unsigned long invalid_sequence;
73         unsigned long num_blobs_remaining;
74         struct list_head blob_list;
75         const struct read_blob_callbacks *saved_cbs;
76         struct filedes tmpfile_fd;
77         tchar *tmpfile_name;
78         unsigned int count_until_file_progress;
79 };
80
81 /* Maximum number of UNIX file descriptors, NTFS attributes, or Windows file
82  * handles that can be opened simultaneously to extract a blob to multiple
83  * destinations.  */
84 #ifndef __APPLE__
85 #define MAX_OPEN_FILES 512
86 #else /* !__APPLE__ */
87 /* With macOS, reduce to 128 because the default value for ulimit -n is 256 */
88 #define MAX_OPEN_FILES 128
89 #endif /* __APPLE__ */
90
91 static inline int
92 extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
93 {
94         return call_progress(ctx->progfunc, msg, &ctx->progress, ctx->progctx);
95 }
96
97 int
98 do_file_extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg);
99
100 #define COUNT_PER_FILE_PROGRESS 256
101
102 static inline int
103 maybe_do_file_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
104 {
105         ctx->progress.extract.current_file_count++;
106         if (unlikely(!--ctx->count_until_file_progress))
107                 return do_file_extract_progress(ctx, msg);
108         return 0;
109 }
110
111 int
112 start_file_structure_phase(struct apply_ctx *ctx, u64 end_file_count);
113
114 int
115 start_file_metadata_phase(struct apply_ctx *ctx, u64 end_file_count);
116
117 /* Report that a file was created, prior to blob extraction.  */
118 static inline int
119 report_file_created(struct apply_ctx *ctx)
120 {
121         return maybe_do_file_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
122 }
123
124 /* Report that file metadata was applied, after blob extraction.  */
125 static inline int
126 report_file_metadata_applied(struct apply_ctx *ctx)
127 {
128         return maybe_do_file_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
129 }
130
131 int
132 end_file_structure_phase(struct apply_ctx *ctx);
133
134 int
135 end_file_metadata_phase(struct apply_ctx *ctx);
136
137 static inline int
138 report_apply_error(struct apply_ctx *ctx, int error_code, const tchar *path)
139 {
140         return report_error(ctx->progfunc, ctx->progctx, error_code, path);
141 }
142
143 bool
144 detect_sparse_region(const void *data, size_t size, size_t *len_ret);
145
146 static inline bool
147 maybe_detect_sparse_region(const void *data, size_t size, size_t *len_ret,
148                            bool enabled)
149 {
150         if (!enabled) {
151                 /* Force non-sparse without checking */
152                 *len_ret = size;
153                 return false;
154         }
155         return detect_sparse_region(data, size, len_ret);
156 }
157
158 #define inode_first_extraction_dentry(inode)                            \
159         ((inode)->i_first_extraction_alias)
160
161 #define inode_for_each_extraction_alias(dentry, inode)                  \
162         for (dentry = inode_first_extraction_dentry(inode);             \
163              dentry != NULL;                                            \
164              dentry = dentry->d_next_extraction_alias)
165
166 int
167 extract_blob_list(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs);
168
169 /*
170  * Represents an extraction backend.
171  */
172 struct apply_operations {
173
174         /* Name of the extraction backend.  */
175         const char *name;
176
177         /*
178          * Query the features supported by the extraction backend.
179          *
180          * @target
181          *      The target string that was provided by the user.  (Often a
182          *      directory, but extraction backends are free to interpret this
183          *      differently.)
184          *
185          * @supported_features
186          *      A structure, each of whose members represents a feature that may
187          *      be supported by the extraction backend.  For each feature that
188          *      the extraction backend supports, this routine must set the
189          *      corresponding member to a nonzero value.
190          *
191          * Return 0 if successful; otherwise a positive wimlib error code.
192          */
193         int (*get_supported_features)(const tchar *target,
194                                       struct wim_features *supported_features);
195
196         /*
197          * Main extraction routine.
198          *
199          * The extraction backend is provided a list of dentries that have been
200          * prepared for extraction.  It is free to extract them in any way that
201          * it chooses.  Ideally, it should choose a method that maximizes
202          * performance.
203          *
204          * The target string will be provided in ctx->common.target.  This might
205          * be a directory, although extraction backends are free to interpret it
206          * as they wish.  TODO: in some cases, the common extraction code also
207          * interprets the target string.  This should be completely isolated to
208          * extraction backends.
209          *
210          * The extraction flags will be provided in ctx->common.extract_flags.
211          * Extraction backends should examine them and implement the behaviors
212          * for as many flags as possible.  Some flags are already handled by the
213          * common extraction code.  TODO: this needs to be better formalized.
214          *
215          * @dentry_list, the list of dentries, will be ordered such that the
216          * ancestor of any dentry always precedes any descendents.  Unless
217          * @single_tree_only is set, it's possible that the dentries consist of
218          * multiple disconnected trees.
219          *
220          * 'd_extraction_name' and 'd_extraction_name_nchars' of each dentry
221          * will be set to indicate the actual name with which the dentry should
222          * be extracted.  This may or may not be the same as 'd_name'.  TODO:
223          * really, the extraction backends should be responsible for generating
224          * 'd_extraction_name'.
225          *
226          * Each dentry will refer to a valid inode in 'd_inode'.  Each inode
227          * will contain a list of dentries of that inode being extracted; this
228          * list may be shorter than the inode's full dentry list.
229          *
230          * The blobs required to be extracted will already be prepared in
231          * 'apply_ctx'.  The extraction backend should call extract_blob_list()
232          * to extract them.
233          *
234          * The will_extract_dentry() utility function, given an arbitrary dentry
235          * in the WIM image (which may not be in the extraction list), can be
236          * used to determine if that dentry is in the extraction list.
237          *
238          * Return 0 if successful; otherwise a positive wimlib error code.
239          */
240         int (*extract)(struct list_head *dentry_list, struct apply_ctx *ctx);
241
242         /*
243          * Query whether the unnamed data stream of the specified file will be
244          * extracted as "externally backed" from the WIM archive itself.  If so,
245          * then the extraction backend is assumed to handle this separately, and
246          * the common extraction code will not register a usage of the unnamed
247          * data stream's blob.
248          *
249          * This routine is optional.
250          *
251          * Return:
252          *      < 0 if the file will *not* be externally backed.
253          *      = 0 if the file will be externally backed.
254          *      > 0 (wimlib error code) if another error occurred.
255          */
256         int (*will_back_from_wim)(struct wim_dentry *dentry, struct apply_ctx *ctx);
257
258         /*
259          * Size of the backend-specific extraction context.  It must contain
260          * 'struct apply_ctx' as its first member.
261          */
262         size_t context_size;
263
264         /*
265          * Set this if the extraction backend only supports extracting dentries
266          * that form a single tree, not multiple trees.
267          */
268         bool single_tree_only;
269 };
270
271 #ifdef _WIN32
272   extern const struct apply_operations win32_apply_ops;
273 #else
274   extern const struct apply_operations unix_apply_ops;
275 #endif
276
277 #ifdef WITH_NTFS_3G
278   extern const struct apply_operations ntfs_3g_apply_ops;
279 #endif
280
281 #endif /* _WIMLIB_APPLY_H */