Changeset 5352d72 in mainline


Ignore:
Timestamp:
2011-02-16T16:47:18Z (13 years ago)
Author:
Martin Sucha <sucha14@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a2a1792
Parents:
ce13577
Message:

Added support for reading and displaying inode contents to ext2info

Location:
uspace
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/ext2info/ext2info.c

    rce13577 r5352d72  
    5656static void print_block_groups(ext2_filesystem_t *);
    5757static void print_block_group(ext2_block_group_t *);
     58static void print_inode_by_number(ext2_filesystem_t *, uint32_t);
     59static void print_inode(ext2_inode_t *);
     60
     61#define ARG_SUPERBLOCK 1
     62#define ARG_BLOCK_GROUPS 2
     63#define ARG_INODE 4
     64#define ARG_STRICT_CHECK 8
     65#define ARG_COMMON (ARG_SUPERBLOCK | ARG_BLOCK_GROUPS)
     66#define ARG_ALL (ARG_COMMON | ARG_INODE)
     67
    5868
    5969int main(int argc, char **argv)
     
    6171
    6272        int rc;
     73        char *endptr;
    6374        char *dev_path;
    6475        devmap_handle_t handle;
    6576        ext2_filesystem_t filesystem;
    66         bool strict_check;
     77        int arg_flags;
     78        uint32_t inode = 0;
     79       
     80        arg_flags = 0;
    6781       
    6882        if (argc < 2) {
     
    7286        }
    7387       
    74         strict_check = false;
     88        // Skip program name
     89        --argc; ++argv;
     90       
    7591        if (str_cmp(*argv, "--strict-check") == 0) {
    7692                --argc; ++argv;
    77                 strict_check = true;
    78         }
    79 
    80         --argc; ++argv;
     93                arg_flags |= ARG_STRICT_CHECK;
     94        }
     95       
     96        if (str_cmp(*argv, "--superblock") == 0) {
     97                --argc; ++argv;
     98                arg_flags |= ARG_SUPERBLOCK;
     99        }
     100       
     101        if (str_cmp(*argv, "--block-groups") == 0) {
     102                --argc; ++argv;
     103                arg_flags |= ARG_BLOCK_GROUPS;
     104        }
     105       
     106        if (str_cmp(*argv, "--inode") == 0) {
     107                --argc; ++argv;
     108                if (argc == 0) {
     109                        printf(NAME ": Argument expected for --inode\n");
     110                        return 2;
     111                }
     112               
     113                inode = strtol(*argv, &endptr, 10);
     114                if (*endptr != '\0') {
     115                        printf(NAME ": Error, invalid argument for --inode.\n");
     116                        syntax_print();
     117                        return 1;
     118                }
     119               
     120                arg_flags |= ARG_INODE;
     121                --argc; ++argv;
     122        }
    81123
    82124        if (argc != 1) {
     
    85127                return 1;
    86128        }
     129       
     130        // Display common things by default
     131        if ((arg_flags & ARG_ALL) == 0) {
     132                arg_flags = ARG_COMMON;
     133        }
    87134
    88135        dev_path = *argv;
     
    103150        if (rc != EOK) {
    104151                printf(NAME ": Filesystem did not pass sanity check.\n");
    105                 if (strict_check) {
     152                if (arg_flags & ARG_STRICT_CHECK) {
    106153                        return 3;
    107154                }
    108155        }
    109156       
    110         print_superblock(filesystem.superblock);
    111         print_block_groups(&filesystem);
     157        if (arg_flags & ARG_SUPERBLOCK) {
     158                print_superblock(filesystem.superblock);
     159        }
     160       
     161        if (arg_flags & ARG_BLOCK_GROUPS) {
     162                print_block_groups(&filesystem);
     163        }
     164       
     165        if (arg_flags & ARG_INODE) {
     166                print_inode_by_number(&filesystem, inode);
     167        }
    112168
    113169        ext2_filesystem_fini(&filesystem);
     
    119175static void syntax_print(void)
    120176{
    121         printf("syntax: ext2info <device_name>\n");
     177        printf("syntax: ext2info --strict-check --superblock --block-groups --inode <i-number> <device_name>\n");
    122178}
    123179
     
    212268}
    213269
    214 void print_block_groups(ext2_filesystem_t *filesystem) {
     270void print_block_groups(ext2_filesystem_t *filesystem)
     271{
    215272        uint32_t block_group_count;
    216273        uint32_t i;
     
    241298}
    242299
    243 void print_block_group(ext2_block_group_t *bg) {
     300void print_block_group(ext2_block_group_t *bg)
     301{
    244302        uint32_t block_bitmap_block;
    245303        uint32_t inode_bitmap_block;
     
    264322}
    265323
     324void print_inode_by_number(ext2_filesystem_t *fs, uint32_t inode)
     325{
     326        int rc;
     327        ext2_inode_ref_t *inode_ref;
     328       
     329        printf("Inode %u\n", inode);
     330       
     331        rc = ext2_filesystem_get_inode_ref(fs, inode, &inode_ref);
     332        if (rc != EOK) {
     333                printf("  Failed getting inode ref\n");
     334                return;
     335        }
     336       
     337        print_inode(inode_ref->inode);
     338       
     339        rc = ext2_filesystem_put_inode_ref(inode_ref);
     340        if (rc != EOK) {
     341                printf("  Failed putting inode ref\n");
     342        }
     343}
     344
     345void print_inode(ext2_inode_t *inode)
     346{
     347        uint16_t mode;
     348        uint32_t user_id;
     349        uint32_t group_id;
     350        uint32_t size;
     351        uint16_t usage_count;
     352        uint32_t flags;
     353        uint16_t access;
     354        const char *type;
     355        uint32_t block;
     356        int i;
     357        bool all_blocks = false;
     358       
     359        mode = ext2_inode_get_mode(inode);
     360        user_id = ext2_inode_get_user_id(inode);
     361        group_id = ext2_inode_get_group_id(inode);
     362        size = ext2_inode_get_size(inode);
     363        usage_count = ext2_inode_get_usage_count(inode);
     364        flags = ext2_inode_get_flags(inode);
     365       
     366        type = "Unknown";
     367        if ((mode & EXT2_INODE_MODE_BLOCKDEV) == EXT2_INODE_MODE_BLOCKDEV) {
     368                type = "Block device";
     369        }
     370        else if (mode & EXT2_INODE_MODE_FIFO) {
     371                type = "Fifo (pipe)";
     372        }
     373        else if (mode & EXT2_INODE_MODE_CHARDEV) {
     374                type = "Character device";
     375        }
     376        else if (mode & EXT2_INODE_MODE_DIRECTORY) {
     377                type = "Directory";
     378        }
     379        else if (mode & EXT2_INODE_MODE_FILE) {
     380                type = "File";
     381        }
     382        else if (mode & EXT2_INODE_MODE_SOFTLINK) {
     383                type = "Soft link";
     384        }
     385        else if (mode & EXT2_INODE_MODE_SOCKET) {
     386                type = "Socket";
     387        }
     388       
     389        access = mode & EXT2_INODE_MODE_ACCESS_MASK;
     390       
     391        printf("  Mode: %04x (Type: %s, Access bits: %04ho)\n", mode, type, access);
     392        printf("  User ID: %u\n", user_id);
     393        printf("  Group ID: %u\n", group_id);
     394        printf("  Size: %u\n", size);
     395        printf("  Usage (link) count: %u\n", usage_count);
     396        printf("  Flags: %u\n", flags);
     397        printf("  Block list: ");
     398        for (i = 0; i < 12; i++) {
     399                block = ext2_inode_get_direct_block(inode, i);
     400                if (block == 0) {
     401                        all_blocks = true;
     402                        break;
     403                }
     404                printf("%u ", block);
     405        }
     406        if (!all_blocks) {
     407                printf(" and more...");
     408        }
     409        printf("\n");
     410}
     411
    266412/**
    267413 * @}
  • uspace/lib/ext2/libext2_filesystem.c

    rce13577 r5352d72  
    225225        }
    226226       
    227         newref->inode = newref->block->data + offset_in_group;
     227        newref->inode = newref->block->data + (offset_in_group*inode_size);
    228228       
    229229        *ref = newref;
  • uspace/lib/ext2/libext2_inode.c

    rce13577 r5352d72  
    3838#include <byteorder.h>
    3939
     40/**
     41 * Get mode stored in the inode
     42 *
     43 * @param inode pointer to inode
     44 */
     45inline uint16_t ext2_inode_get_mode(ext2_inode_t *inode)
     46{
     47        return uint16_t_le2host(inode->mode);
     48}
     49
     50/**
     51 * Get uid this inode is belonging to
     52 *
     53 * @param inode pointer to inode
     54 */
     55inline uint32_t ext2_inode_get_user_id(ext2_inode_t *inode)
     56{
     57        // TODO: use additional OS specific bits
     58        return uint16_t_le2host(inode->user_id);
     59}
     60
     61/**
     62 * Get size of file
     63 *
     64 * @param inode pointer to inode
     65 */
     66inline uint32_t ext2_inode_get_size(ext2_inode_t *inode)
     67{
     68        // TODO: Directory ACLS!
     69        return uint16_t_le2host(inode->size);
     70}
     71
     72/**
     73 * Get gid this inode belongs to
     74 *
     75 * @param inode pointer to inode
     76 */
     77inline uint32_t ext2_inode_get_group_id(ext2_inode_t *inode)
     78{
     79        // TODO: use additional OS specific bits
     80        return uint16_t_le2host(inode->group_id);
     81}
     82
     83/**
     84 * Get usage count (i.e. hard link count)
     85 * A value of 1 is common, while 0 means that the inode should be freed
     86 *
     87 * @param inode pointer to inode
     88 */
     89inline uint16_t ext2_inode_get_usage_count(ext2_inode_t *inode)
     90{
     91        return uint16_t_le2host(inode->usage_count);
     92}
     93
     94/**
     95 * Get size of inode in 512 byte blocks
     96 * TODO: clarify this
     97 *
     98 * @param inode pointer to inode
     99 */
     100inline uint32_t ext2_inode_get_reserved_512_blocks(ext2_inode_t *inode)
     101{
     102        return uint32_t_le2host(inode->reserved_512_blocks);
     103}
     104
     105/**
     106 * Get inode flags
     107 *
     108 * @param inode pointer to inode
     109 */
     110
     111inline uint32_t ext2_inode_get_flags(ext2_inode_t *inode) {
     112        return uint32_t_le2host(inode->flags);
     113}
     114
     115/**
     116 * Get direct block ID
     117 *
     118 * @param inode pointer to inode
     119 * @param idx Index to block. Valid values are 0 <= idx < 12
     120 */
     121inline uint32_t ext2_inode_get_direct_block(ext2_inode_t *inode, uint8_t idx)
     122{
     123        return uint32_t_le2host(inode->direct_blocks[idx]);
     124}
     125
     126/**
     127 * Get indirect block ID
     128 *
     129 * @param inode pointer to inode
     130 */
     131inline uint32_t ext2_inode_get_single_indirect_block(ext2_inode_t *inode)
     132{
     133        return uint32_t_le2host(inode->single_indirect_block);
     134}
     135
     136/**
     137 * Get double indirect block ID
     138 *
     139 * @param inode pointer to inode
     140 */
     141inline uint32_t ext2_inode_get_double_indirect_block(ext2_inode_t *inode)
     142{
     143        return uint32_t_le2host(inode->double_indirect_block);
     144}
     145
     146/**
     147 * Get triple indirect block ID
     148 *
     149 * @param inode pointer to inode
     150 */
     151inline uint32_t ext2_inode_get_triple_indirect_block(ext2_inode_t *inode)
     152{
     153        return uint32_t_le2host(inode->triple_indirect_block);
     154}
    40155
    41156
  • uspace/lib/ext2/libext2_inode.h

    rce13577 r5352d72  
    5555} ext2_inode_t;
    5656
     57#define EXT2_INODE_MODE_FIFO            0x1000
     58#define EXT2_INODE_MODE_CHARDEV         0x2000
     59#define EXT2_INODE_MODE_DIRECTORY       0x4000
     60#define EXT2_INODE_MODE_BLOCKDEV        0x6000
     61#define EXT2_INODE_MODE_FILE            0x8000
     62#define EXT2_INODE_MODE_SOFTLINK        0xA000
     63#define EXT2_INODE_MODE_SOCKET          0xC000
     64#define EXT2_INODE_MODE_ACCESS_MASK     0x0FFF
     65
    5766typedef struct ext2_inode_ref {
    5867        block_t *block; // Reference to a block containing this inode
    5968        ext2_inode_t *inode;
    6069} ext2_inode_ref_t;
     70
     71inline uint16_t ext2_inode_get_mode(ext2_inode_t *);
     72inline uint32_t ext2_inode_get_user_id(ext2_inode_t *);
     73inline uint32_t ext2_inode_get_size(ext2_inode_t *);
     74inline uint32_t ext2_inode_get_group_id(ext2_inode_t *);
     75inline uint16_t ext2_inode_get_usage_count(ext2_inode_t *);
     76inline uint32_t ext2_inode_get_reserved_512_blocks(ext2_inode_t *);
     77inline uint32_t ext2_inode_get_flags(ext2_inode_t *);
     78inline uint32_t ext2_inode_get_direct_block(ext2_inode_t *, uint8_t);
     79inline uint32_t ext2_inode_get_single_indirect_block(ext2_inode_t *);
     80inline uint32_t ext2_inode_get_double_indirect_block(ext2_inode_t *);
     81inline uint32_t ext2_inode_get_triple_indirect_block(ext2_inode_t *);
    6182
    6283
Note: See TracChangeset for help on using the changeset viewer.