]> wimlib.net Git - wimlib/blob - src/symlink.c
imagex-apply.1 updates
[wimlib] / src / symlink.c
1 /*
2  * symlink.c
3  *
4  * Code to read and set symbolic links in WIM files.
5  */
6
7 /*
8  * Copyright (C) 2012 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
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)
15  * any later version.
16  *
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
20  * details.
21  *
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/.
24  */
25
26 #include "dentry.h"
27 #include "io.h"
28 #include "lookup_table.h"
29 #include "sha1.h"
30 #include <errno.h>
31
32 /*
33  * Find the symlink target of a symbolic link or junction point in the WIM.
34  *
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).
39  */
40 static ssize_t get_symlink_name(const u8 *resource, size_t resource_len,
41                                 char *buf, size_t buf_len,
42                                 u32 reparse_tag)
43 {
44         const u8 *p = resource;
45         u16 substitute_name_offset;
46         u16 substitute_name_len;
47         u16 print_name_offset;
48         u16 print_name_len;
49         char *link_target;
50         size_t link_target_len;
51         ssize_t ret;
52         unsigned header_size;
53         char *translated_target;
54         bool is_absolute;
55         u32 flags;
56
57         if (resource_len < 12)
58                 return -EIO;
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);
63         get_u32(p, &flags);
64
65         wimlib_assert(reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
66                       reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
67
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
70          * plausible. */
71         if (flags <= 1)
72                 reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
73
74         if (reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT) {
75                 header_size = 8;
76         } else {
77                 is_absolute = (flags & 1) ? false : true;
78                 header_size = 12;
79                 p += 4;
80         }
81         if (header_size + substitute_name_offset + substitute_name_len > resource_len)
82                 return -EIO;
83         link_target = utf16_to_utf8((const char *)p + substitute_name_offset,
84                                     substitute_name_len,
85                                     &link_target_len);
86
87         if (!link_target)
88                 return -EIO;
89
90         if (link_target_len + 1 > buf_len) {
91                 ret = -ENAMETOOLONG;
92                 goto out;
93         }
94
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] != '\\') {
102                         ret = -EIO;
103                         goto out;
104                 }
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... 
109                  *
110                  * XXX
111                  * NTFS-3g tries to re-map these links to actually point to
112                  * something, so maybe we could do something like that here
113                  * XXX*/
114         } else {
115                 for (size_t i = 0; i < link_target_len; i++)
116                         if (translated_target[i] == '\\')
117                                 translated_target[i] = '/';
118         }
119
120         memcpy(buf, translated_target, link_target_len + 1);
121         ret = link_target_len;
122 out:
123         FREE(link_target);
124         return ret;
125 }
126
127 void *make_symlink_reparse_data_buf(const char *symlink_target, size_t *len_ret)
128 {
129         size_t utf8_len = strlen(symlink_target);
130         size_t utf16_len;
131         char *name_utf16 = utf8_to_utf16(symlink_target, utf8_len, &utf16_len);
132         if (!name_utf16)
133                 return NULL;
134         /*DEBUG("utf16_len = %zu", utf16_len);*/
135         for (size_t i = 0; i < utf16_len / 2; i++)
136                 if (((u16*)name_utf16)[i] == to_le16('/'))
137                         ((u16*)name_utf16)[i] = to_le16('\\');
138         size_t len = 12 + utf16_len * 2 + 4;
139         void *buf = MALLOC(len);
140         if (!buf)
141                 goto out;
142         /* XXX Fix absolute paths */
143
144         u8 *p = buf;
145         p = put_u16(p, utf16_len + 2); /* 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);
150         p = put_bytes(p, utf16_len, (const u8*)name_utf16);
151         p = put_u16(p, 0);
152         p = put_bytes(p, utf16_len, (const u8*)name_utf16);
153         p = put_u16(p, 0);
154         /*DEBUG("utf16_len = %zu, len = %zu", utf16_len, len);*/
155         *len_ret = len;
156 out:
157         FREE(name_utf16);
158         return buf;
159 }
160
161 /* Get the symlink target from a dentry.
162  *
163  * The dentry may be either "real" symlink or a junction point.
164  */
165 ssize_t dentry_readlink(const struct dentry *dentry, char *buf, size_t buf_len,
166                         const WIMStruct *w)
167 {
168         const struct lookup_table_entry *lte;
169         int ret;
170
171         wimlib_assert(dentry_is_symlink(dentry));
172
173         lte = dentry_unnamed_lte(dentry, w->lookup_table);
174         if (!lte)
175                 return -EIO;
176
177         if (wim_resource_size(lte) > 10000)
178                 return -EIO;
179
180         u8 res_buf[wim_resource_size(lte)];
181         ret = read_full_wim_resource(lte, res_buf);
182         if (ret != 0)
183                 return -EIO;
184         return get_symlink_name(res_buf, wim_resource_size(lte), buf,
185                                 buf_len, dentry->reparse_tag);
186 }
187
188 static int dentry_set_symlink_buf(struct dentry *dentry,
189                                   struct lookup_table_entry *lte)
190 {
191         struct ads_entry *ads_entries;
192
193         ads_entries = CALLOC(2, sizeof(struct ads_entry));
194         if (!ads_entries)
195                 return WIMLIB_ERR_NOMEM;
196
197         wimlib_assert(dentry->num_ads == 0);
198         wimlib_assert(dentry->ads_entries == NULL);
199
200         ads_entries[0].lte = lte;
201
202         /*dentry_free_ads_entries(dentry);*/
203         dentry->num_ads = 2;
204         dentry->ads_entries = ads_entries;
205         return 0;
206 }
207
208 /* 
209  * Sets @dentry to be a symbolic link pointing to @target.
210  *
211  * A lookup table entry for the symbolic link data buffer is created and
212  * inserted into @lookup_table, unless there is an existing lookup table entry
213  * for the exact same data, in which its reference count is incremented.
214  *
215  * The lookup table entry is returned in @lte_ret.
216  *
217  * On failure @dentry and @lookup_table are not modified.
218  */
219 int dentry_set_symlink(struct dentry *dentry, const char *target,
220                        struct lookup_table *lookup_table,
221                        struct lookup_table_entry **lte_ret)
222
223 {
224         int ret;
225         size_t symlink_buf_len;
226         struct lookup_table_entry *lte = NULL, *existing_lte;
227         u8 symlink_buf_hash[SHA1_HASH_SIZE];
228         void *symlink_buf;
229         
230         symlink_buf = make_symlink_reparse_data_buf(target, &symlink_buf_len);
231         if (!symlink_buf)
232                 return WIMLIB_ERR_NOMEM;
233
234         DEBUG("Made symlink reparse data buf (len = %zu, name len = %zu)",
235                         symlink_buf_len, symlink_buf_len);
236         
237         sha1_buffer(symlink_buf, symlink_buf_len, symlink_buf_hash);
238
239         existing_lte = __lookup_resource(lookup_table, symlink_buf_hash);
240
241         if (existing_lte) {
242                 lte = existing_lte;
243                 FREE(symlink_buf);
244                 symlink_buf = NULL;
245         } else {
246                 DEBUG("Creating new lookup table entry for symlink buf");
247                 lte = new_lookup_table_entry();
248                 if (!lte) {
249                         ret = WIMLIB_ERR_NOMEM;
250                         goto out_free_symlink_buf;
251                 }
252                 lte->resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
253                 lte->attached_buffer              = symlink_buf;
254                 lte->resource_entry.original_size = symlink_buf_len;
255                 lte->resource_entry.size          = symlink_buf_len;
256                 lte->resource_entry.flags         = 0;
257                 copy_hash(lte->hash, symlink_buf_hash);
258         }
259
260         ret = dentry_set_symlink_buf(dentry, lte);
261
262         if (ret != 0)
263                 goto out_free_lte;
264
265         dentry->resolved = true;
266
267         DEBUG("Loaded symlink buf");
268
269         if (existing_lte)
270                 lte->refcnt++;
271         else
272                 lookup_table_insert(lookup_table, lte);
273         if (lte_ret)
274                 *lte_ret = lte;
275         return 0;
276 out_free_lte:
277         if (lte != existing_lte)
278                 FREE(lte);
279 out_free_symlink_buf:
280         FREE(symlink_buf);
281         return ret;
282 }