3
0

dirblock.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * dirblock.c --- directory block routines.
  3. *
  4. * Copyright (C) 1995, 1996 Theodore Ts'o.
  5. *
  6. * %Begin-Header%
  7. * This file may be redistributed under the terms of the GNU Public
  8. * License.
  9. * %End-Header%
  10. */
  11. #include <stdio.h>
  12. #if HAVE_UNISTD_H
  13. #include <unistd.h>
  14. #endif
  15. #include <string.h>
  16. #include <time.h>
  17. #include "ext2_fs.h"
  18. #include "ext2fs.h"
  19. errcode_t ext2fs_read_dir_block2(ext2_filsys fs, blk_t block,
  20. void *buf, int flags EXT2FS_ATTR((unused)))
  21. {
  22. errcode_t retval;
  23. char *p, *end;
  24. struct ext2_dir_entry *dirent;
  25. unsigned int name_len, rec_len, do_swap;
  26. retval = io_channel_read_blk(fs->io, block, 1, buf);
  27. if (retval)
  28. return retval;
  29. #ifdef EXT2FS_ENABLE_SWAPFS
  30. do_swap = (fs->flags & (EXT2_FLAG_SWAP_BYTES|
  31. EXT2_FLAG_SWAP_BYTES_READ)) != 0;
  32. #endif
  33. p = (char *) buf;
  34. end = (char *) buf + fs->blocksize;
  35. while (p < end-8) {
  36. dirent = (struct ext2_dir_entry *) p;
  37. #ifdef EXT2FS_ENABLE_SWAPFS
  38. if (do_swap) {
  39. dirent->inode = ext2fs_swab32(dirent->inode);
  40. dirent->rec_len = ext2fs_swab16(dirent->rec_len);
  41. dirent->name_len = ext2fs_swab16(dirent->name_len);
  42. }
  43. #endif
  44. name_len = dirent->name_len;
  45. #ifdef WORDS_BIGENDIAN
  46. if (flags & EXT2_DIRBLOCK_V2_STRUCT)
  47. dirent->name_len = ext2fs_swab16(dirent->name_len);
  48. #endif
  49. rec_len = dirent->rec_len;
  50. if ((rec_len < 8) || (rec_len % 4)) {
  51. rec_len = 8;
  52. retval = EXT2_ET_DIR_CORRUPTED;
  53. }
  54. if (((name_len & 0xFF) + 8) > dirent->rec_len)
  55. retval = EXT2_ET_DIR_CORRUPTED;
  56. p += rec_len;
  57. }
  58. return retval;
  59. }
  60. errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
  61. void *buf)
  62. {
  63. return ext2fs_read_dir_block2(fs, block, buf, 0);
  64. }
  65. errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
  66. void *inbuf, int flags EXT2FS_ATTR((unused)))
  67. {
  68. #ifdef EXT2FS_ENABLE_SWAPFS
  69. int do_swap = 0;
  70. errcode_t retval;
  71. char *p, *end;
  72. char *buf = 0;
  73. struct ext2_dir_entry *dirent;
  74. if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  75. (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
  76. do_swap = 1;
  77. #ifndef WORDS_BIGENDIAN
  78. if (!do_swap)
  79. return io_channel_write_blk(fs->io, block, 1, (char *) inbuf);
  80. #endif
  81. retval = ext2fs_get_mem(fs->blocksize, &buf);
  82. if (retval)
  83. return retval;
  84. memcpy(buf, inbuf, fs->blocksize);
  85. p = buf;
  86. end = buf + fs->blocksize;
  87. while (p < end) {
  88. dirent = (struct ext2_dir_entry *) p;
  89. if ((dirent->rec_len < 8) ||
  90. (dirent->rec_len % 4)) {
  91. ext2fs_free_mem(&buf);
  92. return (EXT2_ET_DIR_CORRUPTED);
  93. }
  94. p += dirent->rec_len;
  95. if (do_swap) {
  96. dirent->inode = ext2fs_swab32(dirent->inode);
  97. dirent->rec_len = ext2fs_swab16(dirent->rec_len);
  98. dirent->name_len = ext2fs_swab16(dirent->name_len);
  99. }
  100. #ifdef WORDS_BIGENDIAN
  101. if (flags & EXT2_DIRBLOCK_V2_STRUCT)
  102. dirent->name_len = ext2fs_swab16(dirent->name_len);
  103. #endif
  104. }
  105. retval = io_channel_write_blk(fs->io, block, 1, buf);
  106. ext2fs_free_mem(&buf);
  107. return retval;
  108. #else
  109. return io_channel_write_blk(fs->io, block, 1, (char *) inbuf);
  110. #endif
  111. }
  112. errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
  113. void *inbuf)
  114. {
  115. return ext2fs_write_dir_block2(fs, block, inbuf, 0);
  116. }