source: mainline/uspace/lib/ext2/libext2_directory.c@ 4e36219

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4e36219 was 4e36219, checked in by Martin Sucha <sucha14@…>, 14 years ago

Allow libext2 directory iterator to seek to arbitrary position

  • Property mode set to 100644
File size: 6.8 KB
RevLine 
[102d400]1/*
2 * Copyright (c) 2011 Martin Sucha
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libext2
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include "libext2.h"
37#include "libext2_directory.h"
38#include <byteorder.h>
[a8e1aae]39#include <errno.h>
40#include <assert.h>
[102d400]41
[95afd72]42static int ext2_directory_iterator_set(ext2_directory_iterator_t *it,
43 uint32_t block_size);
44
[102d400]45/**
46 * Get inode number for the directory entry
47 *
48 * @param de pointer to linked list directory entry
49 */
[18626b3]50uint32_t ext2_directory_entry_ll_get_inode(ext2_directory_entry_ll_t *de)
[102d400]51{
52 return uint32_t_le2host(de->inode);
53}
54
55/**
56 * Get length of the directory entry
57 *
58 * @param de pointer to linked list directory entry
59 */
[18626b3]60uint16_t ext2_directory_entry_ll_get_entry_length(
[102d400]61 ext2_directory_entry_ll_t *de)
62{
63 return uint16_t_le2host(de->entry_length);
64}
65
66/**
67 * Get length of the name stored in the directory entry
68 *
69 * @param de pointer to linked list directory entry
70 */
[18626b3]71uint16_t ext2_directory_entry_ll_get_name_length(
[102d400]72 ext2_superblock_t *sb, ext2_directory_entry_ll_t *de)
73{
74 if (ext2_superblock_get_rev_major(sb) == 0 &&
75 ext2_superblock_get_rev_minor(sb) < 5) {
76 return ((uint16_t)de->name_length_high) << 8 |
77 ((uint16_t)de->name_length);
78 }
79 return de->name_length;
80}
81
[a8e1aae]82/**
83 * Initialize a directory iterator
84 *
85 * @param it pointer to iterator to initialize
86 * @param fs pointer to filesystem structure
87 * @param inode pointer to inode reference structure
[4e36219]88 * @param pos position within inode to start at, 0 is the first entry
[a8e1aae]89 * @return EOK on success or negative error code on failure
90 */
91int ext2_directory_iterator_init(ext2_directory_iterator_t *it,
[4e36219]92 ext2_filesystem_t *fs, ext2_inode_ref_t *inode_ref, aoff64_t pos)
93{
[a8e1aae]94 it->inode_ref = inode_ref;
95 it->fs = fs;
[4e36219]96 it->current = NULL;
97 it->current_offset = 0;
98 it->current_block = NULL;
[a8e1aae]99
[4e36219]100 return ext2_directory_iterator_seek(it, pos);
101}
102
103/**
104 * Advance the directory iterator to the next entry
105 *
106 * @param it pointer to iterator to initialize
107 * @return EOK on success or negative error code on failure
108 */
109int ext2_directory_iterator_next(ext2_directory_iterator_t *it)
110{
111 uint16_t skip;
[a8e1aae]112
[4e36219]113 assert(it->current != NULL);
[a8e1aae]114
[4e36219]115 skip = ext2_directory_entry_ll_get_entry_length(it->current);
[a8e1aae]116
[4e36219]117 return ext2_directory_iterator_seek(it, it->current_offset + skip);
[a8e1aae]118}
119
120/**
[4e36219]121 * Seek the directory iterator to the given byte offset within the inode.
[a8e1aae]122 *
123 * @param it pointer to iterator to initialize
124 * @return EOK on success or negative error code on failure
125 */
[4e36219]126int ext2_directory_iterator_seek(ext2_directory_iterator_t *it, aoff64_t pos)
[a8e1aae]127{
128 int rc;
[4e36219]129
[a8e1aae]130 uint64_t size;
131 aoff64_t current_block_idx;
132 aoff64_t next_block_idx;
133 uint32_t next_block_phys_idx;
134 uint32_t block_size;
135
136 size = ext2_inode_get_size(it->fs->superblock, it->inode_ref->inode);
137
[4e36219]138 /* The iterator is not valid until we seek to the desired position */
139 it->current = NULL;
140
[260c052]141 /* Are we at the end? */
[4e36219]142 if (pos >= size) {
143 if (it->current_block) {
144 rc = block_put(it->current_block);
145 it->current_block = NULL;
146 if (rc != EOK) {
147 return rc;
148 }
[a8e1aae]149 }
150
[4e36219]151 it->current_offset = pos;
[a8e1aae]152 return EOK;
153 }
154
155 block_size = ext2_superblock_get_block_size(it->fs->superblock);
156 current_block_idx = it->current_offset / block_size;
[4e36219]157 next_block_idx = pos / block_size;
[a8e1aae]158
[4e36219]159 /* If we don't have a block or are moving accross block boundary,
[260c052]160 * we need to get another block
161 */
[4e36219]162 if (it->current_block == NULL || current_block_idx != next_block_idx) {
163 if (it->current_block) {
164 rc = block_put(it->current_block);
165 it->current_block = NULL;
166 if (rc != EOK) {
167 return rc;
168 }
[a8e1aae]169 }
170
171 rc = ext2_filesystem_get_inode_data_block_index(it->fs,
172 it->inode_ref->inode, next_block_idx, &next_block_phys_idx);
173 if (rc != EOK) {
174 return rc;
175 }
176
177 rc = block_get(&it->current_block, it->fs->device, next_block_phys_idx,
178 BLOCK_FLAGS_NONE);
179 if (rc != EOK) {
180 it->current_block = NULL;
181 return rc;
182 }
183 }
184
[4e36219]185 it->current_offset = pos;
[95afd72]186 return ext2_directory_iterator_set(it, block_size);
187}
188
[4e36219]189/** Setup the entry at the current iterator offset.
190 *
191 * This function checks the validity of the directory entry,
192 * before setting the data pointer.
193 *
194 * @return EOK on success or error code on failure
195 */
[95afd72]196static int ext2_directory_iterator_set(ext2_directory_iterator_t *it,
197 uint32_t block_size)
198{
199 uint32_t offset_in_block = it->current_offset % block_size;
200
201 it->current = NULL;
[a8e1aae]202
[260c052]203 /* Ensure proper alignment */
[a8e1aae]204 if ((offset_in_block % 4) != 0) {
205 return EIO;
206 }
207
[260c052]208 /* Ensure that the core of the entry does not overflow the block */
[a8e1aae]209 if (offset_in_block > block_size - 8) {
210 return EIO;
211 }
[95afd72]212
213 ext2_directory_entry_ll_t *entry = it->current_block->data + offset_in_block;
[a8e1aae]214
[260c052]215 /* Ensure that the whole entry does not overflow the block */
[95afd72]216 uint16_t length = ext2_directory_entry_ll_get_entry_length(entry);
217 if (offset_in_block + length > block_size) {
[a8e1aae]218 return EIO;
219 }
220
[260c052]221 /* Ensure the name length is not too large */
[a8e1aae]222 if (ext2_directory_entry_ll_get_name_length(it->fs->superblock,
[95afd72]223 entry) > length-8) {
[a8e1aae]224 return EIO;
225 }
226
[95afd72]227 it->current = entry;
[a8e1aae]228 return EOK;
229}
230
231/**
232 * Release all resources asociated with the directory iterator
233 *
234 * @param it pointer to iterator to initialize
235 * @return EOK on success or negative error code on failure
236 */
237int ext2_directory_iterator_fini(ext2_directory_iterator_t *it)
238{
239 int rc;
240
241 it->fs = NULL;
242 it->inode_ref = NULL;
243 it->current = NULL;
244
245 if (it->current_block) {
246 rc = block_put(it->current_block);
247 if (rc != EOK) {
248 return rc;
249 }
250 }
251
252 return EOK;
253}
254
[102d400]255/** @}
256 */
Note: See TracBrowser for help on using the repository browser.