clang-format: lwext4 modules
[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\r
57 ext4_dir_entry_ll_get_inode(struct ext4_directory_entry_ll *de)\r
58 {\r
59     return to_le32(de->inode);\r
60 }\r
61 \r
62 /**@brief Set i-node number to directory entry.\r
63  * @param de Directory entry\r
64  * @param inode I-node number\r
65  */\r
66 static inline void\r
67 ext4_dir_entry_ll_set_inode(struct ext4_directory_entry_ll *de, uint32_t inode)\r
68 {\r
69     de->inode = to_le32(inode);\r
70 }\r
71 \r
72 /**@brief Get directory entry length.\r
73  * @param de Directory entry\r
74  * @return Entry length\r
75  */\r
76 static inline uint16_t\r
77 ext4_dir_entry_ll_get_entry_length(struct ext4_directory_entry_ll *de)\r
78 {\r
79     return to_le16(de->entry_length);\r
80 }\r
81 \r
82 /**@brief Set directory entry length.\r
83  * @param de     Directory entry\r
84  * @param length Entry length\r
85  */\r
86 static inline void\r
87 ext4_dir_entry_ll_set_entry_length(struct ext4_directory_entry_ll *de,\r
88                                    uint16_t len)\r
89 {\r
90     de->entry_length = to_le16(len);\r
91 }\r
92 \r
93 /**@brief Get directory entry name length.\r
94  * @param sb Superblock\r
95  * @param de Directory entry\r
96  * @return Entry name length\r
97  */\r
98 static inline uint16_t\r
99 ext4_dir_entry_ll_get_name_length(struct ext4_sblock *sb,\r
100                                   struct ext4_directory_entry_ll *de)\r
101 {\r
102     uint16_t v = de->name_length;\r
103 \r
104     if ((ext4_get32(sb, rev_level) == 0) &&\r
105         (ext4_get32(sb, minor_rev_level) < 5))\r
106         v |= ((uint16_t)de->in.name_length_high) << 8;\r
107 \r
108     return v;\r
109 }\r
110 \r
111 /**@brief Set directory entry name length.\r
112  * @param sb     Superblock\r
113  * @param de     Directory entry\r
114  * @param length Entry name length\r
115  */\r
116 static inline void ext4_dir_entry_ll_set_name_length(\r
117     struct ext4_sblock *sb, struct ext4_directory_entry_ll *de, uint16_t len)\r
118 {\r
119     de->name_length = (len << 8) >> 8;\r
120 \r
121     if ((ext4_get32(sb, rev_level) == 0) &&\r
122         (ext4_get32(sb, minor_rev_level) < 5))\r
123         de->in.name_length_high = len >> 8;\r
124 }\r
125 \r
126 /**@brief Get i-node type of directory entry.\r
127  * @param sb Superblock\r
128  * @param de Directory entry\r
129  * @return I-node type (file, dir, etc.)\r
130  */\r
131 static inline uint8_t\r
132 ext4_dir_entry_ll_get_inode_type(struct ext4_sblock *sb,\r
133                                  struct ext4_directory_entry_ll *de)\r
134 {\r
135     if ((ext4_get32(sb, rev_level) > 0) ||\r
136         (ext4_get32(sb, minor_rev_level) >= 5))\r
137         return de->in.inode_type;\r
138 \r
139     return EXT4_DIRECTORY_FILETYPE_UNKNOWN;\r
140 }\r
141 /**@brief Set i-node type of directory entry.\r
142  * @param sb   Superblock\r
143  * @param de   Directory entry\r
144  * @param type I-node type (file, dir, etc.)\r
145  */\r
146 \r
147 static inline void ext4_dir_entry_ll_set_inode_type(\r
148     struct ext4_sblock *sb, struct ext4_directory_entry_ll *de, uint8_t type)\r
149 {\r
150     if ((ext4_get32(sb, rev_level) > 0) ||\r
151         (ext4_get32(sb, minor_rev_level) >= 5))\r
152         de->in.inode_type = type;\r
153 }\r
154 \r
155 /**@brief Initialize directory iterator.\r
156  * Set position to the first valid entry from the required position.\r
157  * @param it        Pointer to iterator to be initialized\r
158  * @param inode_ref Directory i-node\r
159  * @param pos       Position to start reading entries from\r
160  * @return Error code\r
161  */\r
162 int ext4_dir_iterator_init(struct ext4_directory_iterator *it,\r
163                            struct ext4_inode_ref *inode_ref, uint64_t pos);\r
164 \r
165 /**@brief Jump to the next valid entry\r
166  * @param it Initialized iterator\r
167  * @return Error code\r
168  */\r
169 int ext4_dir_iterator_next(struct ext4_directory_iterator *it);\r
170 \r
171 /**@brief Uninitialize directory iterator.\r
172  *        Release all allocated structures.\r
173  * @param it Iterator to be finished\r
174  * @return Error code\r
175  */\r
176 int ext4_dir_iterator_fini(struct ext4_directory_iterator *it);\r
177 \r
178 /**@brief Write directory entry to concrete data block.\r
179  * @param sb        Superblock\r
180  * @param entry     Pointer to entry to be written\r
181  * @param entry_len Length of new entry\r
182  * @param child     Child i-node to be written to new entry\r
183  * @param name      Name of the new entry\r
184  * @param name_len  Length of entry name\r
185  */\r
186 void ext4_dir_write_entry(struct ext4_sblock *sb,\r
187                           struct ext4_directory_entry_ll *entry,\r
188                           uint16_t entry_len, struct ext4_inode_ref *child,\r
189                           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,\r
209                         uint32_t name_len);\r
210 \r
211 /**@brief Remove directory entry.\r
212  * @param parent Directory i-node\r
213  * @param name   Name of the entry to be removed\r
214  * @param name_len  Name length\r
215  * @return Error code\r
216  */\r
217 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,\r
218                           uint32_t name_len);\r
219 \r
220 /**@brief Try to insert entry to concrete data block.\r
221  * @param sb           Superblock\r
222  * @param target_block Block to try to insert entry to\r
223  * @param child        Child i-node to be inserted by new entry\r
224  * @param name         Name of the new entry\r
225  * @param name_len     Length of the new entry name\r
226  * @return Error code\r
227  */\r
228 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,\r
229                               struct ext4_block *target_block,\r
230                               struct ext4_inode_ref *child, const char *name,\r
231                               uint32_t name_len);\r
232 \r
233 /**@brief Try to find entry in block by name.\r
234  * @param block     Block containing entries\r
235  * @param sb        Superblock\r
236  * @param name_len  Length of entry name\r
237  * @param name      Name of entry to be found\r
238  * @param res_entry Output pointer to found entry, NULL if not found\r
239  * @return Error code\r
240  */\r
241 int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,\r
242                            size_t name_len, const char *name,\r
243                            struct ext4_directory_entry_ll **res_entry);\r
244 \r
245 /**@brief Simple function to release allocated data from result.\r
246  * @param parent Parent inode\r
247  * @param result Search result to destroy\r
248  * @return Error code\r
249  *\r
250  */\r
251 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,\r
252                             struct ext4_directory_search_result *result);\r
253 \r
254 #endif /* EXT4_DIR_H_ */\r
255 \r
256 /**\r
257  * @}\r
258  */\r