3
0

dupfs.c 2.0 KB

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