]> wimlib.net Git - wimlib/blob - src/reparse.c
Update progress functions
[wimlib] / src / reparse.c
1 /*
2  * reparse.c - Handle reparse data.
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib/assert.h"
29 #include "wimlib/compiler.h"
30 #include "wimlib/endianness.h"
31 #include "wimlib/encoding.h"
32 #include "wimlib/error.h"
33 #include "wimlib/inode.h"
34 #include "wimlib/lookup_table.h"
35 #include "wimlib/reparse.h"
36 #include "wimlib/resource.h"
37
38 #ifdef HAVE_ALLOCA_H
39 #  include <alloca.h>
40 #endif
41 #include <errno.h>
42 #include <stdlib.h>
43
44 /*
45  * Read the data from a symbolic link, junction, or mount point reparse point
46  * buffer into a `struct reparse_data'.
47  *
48  * See http://msdn.microsoft.com/en-us/library/cc232006(v=prot.10).aspx for a
49  * description of the format of the reparse point buffers.
50  */
51 int
52 parse_reparse_data(const u8 * restrict rpbuf, u16 rpbuflen,
53                    struct reparse_data * restrict rpdata)
54 {
55         u16 substitute_name_offset;
56         u16 print_name_offset;
57         const struct reparse_buffer_disk *rpbuf_disk =
58                 (const struct reparse_buffer_disk*)rpbuf;
59         const u8 *data;
60
61         memset(rpdata, 0, sizeof(*rpdata));
62         if (rpbuflen < 16)
63                 goto out_invalid;
64         rpdata->rptag = le32_to_cpu(rpbuf_disk->rptag);
65         wimlib_assert(rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK ||
66                       rpdata->rptag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
67         rpdata->rpdatalen = le16_to_cpu(rpbuf_disk->rpdatalen);
68         rpdata->rpreserved = le16_to_cpu(rpbuf_disk->rpreserved);
69         substitute_name_offset = le16_to_cpu(rpbuf_disk->symlink.substitute_name_offset);
70         rpdata->substitute_name_nbytes = le16_to_cpu(rpbuf_disk->symlink.substitute_name_nbytes);
71         print_name_offset = le16_to_cpu(rpbuf_disk->symlink.print_name_offset);
72         rpdata->print_name_nbytes = le16_to_cpu(rpbuf_disk->symlink.print_name_nbytes);
73
74         if ((substitute_name_offset & 1) | (print_name_offset & 1) |
75             (rpdata->substitute_name_nbytes & 1) | (rpdata->print_name_nbytes & 1))
76         {
77                 /* Names would be unaligned... */
78                 goto out_invalid;
79         }
80
81         if (rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK) {
82                 if (rpbuflen < 20)
83                         goto out_invalid;
84                 rpdata->rpflags = le32_to_cpu(rpbuf_disk->symlink.rpflags);
85                 data = rpbuf_disk->symlink.data;
86         } else {
87                 data = rpbuf_disk->junction.data;
88         }
89         if ((size_t)substitute_name_offset + rpdata->substitute_name_nbytes +
90             (data - rpbuf) > rpbuflen)
91                 goto out_invalid;
92         if ((size_t)print_name_offset + rpdata->print_name_nbytes +
93             (data - rpbuf) > rpbuflen)
94                 goto out_invalid;
95         rpdata->substitute_name = (utf16lechar*)&data[substitute_name_offset];
96         rpdata->print_name = (utf16lechar*)&data[print_name_offset];
97         return 0;
98 out_invalid:
99         ERROR("Invalid reparse data");
100         return WIMLIB_ERR_INVALID_REPARSE_DATA;
101 }
102
103 /*
104  * Create a reparse point data buffer.
105  *
106  * @rpdata:  Structure that contains the data we need.
107  *
108  * @rpbuf:     Buffer into which to write the reparse point data buffer.  Must be
109  *              at least REPARSE_POINT_MAX_SIZE bytes long.
110  */
111 int
112 make_reparse_buffer(const struct reparse_data * restrict rpdata,
113                     u8 * restrict rpbuf,
114                     u16 * restrict rpbuflen_ret)
115 {
116         struct reparse_buffer_disk *rpbuf_disk =
117                 (struct reparse_buffer_disk*)rpbuf;
118         u8 *data;
119
120         rpbuf_disk->rptag = cpu_to_le32(rpdata->rptag);
121         rpbuf_disk->rpreserved = cpu_to_le16(rpdata->rpreserved);
122         rpbuf_disk->symlink.substitute_name_offset = cpu_to_le16(0);
123         rpbuf_disk->symlink.substitute_name_nbytes = cpu_to_le16(rpdata->substitute_name_nbytes);
124         rpbuf_disk->symlink.print_name_offset = cpu_to_le16(rpdata->substitute_name_nbytes + 2);
125         rpbuf_disk->symlink.print_name_nbytes = cpu_to_le16(rpdata->print_name_nbytes);
126
127         if (rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK) {
128                 rpbuf_disk->symlink.rpflags = cpu_to_le32(rpdata->rpflags);
129                 data = rpbuf_disk->symlink.data;
130         } else {
131                 data = rpbuf_disk->junction.data;
132         }
133
134         /* We null-terminate the substitute and print names, although this may
135          * not be strictly necessary.  Note that the byte counts should not
136          * include the null terminators. */
137         if (data + rpdata->substitute_name_nbytes +
138             rpdata->print_name_nbytes +
139             2 * sizeof(utf16lechar) - rpbuf > REPARSE_POINT_MAX_SIZE)
140         {
141                 ERROR("Reparse data is too long!");
142                 return WIMLIB_ERR_INVALID_REPARSE_DATA;
143         }
144         data = mempcpy(data, rpdata->substitute_name, rpdata->substitute_name_nbytes);
145         *(utf16lechar*)data = cpu_to_le16(0);
146         data += 2;
147         data = mempcpy(data, rpdata->print_name, rpdata->print_name_nbytes);
148         *(utf16lechar*)data = cpu_to_le16(0);
149         data += 2;
150         rpbuf_disk->rpdatalen = cpu_to_le16(data - rpbuf - 8);
151         *rpbuflen_ret = data - rpbuf;
152         return 0;
153 }
154
155 /*
156  * Read the reparse data from a WIM inode that is a reparse point.
157  *
158  * @rpbuf points to a buffer at least REPARSE_POINT_MAX_SIZE bytes into which
159  * the reparse point data buffer will be reconstructed.
160  *
161  * Note: in the WIM format, the first 8 bytes of the reparse point data buffer
162  * are omitted, presumably because we already know the reparse tag from the
163  * dentry, and we already know the reparse tag length from the lookup table
164  * entry resource length.  However, we reconstruct the first 8 bytes in the
165  * buffer returned by this function.
166  */
167 int
168 wim_inode_get_reparse_data(const struct wim_inode * restrict inode,
169                            u8 * restrict rpbuf,
170                            u16 * restrict rpbuflen_ret,
171                            struct wim_lookup_table_entry *lte_override)
172 {
173         struct wim_lookup_table_entry *lte;
174         int ret;
175         struct reparse_buffer_disk *rpbuf_disk;
176         u16 rpdatalen;
177
178         wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
179
180         if (!lte_override) {
181                 lte = inode_unnamed_lte_resolved(inode);
182                 if (!lte) {
183                         ERROR("Reparse point has no reparse data!");
184                         return WIMLIB_ERR_INVALID_REPARSE_DATA;
185                 }
186         } else {
187                 lte = lte_override;
188         }
189
190         if (lte->size > REPARSE_POINT_MAX_SIZE - 8) {
191                 ERROR("Reparse data is too long!");
192                 return WIMLIB_ERR_INVALID_REPARSE_DATA;
193         }
194         rpdatalen = lte->size;
195
196         /* Read the data from the WIM file */
197         ret = read_full_stream_into_buf(lte, rpbuf + 8);
198         if (ret)
199                 return ret;
200
201         /* Reconstruct the first 8 bytes of the reparse point buffer */
202         rpbuf_disk = (struct reparse_buffer_disk*)rpbuf;
203
204         /* ReparseTag */
205         rpbuf_disk->rptag = cpu_to_le32(inode->i_reparse_tag);
206
207         /* ReparseDataLength */
208         rpbuf_disk->rpdatalen = cpu_to_le16(rpdatalen);
209
210         /* ReparseReserved
211          * XXX this could be one of the unknown fields in the WIM dentry. */
212         rpbuf_disk->rpreserved = cpu_to_le16(0);
213
214         *rpbuflen_ret = rpdatalen + 8;
215         return 0;
216 }
217
218 /* UNIX version of getting and setting the data in reparse points */
219 #ifndef __WIN32__
220
221 static const utf16lechar volume_junction_prefix[11] = {
222         cpu_to_le16('\\'),
223         cpu_to_le16('?'),
224         cpu_to_le16('?'),
225         cpu_to_le16('\\'),
226         cpu_to_le16('V'),
227         cpu_to_le16('o'),
228         cpu_to_le16('l'),
229         cpu_to_le16('u'),
230         cpu_to_le16('m'),
231         cpu_to_le16('e'),
232         cpu_to_le16('{'),
233 };
234
235 enum {
236         SUBST_NAME_IS_RELATIVE_LINK = -1,
237         SUBST_NAME_IS_VOLUME_JUNCTION = -2,
238         SUBST_NAME_IS_UNKNOWN = -3,
239 };
240
241 /* Parse the "substitute name" (link target) from a symbolic link or junction
242  * reparse point.
243  *
244  * Return value is:
245  *
246  * Non-negative integer:
247  *      The name is an absolute symbolic link in one of several formats,
248  *      and the return value is the number of UTF-16LE characters that need to
249  *      be advanced to reach a simple "absolute" path starting with a backslash
250  *      (i.e. skip over \??\ and/or drive letter)
251  * Negative integer:
252  *      SUBST_NAME_IS_VOLUME_JUNCTION:
253  *              The name is a volume junction.
254  *      SUBST_NAME_IS_RELATIVE_LINK:
255  *              The name is a relative symbolic link.
256  *      SUBST_NAME_IS_UNKNOWN:
257  *              The name does not appear to be a valid symbolic link, junction,
258  *              or mount point.
259  */
260 static int
261 parse_substitute_name(const utf16lechar *substitute_name,
262                       u16 substitute_name_nbytes, u32 rptag)
263 {
264         u16 substitute_name_nchars = substitute_name_nbytes / 2;
265
266         if (substitute_name_nchars >= 7 &&
267             substitute_name[0] == cpu_to_le16('\\') &&
268             substitute_name[1] == cpu_to_le16('?') &&
269             substitute_name[2] == cpu_to_le16('?') &&
270             substitute_name[3] == cpu_to_le16('\\') &&
271             substitute_name[4] != cpu_to_le16('\0') &&
272             substitute_name[5] == cpu_to_le16(':') &&
273             substitute_name[6] == cpu_to_le16('\\'))
274         {
275                 /* "Full" symlink or junction (\??\x:\ prefixed path) */
276                 return 6;
277         } else if (rptag == WIM_IO_REPARSE_TAG_MOUNT_POINT &&
278                    substitute_name_nchars >= 12 &&
279                    memcmp(substitute_name, volume_junction_prefix,
280                           sizeof(volume_junction_prefix)) == 0 &&
281                    substitute_name[substitute_name_nchars - 1] == cpu_to_le16('\\'))
282         {
283                 /* Volume junction.  Can't really do anything with it. */
284                 return SUBST_NAME_IS_VOLUME_JUNCTION;
285         } else if (rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
286                    substitute_name_nchars >= 3 &&
287                    substitute_name[0] != cpu_to_le16('\0') &&
288                    substitute_name[1] == cpu_to_le16(':') &&
289                    substitute_name[2] == cpu_to_le16('\\'))
290         {
291                 /* "Absolute" symlink, with drive letter */
292                 return 2;
293         } else if (rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
294                    substitute_name_nchars >= 1)
295         {
296                 if (substitute_name[0] == cpu_to_le16('\\'))
297                         /* "Absolute" symlink, without drive letter */
298                         return 0;
299                 else
300                         /* "Relative" symlink, without drive letter */
301                         return SUBST_NAME_IS_RELATIVE_LINK;
302         } else {
303                 return SUBST_NAME_IS_UNKNOWN;
304         }
305 }
306
307 /*
308  * Get the UNIX-style symlink target from the WIM inode for a reparse point.
309  * Specifically, this translates the target from UTF-16 to the current multibyte
310  * encoding, strips the drive prefix if present, and replaces backslashes with
311  * forward slashes.
312  *
313  * @inode
314  *      The inode to read the symlink from.  It must be a reparse point with
315  *      tag WIM_IO_REPARSE_TAG_SYMLINK (a real symlink) or
316  *      WIM_IO_REPARSE_TAG_MOUNT_POINT (a mount point or junction point).
317  *
318  * @buf
319  *      Buffer into which to place the link target.
320  *
321  * @bufsize
322  *      Available space in @buf, in bytes.
323  *
324  * @lte_override
325  *      If not NULL, the stream from which to read the reparse data.  Otherwise,
326  *      the reparse data will be read from the unnamed stream of @inode.
327  *
328  * If the entire symbolic link target was placed in the buffer, returns the
329  * number of bytes written.  The resulting string is not null-terminated.  If
330  * the symbolic link target was too large to be placed in the buffer, the first
331  * @bufsize bytes of it are placed in the buffer and
332  * -ENAMETOOLONG is returned.  Otherwise, a negative errno value indicating
333  *  another error is returned.
334  */
335 ssize_t
336 wim_inode_readlink(const struct wim_inode * restrict inode,
337                    char * restrict buf, size_t bufsize,
338                    struct wim_lookup_table_entry *lte_override)
339 {
340         int ret;
341         struct reparse_buffer_disk rpbuf_disk _aligned_attribute(8);
342         struct reparse_data rpdata;
343         char *link_target;
344         char *translated_target;
345         size_t link_target_len;
346         u16 rpbuflen;
347
348         wimlib_assert(inode_is_symlink(inode));
349
350         if (wim_inode_get_reparse_data(inode, (u8*)&rpbuf_disk, &rpbuflen,
351                                        lte_override))
352                 return -EIO;
353
354         if (parse_reparse_data((const u8*)&rpbuf_disk, rpbuflen, &rpdata))
355                 return -EINVAL;
356
357         ret = utf16le_to_tstr(rpdata.substitute_name,
358                               rpdata.substitute_name_nbytes,
359                               &link_target, &link_target_len);
360         if (ret)
361                 return -errno;
362
363         translated_target = link_target;
364         ret = parse_substitute_name(rpdata.substitute_name,
365                                     rpdata.substitute_name_nbytes,
366                                     rpdata.rptag);
367         switch (ret) {
368         case SUBST_NAME_IS_RELATIVE_LINK:
369                 goto out_translate_slashes;
370         case SUBST_NAME_IS_VOLUME_JUNCTION:
371                 goto out_have_link;
372         case SUBST_NAME_IS_UNKNOWN:
373                 ERROR("Can't understand reparse point "
374                       "substitute name \"%s\"", link_target);
375                 ret = -EIO;
376                 goto out_free_link_target;
377         default:
378                 translated_target += ret;
379                 link_target_len -= ret;
380                 break;
381         }
382
383 out_translate_slashes:
384         for (size_t i = 0; i < link_target_len; i++)
385                 if (translated_target[i] == '\\')
386                         translated_target[i] = '/';
387 out_have_link:
388         if (link_target_len > bufsize) {
389                 link_target_len = bufsize;
390                 ret = -ENAMETOOLONG;
391         } else {
392                 ret = link_target_len;
393         }
394         memcpy(buf, translated_target, link_target_len);
395 out_free_link_target:
396         FREE(link_target);
397         return ret;
398 }
399
400 int
401 wim_inode_set_symlink(struct wim_inode *inode,
402                       const char *target,
403                       struct wim_lookup_table *lookup_table)
404
405 {
406         struct reparse_buffer_disk rpbuf_disk _aligned_attribute(8);
407         struct reparse_data rpdata;
408         static const char abs_subst_name_prefix[12] = "\\\0?\0?\0\\\0C\0:\0";
409         static const char abs_print_name_prefix[4] = "C\0:\0";
410         utf16lechar *name_utf16le;
411         size_t name_utf16le_nbytes;
412         int ret;
413         u16 rpbuflen;
414
415         DEBUG("Creating reparse point data buffer for UNIX "
416               "symlink target \"%s\"", target);
417         memset(&rpdata, 0, sizeof(rpdata));
418         ret = tstr_to_utf16le(target, strlen(target),
419                               &name_utf16le, &name_utf16le_nbytes);
420         if (ret)
421                 return ret;
422
423         for (size_t i = 0; i < name_utf16le_nbytes / 2; i++)
424                 if (name_utf16le[i] == cpu_to_le16('/'))
425                         name_utf16le[i] = cpu_to_le16('\\');
426
427         /* Compatability notes:
428          *
429          * On UNIX, an absolute symbolic link begins with '/'; everything else
430          * is a relative symbolic link.  (Quite simple compared to the various
431          * ways to provide Windows paths.)
432          *
433          * To change a UNIX relative symbolic link to Windows format, we only
434          * need to translate it to UTF-16LE and replace forward slashes with
435          * backslashes.  We do not make any attempt to handle filename character
436          * problems, such as a link target that itself contains backslashes on
437          * UNIX.  Then, for these relative links, we set the reparse header
438          * @flags field to SYMBOLIC_LINK_RELATIVE.
439          *
440          * For UNIX absolute symbolic links, we must set the @flags field to 0.
441          * Then, there are multiple options as to actually represent the
442          * absolute link targets:
443          *
444          * (1) An absolute path beginning with one backslash character. similar
445          * to UNIX-style, just with a different path separator.  Print name same
446          * as substitute name.
447          *
448          * (2) Absolute path beginning with drive letter followed by a
449          * backslash.  Print name same as substitute name.
450          *
451          * (3) Absolute path beginning with drive letter followed by a
452          * backslash; substitute name prefixed with \??\, otherwise same as
453          * print name.
454          *
455          * We choose option (3) here, and we just assume C: for the drive
456          * letter.  The reasoning for this is:
457          *
458          * (1) Microsoft imagex.exe has a bug where it does not attempt to do
459          * reparse point fixups for these links, even though they are valid
460          * absolute links.  (Note: in this case prefixing the substitute name
461          * with \??\ does not work; it just makes the data unable to be restored
462          * at all.)
463          * (2) Microsoft imagex.exe will fail when doing reparse point fixups
464          * for these.  It apparently contains a bug that causes it to create an
465          * invalid reparse point, which then cannot be restored.
466          * (3) This is the only option I tested for which reparse point fixups
467          * worked properly in Microsoft imagex.exe.
468          *
469          * So option (3) it is.
470          */
471
472         rpdata.rptag = inode->i_reparse_tag;
473         if (target[0] == '/') {
474                 rpdata.substitute_name_nbytes = name_utf16le_nbytes +
475                                                 sizeof(abs_subst_name_prefix);
476                 rpdata.print_name_nbytes = name_utf16le_nbytes +
477                                            sizeof(abs_print_name_prefix);
478                 rpdata.substitute_name = alloca(rpdata.substitute_name_nbytes);
479                 rpdata.print_name = alloca(rpdata.print_name_nbytes);
480                 memcpy(rpdata.substitute_name, abs_subst_name_prefix,
481                        sizeof(abs_subst_name_prefix));
482                 memcpy(rpdata.print_name, abs_print_name_prefix,
483                        sizeof(abs_print_name_prefix));
484                 memcpy((void*)rpdata.substitute_name + sizeof(abs_subst_name_prefix),
485                        name_utf16le, name_utf16le_nbytes);
486                 memcpy((void*)rpdata.print_name + sizeof(abs_print_name_prefix),
487                        name_utf16le, name_utf16le_nbytes);
488         } else {
489                 rpdata.substitute_name_nbytes = name_utf16le_nbytes;
490                 rpdata.print_name_nbytes = name_utf16le_nbytes;
491                 rpdata.substitute_name = name_utf16le;
492                 rpdata.print_name = name_utf16le;
493                 rpdata.rpflags = SYMBOLIC_LINK_RELATIVE;
494         }
495
496         ret = make_reparse_buffer(&rpdata, (u8*)&rpbuf_disk, &rpbuflen);
497         if (ret == 0) {
498                 ret = inode_set_unnamed_stream(inode,
499                                                (u8*)&rpbuf_disk + 8,
500                                                rpbuflen - 8,
501                                                lookup_table);
502         }
503         FREE(name_utf16le);
504         return ret;
505 }
506
507 #endif /* !__WIN32__ */