2 * reparse.c - Handle reparse data.
6 * Copyright (C) 2012, 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
28 #include "wimlib/assert.h"
29 #include "wimlib/compiler.h"
30 #include "wimlib/endianness.h"
31 #include "wimlib/dentry.h"
32 #include "wimlib/encoding.h"
33 #include "wimlib/error.h"
34 #include "wimlib/lookup_table.h"
35 #include "wimlib/reparse.h"
36 #include "wimlib/resource.h"
39 # include "wimlib/win32.h" /* for win32_get_file_and_vol_ids() */
48 /* On-disk format of a symbolic link (WIM_IO_REPARSE_TAG_SYMLINK) or junction
49 * point (WIM_IO_REPARSE_TAG_MOUNT_POINT) reparse data buffer. */
50 struct reparse_buffer_disk {
54 le16 substitute_name_offset;
55 le16 substitute_name_nbytes;
56 le16 print_name_offset;
57 le16 print_name_nbytes;
61 u8 data[REPARSE_POINT_MAX_SIZE - 20];
62 } _packed_attribute symlink;
64 u8 data[REPARSE_POINT_MAX_SIZE - 16];
65 } _packed_attribute junction;
69 static const utf16lechar volume_junction_prefix[11] = {
83 /* Parse the "substitute name" (link target) from a symbolic link or junction
88 * Non-negative integer:
89 * The name is an absolute symbolic link in one of several formats,
90 * and the return value is the number of UTF-16LE characters that need to
91 * be advanced to reach a simple "absolute" path starting with a backslash
92 * (i.e. skip over \??\ and/or drive letter)
94 * SUBST_NAME_IS_VOLUME_JUNCTION:
95 * The name is a volume junction.
96 * SUBST_NAME_IS_RELATIVE_LINK:
97 * The name is a relative symbolic link.
98 * SUBST_NAME_IS_UNKNOWN:
99 * The name does not appear to be a valid symbolic link, junction,
103 parse_substitute_name(const utf16lechar *substitute_name,
104 u16 substitute_name_nbytes, u32 rptag)
106 u16 substitute_name_nchars = substitute_name_nbytes / 2;
108 if (substitute_name_nchars >= 7 &&
109 substitute_name[0] == cpu_to_le16('\\') &&
110 substitute_name[1] == cpu_to_le16('?') &&
111 substitute_name[2] == cpu_to_le16('?') &&
112 substitute_name[3] == cpu_to_le16('\\') &&
113 substitute_name[4] != cpu_to_le16('\0') &&
114 substitute_name[5] == cpu_to_le16(':') &&
115 substitute_name[6] == cpu_to_le16('\\'))
117 /* "Full" symlink or junction (\??\x:\ prefixed path) */
119 } else if (rptag == WIM_IO_REPARSE_TAG_MOUNT_POINT &&
120 substitute_name_nchars >= 12 &&
121 memcmp(substitute_name, volume_junction_prefix,
122 sizeof(volume_junction_prefix)) == 0 &&
123 substitute_name[substitute_name_nchars - 1] == cpu_to_le16('\\'))
125 /* Volume junction. Can't really do anything with it. */
126 return SUBST_NAME_IS_VOLUME_JUNCTION;
127 } else if (rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
128 substitute_name_nchars >= 3 &&
129 substitute_name[0] != cpu_to_le16('\0') &&
130 substitute_name[1] == cpu_to_le16(':') &&
131 substitute_name[2] == cpu_to_le16('\\'))
133 /* "Absolute" symlink, with drive letter */
135 } else if (rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
136 substitute_name_nchars >= 1)
138 if (substitute_name[0] == cpu_to_le16('\\'))
139 /* "Absolute" symlink, without drive letter */
142 /* "Relative" symlink, without drive letter */
143 return SUBST_NAME_IS_RELATIVE_LINK;
145 return SUBST_NAME_IS_UNKNOWN;
150 * Read the data from a symbolic link, junction, or mount point reparse point
151 * buffer into a `struct reparse_data'.
153 * See http://msdn.microsoft.com/en-us/library/cc232006(v=prot.10).aspx for a
154 * description of the format of the reparse point buffers.
157 parse_reparse_data(const u8 * restrict rpbuf, u16 rpbuflen,
158 struct reparse_data * restrict rpdata)
160 u16 substitute_name_offset;
161 u16 print_name_offset;
162 const struct reparse_buffer_disk *rpbuf_disk =
163 (const struct reparse_buffer_disk*)rpbuf;
166 memset(rpdata, 0, sizeof(*rpdata));
169 rpdata->rptag = le32_to_cpu(rpbuf_disk->rptag);
170 wimlib_assert(rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK ||
171 rpdata->rptag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
172 rpdata->rpdatalen = le16_to_cpu(rpbuf_disk->rpdatalen);
173 rpdata->rpreserved = le16_to_cpu(rpbuf_disk->rpreserved);
174 substitute_name_offset = le16_to_cpu(rpbuf_disk->substitute_name_offset);
175 rpdata->substitute_name_nbytes = le16_to_cpu(rpbuf_disk->substitute_name_nbytes);
176 print_name_offset = le16_to_cpu(rpbuf_disk->print_name_offset);
177 rpdata->print_name_nbytes = le16_to_cpu(rpbuf_disk->print_name_nbytes);
179 if ((substitute_name_offset & 1) | (print_name_offset & 1) |
180 (rpdata->substitute_name_nbytes & 1) | (rpdata->print_name_nbytes & 1))
182 /* Names would be unaligned... */
186 if (rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK) {
189 rpdata->rpflags = le16_to_cpu(rpbuf_disk->symlink.rpflags);
190 data = rpbuf_disk->symlink.data;
192 data = rpbuf_disk->junction.data;
194 if ((size_t)substitute_name_offset + rpdata->substitute_name_nbytes +
195 (data - rpbuf) > rpbuflen)
197 if ((size_t)print_name_offset + rpdata->print_name_nbytes +
198 (data - rpbuf) > rpbuflen)
200 rpdata->substitute_name = (utf16lechar*)&data[substitute_name_offset];
201 rpdata->print_name = (utf16lechar*)&data[print_name_offset];
204 ERROR("Invalid reparse data");
205 return WIMLIB_ERR_INVALID_REPARSE_DATA;
209 * Create a reparse point data buffer.
211 * @rpdata: Structure that contains the data we need.
213 * @rpbuf: Buffer into which to write the reparse point data buffer. Must be
214 * at least REPARSE_POINT_MAX_SIZE bytes long.
217 make_reparse_buffer(const struct reparse_data * restrict rpdata,
220 struct reparse_buffer_disk *rpbuf_disk =
221 (struct reparse_buffer_disk*)rpbuf;
224 rpbuf_disk->rptag = cpu_to_le32(rpdata->rptag);
225 rpbuf_disk->rpreserved = cpu_to_le16(rpdata->rpreserved);
226 rpbuf_disk->substitute_name_offset = cpu_to_le16(0);
227 rpbuf_disk->substitute_name_nbytes = cpu_to_le16(rpdata->substitute_name_nbytes);
228 rpbuf_disk->print_name_offset = cpu_to_le16(rpdata->substitute_name_nbytes + 2);
229 rpbuf_disk->print_name_nbytes = cpu_to_le16(rpdata->print_name_nbytes);
231 if (rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK) {
232 rpbuf_disk->symlink.rpflags = cpu_to_le32(rpdata->rpflags);
233 data = rpbuf_disk->symlink.data;
235 data = rpbuf_disk->junction.data;
238 /* We null-terminate the substitute and print names, although this may
239 * not be strictly necessary. Note that the byte counts should not
240 * include the null terminators. */
241 if (data + rpdata->substitute_name_nbytes +
242 rpdata->print_name_nbytes +
243 2 * sizeof(utf16lechar) - rpbuf > REPARSE_POINT_MAX_SIZE)
245 ERROR("Reparse data is too long!");
246 return WIMLIB_ERR_INVALID_REPARSE_DATA;
248 data = mempcpy(data, rpdata->substitute_name, rpdata->substitute_name_nbytes);
249 *(utf16lechar*)data = cpu_to_le16(0);
251 data = mempcpy(data, rpdata->print_name, rpdata->print_name_nbytes);
252 *(utf16lechar*)data = cpu_to_le16(0);
254 rpbuf_disk->rpdatalen = cpu_to_le16(data - rpbuf - 8);
259 * Read the reparse data from a WIM inode that is a reparse point.
261 * @rpbuf points to a buffer at least REPARSE_POINT_MAX_SIZE bytes into which
262 * the reparse point data buffer will be reconstructed.
264 * Note: in the WIM format, the first 8 bytes of the reparse point data buffer
265 * are omitted, presumably because we already know the reparse tag from the
266 * dentry, and we already know the reparse tag length from the lookup table
267 * entry resource length. However, we reconstruct the first 8 bytes in the
268 * buffer returned by this function.
271 wim_inode_get_reparse_data(const struct wim_inode * restrict inode,
273 u16 * restrict rpbuflen_ret)
275 struct wim_lookup_table_entry *lte;
277 struct reparse_buffer_disk *rpbuf_disk;
280 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
282 lte = inode_unnamed_lte_resolved(inode);
284 ERROR("Reparse point has no reparse data!");
285 return WIMLIB_ERR_INVALID_REPARSE_DATA;
288 if (wim_resource_size(lte) > REPARSE_POINT_MAX_SIZE - 8) {
289 ERROR("Reparse data is too long!");
290 return WIMLIB_ERR_INVALID_REPARSE_DATA;
292 rpdatalen = wim_resource_size(lte);
294 /* Read the data from the WIM file */
295 ret = read_full_resource_into_buf(lte, rpbuf + 8);
299 /* Reconstruct the first 8 bytes of the reparse point buffer */
300 rpbuf_disk = (struct reparse_buffer_disk*)rpbuf;
303 rpbuf_disk->rptag = cpu_to_le32(inode->i_reparse_tag);
305 /* ReparseDataLength */
306 rpbuf_disk->rpdatalen = cpu_to_le16(rpdatalen);
309 * XXX this could be one of the unknown fields in the WIM dentry. */
310 rpbuf_disk->rpreserved = cpu_to_le16(0);
312 *rpbuflen_ret = rpdatalen + 8;
316 /* UNIX version of getting and setting the data in reparse points */
317 #if !defined(__WIN32__)
319 /* Get the UNIX symlink target from a WIM inode. The inode may be either a
320 * "real" symlink (reparse tag WIM_IO_REPARSE_TAG_SYMLINK), or it may be a
321 * junction point (reparse tag WIM_IO_REPARSE_TAG_MOUNT_POINT).
323 * This has similar semantics to the UNIX readlink() function, except the path
324 * argument is swapped out with the `struct wim_inode' for a reparse point, and
325 * on failure a negated error code is returned rather than -1 with errno set. */
327 wim_inode_readlink(const struct wim_inode * restrict inode,
328 char * restrict buf, size_t bufsize)
331 struct reparse_buffer_disk rpbuf_disk _aligned_attribute(8);
332 struct reparse_data rpdata;
334 char *translated_target;
335 size_t link_target_len;
338 wimlib_assert(inode_is_symlink(inode));
340 if (wim_inode_get_reparse_data(inode, (u8*)&rpbuf_disk, &rpbuflen))
343 if (parse_reparse_data((const u8*)&rpbuf_disk, rpbuflen, &rpdata))
346 ret = utf16le_to_tstr(rpdata.substitute_name,
347 rpdata.substitute_name_nbytes,
348 &link_target, &link_target_len);
352 translated_target = link_target;
353 ret = parse_substitute_name(rpdata.substitute_name,
354 rpdata.substitute_name_nbytes,
357 case SUBST_NAME_IS_RELATIVE_LINK:
358 goto out_translate_slashes;
359 case SUBST_NAME_IS_VOLUME_JUNCTION:
361 case SUBST_NAME_IS_UNKNOWN:
362 ERROR("Can't understand reparse point "
363 "substitute name \"%s\"", link_target);
365 goto out_free_link_target;
367 translated_target += ret;
368 link_target_len -= ret;
372 out_translate_slashes:
373 for (size_t i = 0; i < link_target_len; i++)
374 if (translated_target[i] == '\\')
375 translated_target[i] = '/';
377 if (link_target_len > bufsize) {
378 link_target_len = bufsize;
381 ret = link_target_len;
383 memcpy(buf, translated_target, link_target_len);
384 out_free_link_target:
390 wim_inode_set_symlink(struct wim_inode *inode,
392 struct wim_lookup_table *lookup_table)
395 struct reparse_buffer_disk rpbuf_disk _aligned_attribute(8);
396 struct reparse_data rpdata;
397 static const char abs_subst_name_prefix[12] = "\\\0?\0?\0\\\0C\0:\0";
398 static const char abs_print_name_prefix[4] = "C\0:\0";
399 utf16lechar *name_utf16le;
400 size_t name_utf16le_nbytes;
403 DEBUG("Creating reparse point data buffer for UNIX "
404 "symlink target \"%s\"", target);
405 memset(&rpdata, 0, sizeof(rpdata));
406 ret = tstr_to_utf16le(target, strlen(target),
407 &name_utf16le, &name_utf16le_nbytes);
411 for (size_t i = 0; i < name_utf16le_nbytes / 2; i++)
412 if (name_utf16le[i] == cpu_to_le16('/'))
413 name_utf16le[i] = cpu_to_le16('\\');
415 /* Compatability notes:
417 * On UNIX, an absolute symbolic link begins with '/'; everything else
418 * is a relative symbolic link. (Quite simple compared to the various
419 * ways to provide Windows paths.)
421 * To change a UNIX relative symbolic link to Windows format, we only
422 * need to translate it to UTF-16LE and replace backslashes with forward
423 * slashes. We do not make any attempt to handle filename character
424 * problems, such as a link target that itself contains backslashes on
425 * UNIX. Then, for these relative links, we set the reparse header
426 * @flags field to SYMBOLIC_LINK_RELATIVE.
428 * For UNIX absolute symbolic links, we must set the @flags field to 0.
429 * Then, there are multiple options as to actually represent the
430 * absolute link targets:
432 * (1) An absolute path beginning with one backslash character. similar
433 * to UNIX-style, just with a different path separator. Print name same
434 * as substitute name.
436 * (2) Absolute path beginning with drive letter followed by a
437 * backslash. Print name same as substitute name.
439 * (3) Absolute path beginning with drive letter followed by a
440 * backslash; substitute name prefixed with \??\, otherwise same as
443 * We choose option (3) here, and we just assume C: for the drive
444 * letter. The reasoning for this is:
446 * (1) Microsoft imagex.exe has a bug where it does not attempt to do
447 * reparse point fixups for these links, even though they are valid
448 * absolute links. (Note: in this case prefixing the substitute name
449 * with \??\ does not work; it just makes the data unable to be restored
451 * (2) Microsoft imagex.exe will fail when doing reparse point fixups
452 * for these. It apparently contains a bug that causes it to create an
453 * invalid reparse point, which then cannot be restored.
454 * (3) This is the only option I tested for which reparse point fixups
455 * worked properly in Microsoft imagex.exe.
457 * So option (3) it is.
460 rpdata.rptag = inode->i_reparse_tag;
461 if (target[0] == '/') {
462 rpdata.substitute_name_nbytes = name_utf16le_nbytes +
463 sizeof(abs_subst_name_prefix);
464 rpdata.print_name_nbytes = name_utf16le_nbytes +
465 sizeof(abs_print_name_prefix);
466 rpdata.substitute_name = alloca(rpdata.substitute_name_nbytes);
467 rpdata.print_name = alloca(rpdata.print_name_nbytes);
468 memcpy(rpdata.substitute_name, abs_subst_name_prefix,
469 sizeof(abs_subst_name_prefix));
470 memcpy(rpdata.print_name, abs_print_name_prefix,
471 sizeof(abs_print_name_prefix));
472 memcpy((void*)rpdata.substitute_name + sizeof(abs_subst_name_prefix),
473 name_utf16le, name_utf16le_nbytes);
474 memcpy((void*)rpdata.print_name + sizeof(abs_print_name_prefix),
475 name_utf16le, name_utf16le_nbytes);
477 rpdata.substitute_name_nbytes = name_utf16le_nbytes;
478 rpdata.print_name_nbytes = name_utf16le_nbytes;
479 rpdata.substitute_name = name_utf16le;
480 rpdata.print_name = name_utf16le;
481 rpdata.rpflags = SYMBOLIC_LINK_RELATIVE;
484 ret = make_reparse_buffer(&rpdata, (u8*)&rpbuf_disk);
486 ret = inode_set_unnamed_stream(inode,
487 (u8*)&rpbuf_disk + 8,
488 le16_to_cpu(rpbuf_disk.rpdatalen),
495 #include <sys/stat.h>
498 unix_get_ino_and_dev(const char *path, u64 *ino_ret, u64 *dev_ret)
501 if (stat(path, &stbuf)) {
503 WARNING_WITH_ERRNO("Failed to stat \"%s\"", path);
504 /* Treat as a link pointing outside the capture root (it
505 * most likely is). */
506 return WIMLIB_ERR_STAT;
508 *ino_ret = stbuf.st_ino;
509 *dev_ret = stbuf.st_dev;
514 #endif /* !defined(__WIN32__) */
517 # define RP_PATH_SEPARATOR L'\\'
518 # define is_rp_path_separator(c) ((c) == L'\\' || (c) == L'/')
519 # define os_get_ino_and_dev win32_get_file_and_vol_ids
521 # define RP_PATH_SEPARATOR '/'
522 # define is_rp_path_separator(c) ((c) == '/')
523 # define os_get_ino_and_dev unix_get_ino_and_dev
526 /* Fix up absolute symbolic link targets--- mostly shared between UNIX and
529 capture_fixup_absolute_symlink(tchar *dest,
530 u64 capture_root_ino, u64 capture_root_dev)
535 /* Skip drive letter */
536 if (!is_rp_path_separator(*dest))
540 DEBUG("Fixing symlink or junction \"%"TS"\"", dest);
547 while (is_rp_path_separator(*p))
552 ret = os_get_ino_and_dev(dest, &ino, &dev);
555 if (ret) /* stat() failed before we got to the capture root---
556 assume the link points outside it. */
559 if (ino == capture_root_ino && dev == capture_root_dev) {
560 /* Link points inside capture root. Return abbreviated
563 *(p - 1) = RP_PATH_SEPARATOR;
564 while (p - 1 >= dest && is_rp_path_separator(*(p - 1)))
567 if (!is_rp_path_separator(dest[0])) {
572 wimlib_assert(p >= dest);
577 /* Link points outside capture root. */
583 } while (!is_rp_path_separator(*p) && *p != T('\0'));