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