2 * reparse.c - Handle reparse data.
6 * Copyright (C) 2012, 2013 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/alloca.h"
29 #include "wimlib/assert.h"
30 #include "wimlib/compiler.h"
31 #include "wimlib/endianness.h"
32 #include "wimlib/encoding.h"
33 #include "wimlib/error.h"
34 #include "wimlib/inode.h"
35 #include "wimlib/lookup_table.h"
36 #include "wimlib/reparse.h"
37 #include "wimlib/resource.h"
40 * Read the data from a symbolic link, junction, or mount point reparse point
41 * buffer into a `struct reparse_data'.
43 * See http://msdn.microsoft.com/en-us/library/cc232006(v=prot.10).aspx for a
44 * description of the format of the reparse point buffers.
47 parse_reparse_data(const u8 * restrict rpbuf, u16 rpbuflen,
48 struct reparse_data * restrict rpdata)
50 u16 substitute_name_offset;
51 u16 print_name_offset;
52 const struct reparse_buffer_disk *rpbuf_disk =
53 (const struct reparse_buffer_disk*)rpbuf;
56 memset(rpdata, 0, sizeof(*rpdata));
59 rpdata->rptag = le32_to_cpu(rpbuf_disk->rptag);
60 wimlib_assert(rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK ||
61 rpdata->rptag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
62 rpdata->rpdatalen = le16_to_cpu(rpbuf_disk->rpdatalen);
63 rpdata->rpreserved = le16_to_cpu(rpbuf_disk->rpreserved);
64 substitute_name_offset = le16_to_cpu(rpbuf_disk->symlink.substitute_name_offset);
65 rpdata->substitute_name_nbytes = le16_to_cpu(rpbuf_disk->symlink.substitute_name_nbytes);
66 print_name_offset = le16_to_cpu(rpbuf_disk->symlink.print_name_offset);
67 rpdata->print_name_nbytes = le16_to_cpu(rpbuf_disk->symlink.print_name_nbytes);
69 if ((substitute_name_offset & 1) | (print_name_offset & 1) |
70 (rpdata->substitute_name_nbytes & 1) | (rpdata->print_name_nbytes & 1))
72 /* Names would be unaligned... */
76 if (rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK) {
79 rpdata->rpflags = le32_to_cpu(rpbuf_disk->symlink.rpflags);
80 data = rpbuf_disk->symlink.data;
82 data = rpbuf_disk->junction.data;
84 if ((size_t)substitute_name_offset + rpdata->substitute_name_nbytes +
85 (data - rpbuf) > rpbuflen)
87 if ((size_t)print_name_offset + rpdata->print_name_nbytes +
88 (data - rpbuf) > rpbuflen)
90 rpdata->substitute_name = (utf16lechar*)&data[substitute_name_offset];
91 rpdata->print_name = (utf16lechar*)&data[print_name_offset];
94 ERROR("Invalid reparse data");
95 return WIMLIB_ERR_INVALID_REPARSE_DATA;
99 * Create a reparse point data buffer.
101 * @rpdata: Structure that contains the data we need.
103 * @rpbuf: Buffer into which to write the reparse point data buffer. Must be
104 * at least REPARSE_POINT_MAX_SIZE bytes long.
107 make_reparse_buffer(const struct reparse_data * restrict rpdata,
109 u16 * restrict rpbuflen_ret)
111 struct reparse_buffer_disk *rpbuf_disk =
112 (struct reparse_buffer_disk*)rpbuf;
115 if (rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK)
116 data = rpbuf_disk->symlink.data;
118 data = rpbuf_disk->junction.data;
120 if ((data - rpbuf) + rpdata->substitute_name_nbytes +
121 rpdata->print_name_nbytes +
122 2 * sizeof(utf16lechar) > REPARSE_POINT_MAX_SIZE)
124 ERROR("Reparse data is too long!");
125 return WIMLIB_ERR_INVALID_REPARSE_DATA;
128 rpbuf_disk->rptag = cpu_to_le32(rpdata->rptag);
129 rpbuf_disk->rpreserved = cpu_to_le16(rpdata->rpreserved);
130 rpbuf_disk->symlink.substitute_name_offset = cpu_to_le16(0);
131 rpbuf_disk->symlink.substitute_name_nbytes = cpu_to_le16(rpdata->substitute_name_nbytes);
132 rpbuf_disk->symlink.print_name_offset = cpu_to_le16(rpdata->substitute_name_nbytes + 2);
133 rpbuf_disk->symlink.print_name_nbytes = cpu_to_le16(rpdata->print_name_nbytes);
135 if (rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK)
136 rpbuf_disk->symlink.rpflags = cpu_to_le32(rpdata->rpflags);
138 /* We null-terminate the substitute and print names, although this may
139 * not be strictly necessary. Note that the byte counts should not
140 * include the null terminators. */
141 data = mempcpy(data, rpdata->substitute_name, rpdata->substitute_name_nbytes);
142 *(utf16lechar*)data = cpu_to_le16(0);
144 data = mempcpy(data, rpdata->print_name, rpdata->print_name_nbytes);
145 *(utf16lechar*)data = cpu_to_le16(0);
147 rpbuf_disk->rpdatalen = cpu_to_le16(data - rpbuf - 8);
148 *rpbuflen_ret = data - rpbuf;
153 * Read the reparse data from a WIM inode that is a reparse point.
155 * @rpbuf points to a buffer at least REPARSE_POINT_MAX_SIZE bytes into which
156 * the reparse point data buffer will be reconstructed.
158 * Note: in the WIM format, the first 8 bytes of the reparse point data buffer
159 * are omitted, presumably because we already know the reparse tag from the
160 * dentry, and we already know the reparse tag length from the lookup table
161 * entry resource length. However, we reconstruct the first 8 bytes in the
162 * buffer returned by this function.
165 wim_inode_get_reparse_data(const struct wim_inode * restrict inode,
167 u16 * restrict rpbuflen_ret,
168 struct wim_lookup_table_entry *lte_override)
170 struct wim_lookup_table_entry *lte;
172 struct reparse_buffer_disk *rpbuf_disk;
175 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
178 lte = inode_unnamed_lte_resolved(inode);
180 ERROR("Reparse point has no reparse data!");
181 return WIMLIB_ERR_INVALID_REPARSE_DATA;
187 if (lte->size > REPARSE_POINT_MAX_SIZE - 8) {
188 ERROR("Reparse data is too long!");
189 return WIMLIB_ERR_INVALID_REPARSE_DATA;
191 rpdatalen = lte->size;
193 /* Read the data from the WIM file */
194 ret = read_full_stream_into_buf(lte, rpbuf + 8);
198 /* Reconstruct the first 8 bytes of the reparse point buffer */
199 rpbuf_disk = (struct reparse_buffer_disk*)rpbuf;
202 rpbuf_disk->rptag = cpu_to_le32(inode->i_reparse_tag);
204 /* ReparseDataLength */
205 rpbuf_disk->rpdatalen = cpu_to_le16(rpdatalen);
208 * XXX this could be one of the unknown fields in the WIM dentry. */
209 rpbuf_disk->rpreserved = cpu_to_le16(0);
211 *rpbuflen_ret = rpdatalen + 8;
215 /* UNIX version of getting and setting the data in reparse points */
218 static const utf16lechar volume_junction_prefix[11] = {
233 SUBST_NAME_IS_RELATIVE_LINK = -1,
234 SUBST_NAME_IS_VOLUME_JUNCTION = -2,
235 SUBST_NAME_IS_UNKNOWN = -3,
238 /* Parse the "substitute name" (link target) from a symbolic link or junction
243 * Non-negative integer:
244 * The name is an absolute symbolic link in one of several formats,
245 * and the return value is the number of UTF-16LE characters that need to
246 * be advanced to reach a simple "absolute" path starting with a backslash
247 * (i.e. skip over \??\ and/or drive letter)
249 * SUBST_NAME_IS_VOLUME_JUNCTION:
250 * The name is a volume junction.
251 * SUBST_NAME_IS_RELATIVE_LINK:
252 * The name is a relative symbolic link.
253 * SUBST_NAME_IS_UNKNOWN:
254 * The name does not appear to be a valid symbolic link, junction,
258 parse_substitute_name(const utf16lechar *substitute_name,
259 u16 substitute_name_nbytes, u32 rptag)
261 u16 substitute_name_nchars = substitute_name_nbytes / 2;
263 if (substitute_name_nchars >= 7 &&
264 substitute_name[0] == cpu_to_le16('\\') &&
265 substitute_name[1] == cpu_to_le16('?') &&
266 substitute_name[2] == cpu_to_le16('?') &&
267 substitute_name[3] == cpu_to_le16('\\') &&
268 substitute_name[4] != cpu_to_le16('\0') &&
269 substitute_name[5] == cpu_to_le16(':') &&
270 substitute_name[6] == cpu_to_le16('\\'))
272 /* "Full" symlink or junction (\??\x:\ prefixed path) */
274 } else if (rptag == WIM_IO_REPARSE_TAG_MOUNT_POINT &&
275 substitute_name_nchars >= 12 &&
276 memcmp(substitute_name, volume_junction_prefix,
277 sizeof(volume_junction_prefix)) == 0 &&
278 substitute_name[substitute_name_nchars - 1] == cpu_to_le16('\\'))
280 /* Volume junction. Can't really do anything with it. */
281 return SUBST_NAME_IS_VOLUME_JUNCTION;
282 } else if (rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
283 substitute_name_nchars >= 3 &&
284 substitute_name[0] != cpu_to_le16('\0') &&
285 substitute_name[1] == cpu_to_le16(':') &&
286 substitute_name[2] == cpu_to_le16('\\'))
288 /* "Absolute" symlink, with drive letter */
290 } else if (rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
291 substitute_name_nchars >= 1)
293 if (substitute_name[0] == cpu_to_le16('\\'))
294 /* "Absolute" symlink, without drive letter */
297 /* "Relative" symlink, without drive letter */
298 return SUBST_NAME_IS_RELATIVE_LINK;
300 return SUBST_NAME_IS_UNKNOWN;
305 * Get the UNIX-style symlink target from the WIM inode for a reparse point.
306 * Specifically, this translates the target from UTF-16 to the current multibyte
307 * encoding, strips the drive prefix if present, and replaces backslashes with
311 * The inode to read the symlink from. It must be a reparse point with
312 * tag WIM_IO_REPARSE_TAG_SYMLINK (a real symlink) or
313 * WIM_IO_REPARSE_TAG_MOUNT_POINT (a mount point or junction point).
316 * Buffer into which to place the link target.
319 * Available space in @buf, in bytes.
322 * If not NULL, the stream from which to read the reparse data. Otherwise,
323 * the reparse data will be read from the unnamed stream of @inode.
325 * If the entire symbolic link target was placed in the buffer, returns the
326 * number of bytes written. The resulting string is not null-terminated. If
327 * the symbolic link target was too large to be placed in the buffer, the first
328 * @bufsize bytes of it are placed in the buffer and
329 * -ENAMETOOLONG is returned. Otherwise, a negative errno value indicating
330 * another error is returned.
333 wim_inode_readlink(const struct wim_inode * restrict inode,
334 char * restrict buf, size_t bufsize,
335 struct wim_lookup_table_entry *lte_override)
338 struct reparse_buffer_disk rpbuf_disk _aligned_attribute(8);
339 struct reparse_data rpdata;
341 char *translated_target;
342 size_t link_target_len;
345 wimlib_assert(inode_is_symlink(inode));
347 if (wim_inode_get_reparse_data(inode, (u8*)&rpbuf_disk, &rpbuflen,
351 if (parse_reparse_data((const u8*)&rpbuf_disk, rpbuflen, &rpdata))
354 ret = utf16le_to_tstr(rpdata.substitute_name,
355 rpdata.substitute_name_nbytes,
356 &link_target, &link_target_len);
360 translated_target = link_target;
361 ret = parse_substitute_name(rpdata.substitute_name,
362 rpdata.substitute_name_nbytes,
365 case SUBST_NAME_IS_RELATIVE_LINK:
366 goto out_translate_slashes;
367 case SUBST_NAME_IS_VOLUME_JUNCTION:
369 case SUBST_NAME_IS_UNKNOWN:
370 ERROR("Can't understand reparse point "
371 "substitute name \"%s\"", link_target);
373 goto out_free_link_target;
375 translated_target += ret;
376 link_target_len -= ret;
380 out_translate_slashes:
381 for (size_t i = 0; i < link_target_len; i++)
382 if (translated_target[i] == '\\')
383 translated_target[i] = '/';
385 if (link_target_len > bufsize) {
386 link_target_len = bufsize;
389 ret = link_target_len;
391 memcpy(buf, translated_target, link_target_len);
392 out_free_link_target:
398 wim_inode_set_symlink(struct wim_inode *inode,
400 struct wim_lookup_table *lookup_table)
403 struct reparse_buffer_disk rpbuf_disk _aligned_attribute(8);
404 struct reparse_data rpdata;
405 static const char abs_subst_name_prefix[12] = "\\\0?\0?\0\\\0C\0:\0";
406 static const char abs_print_name_prefix[4] = "C\0:\0";
407 utf16lechar *name_utf16le;
408 size_t name_utf16le_nbytes;
412 DEBUG("Creating reparse point data buffer for UNIX "
413 "symlink target \"%s\"", target);
414 memset(&rpdata, 0, sizeof(rpdata));
415 ret = tstr_to_utf16le(target, strlen(target),
416 &name_utf16le, &name_utf16le_nbytes);
420 for (size_t i = 0; i < name_utf16le_nbytes / 2; i++)
421 if (name_utf16le[i] == cpu_to_le16('/'))
422 name_utf16le[i] = cpu_to_le16('\\');
424 /* Compatability notes:
426 * On UNIX, an absolute symbolic link begins with '/'; everything else
427 * is a relative symbolic link. (Quite simple compared to the various
428 * ways to provide Windows paths.)
430 * To change a UNIX relative symbolic link to Windows format, we only
431 * need to translate it to UTF-16LE and replace forward slashes with
432 * backslashes. We do not make any attempt to handle filename character
433 * problems, such as a link target that itself contains backslashes on
434 * UNIX. Then, for these relative links, we set the reparse header
435 * @flags field to SYMBOLIC_LINK_RELATIVE.
437 * For UNIX absolute symbolic links, we must set the @flags field to 0.
438 * Then, there are multiple options as to actually represent the
439 * absolute link targets:
441 * (1) An absolute path beginning with one backslash character. similar
442 * to UNIX-style, just with a different path separator. Print name same
443 * as substitute name.
445 * (2) Absolute path beginning with drive letter followed by a
446 * backslash. Print name same as substitute name.
448 * (3) Absolute path beginning with drive letter followed by a
449 * backslash; substitute name prefixed with \??\, otherwise same as
452 * We choose option (3) here, and we just assume C: for the drive
453 * letter. The reasoning for this is:
455 * (1) Microsoft imagex.exe has a bug where it does not attempt to do
456 * reparse point fixups for these links, even though they are valid
457 * absolute links. (Note: in this case prefixing the substitute name
458 * with \??\ does not work; it just makes the data unable to be restored
460 * (2) Microsoft imagex.exe will fail when doing reparse point fixups
461 * for these. It apparently contains a bug that causes it to create an
462 * invalid reparse point, which then cannot be restored.
463 * (3) This is the only option I tested for which reparse point fixups
464 * worked properly in Microsoft imagex.exe.
466 * So option (3) it is.
469 rpdata.rptag = inode->i_reparse_tag;
470 if (target[0] == '/') {
471 rpdata.substitute_name_nbytes = name_utf16le_nbytes +
472 sizeof(abs_subst_name_prefix);
473 rpdata.print_name_nbytes = name_utf16le_nbytes +
474 sizeof(abs_print_name_prefix);
475 rpdata.substitute_name = alloca(rpdata.substitute_name_nbytes);
476 rpdata.print_name = alloca(rpdata.print_name_nbytes);
477 memcpy(rpdata.substitute_name, abs_subst_name_prefix,
478 sizeof(abs_subst_name_prefix));
479 memcpy(rpdata.print_name, abs_print_name_prefix,
480 sizeof(abs_print_name_prefix));
481 memcpy((void*)rpdata.substitute_name + sizeof(abs_subst_name_prefix),
482 name_utf16le, name_utf16le_nbytes);
483 memcpy((void*)rpdata.print_name + sizeof(abs_print_name_prefix),
484 name_utf16le, name_utf16le_nbytes);
486 rpdata.substitute_name_nbytes = name_utf16le_nbytes;
487 rpdata.print_name_nbytes = name_utf16le_nbytes;
488 rpdata.substitute_name = name_utf16le;
489 rpdata.print_name = name_utf16le;
490 rpdata.rpflags = SYMBOLIC_LINK_RELATIVE;
493 ret = make_reparse_buffer(&rpdata, (u8*)&rpbuf_disk, &rpbuflen);
495 ret = inode_set_unnamed_stream(inode,
496 (u8*)&rpbuf_disk + 8,
504 #endif /* !__WIN32__ */