4 * Code to read and set symbolic links in WIM files.
8 * Copyright (C) 2012 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your option)
17 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 * A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with wimlib; if not, see http://www.gnu.org/licenses/.
27 #include "buffer_io.h"
28 #include "lookup_table.h"
33 * Find the symlink target of a symbolic link or junction point in the WIM.
35 * See http://msdn.microsoft.com/en-us/library/cc232006(v=prot.10).aspx
36 * Except the first 8 bytes aren't included in the resource (presumably because
37 * we already know the reparse tag from the dentry, and we already know the
38 * reparse tag len from the lookup table entry resource length).
40 static ssize_t get_symlink_name(const u8 *resource, size_t resource_len,
41 char *buf, size_t buf_len,
44 const u8 *p = resource;
45 u16 substitute_name_offset;
46 u16 substitute_name_len;
47 u16 print_name_offset;
50 size_t link_target_len;
53 char *translated_target;
57 if (resource_len < 12)
59 p = get_u16(p, &substitute_name_offset);
60 p = get_u16(p, &substitute_name_len);
61 p = get_u16(p, &print_name_offset);
62 p = get_u16(p, &print_name_len);
65 wimlib_assert(reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
66 reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
68 /* I think that some junction points incorrectly get marked as symbolic
69 * links. So, parse the link buffer as a symlink if the flags seem
72 reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
74 if (reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT) {
77 is_absolute = (flags & 1) ? false : true;
81 if (header_size + substitute_name_offset + substitute_name_len > resource_len)
83 link_target = utf16_to_utf8((const char *)p + substitute_name_offset,
90 if (link_target_len + 1 > buf_len) {
95 translated_target = link_target;
96 if (reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT || is_absolute) {
97 if (link_target_len < 7
98 || memcmp(translated_target, "\\??\\", 4) != 0
99 || translated_target[4] == '\0'
100 || translated_target[5] != ':'
101 || translated_target[6] != '\\') {
105 translated_target += 4;
106 link_target_len -= 4;
107 /* There's a drive letter, so just leave the backslashes since
108 * it won't go anyhwere on UNIX anyway...
111 * NTFS-3g tries to re-map these links to actually point to
112 * something, so maybe we could do something like that here
115 for (size_t i = 0; i < link_target_len; i++)
116 if (translated_target[i] == '\\')
117 translated_target[i] = '/';
120 memcpy(buf, translated_target, link_target_len + 1);
121 ret = link_target_len;
127 static void *make_symlink_reparse_data_buf(const char *symlink_target,
130 size_t utf8_len = strlen(symlink_target);
132 char *name_utf16 = utf8_to_utf16(symlink_target, utf8_len, &utf16_len);
136 for (size_t i = 0; i < utf16_len / 2; i++)
137 if (((u16*)name_utf16)[i] == cpu_to_le16('/'))
138 ((u16*)name_utf16)[i] = cpu_to_le16('\\');
139 size_t len = 12 + utf16_len * 2;
140 void *buf = MALLOC(len);
145 p = put_u16(p, utf16_len); /* Substitute name offset */
146 p = put_u16(p, utf16_len); /* Substitute name length */
147 p = put_u16(p, 0); /* Print name offset */
148 p = put_u16(p, utf16_len); /* Print name length */
149 p = put_u32(p, 1); /* flags: 0 iff *full* target, including drive letter??? */
150 p = put_bytes(p, utf16_len, (const u8*)name_utf16);
151 p = put_bytes(p, utf16_len, (const u8*)name_utf16);
158 /* Get the symlink target from a dentry.
160 * The dentry may be either "real" symlink or a junction point.
162 ssize_t inode_readlink(const struct inode *inode, char *buf, size_t buf_len,
163 const WIMStruct *w, int read_resource_flags)
165 const struct lookup_table_entry *lte;
168 wimlib_assert(inode_is_symlink(inode));
170 lte = inode_unnamed_lte(inode, w->lookup_table);
174 if (wim_resource_size(lte) > 10000)
177 u8 res_buf[wim_resource_size(lte)];
178 ret = read_full_wim_resource(lte, res_buf, read_resource_flags);
181 return get_symlink_name(res_buf, wim_resource_size(lte), buf,
182 buf_len, inode->reparse_tag);
186 * Sets @inode to be a symbolic link pointing to @target.
188 * A lookup table entry for the symbolic link data buffer is created and
189 * inserted into @lookup_table, unless there is an existing lookup table entry
190 * for the exact same data, in which its reference count is incremented.
192 * The lookup table entry is returned in @lte_ret.
194 * On failure @dentry and @lookup_table are not modified.
196 int inode_set_symlink(struct inode *inode, const char *target,
197 struct lookup_table *lookup_table,
198 struct lookup_table_entry **lte_ret)
202 size_t symlink_buf_len;
203 struct lookup_table_entry *lte = NULL, *existing_lte;
204 u8 symlink_buf_hash[SHA1_HASH_SIZE];
207 symlink_buf = make_symlink_reparse_data_buf(target, &symlink_buf_len);
209 return WIMLIB_ERR_NOMEM;
211 DEBUG("Made symlink reparse data buf (len = %zu, name len = %zu)",
212 symlink_buf_len, symlink_buf_len);
214 sha1_buffer(symlink_buf, symlink_buf_len, symlink_buf_hash);
216 existing_lte = __lookup_resource(lookup_table, symlink_buf_hash);
223 DEBUG("Creating new lookup table entry for symlink buf");
224 lte = new_lookup_table_entry();
226 ret = WIMLIB_ERR_NOMEM;
227 goto out_free_symlink_buf;
229 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
230 lte->attached_buffer = symlink_buf;
231 lte->resource_entry.original_size = symlink_buf_len;
232 lte->resource_entry.size = symlink_buf_len;
233 copy_hash(lte->hash, symlink_buf_hash);
237 inode->resolved = true;
239 DEBUG("Loaded symlink buf");
244 lookup_table_insert(lookup_table, lte);
248 out_free_symlink_buf: