]> wimlib.net Git - wimlib/blob - src/symlink.c
Various fixes
[wimlib] / src / symlink.c
1 /*
2  * Copyright (C) 2012 Eric Biggers
3  *
4  * This file is part of wimlib, a library for working with WIM files.
5  *
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)
9  * any later version.
10  *
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
14  * details.
15  *
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/.
18  */
19
20 #include "dentry.h"
21 #include "io.h"
22 #include "lookup_table.h"
23 #include "sha1.h"
24 #include <errno.h>
25
26 /*
27  * Find the symlink target of a symbolic link or junction point in the WIM.
28  *
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).
33  */
34 static ssize_t get_symlink_name(const u8 *resource, size_t resource_len,
35                                 char *buf, size_t buf_len,
36                                 u32 reparse_tag)
37 {
38         const u8 *p = resource;
39         u16 substitute_name_offset;
40         u16 substitute_name_len;
41         u16 print_name_offset;
42         u16 print_name_len;
43         char *link_target;
44         size_t link_target_len;
45         ssize_t ret;
46         unsigned header_size;
47         char *translated_target;
48         bool is_absolute;
49         u32 flags;
50
51         if (resource_len < 12)
52                 return -EIO;
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);
57         get_u32(p, &flags);
58
59         wimlib_assert(reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
60                       reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
61
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
64          * plausible. */
65         if (flags <= 1)
66                 reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
67
68         if (reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT) {
69                 header_size = 8;
70         } else {
71                 is_absolute = (flags & 1) ? false : true;
72                 header_size = 12;
73                 p += 4;
74         }
75         if (header_size + substitute_name_offset + substitute_name_len > resource_len)
76                 return -EIO;
77         link_target = utf16_to_utf8(p + substitute_name_offset,
78                                     substitute_name_len,
79                                     &link_target_len);
80
81         if (!link_target)
82                 return -EIO;
83
84         if (link_target_len + 1 > buf_len) {
85                 ret = -ENAMETOOLONG;
86                 goto out;
87         }
88
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] != '\\') {
96                         ret = -EIO;
97                         goto out;
98                 }
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... */
103         } else {
104                 for (size_t i = 0; i < link_target_len; i++)
105                         if (translated_target[i] == '\\')
106                                 translated_target[i] = '/';
107         }
108
109         memcpy(buf, translated_target, link_target_len + 1);
110         ret = link_target_len;
111 out:
112         FREE(link_target);
113         return ret;
114 }
115
116 void *make_symlink_reparse_data_buf(const char *symlink_target, size_t *len_ret)
117 {
118         size_t utf8_len = strlen(symlink_target);
119         size_t utf16_len;
120         char *name_utf16 = utf8_to_utf16(symlink_target, utf8_len, &utf16_len);
121         if (!name_utf16)
122                 return NULL;
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);
129         if (!buf)
130                 goto out;
131         /* XXX Fix absolute paths */
132
133         u8 *p = buf;
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 */
138         p = put_u32(p, 1);
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);*/
142         *len_ret = len;
143 out:
144         FREE(name_utf16);
145         return buf;
146 }
147
148 /* Get the symlink target from a dentry that's already checked to be either a
149  * "real" symlink or a junction point. */
150 ssize_t dentry_readlink(const struct dentry *dentry, char *buf, size_t buf_len,
151                         const WIMStruct *w)
152 {
153         const struct resource_entry *res_entry;
154         const struct lookup_table_entry *lte;
155
156         wimlib_assert(dentry_is_symlink(dentry));
157
158         lte = dentry_first_lte(dentry, w->lookup_table);
159         if (!lte)
160                 return -EIO;
161
162         res_entry = &lte->resource_entry;
163         if (res_entry->original_size > 10000)
164                 return -EIO;
165
166         char __res_buf[res_entry->original_size];
167         const char *res_buf;
168         if (lte->is_symlink && lte->symlink_buf) {
169                 res_buf = lte->symlink_buf;
170         } else {
171                 if (read_full_resource(w->fp, res_entry->size, 
172                                        res_entry->original_size,
173                                        res_entry->offset,
174                                        wim_resource_compression_type(w, res_entry),
175                                        __res_buf) != 0)
176                         return -EIO;
177                 res_buf = __res_buf;
178         }
179         return get_symlink_name(res_buf, res_entry->original_size, buf,
180                                 buf_len, dentry->reparse_tag);
181 }
182
183 static int dentry_set_symlink_buf(struct dentry *dentry,
184                                   struct lookup_table_entry *lte)
185 {
186         struct ads_entry *ads_entries;
187
188         ads_entries = CALLOC(2, sizeof(struct ads_entry));
189         if (!ads_entries)
190                 return WIMLIB_ERR_NOMEM;
191
192         wimlib_assert(dentry->num_ads == 0);
193         wimlib_assert(dentry->ads_entries == NULL);
194
195         ads_entries[1].lte = lte;
196
197         /*dentry_free_ads_entries(dentry);*/
198         dentry->num_ads = 2;
199         dentry->ads_entries = ads_entries;
200         return 0;
201 }
202
203 /* 
204  * Sets @dentry to be a symbolic link pointing to @target.
205  *
206  * A lookup table entry for the symbolic link data buffer is created and
207  * inserted into @lookup_table, unless there is an existing lookup table entry
208  * for the exact same data, in which its reference count is incremented.
209  *
210  * The lookup table entry is returned in @lte_ret.
211  *
212  * On failure @dentry and @lookup_table are not modified.
213  */
214 int dentry_set_symlink(struct dentry *dentry, const char *target,
215                        struct lookup_table *lookup_table,
216                        struct lookup_table_entry **lte_ret)
217
218 {
219         int ret;
220         size_t symlink_buf_len;
221         struct lookup_table_entry *lte = NULL, *existing_lte;
222         u8 symlink_buf_hash[SHA1_HASH_SIZE];
223         void *symlink_buf;
224         
225         symlink_buf = make_symlink_reparse_data_buf(target, &symlink_buf_len);
226         if (!symlink_buf)
227                 return WIMLIB_ERR_NOMEM;
228
229         DEBUG("Made symlink reparse data buf (len = %zu, name len = %zu)",
230                         symlink_buf_len, symlink_buf_len);
231         
232         sha1_buffer(symlink_buf, symlink_buf_len, symlink_buf_hash);
233
234         existing_lte = __lookup_resource(lookup_table, symlink_buf_hash);
235
236         if (existing_lte) {
237                 lte = existing_lte;
238         } else {
239                 DEBUG("Creating new lookup table entry for symlink buf");
240                 lte = new_lookup_table_entry();
241                 if (!lte) {
242                         ret = WIMLIB_ERR_NOMEM;
243                         goto out_free_symlink_buf;
244                 }
245                 lte->is_symlink = true;
246                 lte->symlink_buf = symlink_buf;
247                 lte->resource_entry.original_size = symlink_buf_len;
248                 lte->resource_entry.size = symlink_buf_len;
249                 copy_hash(lte->hash, symlink_buf_hash);
250         }
251
252         ret = dentry_set_symlink_buf(dentry, lte);
253
254         if (ret != 0)
255                 goto out_free_lte;
256
257         dentry->resolved = true;
258
259         DEBUG("Loaded symlink buf");
260
261         if (existing_lte)
262                 lte->refcnt++;
263         else
264                 lookup_table_insert(lookup_table, lte);
265         if (lte_ret)
266                 *lte_ret = lte;
267         return 0;
268 out_free_lte:
269         if (lte != existing_lte)
270                 FREE(lte);
271 out_free_symlink_buf:
272         FREE(symlink_buf);
273         return ret;
274 }