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