| 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 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Get inode number for the directory entry
|
|---|
| 44 | *
|
|---|
| 45 | * @param de pointer to linked list directory entry
|
|---|
| 46 | */
|
|---|
| 47 | inline uint32_t ext2_directory_entry_ll_get_inode(ext2_directory_entry_ll_t *de)
|
|---|
| 48 | {
|
|---|
| 49 | return uint32_t_le2host(de->inode);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Get length of the directory entry
|
|---|
| 54 | *
|
|---|
| 55 | * @param de pointer to linked list directory entry
|
|---|
| 56 | */
|
|---|
| 57 | inline uint16_t ext2_directory_entry_ll_get_entry_length(
|
|---|
| 58 | ext2_directory_entry_ll_t *de)
|
|---|
| 59 | {
|
|---|
| 60 | return uint16_t_le2host(de->entry_length);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Get length of the name stored in the directory entry
|
|---|
| 65 | *
|
|---|
| 66 | * @param de pointer to linked list directory entry
|
|---|
| 67 | */
|
|---|
| 68 | inline uint16_t ext2_directory_entry_ll_get_name_length(
|
|---|
| 69 | ext2_superblock_t *sb, ext2_directory_entry_ll_t *de)
|
|---|
| 70 | {
|
|---|
| 71 | if (ext2_superblock_get_rev_major(sb) == 0 &&
|
|---|
| 72 | ext2_superblock_get_rev_minor(sb) < 5) {
|
|---|
| 73 | return ((uint16_t)de->name_length_high) << 8 |
|
|---|
| 74 | ((uint16_t)de->name_length);
|
|---|
| 75 | }
|
|---|
| 76 | return de->name_length;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Initialize a directory iterator
|
|---|
| 81 | *
|
|---|
| 82 | * @param it pointer to iterator to initialize
|
|---|
| 83 | * @param fs pointer to filesystem structure
|
|---|
| 84 | * @param inode pointer to inode reference structure
|
|---|
| 85 | * @return EOK on success or negative error code on failure
|
|---|
| 86 | */
|
|---|
| 87 | int ext2_directory_iterator_init(ext2_directory_iterator_t *it,
|
|---|
| 88 | ext2_filesystem_t *fs, ext2_inode_ref_t *inode_ref)
|
|---|
| 89 | {
|
|---|
| 90 | int rc;
|
|---|
| 91 | uint32_t block_id;
|
|---|
| 92 | it->inode_ref = inode_ref;
|
|---|
| 93 | it->fs = fs;
|
|---|
| 94 |
|
|---|
| 95 | // Get the first data block, so we can get first entry
|
|---|
| 96 | rc = ext2_filesystem_get_inode_data_block_index(fs, inode_ref->inode, 0,
|
|---|
| 97 | &block_id);
|
|---|
| 98 | if (rc != EOK) {
|
|---|
| 99 | return rc;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | rc = block_get(&it->current_block, fs->device, block_id, 0);
|
|---|
| 103 | if (rc != EOK) {
|
|---|
| 104 | return rc;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | it->current = it->current_block->data;
|
|---|
| 108 | it->current_offset = 0;
|
|---|
| 109 |
|
|---|
| 110 | return EOK;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /**
|
|---|
| 114 | * Advance the directory iterator to the next entry
|
|---|
| 115 | *
|
|---|
| 116 | * @param it pointer to iterator to initialize
|
|---|
| 117 | * @return EOK on success or negative error code on failure
|
|---|
| 118 | */
|
|---|
| 119 | int ext2_directory_iterator_next(ext2_directory_iterator_t *it)
|
|---|
| 120 | {
|
|---|
| 121 | int rc;
|
|---|
| 122 | uint16_t skip;
|
|---|
| 123 | uint64_t size;
|
|---|
| 124 | aoff64_t current_block_idx;
|
|---|
| 125 | aoff64_t next_block_idx;
|
|---|
| 126 | uint32_t next_block_phys_idx;
|
|---|
| 127 | uint32_t block_size;
|
|---|
| 128 | uint32_t offset_in_block;
|
|---|
| 129 |
|
|---|
| 130 | assert(it->current != NULL);
|
|---|
| 131 |
|
|---|
| 132 | skip = ext2_directory_entry_ll_get_entry_length(it->current);
|
|---|
| 133 | size = ext2_inode_get_size(it->fs->superblock, it->inode_ref->inode);
|
|---|
| 134 |
|
|---|
| 135 | // Are we at the end?
|
|---|
| 136 | if (it->current_offset + skip >= size) {
|
|---|
| 137 | rc = block_put(it->current_block);
|
|---|
| 138 | it->current_block = NULL;
|
|---|
| 139 | it->current = NULL;
|
|---|
| 140 | if (rc != EOK) {
|
|---|
| 141 | return rc;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | it->current_offset += skip;
|
|---|
| 145 | return EOK;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | block_size = ext2_superblock_get_block_size(it->fs->superblock);
|
|---|
| 149 | current_block_idx = it->current_offset / block_size;
|
|---|
| 150 | next_block_idx = (it->current_offset + skip) / block_size;
|
|---|
| 151 |
|
|---|
| 152 | // If we are moving accross block boundary,
|
|---|
| 153 | // we need to get another block
|
|---|
| 154 | if (current_block_idx != next_block_idx) {
|
|---|
| 155 | rc = block_put(it->current_block);
|
|---|
| 156 | it->current_block = NULL;
|
|---|
| 157 | it->current = NULL;
|
|---|
| 158 | if (rc != EOK) {
|
|---|
| 159 | return rc;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | rc = ext2_filesystem_get_inode_data_block_index(it->fs,
|
|---|
| 163 | it->inode_ref->inode, next_block_idx, &next_block_phys_idx);
|
|---|
| 164 | if (rc != EOK) {
|
|---|
| 165 | return rc;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | rc = block_get(&it->current_block, it->fs->device, next_block_phys_idx,
|
|---|
| 169 | BLOCK_FLAGS_NONE);
|
|---|
| 170 | if (rc != EOK) {
|
|---|
| 171 | it->current_block = NULL;
|
|---|
| 172 | return rc;
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | offset_in_block = (it->current_offset + skip) % block_size;
|
|---|
| 177 |
|
|---|
| 178 | // Ensure proper alignment
|
|---|
| 179 | if ((offset_in_block % 4) != 0) {
|
|---|
| 180 | it->current = NULL;
|
|---|
| 181 | return EIO;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | // Ensure that the core of the entry does not overflow the block
|
|---|
| 185 | if (offset_in_block > block_size - 8) {
|
|---|
| 186 | it->current = NULL;
|
|---|
| 187 | return EIO;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | it->current = it->current_block->data + offset_in_block;
|
|---|
| 191 | it->current_offset += skip;
|
|---|
| 192 |
|
|---|
| 193 | // Ensure that the whole entry does not overflow the block
|
|---|
| 194 | skip = ext2_directory_entry_ll_get_entry_length(it->current);
|
|---|
| 195 | if (offset_in_block + skip > block_size) {
|
|---|
| 196 | it->current = NULL;
|
|---|
| 197 | return EIO;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | // Ensure the name length is not too large
|
|---|
| 201 | if (ext2_directory_entry_ll_get_name_length(it->fs->superblock,
|
|---|
| 202 | it->current) > skip-8) {
|
|---|
| 203 | it->current = NULL;
|
|---|
| 204 | return EIO;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | return EOK;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /**
|
|---|
| 211 | * Release all resources asociated with the directory iterator
|
|---|
| 212 | *
|
|---|
| 213 | * @param it pointer to iterator to initialize
|
|---|
| 214 | * @return EOK on success or negative error code on failure
|
|---|
| 215 | */
|
|---|
| 216 | int ext2_directory_iterator_fini(ext2_directory_iterator_t *it)
|
|---|
| 217 | {
|
|---|
| 218 | int rc;
|
|---|
| 219 |
|
|---|
| 220 | it->fs = NULL;
|
|---|
| 221 | it->inode_ref = NULL;
|
|---|
| 222 | it->current = NULL;
|
|---|
| 223 |
|
|---|
| 224 | if (it->current_block) {
|
|---|
| 225 | rc = block_put(it->current_block);
|
|---|
| 226 | if (rc != EOK) {
|
|---|
| 227 | return rc;
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | return EOK;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /** @}
|
|---|
| 235 | */
|
|---|