ind_block.c 1.5 KB

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