blob: 02721e1a574a1fef6b0c2a59cbe651303b42af39 [file] [log] [blame]
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001/*
2 * dupfs.c --- duplicate a ext2 filesystem handle
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'obc1b8032005-01-05 03:16:09 -05004 * Copyright (C) 1997, 1998, 2001, 2003, 2005 by Theodore Ts'o.
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00005 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00009 * %End-Header%
10 */
11
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000013#include <stdio.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000017#include <time.h>
18#include <string.h>
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000019
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000020#include "ext2_fs.h"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000021#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);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040029
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040030 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000031 if (retval)
32 return retval;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000033
34 *fs = *src;
35 fs->device_name = 0;
36 fs->super = 0;
Theodore Ts'obc1b8032005-01-05 03:16:09 -050037 fs->orig_super = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000038 fs->group_desc = 0;
39 fs->inode_map = 0;
40 fs->block_map = 0;
41 fs->badblocks = 0;
42 fs->dblist = 0;
Darrick J. Wong816d2c92013-09-09 10:39:29 -040043 fs->mmp_buf = 0;
44 fs->mmp_cmp = 0;
45 fs->mmp_fd = -1;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000046
47 io_channel_bumpcount(fs->io);
48 if (fs->icache)
49 fs->icache->refcount++;
50
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040051 retval = ext2fs_get_mem(strlen(src->device_name)+1, &fs->device_name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000052 if (retval)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000053 goto errout;
54 strcpy(fs->device_name, src->device_name);
55
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040056 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000057 if (retval)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000058 goto errout;
59 memcpy(fs->super, src->super, SUPERBLOCK_SIZE);
60
Theodore Ts'obc1b8032005-01-05 03:16:09 -050061 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
Theodore Ts'oee010792007-11-09 19:01:06 -050066 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040067 &fs->group_desc);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000068 if (retval)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000069 goto errout;
70 memcpy(fs->group_desc, src->group_desc,
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000071 (size_t) fs->desc_blocks * fs->blocksize);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000072
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 }
Darrick J. Wong816d2c92013-09-09 10:39:29 -040093 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 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000115 *dest = fs;
116 return 0;
117errout:
118 ext2fs_free(fs);
119 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400120
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000121}
122