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