2 * Copyright (C) 2012 Eric Biggers
4 * This file is part of wimlib, a library for working with WIM files.
6 * wimlib is free software; you can redistribute it and/or modify it under the
7 * terms of the GNU Lesser General Public License as published by the Free
8 * Software Foundation; either version 2.1 of the License, or (at your option)
11 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with wimlib; if not, see http://www.gnu.org/licenses/.
22 #include "lookup_table.h"
27 * Find the symlink target of a symbolic link or junction point in the WIM.
29 * See http://msdn.microsoft.com/en-us/library/cc232006(v=prot.10).aspx
30 * Except the first 8 bytes aren't included in the resource (presumably because
31 * we already know the reparse tag from the dentry, and we already know the
32 * reparse tag len from the lookup table entry resource length).
34 static ssize_t get_symlink_name(const u8 *resource, size_t resource_len,
35 char *buf, size_t buf_len,
38 const u8 *p = resource;
39 u16 substitute_name_offset;
40 u16 substitute_name_len;
41 u16 print_name_offset;
44 size_t link_target_len;
47 char *translated_target;
51 if (resource_len < 12)
53 p = get_u16(p, &substitute_name_offset);
54 p = get_u16(p, &substitute_name_len);
55 p = get_u16(p, &print_name_offset);
56 p = get_u16(p, &print_name_len);
59 wimlib_assert(reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
60 reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
62 /* I think that some junction points incorrectly get marked as symbolic
63 * links. So, parse the link buffer as a symlink if the flags seem
66 reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
68 if (reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT) {
71 is_absolute = (flags & 1) ? false : true;
75 if (header_size + substitute_name_offset + substitute_name_len > resource_len)
77 link_target = utf16_to_utf8(p + substitute_name_offset,
84 if (link_target_len + 1 > buf_len) {
89 translated_target = link_target;
90 if (reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT || is_absolute) {
91 if (link_target_len < 7
92 || memcmp(translated_target, "\\??\\", 4) != 0
93 || translated_target[4] == '\0'
94 || translated_target[5] != ':'
95 || translated_target[6] != '\\') {
99 translated_target += 4;
100 link_target_len -= 4;
101 /* There's a drive letter, so just leave the backslashes since
102 * it won't go anyhwere on UNIX anyway... */
104 for (size_t i = 0; i < link_target_len; i++)
105 if (translated_target[i] == '\\')
106 translated_target[i] = '/';
109 memcpy(buf, translated_target, link_target_len + 1);
110 ret = link_target_len;
116 void *make_symlink_reparse_data_buf(const char *symlink_target, size_t *len_ret)
118 size_t utf8_len = strlen(symlink_target);
120 char *name_utf16 = utf8_to_utf16(symlink_target, utf8_len, &utf16_len);
123 /*DEBUG("utf16_len = %zu", utf16_len);*/
124 for (size_t i = 0; i < utf16_len / 2; i++)
125 if (((u16*)name_utf16)[i] == to_le16('/'))
126 ((u16*)name_utf16)[i] = to_le16('\\');
127 size_t len = 12 + utf16_len * 2;
128 void *buf = MALLOC(len);
131 /* XXX Fix absolute paths */
134 p = put_u16(p, 0); /* Substitute name offset */
135 p = put_u16(p, utf16_len); /* Substitute name length */
136 p = put_u16(p, utf16_len); /* Print name offset */
137 p = put_u16(p, utf16_len); /* Print name length */
139 p = put_bytes(p, utf16_len, name_utf16);
140 p = put_bytes(p, utf16_len, name_utf16);
141 /*DEBUG("utf16_len = %zu, len = %zu", utf16_len, len);*/
148 static const struct lookup_table_entry *
149 dentry_first_lte(const struct dentry *dentry, const struct lookup_table *table)
151 const struct lookup_table_entry *lte;
152 if (dentry->resolved) {
155 for (u16 i = 0; i < dentry->num_ads; i++)
156 if (dentry->ads_entries[i].lte)
157 return dentry->ads_entries[i].lte;
159 const u8 *hash = dentry->hash;
162 if ((lte = __lookup_resource(table, hash)))
164 if (i == dentry->num_ads)
166 hash = dentry->ads_entries[i].hash;
173 /* Get the symlink target from a dentry that's already checked to be either a
174 * "real" symlink or a junction point. */
175 ssize_t dentry_readlink(const struct dentry *dentry, char *buf, size_t buf_len,
178 const struct resource_entry *res_entry;
179 const struct lookup_table_entry *lte;
181 wimlib_assert(dentry_is_symlink(dentry));
183 lte = dentry_first_lte(dentry, w->lookup_table);
187 res_entry = <e->resource_entry;
188 if (res_entry->original_size > 10000)
191 char __res_buf[res_entry->original_size];
193 if (lte->is_symlink && lte->symlink_buf) {
194 res_buf = lte->symlink_buf;
197 if (read_full_resource(w->fp, res_entry->size,
198 res_entry->original_size,
200 wim_resource_compression_type(w, res_entry),
205 return get_symlink_name(res_buf, res_entry->original_size, buf,
206 buf_len, dentry->reparse_tag);
209 static int dentry_set_symlink_buf(struct dentry *dentry,
210 const u8 symlink_buf_hash[])
212 struct ads_entry *ads_entries;
214 ads_entries = CALLOC(2, sizeof(struct ads_entry));
216 return WIMLIB_ERR_NOMEM;
217 memcpy(ads_entries[1].hash, symlink_buf_hash, WIM_HASH_SIZE);
218 wimlib_assert(dentry->num_ads == 0);
219 wimlib_assert(!dentry->ads_entries);
220 /*dentry_free_ads_entries(dentry);*/
222 dentry->ads_entries = ads_entries;
226 int dentry_set_symlink(struct dentry *dentry, const char *target,
227 struct lookup_table *lookup_table,
228 struct lookup_table_entry **lte_ret)
232 size_t symlink_buf_len;
233 struct lookup_table_entry *lte = NULL, *existing_lte;
234 u8 symlink_buf_hash[WIM_HASH_SIZE];
237 symlink_buf = make_symlink_reparse_data_buf(target, &symlink_buf_len);
239 return WIMLIB_ERR_NOMEM;
241 DEBUG("Made symlink reparse data buf (len = %zu, name len = %zu)",
242 symlink_buf_len, ret);
244 sha1_buffer(symlink_buf, symlink_buf_len, symlink_buf_hash);
246 existing_lte = __lookup_resource(lookup_table, symlink_buf_hash);
249 existing_lte->refcnt++;
252 DEBUG("Creating new lookup table entry for symlink buf");
253 lte = new_lookup_table_entry();
255 ret = WIMLIB_ERR_NOMEM;
256 goto out_free_symlink_buf;
258 lte->is_symlink = true;
259 lte->symlink_buf = symlink_buf;
260 lte->resource_entry.original_size = symlink_buf_len;
261 lte->resource_entry.size = symlink_buf_len;
262 memcpy(lte->hash, symlink_buf_hash, WIM_HASH_SIZE);
265 ret = dentry_set_symlink_buf(dentry, symlink_buf_hash);
270 DEBUG("Loaded symlink buf");
273 lookup_table_insert(lookup_table, lte);
278 if (lte != existing_lte)
280 out_free_symlink_buf: