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