]> wimlib.net Git - wimlib/blob - src/header.c
Improve write streams performance and handling of joins
[wimlib] / src / header.c
1 /*
2  * header.c
3  *
4  * Read, write, or create a WIM header.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
12  * wimlib is free software; you can redistribute it and/or modify it under the
13  * terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3 of the License, or (at your option)
15  * any later version.
16  *
17  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19  * A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with wimlib; if not, see http://www.gnu.org/licenses/.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "wimlib.h"
31 #include "wimlib/assert.h"
32 #include "wimlib/endianness.h"
33 #include "wimlib/error.h"
34 #include "wimlib/file_io.h"
35 #include "wimlib/header.h"
36 #include "wimlib/util.h"
37 #include "wimlib/wim.h"
38
39 #include <limits.h>
40 #include <string.h>
41 #include <unistd.h>
42 #ifdef HAVE_ALLOCA_H
43 #  include <alloca.h>
44 #else
45 #  include <stdlib.h>
46 #endif
47
48 /* On-disk format of the WIM header. */
49 struct wim_header_disk {
50
51         /* Magic characters "MSWIM\0\0\0" */
52         le64 magic;
53
54         /* Size of the WIM header, in bytes; WIM_HEADER_DISK_SIZE expected
55          * (currently the only supported value). */
56         u32 hdr_size;
57
58         /* Version of the WIM file; WIM_VERSION expected (currently the only
59          * supported value). */
60         u32 wim_version;
61
62         /* Flags for the WIM file (WIM_HDR_FLAG_*) */
63         u32 wim_flags;
64
65         /* Uncompressed chunk size of resources in the WIM.  0 if the WIM is
66          * uncompressed.  If compressed, WIM_CHUNK_SIZE is expected (currently
67          * the only supported value).  */
68         u32 chunk_size;
69
70         /* Globally unique identifier for the WIM file.  Basically a bunch of
71          * random bytes. */
72         u8 guid[WIM_GID_LEN];
73
74         /* Number of this WIM part in the split WIM file, indexed from 1, or 1
75          * if the WIM is not split. */
76         u16 part_number;
77
78         /* Total number of parts of the split WIM file, or 1 if the WIM is not
79          * split. */
80         u16 total_parts;
81
82         /* Number of images in the WIM. */
83         u32 image_count;
84
85         /* Location and size of the WIM's lookup table. */
86         struct resource_entry_disk lookup_table_res_entry;
87
88         /* Location and size of the WIM's XML data. */
89         struct resource_entry_disk xml_data_res_entry;
90
91         /* Location and size of metadata resource for the bootable image of the
92          * WIM, or all zeroes if no image is bootable. */
93         struct resource_entry_disk boot_metadata_res_entry;
94
95         /* 1-based index of the bootable image of the WIM, or 0 if no image is
96          * bootable. */
97         u32 boot_idx;
98
99         /* Location and size of the WIM's integrity table, or all zeroes if the
100          * WIM has no integrity table.
101          *
102          * Note the integrity_table_res_entry here is 4-byte aligned even though
103          * it would ordinarily be 8-byte aligned--- hence, the _packed_attribute
104          * on the `struct wim_header_disk' is essential. */
105         struct resource_entry_disk integrity_table_res_entry;
106
107         /* Unused bytes. */
108         u8 unused[60];
109 } _packed_attribute;
110
111 /*
112  * Reads the header from a WIM file.
113  *
114  * @filename
115  *      Name of the WIM file (for error/debug messages only), or NULL if reading
116  *      directly from file descriptor.
117  * @in_fd
118  *      File descriptor, positioned at offset 0, to read the header from.
119  * @hdr
120  *      Structure to read the header into.
121  *
122  * Return values:
123  *      WIMLIB_ERR_SUCCESS (0)
124  *      WIMLIB_ERR_IMAGE_COUNT
125  *      WIMLIB_ERR_INVALID_CHUNK_SIZE
126  *      WIMLIB_ERR_INVALID_PART_NUMBER
127  *      WIMLIB_ERR_NOT_A_WIM_FILE
128  *      WIMLIB_ERR_READ
129  *      WIMLIB_ERR_UNEXPECTED_END_OF_FILE
130  *      WIMLIB_ERR_UNKNOWN_VERSION
131  */
132 int
133 read_wim_header(const tchar *filename, struct filedes *in_fd,
134                 struct wim_header *hdr)
135 {
136         struct wim_header_disk disk_hdr _aligned_attribute(8);
137         int ret;
138         tchar *pipe_str;
139
140         wimlib_assert(in_fd->offset == 0);
141
142         if (!filename) {
143                 pipe_str = alloca(40);
144                 tsprintf(pipe_str, T("[fd %d]"), in_fd->fd);
145                 filename = pipe_str;
146         }
147
148         BUILD_BUG_ON(sizeof(struct wim_header_disk) != WIM_HEADER_DISK_SIZE);
149
150         DEBUG("Reading WIM header from \"%"TS"\"", filename);
151
152         ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
153         if (ret)
154                 goto read_error;
155
156         if (disk_hdr.magic != WIM_MAGIC) {
157                 if (disk_hdr.magic == PWM_MAGIC) {
158                         /* Pipable WIM:  Use header at end instead, unless
159                          * actually reading from a pipe.  */
160                         if (!in_fd->is_pipe) {
161                                 lseek(in_fd->fd, -WIM_HEADER_DISK_SIZE, SEEK_END);
162                                 ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
163                                 if (ret)
164                                         goto read_error;
165                         }
166                 } else {
167                         ERROR("\"%"TS"\": Invalid magic characters in header", filename);
168                         return WIMLIB_ERR_NOT_A_WIM_FILE;
169                 }
170         }
171         hdr->magic = disk_hdr.magic;
172
173         if (le32_to_cpu(disk_hdr.hdr_size) != sizeof(struct wim_header_disk)) {
174                 ERROR("\"%"TS"\": Header size is invalid (%u bytes)",
175                       filename, le32_to_cpu(disk_hdr.hdr_size));
176                 return WIMLIB_ERR_INVALID_HEADER;
177         }
178
179         if (le32_to_cpu(disk_hdr.wim_version) != WIM_VERSION) {
180                 ERROR("\"%"TS"\": The WIM header says the WIM version is %u, "
181                       "but wimlib only knows about version %u",
182                       filename, le32_to_cpu(disk_hdr.wim_version), WIM_VERSION);
183                 return WIMLIB_ERR_UNKNOWN_VERSION;
184         }
185
186         hdr->flags = le32_to_cpu(disk_hdr.wim_flags);
187         if (le32_to_cpu(disk_hdr.chunk_size) != WIM_CHUNK_SIZE &&
188             (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
189                 ERROR("\"%"TS"\": Unexpected chunk size of %u! Ask the author to "
190                       "implement support for other chunk sizes.",
191                       filename, le32_to_cpu(disk_hdr.chunk_size));
192                 ERROR("(Or it might just be that the WIM header is invalid.)");
193                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
194         }
195
196         memcpy(hdr->guid, disk_hdr.guid, WIM_GID_LEN);
197
198         hdr->part_number = le16_to_cpu(disk_hdr.part_number);
199         hdr->total_parts = le16_to_cpu(disk_hdr.total_parts);
200
201         if (hdr->total_parts == 0 || hdr->part_number == 0 ||
202             hdr->part_number > hdr->total_parts)
203         {
204                 ERROR("\"%"TS"\": Invalid WIM part number: %hu of %hu",
205                       filename, hdr->part_number, hdr->total_parts);
206                 return WIMLIB_ERR_INVALID_PART_NUMBER;
207         }
208
209         hdr->image_count = le32_to_cpu(disk_hdr.image_count);
210
211         DEBUG("part_number = %u, total_parts = %u, image_count = %u",
212               hdr->part_number, hdr->total_parts, hdr->image_count);
213
214         if (hdr->image_count >= INT_MAX) {
215                 ERROR("\"%"TS"\": Invalid image count (%u)",
216                       filename, hdr->image_count);
217                 return WIMLIB_ERR_IMAGE_COUNT;
218         }
219
220         get_resource_entry(&disk_hdr.lookup_table_res_entry, &hdr->lookup_table_res_entry);
221         get_resource_entry(&disk_hdr.xml_data_res_entry, &hdr->xml_res_entry);
222         get_resource_entry(&disk_hdr.boot_metadata_res_entry, &hdr->boot_metadata_res_entry);
223         hdr->boot_idx = le32_to_cpu(disk_hdr.boot_idx);
224         get_resource_entry(&disk_hdr.integrity_table_res_entry, &hdr->integrity);
225         return 0;
226
227 read_error:
228         ERROR_WITH_ERRNO("\"%"TS"\": Error reading header", filename);
229         return ret;
230 }
231
232 /* Writes the header for a WIM file at the specified offset.  If the offset
233  * specified is the current one, the position is advanced by the size of the
234  * header.  */
235 int
236 write_wim_header_at_offset(const struct wim_header *hdr, struct filedes *out_fd,
237                            off_t offset)
238 {
239         struct wim_header_disk disk_hdr _aligned_attribute(8);
240         int ret;
241
242         DEBUG("Writing %sWIM header at offset %"PRIu64,
243               ((hdr->magic == PWM_MAGIC) ? "pipable " : ""),
244               offset);
245
246         disk_hdr.magic = hdr->magic;
247         disk_hdr.hdr_size = cpu_to_le32(sizeof(struct wim_header_disk));
248         disk_hdr.wim_version = cpu_to_le32(WIM_VERSION);
249         disk_hdr.wim_flags = cpu_to_le32(hdr->flags);
250         disk_hdr.chunk_size = cpu_to_le32((hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
251                                                 WIM_CHUNK_SIZE : 0);
252         memcpy(disk_hdr.guid, hdr->guid, WIM_GID_LEN);
253
254         disk_hdr.part_number = cpu_to_le16(hdr->part_number);
255         disk_hdr.total_parts = cpu_to_le16(hdr->total_parts);
256         disk_hdr.image_count = cpu_to_le32(hdr->image_count);
257         put_resource_entry(&hdr->lookup_table_res_entry, &disk_hdr.lookup_table_res_entry);
258         put_resource_entry(&hdr->xml_res_entry, &disk_hdr.xml_data_res_entry);
259         put_resource_entry(&hdr->boot_metadata_res_entry, &disk_hdr.boot_metadata_res_entry);
260         disk_hdr.boot_idx = cpu_to_le32(hdr->boot_idx);
261         put_resource_entry(&hdr->integrity, &disk_hdr.integrity_table_res_entry);
262         memset(disk_hdr.unused, 0, sizeof(disk_hdr.unused));
263
264         if (offset == out_fd->offset)
265                 ret = full_write(out_fd, &disk_hdr, sizeof(disk_hdr));
266         else
267                 ret = full_pwrite(out_fd, &disk_hdr, sizeof(disk_hdr), offset);
268         if (ret)
269                 ERROR_WITH_ERRNO("Failed to write WIM header");
270         return ret;
271 }
272
273 /* Writes the header for a WIM file at the output file descriptor's current
274  * offset.  */
275 int
276 write_wim_header(const struct wim_header *hdr, struct filedes *out_fd)
277 {
278         return write_wim_header_at_offset(hdr, out_fd, out_fd->offset);
279 }
280
281 /* Update just the wim_flags field. */
282 int
283 write_wim_header_flags(u32 hdr_flags, struct filedes *out_fd)
284 {
285         le32 flags = cpu_to_le32(hdr_flags);
286
287         return full_pwrite(out_fd, &flags, sizeof(flags),
288                            offsetof(struct wim_header_disk, wim_flags));
289 }
290
291 /*
292  * Initializes the header for a WIM file.
293  */
294 int
295 init_wim_header(struct wim_header *hdr, int ctype)
296 {
297         memset(hdr, 0, sizeof(struct wim_header));
298         switch (ctype) {
299         case WIMLIB_COMPRESSION_TYPE_NONE:
300                 hdr->flags = 0;
301                 break;
302         case WIMLIB_COMPRESSION_TYPE_LZX:
303                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
304                              WIM_HDR_FLAG_COMPRESS_LZX;
305                 break;
306         case WIMLIB_COMPRESSION_TYPE_XPRESS:
307                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
308                              WIM_HDR_FLAG_COMPRESS_XPRESS;
309                 break;
310         default:
311                 ERROR("Invalid compression type specified (%d)", ctype);
312                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
313         }
314         hdr->total_parts = 1;
315         hdr->part_number = 1;
316         randomize_byte_array(hdr->guid, sizeof(hdr->guid));
317         hdr->magic = WIM_MAGIC;
318         return 0;
319 }
320
321 struct hdr_flag {
322         u32 flag;
323         const char *name;
324 };
325 struct hdr_flag hdr_flags[] = {
326         {WIM_HDR_FLAG_RESERVED,         "RESERVED"},
327         {WIM_HDR_FLAG_COMPRESSION,      "COMPRESSION"},
328         {WIM_HDR_FLAG_READONLY,         "READONLY"},
329         {WIM_HDR_FLAG_SPANNED,          "SPANNED"},
330         {WIM_HDR_FLAG_RESOURCE_ONLY,    "RESOURCE_ONLY"},
331         {WIM_HDR_FLAG_METADATA_ONLY,    "METADATA_ONLY"},
332         {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
333         {WIM_HDR_FLAG_RP_FIX,           "RP_FIX"},
334         {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
335         {WIM_HDR_FLAG_COMPRESS_LZX,     "COMPRESS_LZX"},
336         {WIM_HDR_FLAG_COMPRESS_XPRESS,  "COMPRESS_XPRESS"},
337 };
338
339 /* API function documented in wimlib.h  */
340 WIMLIBAPI void
341 wimlib_print_header(const WIMStruct *wim)
342 {
343         const struct wim_header *hdr = &wim->hdr;
344
345         tprintf(T("Magic Characters            = MSWIM\\000\\000\\000\n"));
346         tprintf(T("Header Size                 = %u\n"), WIM_HEADER_DISK_SIZE);
347         tprintf(T("Version                     = 0x%x\n"), WIM_VERSION);
348
349         tprintf(T("Flags                       = 0x%x\n"), hdr->flags);
350         for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
351                 if (hdr_flags[i].flag & hdr->flags)
352                         tprintf(T("    WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
353
354         tprintf(T("Chunk Size                  = %u\n"), WIM_CHUNK_SIZE);
355         tfputs (T("GUID                        = "), stdout);
356         print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
357         tputchar(T('\n'));
358         tprintf(T("Part Number                 = %hu\n"), wim->hdr.part_number);
359         tprintf(T("Total Parts                 = %hu\n"), wim->hdr.total_parts);
360         tprintf(T("Image Count                 = %u\n"), hdr->image_count);
361         tprintf(T("Lookup Table Size           = %"PRIu64"\n"),
362                                 (u64)hdr->lookup_table_res_entry.size);
363         tprintf(T("Lookup Table Flags          = 0x%hhx\n"),
364                                 (u8)hdr->lookup_table_res_entry.flags);
365         tprintf(T("Lookup Table Offset         = %"PRIu64"\n"),
366                                 hdr->lookup_table_res_entry.offset);
367         tprintf(T("Lookup Table Original_size  = %"PRIu64"\n"),
368                                 hdr->lookup_table_res_entry.original_size);
369         tprintf(T("XML Data Size               = %"PRIu64"\n"),
370                                 (u64)hdr->xml_res_entry.size);
371         tprintf(T("XML Data Flags              = 0x%hhx\n"),
372                                 (u8)hdr->xml_res_entry.flags);
373         tprintf(T("XML Data Offset             = %"PRIu64"\n"),
374                                 hdr->xml_res_entry.offset);
375         tprintf(T("XML Data Original Size      = %"PRIu64"\n"),
376                                 hdr->xml_res_entry.original_size);
377         tprintf(T("Boot Metadata Size          = %"PRIu64"\n"),
378                                 (u64)hdr->boot_metadata_res_entry.size);
379         tprintf(T("Boot Metadata Flags         = 0x%hhx\n"),
380                                 (u8)hdr->boot_metadata_res_entry.flags);
381         tprintf(T("Boot Metadata Offset        = %"PRIu64"\n"),
382                                 hdr->boot_metadata_res_entry.offset);
383         tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
384                                 hdr->boot_metadata_res_entry.original_size);
385         tprintf(T("Boot Index                  = %u\n"), hdr->boot_idx);
386         tprintf(T("Integrity Size              = %"PRIu64"\n"),
387                                 (u64)hdr->integrity.size);
388         tprintf(T("Integrity Flags             = 0x%hhx\n"),
389                                 (u8)hdr->integrity.flags);
390         tprintf(T("Integrity Offset            = %"PRIu64"\n"),
391                                 hdr->integrity.offset);
392         tprintf(T("Integrity Original_size     = %"PRIu64"\n"),
393                                 hdr->integrity.original_size);
394 }