3
0

expanddir.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * expand.c --- expand an ext2fs directory
  3. *
  4. * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 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. #include <string.h>
  13. #if HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #include "ext2_fs.h"
  17. #include "ext2fs.h"
  18. struct expand_dir_struct {
  19. int done;
  20. int newblocks;
  21. errcode_t err;
  22. };
  23. static int expand_dir_proc(ext2_filsys fs,
  24. blk_t *blocknr,
  25. e2_blkcnt_t blockcnt,
  26. blk_t ref_block EXT2FS_ATTR((unused)),
  27. int ref_offset EXT2FS_ATTR((unused)),
  28. void *priv_data)
  29. {
  30. struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
  31. blk_t new_blk;
  32. static blk_t last_blk = 0;
  33. char *block;
  34. errcode_t retval;
  35. if (*blocknr) {
  36. last_blk = *blocknr;
  37. return 0;
  38. }
  39. retval = ext2fs_new_block(fs, last_blk, 0, &new_blk);
  40. if (retval) {
  41. es->err = retval;
  42. return BLOCK_ABORT;
  43. }
  44. if (blockcnt > 0) {
  45. retval = ext2fs_new_dir_block(fs, 0, 0, &block);
  46. if (retval) {
  47. es->err = retval;
  48. return BLOCK_ABORT;
  49. }
  50. es->done = 1;
  51. retval = ext2fs_write_dir_block(fs, new_blk, block);
  52. } else {
  53. retval = ext2fs_get_mem(fs->blocksize, &block);
  54. if (retval) {
  55. es->err = retval;
  56. return BLOCK_ABORT;
  57. }
  58. memset(block, 0, fs->blocksize);
  59. retval = io_channel_write_blk(fs->io, new_blk, 1, block);
  60. }
  61. if (retval) {
  62. es->err = retval;
  63. return BLOCK_ABORT;
  64. }
  65. ext2fs_free_mem(&block);
  66. *blocknr = new_blk;
  67. ext2fs_block_alloc_stats(fs, new_blk, +1);
  68. es->newblocks++;
  69. if (es->done)
  70. return (BLOCK_CHANGED | BLOCK_ABORT);
  71. else
  72. return BLOCK_CHANGED;
  73. }
  74. errcode_t ext2fs_expand_dir(ext2_filsys fs, ext2_ino_t dir)
  75. {
  76. errcode_t retval;
  77. struct expand_dir_struct es;
  78. struct ext2_inode inode;
  79. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  80. if (!(fs->flags & EXT2_FLAG_RW))
  81. return EXT2_ET_RO_FILSYS;
  82. if (!fs->block_map)
  83. return EXT2_ET_NO_BLOCK_BITMAP;
  84. retval = ext2fs_check_directory(fs, dir);
  85. if (retval)
  86. return retval;
  87. es.done = 0;
  88. es.err = 0;
  89. es.newblocks = 0;
  90. retval = ext2fs_block_iterate2(fs, dir, BLOCK_FLAG_APPEND,
  91. 0, expand_dir_proc, &es);
  92. if (es.err)
  93. return es.err;
  94. if (!es.done)
  95. return EXT2_ET_EXPAND_DIR_ERR;
  96. /*
  97. * Update the size and block count fields in the inode.
  98. */
  99. retval = ext2fs_read_inode(fs, dir, &inode);
  100. if (retval)
  101. return retval;
  102. inode.i_size += fs->blocksize;
  103. inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
  104. retval = ext2fs_write_inode(fs, dir, &inode);
  105. if (retval)
  106. return retval;
  107. return 0;
  108. }