Initial public busybox upstream commit
[busybox4maemo] / e2fsprogs / old_e2fsprogs / ext2fs / dupfs.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * dupfs.c --- duplicate a ext2 filesystem handle
4  *
5  * Copyright (C) 1997, 1998, 2001, 2003, 2005 by Theodore Ts'o.
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Public
9  * License.
10  * %End-Header%
11  */
12
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
23 errcode_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_mem((size_t) 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;
92 errout:
93         ext2fs_free(fs);
94         return retval;
95
96 }
97