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 #define MAX_OPEN_FILES 512
85 
86 static inline int
extract_progress(struct apply_ctx * ctx,enum wimlib_progress_msg msg)87 extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
88 {
89 	return call_progress(ctx->progfunc, msg, &ctx->progress, ctx->progctx);
90 }
91 
92 extern int
93 do_file_extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg);
94 
95 #define COUNT_PER_FILE_PROGRESS 256
96 
97 static inline int
maybe_do_file_progress(struct apply_ctx * ctx,enum wimlib_progress_msg msg)98 maybe_do_file_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
99 {
100 	ctx->progress.extract.current_file_count++;
101 	if (unlikely(!--ctx->count_until_file_progress))
102 		return do_file_extract_progress(ctx, msg);
103 	return 0;
104 }
105 
106 extern int
107 start_file_structure_phase(struct apply_ctx *ctx, u64 end_file_count);
108 
109 extern int
110 start_file_metadata_phase(struct apply_ctx *ctx, u64 end_file_count);
111 
112 /* Report that a file was created, prior to blob extraction.  */
113 static inline int
report_file_created(struct apply_ctx * ctx)114 report_file_created(struct apply_ctx *ctx)
115 {
116 	return maybe_do_file_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
117 }
118 
119 /* Report that file metadata was applied, after blob extraction.  */
120 static inline int
report_file_metadata_applied(struct apply_ctx * ctx)121 report_file_metadata_applied(struct apply_ctx *ctx)
122 {
123 	return maybe_do_file_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
124 }
125 
126 extern int
127 end_file_structure_phase(struct apply_ctx *ctx);
128 
129 extern int
130 end_file_metadata_phase(struct apply_ctx *ctx);
131 
132 static inline int
report_apply_error(struct apply_ctx * ctx,int error_code,const tchar * path)133 report_apply_error(struct apply_ctx *ctx, int error_code, const tchar *path)
134 {
135 	return report_error(ctx->progfunc, ctx->progctx, error_code, path);
136 }
137 
138 extern bool
139 detect_sparse_region(const void *data, size_t size, size_t *len_ret);
140 
141 static inline bool
maybe_detect_sparse_region(const void * data,size_t size,size_t * len_ret,bool enabled)142 maybe_detect_sparse_region(const void *data, size_t size, size_t *len_ret,
143 			   bool enabled)
144 {
145 	if (!enabled) {
146 		/* Force non-sparse without checking */
147 		*len_ret = size;
148 		return false;
149 	}
150 	return detect_sparse_region(data, size, len_ret);
151 }
152 
153 #define inode_first_extraction_dentry(inode)				\
154 	((inode)->i_first_extraction_alias)
155 
156 #define inode_for_each_extraction_alias(dentry, inode)			\
157 	for (dentry = inode_first_extraction_dentry(inode);		\
158 	     dentry != NULL;						\
159 	     dentry = dentry->d_next_extraction_alias)
160 
161 extern int
162 extract_blob_list(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs);
163 
164 /*
165  * Represents an extraction backend.
166  */
167 struct apply_operations {
168 
169 	/* Name of the extraction backend.  */
170 	const char *name;
171 
172 	/*
173 	 * Query the features supported by the extraction backend.
174 	 *
175 	 * @target
176 	 *	The target string that was provided by the user.  (Often a
177 	 *	directory, but extraction backends are free to interpret this
178 	 *	differently.)
179 	 *
180 	 * @supported_features
181 	 *	A structure, each of whose members represents a feature that may
182 	 *	be supported by the extraction backend.  For each feature that
183 	 *	the extraction backend supports, this routine must set the
184 	 *	corresponding member to a nonzero value.
185 	 *
186 	 * Return 0 if successful; otherwise a positive wimlib error code.
187 	 */
188 	int (*get_supported_features)(const tchar *target,
189 				      struct wim_features *supported_features);
190 
191 	/*
192 	 * Main extraction routine.
193 	 *
194 	 * The extraction backend is provided a list of dentries that have been
195 	 * prepared for extraction.  It is free to extract them in any way that
196 	 * it chooses.  Ideally, it should choose a method that maximizes
197 	 * performance.
198 	 *
199 	 * The target string will be provided in ctx->common.target.  This might
200 	 * be a directory, although extraction backends are free to interpret it
201 	 * as they wish.  TODO: in some cases, the common extraction code also
202 	 * interprets the target string.  This should be completely isolated to
203 	 * extraction backends.
204 	 *
205 	 * The extraction flags will be provided in ctx->common.extract_flags.
206 	 * Extraction backends should examine them and implement the behaviors
207 	 * for as many flags as possible.  Some flags are already handled by the
208 	 * common extraction code.  TODO: this needs to be better formalized.
209 	 *
210 	 * @dentry_list, the list of dentries, will be ordered such that the
211 	 * ancestor of any dentry always precedes any descendents.  Unless
212 	 * @single_tree_only is set, it's possible that the dentries consist of
213 	 * multiple disconnected trees.
214 	 *
215 	 * 'd_extraction_name' and 'd_extraction_name_nchars' of each dentry
216 	 * will be set to indicate the actual name with which the dentry should
217 	 * be extracted.  This may or may not be the same as 'd_name'.  TODO:
218 	 * really, the extraction backends should be responsible for generating
219 	 * 'd_extraction_name'.
220 	 *
221 	 * Each dentry will refer to a valid inode in 'd_inode'.  Each inode
222 	 * will contain a list of dentries of that inode being extracted; this
223 	 * list may be shorter than the inode's full dentry list.
224 	 *
225 	 * The blobs required to be extracted will already be prepared in
226 	 * 'apply_ctx'.  The extraction backend should call extract_blob_list()
227 	 * to extract them.
228 	 *
229 	 * The will_extract_dentry() utility function, given an arbitrary dentry
230 	 * in the WIM image (which may not be in the extraction list), can be
231 	 * used to determine if that dentry is in the extraction list.
232 	 *
233 	 * Return 0 if successful; otherwise a positive wimlib error code.
234 	 */
235 	int (*extract)(struct list_head *dentry_list, struct apply_ctx *ctx);
236 
237 	/*
238 	 * Query whether the unnamed data stream of the specified file will be
239 	 * extracted as "externally backed" from the WIM archive itself.  If so,
240 	 * then the extraction backend is assumed to handle this separately, and
241 	 * the common extraction code will not register a usage of the unnamed
242 	 * data stream's blob.
243 	 *
244 	 * This routine is optional.
245 	 *
246 	 * Return:
247 	 *	< 0 if the file will *not* be externally backed.
248 	 *	= 0 if the file will be externally backed.
249 	 *	> 0 (wimlib error code) if another error occurred.
250 	 */
251 	int (*will_back_from_wim)(struct wim_dentry *dentry, struct apply_ctx *ctx);
252 
253 	/*
254 	 * Size of the backend-specific extraction context.  It must contain
255 	 * 'struct apply_ctx' as its first member.
256 	 */
257 	size_t context_size;
258 
259 	/*
260 	 * Set this if the extraction backend only supports extracting dentries
261 	 * that form a single tree, not multiple trees.
262 	 */
263 	bool single_tree_only;
264 };
265 
266 #ifdef __WIN32__
267   extern const struct apply_operations win32_apply_ops;
268 #else
269   extern const struct apply_operations unix_apply_ops;
270 #endif
271 
272 #ifdef WITH_NTFS_3G
273   extern const struct apply_operations ntfs_3g_apply_ops;
274 #endif
275 
276 #endif /* _WIMLIB_APPLY_H */
277