blob: 13a4812139fce1dc41afc1bb78e4d1a141390b10 [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
12#include <stdio.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000013#if HAVE_UNISTD_H
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000014#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#endif
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000016#include <time.h>
17#include <string.h>
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000018
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000019#include "ext2_fs.h"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000020#include "ext2fsP.h"
21
22errcode_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);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040028
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040029 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000030 if (retval)
31 return retval;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000032
33 *fs = *src;
34 fs->device_name = 0;
35 fs->super = 0;
Theodore Ts'obc1b8032005-01-05 03:16:09 -050036 fs->orig_super = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000037 fs->group_desc = 0;
38 fs->inode_map = 0;
39 fs->block_map = 0;
40 fs->badblocks = 0;
41 fs->dblist = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -070042 fs->mmp_buf = 0;
43 fs->mmp_cmp = 0;
44 fs->mmp_fd = -1;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000045
46 io_channel_bumpcount(fs->io);
47 if (fs->icache)
48 fs->icache->refcount++;
49
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040050 retval = ext2fs_get_mem(strlen(src->device_name)+1, &fs->device_name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000051 if (retval)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000052 goto errout;
53 strcpy(fs->device_name, src->device_name);
54
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040055 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000056 if (retval)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000057 goto errout;
58 memcpy(fs->super, src->super, SUPERBLOCK_SIZE);
59
Theodore Ts'obc1b8032005-01-05 03:16:09 -050060 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
Theodore Ts'oee010792007-11-09 19:01:06 -050065 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040066 &fs->group_desc);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000067 if (retval)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000068 goto errout;
69 memcpy(fs->group_desc, src->group_desc,
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000070 (size_t) fs->desc_blocks * fs->blocksize);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000071
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 }
JP Abgralle0ed7402014-03-19 19:08:39 -070092 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 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000114 *dest = fs;
115 return 0;
116errout:
117 ext2fs_free(fs);
118 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400119
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000120}
121