]> wimlib.net Git - wimlib/blob - src/tagged_items.c
Recognize tagged metadata items and use for UNIX data
[wimlib] / src / tagged_items.c
1 /*
2  * tagged_items.c
3  *
4  * Support for tagged metadata items that can be appended to WIM directory
5  * entries.
6  */
7
8 /*
9  * Copyright (C) 2014 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 3 of the License, or (at your option)
16  * any later version.
17  *
18  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20  * A PARTICULAR PURPOSE. See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31 #include "wimlib/endianness.h"
32 #include "wimlib/inode.h"
33 #include "wimlib/types.h"
34 #include "wimlib/unix_data.h"
35
36 /* Used by the Microsoft implementation.  */
37 #define TAG_OBJECT_ID           0x00000001
38
39 /* Random number that we'll use for tagging our UNIX data items.  */
40 #define TAG_WIMLIB_UNIX_DATA    0x337DD873
41
42 /* Header that begins each tagged metadata item in the metadata resource  */
43 struct tagged_item_header {
44
45         /* Unique identifier for this item.  */
46         le32 tag;
47
48         /* Size of the data of this tagged item, in bytes.  This includes this
49          * header and should be a multiple of 8.
50          *
51          * (Actually, the MS implementation seems to round this up to an 8 byte
52          * boundary when calculating the offset to the next tagged item, but
53          * uses this length unmodified when validating the item.  We might as
54          * well do the same.)  */
55         le32 length;
56
57         /* Variable length data  */
58         u8 data[];
59 };
60
61 struct object_id_disk {
62         u8 object_id[16];
63         u8 birth_volume_id[16];
64         u8 birth_object_id[16];
65         u8 domain_id[16];
66 };
67
68 struct wimlib_unix_data_disk {
69         le32 uid;
70         le32 gid;
71         le32 mode;
72         le32 reserved;
73 };
74
75 /* Retrieves the first tagged item with the specified tag and minimum length
76  * from the WIM inode.  Returns a pointer to the tagged data, which can be read
77  * and/or modified in place.  Or, if no matching tagged item is found, returns
78  * NULL.  */
79 static void *
80 inode_get_tagged_item(const struct wim_inode *inode,
81                       u32 desired_tag, u32 min_data_len)
82 {
83         size_t minlen_with_hdr = sizeof(struct tagged_item_header) + min_data_len;
84         size_t len_remaining = inode->i_extra_size;
85         u8 *p = inode->i_extra;
86
87         /* Iterate through the tagged items.  */
88         while (len_remaining >= minlen_with_hdr) {
89                 struct tagged_item_header *hdr;
90                 u32 tag;
91                 u32 len;
92
93                 hdr = (struct tagged_item_header *)p;
94                 tag = le32_to_cpu(hdr->tag);
95                 len = le32_to_cpu(hdr->length);
96
97                 if (tag == desired_tag && len >= min_data_len)
98                         return hdr->data;
99
100                 len = (len + 7) & ~7;
101                 if (len >= len_remaining)
102                         return NULL;
103                 len_remaining -= len;
104                 p = hdr->data + len;
105         }
106         return NULL;
107 }
108
109 /* Adds a tagged item to a WIM inode and returns a pointer to its uninitialized
110  * data, which must be initialized in-place by the caller.  */
111 static void *
112 inode_add_tagged_item(struct wim_inode *inode, u32 tag, u32 len)
113 {
114         size_t itemsize;
115         size_t newsize;
116         u8 *buf;
117         struct tagged_item_header *hdr;
118
119         /* We prepend the item instead of appending it because it's easier.  */
120
121         itemsize = sizeof(struct tagged_item_header) + len;
122         newsize = itemsize + inode->i_extra_size;
123
124         buf = MALLOC(newsize);
125         if (!buf)
126                 return NULL;
127
128         if (inode->i_extra_size) {
129                 memcpy(buf + itemsize, inode->i_extra, inode->i_extra_size);
130                 FREE(inode->i_extra);
131         }
132         inode->i_extra = buf;
133         inode->i_extra_size = newsize;
134
135         hdr = (struct tagged_item_header *)buf;
136         hdr->tag = cpu_to_le32(tag);
137         hdr->length = cpu_to_le32(len);
138         return hdr->data;
139 }
140
141 static inline struct wimlib_unix_data_disk *
142 inode_get_unix_data_disk(const struct wim_inode *inode)
143 {
144         return inode_get_tagged_item(inode, TAG_WIMLIB_UNIX_DATA,
145                                      sizeof(struct wimlib_unix_data_disk));
146 }
147
148 static inline struct wimlib_unix_data_disk *
149 inode_add_unix_data_disk(struct wim_inode *inode)
150 {
151         return inode_add_tagged_item(inode, TAG_WIMLIB_UNIX_DATA,
152                                      sizeof(struct wimlib_unix_data_disk));
153 }
154
155 /* Returns %true if the specified WIM inode has UNIX data; otherwise %false.
156  * This is a wimlib extension.  */
157 bool
158 inode_has_unix_data(const struct wim_inode *inode)
159 {
160         return inode_get_unix_data_disk(inode) != NULL;
161 }
162
163 /* Retrieves UNIX data from the specified WIM inode.
164  * This is a wimlib extension.
165  *
166  * Returns %true and fills @unix_data if the inode has UNIX data.
167  * Otherwise returns %false.  */
168 bool
169 inode_get_unix_data(const struct wim_inode *inode,
170                     struct wimlib_unix_data *unix_data)
171 {
172         const struct wimlib_unix_data_disk *p;
173
174         p = inode_get_unix_data_disk(inode);
175         if (!p)
176                 return false;
177
178         unix_data->uid = le32_to_cpu(p->uid);
179         unix_data->gid = le32_to_cpu(p->gid);
180         unix_data->mode = le32_to_cpu(p->mode);
181         return true;
182 }
183
184 /* Sets UNIX data on the specified WIM inode.
185  * This is a wimlib extension.
186  *
187  * Callers must specify all of @uid, @gid, and @mode.  If the inode does not yet
188  * have UNIX data, it is given these values.  Otherwise, only the values that
189  * also have the corresponding flags in @which set are changed.
190  *
191  * Returns %true if successful, %false if failed (out of memory).  */
192 bool
193 inode_set_unix_data(struct wim_inode *inode, u32 uid, u32 gid, u32 mode,
194                     int which)
195 {
196         struct wimlib_unix_data_disk *p;
197
198         p = inode_get_unix_data_disk(inode);
199         if (!p) {
200                 p = inode_add_unix_data_disk(inode);
201                 if (!p)
202                         return false;
203                 p->reserved = cpu_to_le32(0);
204                 which = UNIX_DATA_UID | UNIX_DATA_GID | UNIX_DATA_MODE;
205         }
206         if (which & UNIX_DATA_UID)
207                 p->uid = cpu_to_le32(uid);
208         if (which & UNIX_DATA_GID)
209                 p->gid = cpu_to_le32(gid);
210         if (which & UNIX_DATA_MODE)
211                 p->mode = cpu_to_le32(mode);
212         return true;
213 }