home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / EXT2FS / BLOCK.C < prev    next >
C/C++ Source or Header  |  1995-05-10  |  6KB  |  236 lines

  1. /*
  2.  * block.c --- iterate over all blocks in an inode
  3.  * 
  4.  * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
  5.  * under the terms of the GNU Public License.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12.  
  13. #include <errno.h>
  14. #include <linux/ext2_fs.h>
  15.  
  16. #include "ext2fs.h"
  17.  
  18. struct block_context {
  19.     ext2_filsys    fs;
  20.     int (*func)(ext2_filsys    fs,
  21.             blk_t    *blocknr,
  22.             int        bcount,
  23.             void    *private);
  24.     int        bcount;
  25.     int        bsize;
  26.     int        flags;
  27.     errcode_t    errcode;
  28.     char    *ind_buf;
  29.     char    *dind_buf;
  30.     char    *tind_buf;
  31.     void    *private;
  32. };
  33.  
  34. static int block_iterate_ind(blk_t *ind_block, struct block_context *ctx)
  35. {
  36.     int    ret = 0, changed = 0;
  37.     int    i, flags;
  38.     blk_t    *block_nr;
  39.  
  40.     if (!(ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE))
  41.         ret = (*ctx->func)(ctx->fs, ind_block, -1, ctx->private);
  42.     if (!*ind_block || (ret & BLOCK_ABORT))
  43.         return ret;
  44.     ctx->errcode = io_channel_read_blk(ctx->fs->io, *ind_block,
  45.                        1, ctx->ind_buf);
  46.     if (ctx->errcode) {
  47.         ret |= BLOCK_ERROR;
  48.         return ret;
  49.     }
  50.     for (i = 0; i < (ctx->fs->blocksize >> 2); i++, ctx->bcount++) {
  51.         block_nr = (blk_t *) ctx->ind_buf + i;
  52.         if (*block_nr || (ctx->flags & BLOCK_FLAG_APPEND)) {
  53.             flags = (*ctx->func)(ctx->fs, block_nr, ctx->bcount,
  54.                          ctx->private);
  55.             changed    |= flags & BLOCK_CHANGED;
  56.             if (flags & BLOCK_ABORT) {
  57.                 ret |= BLOCK_ABORT;
  58.                 break;
  59.             }
  60.         }
  61.     }
  62.     if (changed) {
  63.         ctx->errcode = io_channel_write_blk(ctx->fs->io, *ind_block,
  64.                             1, ctx->ind_buf);
  65.         if (ctx->errcode)
  66.             ret |= BLOCK_ERROR | BLOCK_ABORT;
  67.     }
  68.     if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
  69.         !(ret & BLOCK_ABORT))
  70.         ret |= (*ctx->func)(ctx->fs, ind_block, -1, ctx->private);
  71.     return ret;
  72. }
  73.     
  74. static int block_iterate_dind(blk_t *dind_block, struct block_context *ctx)
  75. {
  76.     int    ret = 0, changed = 0;
  77.     int    i, flags;
  78.     blk_t    *block_nr;
  79.  
  80.     if (!(ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE))
  81.         ret = (*ctx->func)(ctx->fs, dind_block, -2, ctx->private);
  82.     if (!*dind_block || (ret & BLOCK_ABORT))
  83.         return ret;
  84.     ctx->errcode = io_channel_read_blk(ctx->fs->io, *dind_block,
  85.                        1, ctx->dind_buf);
  86.     if (ctx->errcode) {
  87.         ret |= BLOCK_ERROR;
  88.         return ret;
  89.     }
  90.     for (i = 0; i < (ctx->fs->blocksize >> 2); i++) {
  91.         block_nr = (blk_t *) ctx->dind_buf + i;
  92.         if (*block_nr || (ctx->flags & BLOCK_FLAG_APPEND)) {
  93.             flags = block_iterate_ind(block_nr, ctx);
  94.             changed |= flags & BLOCK_CHANGED;
  95.             if (flags & (BLOCK_ABORT | BLOCK_ERROR)) {
  96.                 ret |= flags & (BLOCK_ABORT | BLOCK_ERROR);
  97.                 break;
  98.             }
  99.         }
  100.         else
  101.             ctx->bcount += ctx->fs->blocksize >> 2;
  102.     }
  103.     if (changed) {
  104.         ctx->errcode = io_channel_write_blk(ctx->fs->io, *dind_block,
  105.                             1, ctx->dind_buf);
  106.         if (ctx->errcode)
  107.             ret |= BLOCK_ERROR | BLOCK_ABORT;
  108.     }
  109.     if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
  110.         !(ret & BLOCK_ABORT))
  111.         ret |= (*ctx->func)(ctx->fs, dind_block, -2, ctx->private);
  112.     return ret;
  113. }
  114.     
  115. static int block_iterate_tind(blk_t *tind_block, struct block_context *ctx)
  116. {
  117.     int    ret = 0, changed = 0;
  118.     int    i, flags;
  119.     blk_t    *block_nr;
  120.  
  121.     if (!(ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE))
  122.         ret = (*ctx->func)(ctx->fs, tind_block, -3, ctx->private);
  123.     if (!*tind_block || (ret & BLOCK_ABORT))
  124.         return ret;
  125.     ctx->errcode = io_channel_read_blk(ctx->fs->io, *tind_block,
  126.                        1, ctx->tind_buf);
  127.     if (ctx->errcode) {
  128.         ret |= BLOCK_ERROR;
  129.         return ret;
  130.     }
  131.     for (i = 0; i < (ctx->fs->blocksize >> 2); i++) {
  132.         block_nr = (blk_t *) ctx->tind_buf + i;
  133.         if (*block_nr || (ctx->flags & BLOCK_FLAG_APPEND)) {
  134.             flags = block_iterate_dind(block_nr, ctx);
  135.             if (flags & (BLOCK_ABORT | BLOCK_ERROR)) {
  136.                 ret |= flags & (BLOCK_ABORT | BLOCK_ERROR);
  137.                 break;
  138.             }
  139.         }
  140.         else
  141.             ctx->bcount += (ctx->fs->blocksize >> 2) * (ctx->fs->blocksize >> 2);
  142.     }
  143.     if (changed) {
  144.         ctx->errcode = io_channel_write_blk(ctx->fs->io, *tind_block,
  145.                             1, ctx->tind_buf);
  146.         if (ctx->errcode)
  147.             ret |= BLOCK_ERROR | BLOCK_ABORT;
  148.     }
  149.     if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
  150.         !(ret & BLOCK_ABORT))
  151.         ret |= (*ctx->func)(ctx->fs, tind_block, -3, ctx->private);
  152.     
  153.     return ret;
  154. }
  155.     
  156. errcode_t ext2fs_block_iterate(ext2_filsys fs,
  157.                    ino_t    ino,
  158.                    int    flags,
  159.                    char *block_buf,
  160.                    int (*func)(ext2_filsys fs,
  161.                        blk_t    *blocknr,
  162.                        int    blockcnt,
  163.                        void    *private),
  164.                    void *private)
  165. {
  166.     int    i;
  167.     int    ret = 0;
  168.     struct block_context ctx;
  169.     blk_t    blocks[EXT2_N_BLOCKS];    /* directory data blocks */
  170.     struct ext2_inode inode;
  171.     errcode_t    retval;
  172.     
  173.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  174.  
  175.     ret = ext2fs_get_blocks(fs, ino, blocks);
  176.     if (ret)
  177.         return ret;
  178.  
  179.     ctx.fs = fs;
  180.     ctx.func = func;
  181.     ctx.private = private;
  182.     ctx.bcount = 0;
  183.     ctx.flags = flags;
  184.     if (block_buf) {
  185.         ctx.ind_buf = block_buf;
  186.     } else {
  187.         ctx.ind_buf = malloc(fs->blocksize * 3);
  188.         if (!ctx.ind_buf)
  189.             return ENOMEM;
  190.     }
  191.     ctx.dind_buf = ctx.ind_buf + fs->blocksize;
  192.     ctx.tind_buf = ctx.dind_buf + fs->blocksize;
  193.     
  194.     for (i = 0; i < EXT2_NDIR_BLOCKS ; i++, ctx.bcount++) {
  195.         if (blocks[i] || (flags & BLOCK_FLAG_APPEND)) {
  196.             ret |= (*func)(fs, &blocks[i], ctx.bcount, private);
  197.             if (ret & BLOCK_ABORT)
  198.                 goto abort;
  199.         }
  200.     }
  201.     if (*(blocks + EXT2_IND_BLOCK) || (flags & BLOCK_FLAG_APPEND)) {
  202.         ret |= block_iterate_ind(blocks + EXT2_IND_BLOCK, &ctx);
  203.         if (ret & BLOCK_ABORT)
  204.             goto abort;
  205.     }
  206.     else
  207.         ctx.bcount += fs->blocksize >> 2;
  208.  
  209.     if (*(blocks + EXT2_DIND_BLOCK) || (flags & BLOCK_FLAG_APPEND)) {
  210.         ret |= block_iterate_dind(blocks + EXT2_DIND_BLOCK, &ctx);
  211.         if (ret & BLOCK_ABORT)
  212.             goto abort;
  213.     }
  214.     else
  215.         ctx.bcount += (fs->blocksize >> 2) * (fs->blocksize >> 2);
  216.     if (*(blocks + EXT2_TIND_BLOCK) || (flags & BLOCK_FLAG_APPEND))
  217.         ret |= block_iterate_tind(blocks + EXT2_TIND_BLOCK, &ctx);
  218.  
  219. abort:
  220.     if (ret & BLOCK_CHANGED) {
  221.         retval = ext2fs_read_inode(fs, ino, &inode);
  222.         if (retval)
  223.             return retval;
  224.         for (i=0; i < EXT2_N_BLOCKS; i++)
  225.             inode.i_block[i] = blocks[i];
  226.         retval = ext2fs_write_inode(fs, ino, &inode);
  227.         if (retval)
  228.             return retval;
  229.     }
  230.  
  231.     if (!block_buf)
  232.         free(ctx.ind_buf);
  233.  
  234.     return (ret & BLOCK_ERROR) ? ctx.errcode : 0;
  235. }
  236.