]> wimlib.net Git - wimlib/blob - include/wimlib/wim.h
wim.c: replace finalize_wim_struct() with wim_decrement_refcnt()
[wimlib] / include / wimlib / wim.h
1 /*
2  * wim.h - WIMStruct definition and helper functions
3  */
4
5 #ifndef _WIMLIB_WIM_H
6 #define _WIMLIB_WIM_H
7
8 #include "wimlib.h"
9 #include "wimlib/file_io.h"
10 #include "wimlib/header.h"
11 #include "wimlib/list.h"
12
13 struct wim_image_metadata;
14 struct wim_xml_info;
15 struct blob_table;
16
17 /*
18  * WIMStruct - represents a WIM, or a part of a non-standalone WIM
19  *
20  * Note 1: there are three ways in which a WIMStruct can be created:
21  *
22  *      1. open an on-disk WIM file
23  *      2. start to extract a pipable WIM from a file descriptor
24  *      3. create a new WIMStruct directly
25  *
26  * For (1) and (2), the WIMStruct has a backing file; for (3) it does not.  For
27  * (1), the backing file is a real "on-disk" file from the filesystem, whereas
28  * for (2) the backing file is a file descriptor which may be a pipe.
29  *
30  * Note 2: although this is the top-level data structure in wimlib, there do
31  * exist cases in which a WIMStruct is not standalone:
32  *      - blobs have been referenced from another WIMStruct
33  *      - an image has been imported into this WIMStruct from another
34  *        (as this references the metadata rather than copies it)
35  *
36  * Note 3: It is unsafe for multiple threads to operate on the same WIMStruct at
37  * the same time.  This extends to references to other WIMStructs as noted
38  * above.  But besides this, it is safe to operate on *different* WIMStructs in
39  * different threads concurrently.
40  */
41 struct WIMStruct {
42
43         /* Information from the header of the WIM file.
44          *
45          * This is also maintained for a WIMStruct not backed by a file, but in
46          * that case the 'reshdr' fields are left zeroed.  */
47         struct wim_header hdr;
48
49         /* If the library is currently writing this WIMStruct out to a file,
50          * then this is the header being created for that file.  */
51         struct wim_header out_hdr;
52
53         /* Array of image metadata, one for each image in the WIM (array length
54          * hdr.image_count).  Or, this will be NULL if this WIM does not contain
55          * metadata, which implies that this WIMStruct either represents part of
56          * a non-standalone WIM, or represents a standalone WIM that, oddly
57          * enough, actually contains 0 images.  */
58         struct wim_image_metadata **image_metadata;
59
60         /* Information from the XML data of the WIM file.  This information is
61          * also maintained for a WIMStruct not backed by a file.  */
62         struct wim_xml_info *xml_info;
63
64         /* The blob table for this WIMStruct.  If this WIMStruct has a backing
65          * file, then this table will index the blobs contained in that file.
66          * In addition, this table may index blobs that were added by updates or
67          * referenced from other WIMStructs.  */
68         struct blob_table *blob_table;
69
70         /* The number of references to this WIMStruct.  This is equal to the
71          * number of resource descriptors that reference this WIMStruct, plus 1
72          * if wimlib_free() still needs to be called.  */
73         ssize_t refcnt;
74
75         /*
76          * The 1-based index of the currently selected image in this WIMStruct,
77          * or WIMLIB_NO_IMAGE if no image is currently selected.
78          *
79          * The metadata for the current image is image_metadata[current_image -
80          * 1].  Since we load image metadata lazily, only the metadata for the
81          * current image is guaranteed to actually be present in memory.
82          */
83         int current_image;
84
85         /* The absolute path to the on-disk file backing this WIMStruct, or NULL
86          * if this WIMStruct is not backed by an on-disk file.  */
87         tchar *filename;
88
89         /* If this WIMStruct has a backing file, then this is a file descriptor
90          * open to that file with read access.  Otherwise, this field is invalid
91          * (!filedes_valid(&in_fd)).  */
92         struct filedes in_fd;
93
94         /* If the library is currently writing this WIMStruct out to a file,
95          * then this is a file descriptor open to that file with write access.
96          * Otherwise, this field is invalid (!filedes_valid(&out_fd)).  */
97         struct filedes out_fd;
98
99         /*
100          * This is the cached decompressor for this WIM file, or NULL if no
101          * decompressor is cached yet.  Normally, all the compressed data in a
102          * WIM file has the same compression type and chunk size, so the same
103          * decompressor can be used for all data --- and that decompressor will
104          * be cached here.  However, if we do encounter any data with a
105          * different compression type or chunk size (this is possible in solid
106          * resources), then this cached decompressor will be replaced with a new
107          * one.
108          */
109         struct wimlib_decompressor *decompressor;
110         u8 decompressor_ctype;
111         u32 decompressor_max_block_size;
112
113         /* Temporary field; use sparingly  */
114         void *private;
115
116         /* 1 if any images have been deleted from this WIMStruct, otherwise 0 */
117         u8 image_deletion_occurred : 1;
118
119         /* 1 if the WIM file has been locked for appending, otherwise 0  */
120         u8 locked_for_append : 1;
121
122         /* 1 if the WIM file is currently being compacted by wimlib_overwrite()
123          * with WIMLIB_WRITE_FLAG_UNSAFE_COMPACT  */
124         u8 being_compacted : 1;
125
126         /* If this WIM is backed by a file, then this is the compression type
127          * for non-solid resources in that file.  */
128         u8 compression_type;
129
130         /* Overridden compression type for wimlib_overwrite() or wimlib_write().
131          * Can be changed by wimlib_set_output_compression_type(); otherwise is
132          * the same as compression_type.  */
133         u8 out_compression_type;
134
135         /* Compression type for writing solid resources; can be set with
136          * wimlib_set_output_pack_compression_type().  */
137         u8 out_solid_compression_type;
138
139         /* If this WIM is backed by a file, then this is the compression chunk
140          * size for non-solid resources in that file.  */
141         u32 chunk_size;
142
143         /* Overridden chunk size for wimlib_overwrite() or wimlib_write().  Can
144          * be changed by wimlib_set_output_chunk_size(); otherwise is the same
145          * as chunk_size.  */
146         u32 out_chunk_size;
147
148         /* Chunk size for writing solid resources; can be set with
149          * wimlib_set_output_pack_chunk_size().  */
150         u32 out_solid_chunk_size;
151
152         /* Currently registered progress function for this WIMStruct, or NULL if
153          * no progress function is currently registered for this WIMStruct.  */
154         wimlib_progress_func_t progfunc;
155         void *progctx;
156 };
157
158 /*
159  * Return true if and only if the WIM contains image metadata (actual directory
160  * trees, not just a collection of blobs and their checksums).
161  *
162  * See the description of the 'image_metadata' field.  Note that we return true
163  * when the image count is 0 because it could be a WIM with 0 images.  It's only
164  * when the WIM does not contain the metadata described by its image count that
165  * we return false.
166  */
167 static inline bool wim_has_metadata(const WIMStruct *wim)
168 {
169         return (wim->image_metadata != NULL || wim->hdr.image_count == 0);
170 }
171
172 /* Return true if and only if the WIM has an integrity table.
173  *
174  * If the WIM is not backed by a file, then this always returns false.  */
175 static inline bool wim_has_integrity_table(const WIMStruct *wim)
176 {
177         return (wim->hdr.integrity_table_reshdr.offset_in_wim != 0);
178 }
179
180 /* Return true if and only if the WIM is in pipable format.
181  *
182  * If the WIM is not backed by a file, then this always returns false.  */
183 static inline bool wim_is_pipable(const WIMStruct *wim)
184 {
185         return (wim->hdr.magic == PWM_MAGIC);
186 }
187
188 extern void
189 wim_decrement_refcnt(WIMStruct *wim);
190
191 extern bool
192 wim_has_solid_resources(const WIMStruct *wim);
193
194 extern int
195 read_wim_header(WIMStruct *wim, struct wim_header *hdr);
196
197 extern int
198 write_wim_header(const struct wim_header *hdr, struct filedes *out_fd,
199                  off_t offset);
200
201 extern int
202 write_wim_header_flags(u32 hdr_flags, struct filedes *out_fd);
203
204 extern int
205 select_wim_image(WIMStruct *wim, int image);
206
207 extern void
208 deselect_current_wim_image(WIMStruct *wim);
209
210 extern int
211 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *));
212
213 extern int
214 wim_checksum_unhashed_blobs(WIMStruct *wim);
215
216 extern int
217 delete_wim_image(WIMStruct *wim, int image);
218
219 /* Internal open flags (pass to open_wim_as_WIMStruct(), not wimlib_open_wim())
220  */
221 #define WIMLIB_OPEN_FLAG_FROM_PIPE      0x80000000
222
223 extern int
224 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
225                       WIMStruct **wim_ret,
226                       wimlib_progress_func_t progfunc, void *progctx);
227
228 extern int
229 can_modify_wim(WIMStruct *wim);
230
231 #endif /* _WIMLIB_WIM_H */