pre-resolve streams (IN PROGRESS)
[wimlib] / src / symlink.c
1 #include "dentry.h"
2 #include "io.h"
3 #include "lookup_table.h"
4 #include "sha1.h"
5 #include <errno.h>
6
7 /*
8  * Find the symlink target of a symbolic link or junction point in the WIM.
9  *
10  * See http://msdn.microsoft.com/en-us/library/cc232006(v=prot.10).aspx
11  * Except the first 8 bytes aren't included in the resource (presumably because
12  * we already know the reparse tag from the dentry, and we already know the
13  * reparse tag len from the lookup table entry resource length).
14  */
15 static ssize_t get_symlink_name(const u8 *resource, size_t resource_len,
16                                 char *buf, size_t buf_len,
17                                 u32 reparse_tag)
18 {
19         const u8 *p = resource;
20         u16 substitute_name_offset;
21         u16 substitute_name_len;
22         u16 print_name_offset;
23         u16 print_name_len;
24         char *link_target;
25         size_t link_target_len;
26         ssize_t ret;
27         unsigned header_size;
28         char *translated_target;
29         bool is_absolute;
30         u32 flags;
31
32         if (resource_len < 12)
33                 return -EIO;
34         p = get_u16(p, &substitute_name_offset);
35         p = get_u16(p, &substitute_name_len);
36         p = get_u16(p, &print_name_offset);
37         p = get_u16(p, &print_name_len);
38         get_u32(p, &flags);
39
40         wimlib_assert(reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
41                       reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
42
43         /* I think that some junction points incorrectly get marked as symbolic
44          * links.  So, parse the link buffer as a symlink if the flags seem
45          * plausible. */
46         if (flags <= 1)
47                 reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
48
49         if (reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT) {
50                 header_size = 8;
51         } else {
52                 is_absolute = (flags & 1) ? false : true;
53                 header_size = 12;
54                 p += 4;
55         }
56         if (header_size + substitute_name_offset + substitute_name_len > resource_len)
57                 return -EIO;
58         link_target = utf16_to_utf8(p + substitute_name_offset,
59                                     substitute_name_len,
60                                     &link_target_len);
61
62         if (!link_target)
63                 return -EIO;
64
65         if (link_target_len + 1 > buf_len) {
66                 ret = -ENAMETOOLONG;
67                 goto out;
68         }
69
70         translated_target = link_target;
71         if (reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT || is_absolute) {
72                 if (link_target_len < 7
73                       || memcmp(translated_target, "\\??\\", 4) != 0
74                       || translated_target[4] == '\0'
75                       || translated_target[5] != ':'
76                       || translated_target[6] != '\\') {
77                         ret = -EIO;
78                         goto out;
79                 }
80                 translated_target += 4;
81                 link_target_len -= 4;
82                 /* There's a drive letter, so just leave the backslashes since
83                  * it won't go anyhwere on UNIX anyway... */
84         } else {
85                 for (size_t i = 0; i < link_target_len; i++)
86                         if (translated_target[i] == '\\')
87                                 translated_target[i] = '/';
88         }
89
90         memcpy(buf, translated_target, link_target_len + 1);
91         ret = link_target_len;
92 out:
93         FREE(link_target);
94         return ret;
95 }
96
97 void *make_symlink_reparse_data_buf(const char *symlink_target, size_t *len_ret)
98 {
99         size_t utf8_len = strlen(symlink_target);
100         size_t utf16_len;
101         char *name_utf16 = utf8_to_utf16(symlink_target, utf8_len, &utf16_len);
102         if (!name_utf16)
103                 return NULL;
104         /*DEBUG("utf16_len = %zu", utf16_len);*/
105         for (size_t i = 0; i < utf16_len / 2; i++)
106                 if (((u16*)name_utf16)[i] == to_le16('/'))
107                         ((u16*)name_utf16)[i] = to_le16('\\');
108         size_t len = 12 + utf16_len * 2;
109         void *buf = MALLOC(len);
110         if (!buf)
111                 goto out;
112         /* XXX Fix absolute paths */
113
114         u8 *p = buf;
115         p = put_u16(p, 0); /* Substitute name offset */
116         p = put_u16(p, utf16_len); /* Substitute name length */
117         p = put_u16(p, utf16_len); /* Print name offset */
118         p = put_u16(p, utf16_len); /* Print name length */
119         p = put_u32(p, 1);
120         p = put_bytes(p, utf16_len, name_utf16);
121         p = put_bytes(p, utf16_len, name_utf16);
122         /*DEBUG("utf16_len = %zu, len = %zu", utf16_len, len);*/
123         *len_ret = len;
124 out:
125         FREE(name_utf16);
126         return buf;
127 }
128
129 /* Get the symlink target from a dentry that's already checked to be either a
130  * "real" symlink or a junction point. */
131 ssize_t dentry_readlink(const struct dentry *dentry, char *buf, size_t buf_len,
132                         const WIMStruct *w)
133 {
134         struct resource_entry *res_entry;
135         struct lookup_table_entry *lte;
136         u16 i = 0;
137         const u8 *hash = dentry->hash;
138
139         wimlib_assert(dentry_is_symlink(dentry));
140
141         while (1) {
142                 if ((lte = __lookup_resource(w->lookup_table, hash)))
143                         break;
144                 if (i == dentry->num_ads)
145                         return -EIO;
146                 hash = dentry->ads_entries[i].hash;
147                 i++;
148         }
149         res_entry = &lte->resource_entry;
150         if (res_entry->original_size > 10000)
151                 return -EIO;
152
153         char __res_buf[res_entry->original_size];
154         const char *res_buf;
155         if (lte->is_symlink && lte->symlink_buf) {
156                 res_buf = lte->symlink_buf;
157         } else {
158                 res_buf = __res_buf;
159                 if (read_full_resource(w->fp, res_entry->size, 
160                                        res_entry->original_size,
161                                        res_entry->offset,
162                                        wim_resource_compression_type(w, res_entry),
163                                        __res_buf) != 0)
164                         return -EIO;
165                 res_buf = __res_buf;
166         }
167         return get_symlink_name(res_buf, res_entry->original_size, buf,
168                                 buf_len, dentry->reparse_tag);
169 }
170
171 static int dentry_set_symlink_buf(struct dentry *dentry,
172                                   const u8 symlink_buf_hash[])
173 {
174         struct ads_entry *ads_entries;
175
176         ads_entries = CALLOC(2, sizeof(struct ads_entry));
177         if (!ads_entries)
178                 return WIMLIB_ERR_NOMEM;
179         memcpy(ads_entries[1].hash, symlink_buf_hash, WIM_HASH_SIZE);
180         dentry_free_ads_entries(dentry);
181         dentry->num_ads = 2;
182         dentry->ads_entries = ads_entries;
183         return 0;
184 }
185
186 int dentry_set_symlink(struct dentry *dentry, const char *target,
187                        struct lookup_table *lookup_table,
188                        struct lookup_table_entry **lte_ret)
189
190 {
191         int ret;
192         size_t symlink_buf_len;
193         struct lookup_table_entry *lte = NULL, *existing_lte;
194         u8 symlink_buf_hash[WIM_HASH_SIZE];
195         void *symlink_buf;
196         
197         symlink_buf = make_symlink_reparse_data_buf(target, &symlink_buf_len);
198         if (!symlink_buf)
199                 return WIMLIB_ERR_NOMEM;
200
201         DEBUG("Made symlink reparse data buf (len = %zu, name len = %zu)",
202                         symlink_buf_len, ret);
203         
204         sha1_buffer(symlink_buf, symlink_buf_len, symlink_buf_hash);
205
206         existing_lte = __lookup_resource(lookup_table, symlink_buf_hash);
207
208         if (existing_lte) {
209                 existing_lte->refcnt++;
210         } else {
211                 DEBUG("Creating new lookup table entry for symlink buf");
212                 lte = new_lookup_table_entry();
213                 if (!lte) {
214                         ret = WIMLIB_ERR_NOMEM;
215                         goto out_free_symlink_buf;
216                 }
217                 lte->is_symlink = true;
218                 lte->symlink_buf = symlink_buf;
219                 lte->resource_entry.original_size = symlink_buf_len;
220                 lte->resource_entry.size = symlink_buf_len;
221                 memcpy(lte->hash, symlink_buf_hash, WIM_HASH_SIZE);
222         }
223
224         ret = dentry_set_symlink_buf(dentry, symlink_buf_hash);
225
226         if (ret != 0)
227                 goto out_free_lte;
228
229         DEBUG("Loaded symlink buf");
230
231         if (!existing_lte)
232                 lookup_table_insert(lookup_table, lte);
233         if (lte_ret)
234                 *lte_ret = lte;
235         return 0;
236 out_free_lte:
237         FREE(lte);
238 out_free_symlink_buf:
239         FREE(symlink_buf);
240         return ret;
241 }