]> wimlib.net Git - wimlib/blob - src/symlink.c
Version 1.2.1
[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 "buffer_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
84         ret = utf16_to_utf8((const char *)p + substitute_name_offset,
85                             substitute_name_len,
86                             &link_target, &link_target_len);
87         if (ret == WIMLIB_ERR_INVALID_UTF16_STRING)
88                 return -EILSEQ;
89         else if (ret == WIMLIB_ERR_NOMEM)
90                 return -ENOMEM;
91
92         wimlib_assert(ret == 0);
93
94         if (!link_target)
95                 return -EIO;
96
97         if (link_target_len + 1 > buf_len) {
98                 ret = -ENAMETOOLONG;
99                 goto out;
100         }
101
102         translated_target = link_target;
103         if (reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT || is_absolute) {
104                 if (link_target_len < 7
105                       || memcmp(translated_target, "\\??\\", 4) != 0
106                       || translated_target[4] == '\0'
107                       || translated_target[5] != ':'
108                       || translated_target[6] != '\\') {
109                         ret = -EIO;
110                         goto out;
111                 }
112                 translated_target += 4;
113                 link_target_len -= 4;
114                 /* There's a drive letter, so just leave the backslashes since
115                  * it won't go anyhwere on UNIX anyway...
116                  *
117                  * XXX
118                  * NTFS-3g tries to re-map these links to actually point to
119                  * something, so maybe we could do something like that here
120                  * XXX*/
121         } else {
122                 for (size_t i = 0; i < link_target_len; i++)
123                         if (translated_target[i] == '\\')
124                                 translated_target[i] = '/';
125         }
126
127         memcpy(buf, translated_target, link_target_len + 1);
128         ret = link_target_len;
129 out:
130         FREE(link_target);
131         return ret;
132 }
133
134 static int make_symlink_reparse_data_buf(const char *symlink_target,
135                                          size_t *len_ret, void **buf_ret)
136 {
137         size_t utf8_len = strlen(symlink_target);
138         char *name_utf16;
139         size_t utf16_len;
140         int ret;
141
142         ret = utf8_to_utf16(symlink_target, utf8_len,
143                             &name_utf16, &utf16_len);
144         if (ret != 0)
145                 return ret;
146
147         for (size_t i = 0; i < utf16_len / 2; i++)
148                 if (((u16*)name_utf16)[i] == cpu_to_le16('/'))
149                         ((u16*)name_utf16)[i] = cpu_to_le16('\\');
150         size_t len = 12 + utf16_len * 2;
151         void *buf = MALLOC(len);
152         if (buf) {
153                 u8 *p = buf;
154                 p = put_u16(p, utf16_len); /* Substitute name offset */
155                 p = put_u16(p, utf16_len); /* Substitute name length */
156                 p = put_u16(p, 0); /* Print name offset */
157                 p = put_u16(p, utf16_len); /* Print name length */
158                 p = put_u32(p, 1); /* flags: 0 iff *full* target, including drive letter??? */
159                 p = put_bytes(p, utf16_len, (const u8*)name_utf16);
160                 p = put_bytes(p, utf16_len, (const u8*)name_utf16);
161                 *len_ret = len;
162                 *buf_ret = buf;
163                 ret = 0;
164         } else {
165                 ret = WIMLIB_ERR_NOMEM;
166         }
167         FREE(name_utf16);
168         return ret;
169 }
170
171 /* Get the symlink target from a dentry.
172  *
173  * The dentry may be either "real" symlink or a junction point.
174  */
175 ssize_t inode_readlink(const struct inode *inode, char *buf, size_t buf_len,
176                        const WIMStruct *w, int read_resource_flags)
177 {
178         const struct lookup_table_entry *lte;
179         int ret;
180
181         wimlib_assert(inode_is_symlink(inode));
182
183         lte = inode_unnamed_lte(inode, w->lookup_table);
184         if (!lte)
185                 return -EIO;
186
187         if (wim_resource_size(lte) > 10000)
188                 return -EIO;
189
190         u8 res_buf[wim_resource_size(lte)];
191         ret = read_full_wim_resource(lte, res_buf, read_resource_flags);
192         if (ret != 0)
193                 return -EIO;
194         return get_symlink_name(res_buf, wim_resource_size(lte), buf,
195                                 buf_len, inode->reparse_tag);
196 }
197
198 /*
199  * Sets @inode to be a symbolic link pointing to @target.
200  *
201  * A lookup table entry for the symbolic link data buffer is created and
202  * inserted into @lookup_table, unless there is an existing lookup table entry
203  * for the exact same data, in which its reference count is incremented.
204  *
205  * The lookup table entry is returned in @lte_ret.
206  *
207  * On failure @dentry and @lookup_table are not modified.
208  */
209 int inode_set_symlink(struct inode *inode, const char *target,
210                       struct lookup_table *lookup_table,
211                       struct lookup_table_entry **lte_ret)
212
213 {
214         int ret;
215         size_t symlink_buf_len;
216         struct lookup_table_entry *lte = NULL, *existing_lte;
217         u8 symlink_buf_hash[SHA1_HASH_SIZE];
218         void *symlink_buf;
219
220         ret = make_symlink_reparse_data_buf(target, &symlink_buf_len,
221                                             &symlink_buf);
222         if (ret != 0)
223                 return ret;
224
225         DEBUG("Made symlink reparse data buf (len = %zu, name len = %zu)",
226                         symlink_buf_len, symlink_buf_len);
227
228         sha1_buffer(symlink_buf, symlink_buf_len, symlink_buf_hash);
229
230         existing_lte = __lookup_resource(lookup_table, symlink_buf_hash);
231
232         if (existing_lte) {
233                 lte = existing_lte;
234                 FREE(symlink_buf);
235                 symlink_buf = NULL;
236         } else {
237                 DEBUG("Creating new lookup table entry for symlink buf");
238                 lte = new_lookup_table_entry();
239                 if (!lte) {
240                         ret = WIMLIB_ERR_NOMEM;
241                         goto out_free_symlink_buf;
242                 }
243                 lte->resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
244                 lte->attached_buffer              = symlink_buf;
245                 lte->resource_entry.original_size = symlink_buf_len;
246                 lte->resource_entry.size          = symlink_buf_len;
247                 copy_hash(lte->hash, symlink_buf_hash);
248         }
249
250         inode->lte = lte;
251         inode->resolved = true;
252
253         DEBUG("Loaded symlink buf");
254
255         if (existing_lte)
256                 lte->refcnt++;
257         else
258                 lookup_table_insert(lookup_table, lte);
259         if (lte_ret)
260                 *lte_ret = lte;
261         return 0;
262 out_free_symlink_buf:
263         FREE(symlink_buf);
264         return ret;
265 }