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