Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / dupfs.c
1 /*
2  * dupfs.c --- duplicate a ext2 filesystem handle
3  * 
4  * Copyright (C) 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #if HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #include <stdlib.h>
17 #include <time.h>
18 #include <string.h>
19 #ifdef HAVE_ERRNO_H
20 #include <errno.h>
21 #endif
22
23 #include <linux/ext2_fs.h>
24
25 #include "ext2fsP.h"
26
27 errcode_t ext2fs_dup_handle(ext2_filsys src, ext2_filsys *dest)
28 {
29         ext2_filsys     fs;
30         errcode_t       retval;
31
32         EXT2_CHECK_MAGIC(src, EXT2_ET_MAGIC_EXT2FS_FILSYS);
33         
34         fs = (ext2_filsys) malloc(sizeof(struct struct_ext2_filsys));
35         if (!fs)
36                 return ENOMEM;
37
38         *fs = *src;
39         fs->device_name = 0;
40         fs->super = 0;
41         fs->group_desc = 0;
42         fs->inode_map = 0;
43         fs->block_map = 0;
44         fs->badblocks = 0;
45         fs->dblist = 0;
46
47         io_channel_bumpcount(fs->io);
48         if (fs->icache)
49                 fs->icache->refcount++;
50
51         retval = ENOMEM;
52         fs->device_name = malloc(strlen(src->device_name)+1);
53         if (!fs->device_name)
54                 goto errout;
55         strcpy(fs->device_name, src->device_name);
56
57         fs->super = malloc(SUPERBLOCK_SIZE);
58         if (!fs->super)
59                 goto errout;
60         memcpy(fs->super, src->super, SUPERBLOCK_SIZE);
61
62         fs->group_desc = malloc(fs->desc_blocks * fs->blocksize);
63         if (!fs->group_desc)
64                 goto errout;
65         memcpy(fs->group_desc, src->group_desc,
66                fs->desc_blocks * fs->blocksize);
67
68         if (src->inode_map) {
69                 retval = ext2fs_copy_bitmap(src->inode_map, &fs->inode_map);
70                 if (retval)
71                         goto errout;
72         }
73         if (src->block_map) {
74                 retval = ext2fs_copy_bitmap(src->block_map, &fs->block_map);
75                 if (retval)
76                         goto errout;
77         }
78         if (src->badblocks) {
79                 retval = ext2fs_badblocks_copy(src->badblocks, &fs->badblocks);
80                 if (retval)
81                         goto errout;
82         }
83         if (src->dblist) {
84                 retval = ext2fs_copy_dblist(src->dblist, &fs->dblist);
85                 if (retval)
86                         goto errout;
87         }
88         *dest = fs;
89         return 0;
90 errout:
91         ext2fs_free(fs);
92         return retval;
93         
94 }
95