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