ac3fa4d75995d7818ef7a1d17527b052b2d4ab22
[lwext4.git] / lwext4 / ext4_dir.h
1 /*\r
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)\r
3  *\r
4  *\r
5  * HelenOS:\r
6  * Copyright (c) 2012 Martin Sucha\r
7  * Copyright (c) 2012 Frantisek Princ\r
8  * All rights reserved.\r
9  *\r
10  * Redistribution and use in source and binary forms, with or without\r
11  * modification, are permitted provided that the following conditions\r
12  * are met:\r
13  *\r
14  * - Redistributions of source code must retain the above copyright\r
15  *   notice, this list of conditions and the following disclaimer.\r
16  * - Redistributions in binary form must reproduce the above copyright\r
17  *   notice, this list of conditions and the following disclaimer in the\r
18  *   documentation and/or other materials provided with the distribution.\r
19  * - The name of the author may not be used to endorse or promote products\r
20  *   derived from this software without specific prior written permission.\r
21  *\r
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
32  */\r
33 \r
34 /** @addtogroup lwext4\r
35  * @{\r
36  */\r
37 /**\r
38  * @file  ext4_dir.h\r
39  * @brief Directory handle procedures.\r
40  */\r
41 \r
42 #ifndef EXT4_DIR_H_\r
43 #define EXT4_DIR_H_\r
44 \r
45 #include <ext4_config.h>\r
46 #include <ext4_types.h>\r
47 #include <ext4_blockdev.h>\r
48 #include <ext4_super.h>\r
49 \r
50 #include <stdint.h>\r
51 \r
52 /**@brief Get i-node number from directory entry.\r
53  * @param de Directory entry\r
54  * @return I-node number\r
55  */\r
56 static inline uint32_t ext4_dir_entry_ll_get_inode(\r
57         struct ext4_directory_entry_ll *de)\r
58 {\r
59     return to_le32(de->inode);\r
60 }\r
61 \r
62 \r
63 /**@brief Set i-node number to directory entry.\r
64  * @param de Directory entry\r
65  * @param inode I-node number\r
66  */\r
67 static inline void ext4_dir_entry_ll_set_inode(\r
68         struct ext4_directory_entry_ll *de, uint32_t inode)\r
69 {\r
70     de->inode = to_le32(inode);\r
71 }\r
72 \r
73 \r
74 /**@brief Get directory entry length.\r
75  * @param de Directory entry\r
76  * @return Entry length\r
77  */\r
78 static inline uint16_t ext4_dir_entry_ll_get_entry_length(\r
79         struct ext4_directory_entry_ll *de)\r
80 {\r
81     return to_le16(de->entry_length);\r
82 }\r
83 \r
84 /**@brief Set directory entry length.\r
85  * @param de     Directory entry\r
86  * @param length Entry length\r
87  */\r
88 static inline void ext4_dir_entry_ll_set_entry_length(\r
89         struct ext4_directory_entry_ll *de, uint16_t len)\r
90 {\r
91     de->entry_length = to_le16(len);\r
92 }\r
93 \r
94 \r
95 /**@brief Get directory entry name length.\r
96  * @param sb Superblock\r
97  * @param de Directory entry\r
98  * @return Entry name length\r
99  */\r
100 static inline uint16_t ext4_dir_entry_ll_get_name_length(struct ext4_sblock *sb,\r
101     struct ext4_directory_entry_ll *de)\r
102 {\r
103     uint16_t v = de->name_length;\r
104 \r
105     if ((ext4_get32(sb, rev_level) == 0) &&\r
106             (ext4_get32(sb, minor_rev_level) < 5))\r
107         v |= ((uint16_t)de->in.name_length_high) << 8;\r
108 \r
109     return v;\r
110 }\r
111 \r
112 /**@brief Set directory entry name length.\r
113  * @param sb     Superblock\r
114  * @param de     Directory entry\r
115  * @param length Entry name length\r
116  */\r
117 static inline void ext4_dir_entry_ll_set_name_length(struct ext4_sblock *sb,\r
118     struct ext4_directory_entry_ll *de, uint16_t len)\r
119 {\r
120     de->name_length = (len << 8) >> 8;\r
121 \r
122     if ((ext4_get32(sb, rev_level) == 0) &&\r
123             (ext4_get32(sb, minor_rev_level) < 5))\r
124         de->in.name_length_high = len >> 8;\r
125 }\r
126 \r
127 \r
128 /**@brief Get i-node type of directory entry.\r
129  * @param sb Superblock\r
130  * @param de Directory entry\r
131  * @return I-node type (file, dir, etc.)\r
132  */\r
133 static inline uint8_t ext4_dir_entry_ll_get_inode_type(struct ext4_sblock *sb,\r
134     struct ext4_directory_entry_ll *de)\r
135 {\r
136     if ((ext4_get32(sb, rev_level) > 0) ||\r
137             (ext4_get32(sb, minor_rev_level) >= 5))\r
138         return de->in.inode_type;\r
139 \r
140     return EXT4_DIRECTORY_FILETYPE_UNKNOWN;\r
141 }\r
142 /**@brief Set i-node type of directory entry.\r
143  * @param sb   Superblock\r
144  * @param de   Directory entry\r
145  * @param type I-node type (file, dir, etc.)\r
146  */\r
147 \r
148 static inline void ext4_dir_entry_ll_set_inode_type(struct ext4_sblock *sb,\r
149     struct ext4_directory_entry_ll *de, uint8_t type)\r
150 {\r
151     if ((ext4_get32(sb, rev_level) > 0) ||\r
152             (ext4_get32(sb, minor_rev_level) >= 5))\r
153         de->in.inode_type = type;\r
154 }\r
155 \r
156 /**@brief Initialize directory iterator.\r
157  * Set position to the first valid entry from the required position.\r
158  * @param it        Pointer to iterator to be initialized\r
159  * @param inode_ref Directory i-node\r
160  * @param pos       Position to start reading entries from\r
161  * @return Error code\r
162  */\r
163 int ext4_dir_iterator_init(struct ext4_directory_iterator *it,\r
164     struct ext4_inode_ref *inode_ref, uint64_t pos);\r
165 \r
166 /**@brief Jump to the next valid entry\r
167  * @param it Initialized iterator\r
168  * @return Error code\r
169  */\r
170 int ext4_dir_iterator_next(struct ext4_directory_iterator *it);\r
171 \r
172 /**@brief Uninitialize directory iterator.\r
173  *        Release all allocated structures.\r
174  * @param it Iterator to be finished\r
175  * @return Error code\r
176  */\r
177 int ext4_dir_iterator_fini(struct ext4_directory_iterator *it);\r
178 \r
179 /**@brief Write directory entry to concrete data block.\r
180  * @param sb        Superblock\r
181  * @param entry     Pointer to entry to be written\r
182  * @param entry_len Length of new entry\r
183  * @param child     Child i-node to be written to new entry\r
184  * @param name      Name of the new entry\r
185  * @param name_len  Length of entry name\r
186  */\r
187 void ext4_dir_write_entry(struct ext4_sblock *sb,\r
188     struct ext4_directory_entry_ll *entry, uint16_t entry_len,\r
189     struct ext4_inode_ref *child,  const char *name, size_t name_len);\r
190 \r
191 /**@brief Add new entry to the directory.\r
192  * @param parent Directory i-node\r
193  * @param name   Name of new entry\r
194  * @param child  I-node to be referenced from new entry\r
195  * @return Error code\r
196  */\r
197 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,\r
198     uint32_t name_len, struct ext4_inode_ref *child);\r
199 \r
200 /**@brief Find directory entry with passed name.\r
201  * @param result Result structure to be returned if entry found\r
202  * @param parent Directory i-node\r
203  * @param name   Name of entry to be found\r
204  * @param name_len  Name length\r
205  * @return Error code\r
206  */\r
207 int ext4_dir_find_entry(struct ext4_directory_search_result *result,\r
208     struct ext4_inode_ref *parent, const char *name, uint32_t name_len);\r
209 \r
210 /**@brief Remove directory entry.\r
211  * @param parent Directory i-node\r
212  * @param name   Name of the entry to be removed\r
213  * @param name_len  Name length\r
214  * @return Error code\r
215  */\r
216 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,\r
217     uint32_t name_len);\r
218 \r
219 /**@brief Try to insert entry to concrete data block.\r
220  * @param sb           Superblock\r
221  * @param target_block Block to try to insert entry to\r
222  * @param child        Child i-node to be inserted by new entry\r
223  * @param name         Name of the new entry\r
224  * @param name_len     Length of the new entry name\r
225  * @return Error code\r
226  */\r
227 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,\r
228     struct ext4_block *target_block, struct ext4_inode_ref *child,\r
229     const char *name, uint32_t name_len);\r
230 \r
231 /**@brief Try to find entry in block by name.\r
232  * @param block     Block containing entries\r
233  * @param sb        Superblock\r
234  * @param name_len  Length of entry name\r
235  * @param name      Name of entry to be found\r
236  * @param res_entry Output pointer to found entry, NULL if not found\r
237  * @return Error code\r
238  */\r
239 int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,\r
240     size_t name_len, const char *name,\r
241     struct ext4_directory_entry_ll **res_entry);\r
242 \r
243 /**@brief Simple function to release allocated data from result.\r
244  * @param parent Parent inode\r
245  * @param result Search result to destroy\r
246  * @return Error code\r
247  *\r
248  */\r
249 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,\r
250     struct ext4_directory_search_result *result);\r
251 \r
252 #endif /* EXT4_DIR_H_ */\r
253 \r
254 /**\r
255  * @}\r
256  */\r
257 \r
258 \r