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 Library 8 * General Public License, version 2. 9 * %End-Header% 10 */ 11 12 #include "config.h" 13 #include <stdio.h> 14 #if HAVE_UNISTD_H 15 #include <unistd.h> 16 #endif 17 #include <time.h> 18 #include <string.h> 19 20 #include "ext2_fs.h" 21 #include "ext2fsP.h" 22 23 errcode_t ext2fs_dup_handle(ext2_filsys src, ext2_filsys *dest) 24 { 25 ext2_filsys fs; 26 errcode_t retval; 27 28 EXT2_CHECK_MAGIC(src, EXT2_ET_MAGIC_EXT2FS_FILSYS); 29 30 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs); 31 if (retval) 32 return retval; 33 34 *fs = *src; 35 fs->device_name = 0; 36 fs->super = 0; 37 fs->orig_super = 0; 38 fs->group_desc = 0; 39 fs->inode_map = 0; 40 fs->block_map = 0; 41 fs->badblocks = 0; 42 fs->dblist = 0; 43 fs->mmp_buf = 0; 44 fs->mmp_cmp = 0; 45 fs->mmp_fd = -1; 46 47 io_channel_bumpcount(fs->io); 48 if (fs->icache) 49 fs->icache->refcount++; 50 51 retval = ext2fs_get_mem(strlen(src->device_name)+1, &fs->device_name); 52 if (retval) 53 goto errout; 54 strcpy(fs->device_name, src->device_name); 55 56 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super); 57 if (retval) 58 goto errout; 59 memcpy(fs->super, src->super, SUPERBLOCK_SIZE); 60 61 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super); 62 if (retval) 63 goto errout; 64 memcpy(fs->orig_super, src->orig_super, SUPERBLOCK_SIZE); 65 66 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize, 67 &fs->group_desc); 68 if (retval) 69 goto errout; 70 memcpy(fs->group_desc, src->group_desc, 71 (size_t) fs->desc_blocks * fs->blocksize); 72 73 if (src->inode_map) { 74 retval = ext2fs_copy_bitmap(src->inode_map, &fs->inode_map); 75 if (retval) 76 goto errout; 77 } 78 if (src->block_map) { 79 retval = ext2fs_copy_bitmap(src->block_map, &fs->block_map); 80 if (retval) 81 goto errout; 82 } 83 if (src->badblocks) { 84 retval = ext2fs_badblocks_copy(src->badblocks, &fs->badblocks); 85 if (retval) 86 goto errout; 87 } 88 if (src->dblist) { 89 retval = ext2fs_copy_dblist(src->dblist, &fs->dblist); 90 if (retval) 91 goto errout; 92 } 93 if (src->mmp_buf) { 94 retval = ext2fs_get_mem(src->blocksize, &fs->mmp_buf); 95 if (retval) 96 goto errout; 97 memcpy(fs->mmp_buf, src->mmp_buf, src->blocksize); 98 } 99 if (src->mmp_fd >= 0) { 100 fs->mmp_fd = dup(src->mmp_fd); 101 if (fs->mmp_fd < 0) { 102 retval = EXT2_ET_MMP_OPEN_DIRECT; 103 goto errout; 104 } 105 } 106 if (src->mmp_cmp) { 107 int align = ext2fs_get_dio_alignment(src->mmp_fd); 108 109 retval = ext2fs_get_memalign(src->blocksize, align, 110 &fs->mmp_cmp); 111 if (retval) 112 goto errout; 113 memcpy(fs->mmp_cmp, src->mmp_cmp, src->blocksize); 114 } 115 *dest = fs; 116 return 0; 117 errout: 118 ext2fs_free(fs); 119 return retval; 120 121 } 122 123