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/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"
45 * Read the data from a symbolic link, junction, or mount point reparse point
46 * buffer into a `struct reparse_data'.
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.
52 parse_reparse_data(const u8 * restrict rpbuf, u16 rpbuflen,
53 struct reparse_data * restrict rpdata)
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;
61 memset(rpdata, 0, sizeof(*rpdata));
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);
74 if ((substitute_name_offset & 1) | (print_name_offset & 1) |
75 (rpdata->substitute_name_nbytes & 1) | (rpdata->print_name_nbytes & 1))
77 /* Names would be unaligned... */
81 if (rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK) {
84 rpdata->rpflags = le32_to_cpu(rpbuf_disk->symlink.rpflags);
85 data = rpbuf_disk->symlink.data;
87 data = rpbuf_disk->junction.data;
89 if ((size_t)substitute_name_offset + rpdata->substitute_name_nbytes +
90 (data - rpbuf) > rpbuflen)
92 if ((size_t)print_name_offset + rpdata->print_name_nbytes +
93 (data - rpbuf) > rpbuflen)
95 rpdata->substitute_name = (utf16lechar*)&data[substitute_name_offset];
96 rpdata->print_name = (utf16lechar*)&data[print_name_offset];
99 ERROR("Invalid reparse data");
100 return WIMLIB_ERR_INVALID_REPARSE_DATA;
104 * Create a reparse point data buffer.
106 * @rpdata: Structure that contains the data we need.
108 * @rpbuf: Buffer into which to write the reparse point data buffer. Must be
109 * at least REPARSE_POINT_MAX_SIZE bytes long.
112 make_reparse_buffer(const struct reparse_data * restrict rpdata,
114 u16 * restrict rpbuflen_ret)
116 struct reparse_buffer_disk *rpbuf_disk =
117 (struct reparse_buffer_disk*)rpbuf;
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);
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;
131 data = rpbuf_disk->junction.data;
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)
141 ERROR("Reparse data is too long!");
142 return WIMLIB_ERR_INVALID_REPARSE_DATA;
144 data = mempcpy(data, rpdata->substitute_name, rpdata->substitute_name_nbytes);
145 *(utf16lechar*)data = cpu_to_le16(0);
147 data = mempcpy(data, rpdata->print_name, rpdata->print_name_nbytes);
148 *(utf16lechar*)data = cpu_to_le16(0);
150 rpbuf_disk->rpdatalen = cpu_to_le16(data - rpbuf - 8);
151 *rpbuflen_ret = data - rpbuf;
156 * Read the reparse data from a WIM inode that is a reparse point.
158 * @rpbuf points to a buffer at least REPARSE_POINT_MAX_SIZE bytes into which
159 * the reparse point data buffer will be reconstructed.
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.
168 wim_inode_get_reparse_data(const struct wim_inode * restrict inode,
170 u16 * restrict rpbuflen_ret,
171 struct wim_lookup_table_entry *lte_override)
173 struct wim_lookup_table_entry *lte;
175 struct reparse_buffer_disk *rpbuf_disk;
178 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
181 lte = inode_unnamed_lte_resolved(inode);
183 ERROR("Reparse point has no reparse data!");
184 return WIMLIB_ERR_INVALID_REPARSE_DATA;
190 if (lte->size > REPARSE_POINT_MAX_SIZE - 8) {
191 ERROR("Reparse data is too long!");
192 return WIMLIB_ERR_INVALID_REPARSE_DATA;
194 rpdatalen = lte->size;
196 /* Read the data from the WIM file */
197 ret = read_full_stream_into_buf(lte, rpbuf + 8);
201 /* Reconstruct the first 8 bytes of the reparse point buffer */
202 rpbuf_disk = (struct reparse_buffer_disk*)rpbuf;
205 rpbuf_disk->rptag = cpu_to_le32(inode->i_reparse_tag);
207 /* ReparseDataLength */
208 rpbuf_disk->rpdatalen = cpu_to_le16(rpdatalen);
211 * XXX this could be one of the unknown fields in the WIM dentry. */
212 rpbuf_disk->rpreserved = cpu_to_le16(0);
214 *rpbuflen_ret = rpdatalen + 8;
218 /* UNIX version of getting and setting the data in reparse points */
221 static const utf16lechar volume_junction_prefix[11] = {
236 SUBST_NAME_IS_RELATIVE_LINK = -1,
237 SUBST_NAME_IS_VOLUME_JUNCTION = -2,
238 SUBST_NAME_IS_UNKNOWN = -3,
241 /* Parse the "substitute name" (link target) from a symbolic link or junction
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)
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,
261 parse_substitute_name(const utf16lechar *substitute_name,
262 u16 substitute_name_nbytes, u32 rptag)
264 u16 substitute_name_nchars = substitute_name_nbytes / 2;
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('\\'))
275 /* "Full" symlink or junction (\??\x:\ prefixed path) */
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('\\'))
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('\\'))
291 /* "Absolute" symlink, with drive letter */
293 } else if (rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
294 substitute_name_nchars >= 1)
296 if (substitute_name[0] == cpu_to_le16('\\'))
297 /* "Absolute" symlink, without drive letter */
300 /* "Relative" symlink, without drive letter */
301 return SUBST_NAME_IS_RELATIVE_LINK;
303 return SUBST_NAME_IS_UNKNOWN;
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
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).
319 * Buffer into which to place the link target.
322 * Available space in @buf, in bytes.
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.
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.
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)
341 struct reparse_buffer_disk rpbuf_disk _aligned_attribute(8);
342 struct reparse_data rpdata;
344 char *translated_target;
345 size_t link_target_len;
348 wimlib_assert(inode_is_symlink(inode));
350 if (wim_inode_get_reparse_data(inode, (u8*)&rpbuf_disk, &rpbuflen,
354 if (parse_reparse_data((const u8*)&rpbuf_disk, rpbuflen, &rpdata))
357 ret = utf16le_to_tstr(rpdata.substitute_name,
358 rpdata.substitute_name_nbytes,
359 &link_target, &link_target_len);
363 translated_target = link_target;
364 ret = parse_substitute_name(rpdata.substitute_name,
365 rpdata.substitute_name_nbytes,
368 case SUBST_NAME_IS_RELATIVE_LINK:
369 goto out_translate_slashes;
370 case SUBST_NAME_IS_VOLUME_JUNCTION:
372 case SUBST_NAME_IS_UNKNOWN:
373 ERROR("Can't understand reparse point "
374 "substitute name \"%s\"", link_target);
376 goto out_free_link_target;
378 translated_target += ret;
379 link_target_len -= ret;
383 out_translate_slashes:
384 for (size_t i = 0; i < link_target_len; i++)
385 if (translated_target[i] == '\\')
386 translated_target[i] = '/';
388 if (link_target_len > bufsize) {
389 link_target_len = bufsize;
392 ret = link_target_len;
394 memcpy(buf, translated_target, link_target_len);
395 out_free_link_target:
401 wim_inode_set_symlink(struct wim_inode *inode,
403 struct wim_lookup_table *lookup_table)
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;
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);
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('\\');
427 /* Compatability notes:
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.)
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.
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:
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.
448 * (2) Absolute path beginning with drive letter followed by a
449 * backslash. Print name same as substitute name.
451 * (3) Absolute path beginning with drive letter followed by a
452 * backslash; substitute name prefixed with \??\, otherwise same as
455 * We choose option (3) here, and we just assume C: for the drive
456 * letter. The reasoning for this is:
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
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.
469 * So option (3) it is.
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);
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;
496 ret = make_reparse_buffer(&rpdata, (u8*)&rpbuf_disk, &rpbuflen);
498 ret = inode_set_unnamed_stream(inode,
499 (u8*)&rpbuf_disk + 8,
507 #endif /* !__WIN32__ */