2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
6 * Copyright (c) 2012 Martin Sucha
7 * Copyright (c) 2012 Frantisek Princ
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
34 /** @addtogroup lwext4
39 * @brief Directory handle procedures.
45 #include "ext4_config.h"
46 #include "ext4_types.h"
47 #include "ext4_blockdev.h"
48 #include "ext4_super.h"
52 /**@brief Get i-node number from directory entry.
53 * @param de Directory entry
54 * @return I-node number
56 static inline uint32_t
57 ext4_dir_entry_ll_get_inode(struct ext4_directory_entry_ll *de)
59 return to_le32(de->inode);
62 /**@brief Set i-node number to directory entry.
63 * @param de Directory entry
64 * @param inode I-node number
67 ext4_dir_entry_ll_set_inode(struct ext4_directory_entry_ll *de, uint32_t inode)
69 de->inode = to_le32(inode);
72 /**@brief Get directory entry length.
73 * @param de Directory entry
74 * @return Entry length
76 static inline uint16_t
77 ext4_dir_entry_ll_get_entry_length(struct ext4_directory_entry_ll *de)
79 return to_le16(de->entry_length);
82 /**@brief Set directory entry length.
83 * @param de Directory entry
84 * @param length Entry length
87 ext4_dir_entry_ll_set_entry_length(struct ext4_directory_entry_ll *de,
90 de->entry_length = to_le16(len);
93 /**@brief Get directory entry name length.
94 * @param sb Superblock
95 * @param de Directory entry
96 * @return Entry name length
98 static inline uint16_t
99 ext4_dir_entry_ll_get_name_length(struct ext4_sblock *sb,
100 struct ext4_directory_entry_ll *de)
102 uint16_t v = de->name_length;
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;
111 /**@brief Set directory entry name length.
112 * @param sb Superblock
113 * @param de Directory entry
114 * @param length Entry name length
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)
119 de->name_length = (len << 8) >> 8;
121 if ((ext4_get32(sb, rev_level) == 0) &&
122 (ext4_get32(sb, minor_rev_level) < 5))
123 de->in.name_length_high = len >> 8;
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.)
131 static inline uint8_t
132 ext4_dir_entry_ll_get_inode_type(struct ext4_sblock *sb,
133 struct ext4_directory_entry_ll *de)
135 if ((ext4_get32(sb, rev_level) > 0) ||
136 (ext4_get32(sb, minor_rev_level) >= 5))
137 return de->in.inode_type;
139 return EXT4_DIRECTORY_FILETYPE_UNKNOWN;
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.)
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)
150 if ((ext4_get32(sb, rev_level) > 0) ||
151 (ext4_get32(sb, minor_rev_level) >= 5))
152 de->in.inode_type = type;
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
162 int ext4_dir_iterator_init(struct ext4_directory_iterator *it,
163 struct ext4_inode_ref *inode_ref, uint64_t pos);
165 /**@brief Jump to the next valid entry
166 * @param it Initialized iterator
169 int ext4_dir_iterator_next(struct ext4_directory_iterator *it);
171 /**@brief Uninitialize directory iterator.
172 * Release all allocated structures.
173 * @param it Iterator to be finished
176 int ext4_dir_iterator_fini(struct ext4_directory_iterator *it);
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
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);
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
197 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
198 uint32_t name_len, struct ext4_inode_ref *child);
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
207 int ext4_dir_find_entry(struct ext4_directory_search_result *result,
208 struct ext4_inode_ref *parent, const char *name,
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
217 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
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
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,
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
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);
245 /**@brief Simple function to release allocated data from result.
246 * @param parent Parent inode
247 * @param result Search result to destroy
251 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
252 struct ext4_directory_search_result *result);
254 #endif /* EXT4_DIR_H_ */