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