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