]> wimlib.net Git - wimlib/blob - src/symlink.c
Improve char encoding support (IN PROGRESS)
[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, 2013 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 for a
36  * description of the format of the so-called "reparse point data buffers".
37  *
38  * But, in the WIM format, the first 8 bytes of the reparse point data buffer
39  * are omitted, presumably because we already know the reparse tag from the
40  * dentry, and we already know the reparse tag length from the lookup table
41  * entry resource length.
42  */
43 static ssize_t
44 get_symlink_name(const void *resource, size_t resource_len,
45                  void *buf, size_t buf_len,
46                  u32 reparse_tag)
47 {
48         const u8 *p = resource;
49         u16 substitute_name_offset;
50         u16 substitute_name_len;
51         u16 print_name_offset;
52         u16 print_name_len;
53         char *link_target;
54         size_t link_target_len;
55         ssize_t ret;
56         unsigned header_size;
57         char *translated_target;
58         bool is_absolute;
59         u32 flags;
60
61         if (resource_len < 12)
62                 return -EIO;
63         p = get_u16(p, &substitute_name_offset);
64         p = get_u16(p, &substitute_name_len);
65         p = get_u16(p, &print_name_offset);
66         p = get_u16(p, &print_name_len);
67         get_u32(p, &flags);
68
69         wimlib_assert(reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
70                       reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
71
72         if (reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT) {
73                 header_size = 8;
74         } else {
75                 is_absolute = (flags & 1) ? false : true;
76                 header_size = 12;
77                 p += 4;
78         }
79         if (header_size + substitute_name_offset + substitute_name_len > resource_len)
80                 return -EIO;
81
82         ret = utf16_to_utf8((const char *)p + substitute_name_offset,
83                             substitute_name_len,
84                             &link_target, &link_target_len);
85         if (ret == WIMLIB_ERR_INVALID_UTF16_STRING)
86                 return -EILSEQ;
87         else if (ret == WIMLIB_ERR_NOMEM)
88                 return -ENOMEM;
89
90         wimlib_assert(ret == 0);
91
92         if (!link_target)
93                 return -EIO;
94
95         if (link_target_len + 1 > buf_len) {
96                 ret = -ENAMETOOLONG;
97                 goto out;
98         }
99
100         translated_target = link_target;
101         if (reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT || is_absolute) {
102                 if (link_target_len < 7
103                       || memcmp(translated_target, "\\??\\", 4) != 0
104                       || translated_target[4] == '\0'
105                       || translated_target[5] != ':'
106                       || translated_target[6] != '\\') {
107                         ret = -EIO;
108                         goto out;
109                 }
110                 translated_target += 4;
111                 link_target_len -= 4;
112                 /* There's a drive letter, so just leave the backslashes since
113                  * it won't go anyhwere on UNIX anyway...
114                  *
115                  * XXX
116                  * NTFS-3g tries to re-map these links to actually point to
117                  * something, so maybe we could do something like that here
118                  * XXX*/
119         } else {
120                 for (size_t i = 0; i < link_target_len; i++)
121                         if (translated_target[i] == '\\')
122                                 translated_target[i] = '/';
123         }
124
125         memcpy(buf, translated_target, link_target_len + 1);
126         ret = link_target_len;
127 out:
128         FREE(link_target);
129         return ret;
130 }
131
132 static int
133 make_symlink_reparse_data_buf(const mbchar *symlink_target,
134                               size_t *len_ret, void **buf_ret)
135 {
136         size_t utf8_len = strlen(symlink_target);
137         char *name_utf16;
138         size_t utf16_len;
139         int ret;
140
141         ret = utf8_to_utf16(symlink_target, utf8_len,
142                             &name_utf16, &utf16_len);
143         if (ret != 0)
144                 return ret;
145
146         for (size_t i = 0; i < utf16_len / 2; i++)
147                 if (((u16*)name_utf16)[i] == cpu_to_le16('/'))
148                         ((u16*)name_utf16)[i] = cpu_to_le16('\\');
149         size_t len = 12 + utf16_len * 2;
150         void *buf = MALLOC(len);
151         if (buf) {
152                 u8 *p = buf;
153                 p = put_u16(p, utf16_len); /* Substitute name offset */
154                 p = put_u16(p, utf16_len); /* Substitute name length */
155                 p = put_u16(p, 0); /* Print name offset */
156                 p = put_u16(p, utf16_len); /* Print name length */
157                 p = put_u32(p, 1); /* flags: 0 iff *full* target, including drive letter??? */
158                 p = put_bytes(p, utf16_len, (const u8*)name_utf16);
159                 p = put_bytes(p, utf16_len, (const u8*)name_utf16);
160                 *len_ret = len;
161                 *buf_ret = buf;
162                 ret = 0;
163         } else {
164                 ret = WIMLIB_ERR_NOMEM;
165         }
166         FREE(name_utf16);
167         return ret;
168 }
169
170 /* Get the symlink target from a WIM inode.
171  *
172  * The inode may be either a "real" symlink (reparse tag
173  * WIM_IO_REPARSE_TAG_SYMLINK), or it may be a junction point (reparse tag
174  * WIM_IO_REPARSE_TAG_MOUNT_POINT).
175  */
176 ssize_t
177 inode_readlink(const struct wim_inode *inode, mbchar *buf, size_t buf_len,
178                const WIMStruct *w, int read_resource_flags)
179 {
180         const struct wim_lookup_table_entry *lte;
181         int ret;
182
183         wimlib_assert(inode_is_symlink(inode));
184
185         lte = inode_unnamed_lte(inode, w->lookup_table);
186         if (!lte)
187                 return -EIO;
188
189         if (wim_resource_size(lte) > 10000)
190                 return -EIO;
191
192         u8 res_buf[wim_resource_size(lte)];
193         ret = read_full_wim_resource(lte, res_buf, read_resource_flags);
194         if (ret != 0)
195                 return -EIO;
196         return get_symlink_name(res_buf, wim_resource_size(lte), buf,
197                                 buf_len, inode->i_reparse_tag);
198 }
199
200 /*
201  * Sets @inode to be a symbolic link pointing to @target.
202  *
203  * A lookup table entry for the symbolic link data buffer is created and
204  * inserted into @lookup_table, unless there is an existing lookup table entry
205  * for the exact same data, in which its reference count is incremented.
206  *
207  * The lookup table entry is returned in @lte_ret.
208  *
209  * On failure @dentry and @lookup_table are not modified.
210  */
211 int
212 inode_set_symlink(struct wim_inode *inode,
213                   const mbchar *target,
214                   struct wim_lookup_table *lookup_table,
215                   struct wim_lookup_table_entry **lte_ret)
216
217 {
218         int ret;
219         size_t symlink_buf_len;
220         struct wim_lookup_table_entry *lte = NULL, *existing_lte;
221         u8 symlink_buf_hash[SHA1_HASH_SIZE];
222         void *symlink_buf;
223
224         ret = make_symlink_reparse_data_buf(target, &symlink_buf_len,
225                                             &symlink_buf);
226         if (ret != 0)
227                 return ret;
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                 FREE(symlink_buf);
239                 symlink_buf = NULL;
240         } else {
241                 DEBUG("Creating new lookup table entry for symlink buf");
242                 lte = new_lookup_table_entry();
243                 if (!lte) {
244                         ret = WIMLIB_ERR_NOMEM;
245                         goto out_free_symlink_buf;
246                 }
247                 lte->resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
248                 lte->attached_buffer              = symlink_buf;
249                 lte->resource_entry.original_size = symlink_buf_len;
250                 lte->resource_entry.size          = symlink_buf_len;
251                 copy_hash(lte->hash, symlink_buf_hash);
252         }
253
254         inode->i_lte = lte;
255         inode->i_resolved = 1;
256
257         DEBUG("Loaded symlink buf");
258
259         if (existing_lte)
260                 lte->refcnt++;
261         else
262                 lookup_table_insert(lookup_table, lte);
263         if (lte_ret)
264                 *lte_ret = lte;
265         return 0;
266 out_free_symlink_buf:
267         FREE(symlink_buf);
268         return ret;
269 }