[ce13577] | 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_inode.h"
|
---|
[b65cae22] | 38 | #include "libext2_superblock.h"
|
---|
[ce13577] | 39 | #include <byteorder.h>
|
---|
[ad34feb] | 40 | #include <assert.h>
|
---|
[ce13577] | 41 |
|
---|
[5352d72] | 42 | /**
|
---|
| 43 | * Get mode stored in the inode
|
---|
| 44 | *
|
---|
| 45 | * @param inode pointer to inode
|
---|
| 46 | */
|
---|
[18626b3] | 47 | uint32_t ext2_inode_get_mode(ext2_superblock_t *sb, ext2_inode_t *inode)
|
---|
[5352d72] | 48 | {
|
---|
[b65cae22] | 49 | if (ext2_superblock_get_os(sb) == EXT2_SUPERBLOCK_OS_HURD) {
|
---|
| 50 | return ((uint32_t)uint16_t_le2host(inode->mode_high)) << 16 |
|
---|
| 51 | ((uint32_t)uint16_t_le2host(inode->mode));
|
---|
| 52 | }
|
---|
[5352d72] | 53 | return uint16_t_le2host(inode->mode);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[a54af66] | 56 | /**
|
---|
| 57 | * Check whether inode is of given type
|
---|
| 58 | *
|
---|
| 59 | * @param sb pointer to superblock structure
|
---|
| 60 | * @param inode pointer to inode
|
---|
| 61 | * @param type EXT2_INODE_MODE_TYPE_* constant to check
|
---|
| 62 | */
|
---|
[18626b3] | 63 | bool ext2_inode_is_type(ext2_superblock_t *sb, ext2_inode_t *inode, uint32_t type)
|
---|
[a54af66] | 64 | {
|
---|
| 65 | uint32_t mode = ext2_inode_get_mode(sb, inode);
|
---|
| 66 | return (mode & EXT2_INODE_MODE_TYPE_MASK) == type;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[5352d72] | 69 | /**
|
---|
| 70 | * Get uid this inode is belonging to
|
---|
| 71 | *
|
---|
| 72 | * @param inode pointer to inode
|
---|
| 73 | */
|
---|
[18626b3] | 74 | uint32_t ext2_inode_get_user_id(ext2_superblock_t *sb, ext2_inode_t *inode)
|
---|
[5352d72] | 75 | {
|
---|
[b65cae22] | 76 | uint32_t os = ext2_superblock_get_os(sb);
|
---|
| 77 | if (os == EXT2_SUPERBLOCK_OS_LINUX || os == EXT2_SUPERBLOCK_OS_HURD) {
|
---|
| 78 | return ((uint32_t)uint16_t_le2host(inode->user_id_high)) << 16 |
|
---|
| 79 | ((uint32_t)uint16_t_le2host(inode->user_id));
|
---|
| 80 | }
|
---|
[5352d72] | 81 | return uint16_t_le2host(inode->user_id);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * Get size of file
|
---|
| 86 | *
|
---|
[b65cae22] | 87 | * For regular files in revision 1 and later, the high 32 bits of
|
---|
| 88 | * file size are stored in inode->size_high and are 0 otherwise
|
---|
| 89 | *
|
---|
[5352d72] | 90 | * @param inode pointer to inode
|
---|
| 91 | */
|
---|
[18626b3] | 92 | uint64_t ext2_inode_get_size(ext2_superblock_t *sb, ext2_inode_t *inode)
|
---|
[5352d72] | 93 | {
|
---|
[b65cae22] | 94 | uint32_t major_rev = ext2_superblock_get_rev_major(sb);
|
---|
| 95 |
|
---|
[a54af66] | 96 | if (major_rev > 0 && ext2_inode_is_type(sb, inode, EXT2_INODE_MODE_FILE)) {
|
---|
[b65cae22] | 97 | return ((uint64_t)uint32_t_le2host(inode->size_high)) << 32 |
|
---|
| 98 | ((uint64_t)uint32_t_le2host(inode->size));
|
---|
| 99 | }
|
---|
| 100 | return uint32_t_le2host(inode->size);
|
---|
[5352d72] | 101 | }
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * Get gid this inode belongs to
|
---|
| 105 | *
|
---|
[b65cae22] | 106 | * For Linux and Hurd, the high 16 bits are stored in OS dependent part
|
---|
| 107 | * of inode structure
|
---|
| 108 | *
|
---|
[5352d72] | 109 | * @param inode pointer to inode
|
---|
| 110 | */
|
---|
[18626b3] | 111 | uint32_t ext2_inode_get_group_id(ext2_superblock_t *sb, ext2_inode_t *inode)
|
---|
[5352d72] | 112 | {
|
---|
[b65cae22] | 113 | uint32_t os = ext2_superblock_get_os(sb);
|
---|
| 114 | if (os == EXT2_SUPERBLOCK_OS_LINUX || os == EXT2_SUPERBLOCK_OS_HURD) {
|
---|
| 115 | return ((uint32_t)uint16_t_le2host(inode->group_id_high)) << 16 |
|
---|
| 116 | ((uint32_t)uint16_t_le2host(inode->group_id));
|
---|
| 117 | }
|
---|
[5352d72] | 118 | return uint16_t_le2host(inode->group_id);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /**
|
---|
| 122 | * Get usage count (i.e. hard link count)
|
---|
| 123 | * A value of 1 is common, while 0 means that the inode should be freed
|
---|
| 124 | *
|
---|
| 125 | * @param inode pointer to inode
|
---|
| 126 | */
|
---|
[18626b3] | 127 | uint16_t ext2_inode_get_usage_count(ext2_inode_t *inode)
|
---|
[5352d72] | 128 | {
|
---|
| 129 | return uint16_t_le2host(inode->usage_count);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
[b65cae22] | 133 | * Get number of 512-byte data blocks allocated for contents of the file
|
---|
| 134 | * represented by this inode.
|
---|
| 135 | * This should be multiple of block size unless fragments are used.
|
---|
[5352d72] | 136 | *
|
---|
| 137 | * @param inode pointer to inode
|
---|
| 138 | */
|
---|
[18626b3] | 139 | uint32_t ext2_inode_get_reserved_512_blocks(ext2_inode_t *inode)
|
---|
[5352d72] | 140 | {
|
---|
| 141 | return uint32_t_le2host(inode->reserved_512_blocks);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[ad34feb] | 144 | /**
|
---|
| 145 | * Get number of blocks allocated for contents of the file
|
---|
| 146 | * represented by this inode.
|
---|
| 147 | *
|
---|
| 148 | * @param inode pointer to inode
|
---|
| 149 | */
|
---|
[18626b3] | 150 | uint32_t ext2_inode_get_reserved_blocks(ext2_superblock_t *sb,
|
---|
[ad34feb] | 151 | ext2_inode_t *inode)
|
---|
| 152 | {
|
---|
| 153 | return ext2_inode_get_reserved_512_blocks(inode) /
|
---|
| 154 | (ext2_superblock_get_block_size(sb) / 512);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[5352d72] | 157 | /**
|
---|
| 158 | * Get inode flags
|
---|
| 159 | *
|
---|
| 160 | * @param inode pointer to inode
|
---|
| 161 | */
|
---|
[18626b3] | 162 | uint32_t ext2_inode_get_flags(ext2_inode_t *inode) {
|
---|
[5352d72] | 163 | return uint32_t_le2host(inode->flags);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /**
|
---|
| 167 | * Get direct block ID
|
---|
| 168 | *
|
---|
| 169 | * @param inode pointer to inode
|
---|
| 170 | * @param idx Index to block. Valid values are 0 <= idx < 12
|
---|
| 171 | */
|
---|
[18626b3] | 172 | uint32_t ext2_inode_get_direct_block(ext2_inode_t *inode, uint8_t idx)
|
---|
[5352d72] | 173 | {
|
---|
[ad34feb] | 174 | assert(idx < EXT2_INODE_DIRECT_BLOCKS);
|
---|
[5352d72] | 175 | return uint32_t_le2host(inode->direct_blocks[idx]);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /**
|
---|
| 179 | * Get indirect block ID
|
---|
| 180 | *
|
---|
| 181 | * @param inode pointer to inode
|
---|
[ad34feb] | 182 | * @param idx Indirection level. Valid values are 0 <= idx < 3, where 0 is
|
---|
| 183 | * singly-indirect block and 2 is triply-indirect-block
|
---|
[5352d72] | 184 | */
|
---|
[18626b3] | 185 | uint32_t ext2_inode_get_indirect_block(ext2_inode_t *inode, uint8_t idx)
|
---|
[5352d72] | 186 | {
|
---|
[ad34feb] | 187 | assert(idx < 3);
|
---|
| 188 | return uint32_t_le2host(inode->indirect_blocks[idx]);
|
---|
[5352d72] | 189 | }
|
---|
[ce13577] | 190 |
|
---|
| 191 | /** @}
|
---|
| 192 | */
|
---|