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