Changeset 5352d72 in mainline for uspace/app/ext2info/ext2info.c


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

File:
1 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 * @}
Note: See TracChangeset for help on using the changeset viewer.