dupfs.c revision d1154eb460efe588eaed3d439c1caaca149fa362
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
23errcode_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
44	io_channel_bumpcount(fs->io);
45	if (fs->icache)
46		fs->icache->refcount++;
47
48	retval = ext2fs_get_mem(strlen(src->device_name)+1, &fs->device_name);
49	if (retval)
50		goto errout;
51	strcpy(fs->device_name, src->device_name);
52
53	retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
54	if (retval)
55		goto errout;
56	memcpy(fs->super, src->super, SUPERBLOCK_SIZE);
57
58	retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
59	if (retval)
60		goto errout;
61	memcpy(fs->orig_super, src->orig_super, SUPERBLOCK_SIZE);
62
63	retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
64				&fs->group_desc);
65	if (retval)
66		goto errout;
67	memcpy(fs->group_desc, src->group_desc,
68	       (size_t) fs->desc_blocks * fs->blocksize);
69
70	if (src->inode_map) {
71		retval = ext2fs_copy_bitmap(src->inode_map, &fs->inode_map);
72		if (retval)
73			goto errout;
74	}
75	if (src->block_map) {
76		retval = ext2fs_copy_bitmap(src->block_map, &fs->block_map);
77		if (retval)
78			goto errout;
79	}
80	if (src->badblocks) {
81		retval = ext2fs_badblocks_copy(src->badblocks, &fs->badblocks);
82		if (retval)
83			goto errout;
84	}
85	if (src->dblist) {
86		retval = ext2fs_copy_dblist(src->dblist, &fs->dblist);
87		if (retval)
88			goto errout;
89	}
90	*dest = fs;
91	return 0;
92errout:
93	ext2fs_free(fs);
94	return retval;
95
96}
97
98