source: mainline/uspace/lib/ext2/libext2_directory.c@ 95afd72

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

Refactor checks in ext2_directory_iterator_next into a separate function

  • Property mode set to 100644
File size: 6.3 KB
Line 
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>
39#include <errno.h>
40#include <assert.h>
41
42static int ext2_directory_iterator_set(ext2_directory_iterator_t *it,
43 uint32_t block_size);
44
45/**
46 * Get inode number for the directory entry
47 *
48 * @param de pointer to linked list directory entry
49 */
50uint32_t ext2_directory_entry_ll_get_inode(ext2_directory_entry_ll_t *de)
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 */
60uint16_t ext2_directory_entry_ll_get_entry_length(
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 */
71uint16_t ext2_directory_entry_ll_get_name_length(
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
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
88 * @return EOK on success or negative error code on failure
89 */
90int ext2_directory_iterator_init(ext2_directory_iterator_t *it,
91 ext2_filesystem_t *fs, ext2_inode_ref_t *inode_ref)
92{
93 int rc;
94 uint32_t block_id;
95 it->inode_ref = inode_ref;
96 it->fs = fs;
97
98 /* Get the first data block, so we can get the first entry */
99 rc = ext2_filesystem_get_inode_data_block_index(fs, inode_ref->inode, 0,
100 &block_id);
101 if (rc != EOK) {
102 return rc;
103 }
104
105 rc = block_get(&it->current_block, fs->device, block_id, 0);
106 if (rc != EOK) {
107 return rc;
108 }
109
110 it->current = it->current_block->data;
111 it->current_offset = 0;
112
113 return EOK;
114}
115
116/**
117 * Advance the directory iterator to the next entry
118 *
119 * @param it pointer to iterator to initialize
120 * @return EOK on success or negative error code on failure
121 */
122int ext2_directory_iterator_next(ext2_directory_iterator_t *it)
123{
124 int rc;
125 uint16_t skip;
126 uint64_t size;
127 aoff64_t current_block_idx;
128 aoff64_t next_block_idx;
129 uint32_t next_block_phys_idx;
130 uint32_t block_size;
131
132 assert(it->current != NULL);
133
134 skip = ext2_directory_entry_ll_get_entry_length(it->current);
135 size = ext2_inode_get_size(it->fs->superblock, it->inode_ref->inode);
136
137 /* Are we at the end? */
138 if (it->current_offset + skip >= size) {
139 rc = block_put(it->current_block);
140 it->current_block = NULL;
141 it->current = NULL;
142 if (rc != EOK) {
143 return rc;
144 }
145
146 it->current_offset += skip;
147 return EOK;
148 }
149
150 block_size = ext2_superblock_get_block_size(it->fs->superblock);
151 current_block_idx = it->current_offset / block_size;
152 next_block_idx = (it->current_offset + skip) / block_size;
153
154 /* If we are moving accross block boundary,
155 * we need to get another block
156 */
157 if (current_block_idx != next_block_idx) {
158 rc = block_put(it->current_block);
159 it->current_block = NULL;
160 it->current = NULL;
161 if (rc != EOK) {
162 return rc;
163 }
164
165 rc = ext2_filesystem_get_inode_data_block_index(it->fs,
166 it->inode_ref->inode, next_block_idx, &next_block_phys_idx);
167 if (rc != EOK) {
168 return rc;
169 }
170
171 rc = block_get(&it->current_block, it->fs->device, next_block_phys_idx,
172 BLOCK_FLAGS_NONE);
173 if (rc != EOK) {
174 it->current_block = NULL;
175 return rc;
176 }
177 }
178
179 it->current_offset += skip;
180
181 return ext2_directory_iterator_set(it, block_size);
182}
183
184static int ext2_directory_iterator_set(ext2_directory_iterator_t *it,
185 uint32_t block_size)
186{
187 uint32_t offset_in_block = it->current_offset % block_size;
188
189 it->current = NULL;
190
191 /* Ensure proper alignment */
192 if ((offset_in_block % 4) != 0) {
193 return EIO;
194 }
195
196 /* Ensure that the core of the entry does not overflow the block */
197 if (offset_in_block > block_size - 8) {
198 return EIO;
199 }
200
201 ext2_directory_entry_ll_t *entry = it->current_block->data + offset_in_block;
202
203 /* Ensure that the whole entry does not overflow the block */
204 uint16_t length = ext2_directory_entry_ll_get_entry_length(entry);
205 if (offset_in_block + length > block_size) {
206 return EIO;
207 }
208
209 /* Ensure the name length is not too large */
210 if (ext2_directory_entry_ll_get_name_length(it->fs->superblock,
211 entry) > length-8) {
212 return EIO;
213 }
214
215 it->current = entry;
216 return EOK;
217}
218
219/**
220 * Release all resources asociated with the directory iterator
221 *
222 * @param it pointer to iterator to initialize
223 * @return EOK on success or negative error code on failure
224 */
225int ext2_directory_iterator_fini(ext2_directory_iterator_t *it)
226{
227 int rc;
228
229 it->fs = NULL;
230 it->inode_ref = NULL;
231 it->current = NULL;
232
233 if (it->current_block) {
234 rc = block_put(it->current_block);
235 if (rc != EOK) {
236 return rc;
237 }
238 }
239
240 return EOK;
241}
242
243/** @}
244 */
Note: See TracBrowser for help on using the repository browser.