]> wimlib.net Git - wimlib/blob - src/dentry.h
Some preparations for supporting NTFS capture and apply.
[wimlib] / src / dentry.h
1 #ifndef _WIMLIB_DENTRY_H
2 #define _WIMLIB_DENTRY_H
3
4 #include "util.h"
5 #include <string.h>
6
7 /* Size of the struct dentry up to and including the file_name_len. */
8 #define WIM_DENTRY_DISK_SIZE 102
9
10 #define WIM_ADS_ENTRY_DISK_SIZE (2 * sizeof(u64) + WIM_HASH_SIZE + sizeof(u16))
11
12 /* Alternate data stream entry */
13 struct ads_entry {
14         /* SHA-1 message digest of stream contents */
15         u8 hash[WIM_HASH_SIZE];
16
17         /* Length of stream name (UTF-16) */
18         u16 stream_name_len;
19
20         /* Length of stream name (UTF-8) */
21         u16 stream_name_len_utf8;
22
23         /* Stream name (UTF-16) */
24         char *stream_name;
25
26         /* Stream name (UTF-8) */
27         char *stream_name_utf8;
28 };
29
30 static inline u64 ads_entry_length(const struct ads_entry *entry)
31 {
32         return WIM_ADS_ENTRY_DISK_SIZE + entry->stream_name_len;
33 }
34
35 /* In-memory structure for a directory entry.  There is a directory tree for
36  * each image in the WIM.  */
37 struct dentry {
38         /* The parent of this directory entry. */
39         struct dentry *parent;
40
41         /* Linked list of sibling directory entries. */
42         struct dentry *next;
43
44         struct dentry *prev;
45
46         /* Pointer to a child of this directory entry. */
47         struct dentry *children;
48
49         /* Size of directory entry, in bytes.  Typical size is around 104 to 120
50          * bytes. */
51         /* It is possible for the length field to be 0.  This situation, which
52          * is undocumented, indicates the end of a list of sibling nodes in a
53          * directory.  It also means the real length is 8, because the dentry
54          * included only the length field, but that takes up 8 bytes. */
55         u64 length;
56
57
58         /* The file attributes associated with this file. */
59         u32 attributes;
60
61         /* The index of the node in the security table that contains this file's
62          * security information.  If -1, no security information exists for this
63          * file.  */
64         int32_t security_id;
65
66         /* The offset, from the start of the metadata section, of this directory
67          * entry's child files.  0 if the directory entry has no children. */
68         u64 subdir_offset;
69
70         /* Reserved for future disuse.  Currently ignoring these fields. */
71         //u64 unused1;
72         //u64 unused2;
73
74         /* Timestamps for the entry.  The timestamps are the number of
75          * 100-nanosecond intervals that have elapsed since 12:00 A.M., January
76          * 1st, 1601, UTC. */
77         u64 creation_time;
78         u64 last_access_time;
79         u64 last_write_time;
80
81         /* A hash of the file's contents. */
82         u8 hash[WIM_HASH_SIZE];
83
84         /* Identity of a reparse point.  See
85          * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
86          * for what a reparse point is. */
87         u32 reparse_tag;
88
89         /* Although M$'s documentation does not tell you this, it seems that the
90          * reparse_reserved field does not actually exist.  So the hard_link
91          * field directly follows the reparse_tag on disk. */
92         //u32 reparse_reserved;
93
94         /* If the reparse_reserved field existed, there would be a 4-byte gap
95          * here to align hard_link on an 8-byte field.  However,
96          * reparse_reserved does not actually exist, so there is no gap here. */
97
98         /* If the file is part of a hard link set, all the directory entries in
99          * the set will share the same value for this field. */
100         u64 hard_link;
101
102         /* Number of alternate data streams associated with this file. */
103         u16 num_ads;
104
105         /* Length of short filename, in bytes, not including the terminating
106          * zero wide-character. */
107         u16 short_name_len;
108
109         /* Length of file name, in bytes, not including the terminating zero
110          * wide-character. */
111         u16 file_name_len;
112
113         /* Length of the filename converted into UTF-8, in bytes, not including
114          * the terminating zero byte. */
115         u16 file_name_utf8_len;
116
117         /* Pointer to the short filename */
118         char *short_name;
119
120         /* Pointer to the filename. */
121         char *file_name;
122
123         /* Pointer to the filename converted to UTF-8. */
124         char *file_name_utf8;
125
126         /* Full path to this dentry. */
127         char *full_path_utf8;
128         u32   full_path_utf8_len;
129
130         /* Alternate stream entries for this dentry. */
131         struct ads_entry *ads_entries;
132
133         /* Number of references to the dentry tree itself, as in multiple
134          * WIMStructs */
135         int refcnt;
136 };
137
138 #define WIM_FILE_ATTRIBUTE_READONLY            0x00000001
139 #define WIM_FILE_ATTRIBUTE_HIDDEN              0x00000002
140 #define WIM_FILE_ATTRIBUTE_SYSTEM              0x00000004
141 #define WIM_FILE_ATTRIBUTE_DIRECTORY           0x00000010
142 #define WIM_FILE_ATTRIBUTE_ARCHIVE             0x00000020
143 #define WIM_FILE_ATTRIBUTE_DEVICE              0x00000040
144 #define WIM_FILE_ATTRIBUTE_NORMAL              0x00000080
145 #define WIM_FILE_ATTRIBUTE_TEMPORARY           0x00000100
146 #define WIM_FILE_ATTRIBUTE_SPARSE_FILE         0x00000200
147 #define WIM_FILE_ATTRIBUTE_REPARSE_POINT       0x00000400
148 #define WIM_FILE_ATTRIBUTE_COMPRESSED          0x00000800
149 #define WIM_FILE_ATTRIBUTE_OFFLINE             0x00001000
150 #define WIM_FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
151 #define WIM_FILE_ATTRIBUTE_ENCRYPTED           0x00004000
152 #define WIM_FILE_ATTRIBUTE_VIRTUAL             0x00010000
153
154 extern void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry);
155
156 extern void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf, 
157                             const struct lookup_table *table);
158
159 extern int for_dentry_in_tree(struct dentry *root, 
160                               int (*visitor)(struct dentry*, void*), 
161                               void *args);
162
163 extern int for_dentry_in_tree_depth(struct dentry *root, 
164                                     int (*visitor)(struct dentry*, void*), 
165                                     void *args);
166
167 extern int calculate_dentry_full_path(struct dentry *dentry, void *ignore);
168 extern void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p);
169 extern int change_dentry_name(struct dentry *dentry, const char *new_name);
170
171 extern void unlink_dentry(struct dentry *dentry);
172 extern void link_dentry(struct dentry *dentry, struct dentry *parent);
173
174 extern int print_dentry(struct dentry *dentry, void *lookup_table);
175 extern int print_dentry_full_path(struct dentry *entry, void *ignore);
176
177 extern struct dentry *get_dentry(WIMStruct *w, const char *path);
178 extern struct dentry *get_parent_dentry(WIMStruct *w, const char *path);
179 extern struct dentry *get_dentry_child_with_name(const struct dentry *dentry, 
180                                                         const char *name);
181 extern void dentry_update_all_timestamps(struct dentry *dentry);
182 extern void init_dentry(struct dentry *dentry, const char *name);
183 extern struct dentry *new_dentry(const char *name);
184
185 extern void free_dentry(struct dentry *dentry);
186 extern void free_dentry_tree(struct dentry *root,
187                              struct lookup_table *lookup_table, 
188                              bool lt_decrement_refcnt);
189 extern int increment_dentry_refcnt(struct dentry *dentry, void *ignore);
190 extern int decrement_dentry_refcnt(struct dentry *dentry, void *ignore);
191
192 extern void calculate_dir_tree_statistics(struct dentry *root, 
193                                           struct lookup_table *table, 
194                                           u64 *dir_count_ret, 
195                                           u64 *file_count_ret, 
196                                           u64 *total_bytes_ret, 
197                                           u64 *hard_link_bytes_ret);
198
199 extern int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len, 
200                        u64 offset, struct dentry *dentry);
201
202 extern int read_dentry_tree(const u8 metadata_resource[], 
203                             u64 metadata_resource_len, struct dentry *dentry);
204
205 extern u8 *write_dentry_tree(const struct dentry *tree, u8 *p);
206
207 /* Inline utility functions for WIMDentries */
208
209 /*
210  * Returns true if @dentry has the UTF-8 file name @name that has length
211  * @name_len.
212  */
213 static inline bool dentry_has_name(const struct dentry *dentry, const char *name, 
214                                    size_t name_len)
215 {
216         if (dentry->file_name_utf8_len != name_len)
217                 return false;
218         return memcmp(dentry->file_name_utf8, name, name_len) == 0;
219 }
220
221 static inline bool dentry_is_root(const struct dentry *dentry)
222 {
223         return dentry->parent == dentry;
224 }
225
226 static inline bool dentry_is_first_sibling(const struct dentry *dentry)
227 {
228         return dentry_is_root(dentry) || dentry->parent->children == dentry;
229 }
230
231 static inline bool dentry_is_only_child(const struct dentry *dentry)
232 {
233         return dentry->next == dentry;
234 }
235
236 static inline bool dentry_is_directory(const struct dentry *dentry)
237 {
238         return (dentry->attributes & WIM_FILE_ATTRIBUTE_DIRECTORY) != 0;
239 }
240
241 static inline bool dentry_is_regular_file(const struct dentry *dentry)
242 {
243         return !dentry_is_directory(dentry);
244 }
245
246 static inline bool dentry_is_empty_directory(const struct dentry *dentry)
247 {
248         return dentry_is_directory(dentry) && dentry->children == NULL;
249 }
250
251 #endif