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