dupfs.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * dupfs.c --- duplicate a ext2 filesystem handle
  3. *
  4. * Copyright (C) 1997, 1998, 2001, 2003, 2005 by 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 <time.h>
  16. #include <string.h>
  17. #include "ext2_fs.h"
  18. #include "ext2fsP.h"
  19. errcode_t ext2fs_dup_handle(ext2_filsys src, ext2_filsys *dest)
  20. {
  21. ext2_filsys fs;
  22. errcode_t retval;
  23. EXT2_CHECK_MAGIC(src, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  24. retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
  25. if (retval)
  26. return retval;
  27. *fs = *src;
  28. fs->device_name = 0;
  29. fs->super = 0;
  30. fs->orig_super = 0;
  31. fs->group_desc = 0;
  32. fs->inode_map = 0;
  33. fs->block_map = 0;
  34. fs->badblocks = 0;
  35. fs->dblist = 0;
  36. io_channel_bumpcount(fs->io);
  37. if (fs->icache)
  38. fs->icache->refcount++;
  39. retval = ext2fs_get_mem(strlen(src->device_name)+1, &fs->device_name);
  40. if (retval)
  41. goto errout;
  42. strcpy(fs->device_name, src->device_name);
  43. retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
  44. if (retval)
  45. goto errout;
  46. memcpy(fs->super, src->super, SUPERBLOCK_SIZE);
  47. retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
  48. if (retval)
  49. goto errout;
  50. memcpy(fs->orig_super, src->orig_super, SUPERBLOCK_SIZE);
  51. retval = ext2fs_get_mem((size_t) fs->desc_blocks * fs->blocksize,
  52. &fs->group_desc);
  53. if (retval)
  54. goto errout;
  55. memcpy(fs->group_desc, src->group_desc,
  56. (size_t) fs->desc_blocks * fs->blocksize);
  57. if (src->inode_map) {
  58. retval = ext2fs_copy_bitmap(src->inode_map, &fs->inode_map);
  59. if (retval)
  60. goto errout;
  61. }
  62. if (src->block_map) {
  63. retval = ext2fs_copy_bitmap(src->block_map, &fs->block_map);
  64. if (retval)
  65. goto errout;
  66. }
  67. if (src->badblocks) {
  68. retval = ext2fs_badblocks_copy(src->badblocks, &fs->badblocks);
  69. if (retval)
  70. goto errout;
  71. }
  72. if (src->dblist) {
  73. retval = ext2fs_copy_dblist(src->dblist, &fs->dblist);
  74. if (retval)
  75. goto errout;
  76. }
  77. *dest = fs;
  78. return 0;
  79. errout:
  80. ext2fs_free(fs);
  81. return retval;
  82. }