ind_block.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ind_block.c --- indirect block I/O routines
  4. *
  5. * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
  6. * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
  7. *
  8. * %Begin-Header%
  9. * This file may be redistributed under the terms of the GNU Public
  10. * License.
  11. * %End-Header%
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #if HAVE_UNISTD_H
  16. #include <unistd.h>
  17. #endif
  18. #include "ext2_fs.h"
  19. #include "ext2fs.h"
  20. errcode_t ext2fs_read_ind_block(ext2_filsys fs, blk_t blk, void *buf)
  21. {
  22. errcode_t retval;
  23. #if BB_BIG_ENDIAN
  24. blk_t *block_nr;
  25. int i;
  26. int limit = fs->blocksize >> 2;
  27. #endif
  28. if ((fs->flags & EXT2_FLAG_IMAGE_FILE) &&
  29. (fs->io != fs->image_io))
  30. memset(buf, 0, fs->blocksize);
  31. else {
  32. retval = io_channel_read_blk(fs->io, blk, 1, buf);
  33. if (retval)
  34. return retval;
  35. }
  36. #if BB_BIG_ENDIAN
  37. if (fs->flags & (EXT2_FLAG_SWAP_BYTES | EXT2_FLAG_SWAP_BYTES_READ)) {
  38. block_nr = (blk_t *) buf;
  39. for (i = 0; i < limit; i++, block_nr++)
  40. *block_nr = ext2fs_swab32(*block_nr);
  41. }
  42. #endif
  43. return 0;
  44. }
  45. errcode_t ext2fs_write_ind_block(ext2_filsys fs, blk_t blk, void *buf)
  46. {
  47. #if BB_BIG_ENDIAN
  48. blk_t *block_nr;
  49. int i;
  50. int limit = fs->blocksize >> 2;
  51. #endif
  52. if (fs->flags & EXT2_FLAG_IMAGE_FILE)
  53. return 0;
  54. #if BB_BIG_ENDIAN
  55. if (fs->flags & (EXT2_FLAG_SWAP_BYTES | EXT2_FLAG_SWAP_BYTES_WRITE)) {
  56. block_nr = (blk_t *) buf;
  57. for (i = 0; i < limit; i++, block_nr++)
  58. *block_nr = ext2fs_swab32(*block_nr);
  59. }
  60. #endif
  61. return io_channel_write_blk(fs->io, blk, 1, buf);
  62. }