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