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/blob_table.h"
31 #include "wimlib/compiler.h"
32 #include "wimlib/endianness.h"
33 #include "wimlib/encoding.h"
34 #include "wimlib/error.h"
35 #include "wimlib/inode.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 blob length.
161 * However, we reconstruct the first 8 bytes in the buffer returned by this
165 wim_inode_get_reparse_data(const struct wim_inode * restrict inode,
167 u16 * restrict rpbuflen_ret,
168 struct blob_descriptor *blob_override)
170 struct blob_descriptor *blob;
172 struct reparse_buffer_disk *rpbuf_disk;
175 wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
178 blob = blob_override;
180 struct wim_inode_stream *strm;
182 strm = inode_get_stream(inode, STREAM_TYPE_REPARSE_POINT,
185 blob = stream_blob_resolved(strm);
189 ERROR("Reparse point has no reparse data!");
190 return WIMLIB_ERR_INVALID_REPARSE_DATA;
194 if (blob->size > REPARSE_DATA_MAX_SIZE) {
195 ERROR("Reparse data is too long!");
196 return WIMLIB_ERR_INVALID_REPARSE_DATA;
198 rpdatalen = blob->size;
200 /* Read the reparse data from blob */
201 ret = read_full_blob_into_buf(blob, rpbuf + REPARSE_DATA_OFFSET);
205 /* Reconstruct the first 8 bytes of the reparse point buffer */
206 rpbuf_disk = (struct reparse_buffer_disk*)rpbuf;
209 rpbuf_disk->rptag = cpu_to_le32(inode->i_reparse_tag);
211 /* ReparseDataLength */
212 rpbuf_disk->rpdatalen = cpu_to_le16(rpdatalen);
215 * XXX this could be one of the unknown fields in the WIM dentry. */
216 rpbuf_disk->rpreserved = cpu_to_le16(0);
218 *rpbuflen_ret = rpdatalen + 8;
222 /* UNIX version of getting and setting the data in reparse points */
225 static const utf16lechar volume_junction_prefix[11] = {
240 SUBST_NAME_IS_RELATIVE_LINK = -1,
241 SUBST_NAME_IS_VOLUME_JUNCTION = -2,
242 SUBST_NAME_IS_UNKNOWN = -3,
245 /* Parse the "substitute name" (link target) from a symbolic link or junction
250 * Non-negative integer:
251 * The name is an absolute symbolic link in one of several formats,
252 * and the return value is the number of UTF-16LE characters that need to
253 * be advanced to reach a simple "absolute" path starting with a backslash
254 * (i.e. skip over \??\ and/or drive letter)
256 * SUBST_NAME_IS_VOLUME_JUNCTION:
257 * The name is a volume junction.
258 * SUBST_NAME_IS_RELATIVE_LINK:
259 * The name is a relative symbolic link.
260 * SUBST_NAME_IS_UNKNOWN:
261 * The name does not appear to be a valid symbolic link, junction,
265 parse_substitute_name(const utf16lechar *substitute_name,
266 u16 substitute_name_nbytes, u32 rptag)
268 u16 substitute_name_nchars = substitute_name_nbytes / 2;
270 if (substitute_name_nchars >= 7 &&
271 substitute_name[0] == cpu_to_le16('\\') &&
272 substitute_name[1] == cpu_to_le16('?') &&
273 substitute_name[2] == cpu_to_le16('?') &&
274 substitute_name[3] == cpu_to_le16('\\') &&
275 substitute_name[4] != cpu_to_le16('\0') &&
276 substitute_name[5] == cpu_to_le16(':') &&
277 substitute_name[6] == cpu_to_le16('\\'))
279 /* "Full" symlink or junction (\??\x:\ prefixed path) */
281 } else if (rptag == WIM_IO_REPARSE_TAG_MOUNT_POINT &&
282 substitute_name_nchars >= 12 &&
283 memcmp(substitute_name, volume_junction_prefix,
284 sizeof(volume_junction_prefix)) == 0 &&
285 substitute_name[substitute_name_nchars - 1] == cpu_to_le16('\\'))
287 /* Volume junction. Can't really do anything with it. */
288 return SUBST_NAME_IS_VOLUME_JUNCTION;
289 } else if (rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
290 substitute_name_nchars >= 3 &&
291 substitute_name[0] != cpu_to_le16('\0') &&
292 substitute_name[1] == cpu_to_le16(':') &&
293 substitute_name[2] == cpu_to_le16('\\'))
295 /* "Absolute" symlink, with drive letter */
297 } else if (rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
298 substitute_name_nchars >= 1)
300 if (substitute_name[0] == cpu_to_le16('\\'))
301 /* "Absolute" symlink, without drive letter */
304 /* "Relative" symlink, without drive letter */
305 return SUBST_NAME_IS_RELATIVE_LINK;
307 return SUBST_NAME_IS_UNKNOWN;
312 * Get the UNIX-style symlink target from the WIM inode for a reparse point.
313 * Specifically, this translates the target from UTF-16 to the current multibyte
314 * encoding, strips the drive prefix if present, and replaces backslashes with
318 * The inode to read the symlink from. It must be a reparse point with
319 * tag WIM_IO_REPARSE_TAG_SYMLINK (a real symlink) or
320 * WIM_IO_REPARSE_TAG_MOUNT_POINT (a mount point or junction point).
323 * Buffer into which to place the link target.
326 * Available space in @buf, in bytes.
329 * If not NULL, the blob from which to read the reparse data. Otherwise,
330 * the reparse data will be read from the reparse point stream of @inode.
332 * If the entire symbolic link target was placed in the buffer, returns the
333 * number of bytes written. The resulting string is not null-terminated. If
334 * the symbolic link target was too large to be placed in the buffer, the first
335 * @bufsize bytes of it are placed in the buffer and
336 * -ENAMETOOLONG is returned. Otherwise, a negative errno value indicating
337 * another error is returned.
340 wim_inode_readlink(const struct wim_inode * restrict inode,
341 char * restrict buf, size_t bufsize,
342 struct blob_descriptor *blob_override)
345 struct reparse_buffer_disk rpbuf_disk _aligned_attribute(8);
346 struct reparse_data rpdata;
348 char *translated_target;
349 size_t link_target_len;
352 wimlib_assert(inode_is_symlink(inode));
354 if (wim_inode_get_reparse_data(inode, (u8*)&rpbuf_disk, &rpbuflen,
358 if (parse_reparse_data((const u8*)&rpbuf_disk, rpbuflen, &rpdata))
361 ret = utf16le_to_tstr(rpdata.substitute_name,
362 rpdata.substitute_name_nbytes,
363 &link_target, &link_target_len);
367 translated_target = link_target;
368 ret = parse_substitute_name(rpdata.substitute_name,
369 rpdata.substitute_name_nbytes,
372 case SUBST_NAME_IS_RELATIVE_LINK:
373 goto out_translate_slashes;
374 case SUBST_NAME_IS_VOLUME_JUNCTION:
376 case SUBST_NAME_IS_UNKNOWN:
377 ERROR("Can't understand reparse point "
378 "substitute name \"%s\"", link_target);
380 goto out_free_link_target;
382 translated_target += ret;
383 link_target_len -= ret;
387 out_translate_slashes:
388 for (size_t i = 0; i < link_target_len; i++)
389 if (translated_target[i] == '\\')
390 translated_target[i] = '/';
392 if (link_target_len > bufsize) {
393 link_target_len = bufsize;
396 ret = link_target_len;
398 memcpy(buf, translated_target, link_target_len);
399 out_free_link_target:
404 /* Given a UNIX-style symbolic link target, create a Windows-style reparse point
405 * buffer and assign it to the specified inode. */
407 wim_inode_set_symlink(struct wim_inode *inode, const char *target,
408 struct blob_table *blob_table)
411 struct reparse_buffer_disk rpbuf_disk _aligned_attribute(8);
412 struct reparse_data rpdata;
413 static const char abs_subst_name_prefix[12] = "\\\0?\0?\0\\\0C\0:\0";
414 static const char abs_print_name_prefix[4] = "C\0:\0";
415 utf16lechar *name_utf16le;
416 size_t name_utf16le_nbytes;
420 DEBUG("Creating reparse point data buffer for UNIX "
421 "symlink target \"%s\"", target);
422 memset(&rpdata, 0, sizeof(rpdata));
423 ret = tstr_to_utf16le(target, strlen(target),
424 &name_utf16le, &name_utf16le_nbytes);
428 for (size_t i = 0; i < name_utf16le_nbytes / 2; i++)
429 if (name_utf16le[i] == cpu_to_le16('/'))
430 name_utf16le[i] = cpu_to_le16('\\');
432 /* Compatability notes:
434 * On UNIX, an absolute symbolic link begins with '/'; everything else
435 * is a relative symbolic link. (Quite simple compared to the various
436 * ways to provide Windows paths.)
438 * To change a UNIX relative symbolic link to Windows format, we only
439 * need to translate it to UTF-16LE and replace forward slashes with
440 * backslashes. We do not make any attempt to handle filename character
441 * problems, such as a link target that itself contains backslashes on
442 * UNIX. Then, for these relative links, we set the reparse header
443 * @flags field to SYMBOLIC_LINK_RELATIVE.
445 * For UNIX absolute symbolic links, we must set the @flags field to 0.
446 * Then, there are multiple options as to actually represent the
447 * absolute link targets:
449 * (1) An absolute path beginning with one backslash character. similar
450 * to UNIX-style, just with a different path separator. Print name same
451 * as substitute name.
453 * (2) Absolute path beginning with drive letter followed by a
454 * backslash. Print name same as substitute name.
456 * (3) Absolute path beginning with drive letter followed by a
457 * backslash; substitute name prefixed with \??\, otherwise same as
460 * We choose option (3) here, and we just assume C: for the drive
461 * letter. The reasoning for this is:
463 * (1) Microsoft imagex.exe has a bug where it does not attempt to do
464 * reparse point fixups for these links, even though they are valid
465 * absolute links. (Note: in this case prefixing the substitute name
466 * with \??\ does not work; it just makes the data unable to be restored
468 * (2) Microsoft imagex.exe will fail when doing reparse point fixups
469 * for these. It apparently contains a bug that causes it to create an
470 * invalid reparse point, which then cannot be restored.
471 * (3) This is the only option I tested for which reparse point fixups
472 * worked properly in Microsoft imagex.exe.
474 * So option (3) it is.
477 rpdata.rptag = inode->i_reparse_tag;
478 if (target[0] == '/') {
479 rpdata.substitute_name_nbytes = name_utf16le_nbytes +
480 sizeof(abs_subst_name_prefix);
481 rpdata.print_name_nbytes = name_utf16le_nbytes +
482 sizeof(abs_print_name_prefix);
483 rpdata.substitute_name = alloca(rpdata.substitute_name_nbytes);
484 rpdata.print_name = alloca(rpdata.print_name_nbytes);
485 memcpy(rpdata.substitute_name, abs_subst_name_prefix,
486 sizeof(abs_subst_name_prefix));
487 memcpy(rpdata.print_name, abs_print_name_prefix,
488 sizeof(abs_print_name_prefix));
489 memcpy((void*)rpdata.substitute_name + sizeof(abs_subst_name_prefix),
490 name_utf16le, name_utf16le_nbytes);
491 memcpy((void*)rpdata.print_name + sizeof(abs_print_name_prefix),
492 name_utf16le, name_utf16le_nbytes);
494 rpdata.substitute_name_nbytes = name_utf16le_nbytes;
495 rpdata.print_name_nbytes = name_utf16le_nbytes;
496 rpdata.substitute_name = name_utf16le;
497 rpdata.print_name = name_utf16le;
498 rpdata.rpflags = SYMBOLIC_LINK_RELATIVE;
501 ret = make_reparse_buffer(&rpdata, (u8*)&rpbuf_disk, &rpbuflen);
503 if (!inode_add_stream_with_data(inode,
504 STREAM_TYPE_REPARSE_POINT,
506 (u8*)&rpbuf_disk + 8,
509 ret = WIMLIB_ERR_NOMEM;
515 #endif /* !__WIN32__ */