[821] | 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 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 <time.h>
|
---|
| 17 | #include <string.h>
|
---|
| 18 |
|
---|
| 19 | #include "ext2_fs.h"
|
---|
| 20 | #include "ext2fsP.h"
|
---|
| 21 |
|
---|
| 22 | errcode_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);
|
---|
| 28 |
|
---|
| 29 | retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
|
---|
| 30 | if (retval)
|
---|
| 31 | return retval;
|
---|
| 32 |
|
---|
| 33 | *fs = *src;
|
---|
| 34 | fs->device_name = 0;
|
---|
| 35 | fs->super = 0;
|
---|
| 36 | fs->orig_super = 0;
|
---|
| 37 | fs->group_desc = 0;
|
---|
| 38 | fs->inode_map = 0;
|
---|
| 39 | fs->block_map = 0;
|
---|
| 40 | fs->badblocks = 0;
|
---|
| 41 | fs->dblist = 0;
|
---|
| 42 |
|
---|
| 43 | io_channel_bumpcount(fs->io);
|
---|
| 44 | if (fs->icache)
|
---|
| 45 | fs->icache->refcount++;
|
---|
| 46 |
|
---|
| 47 | retval = ext2fs_get_mem(strlen(src->device_name)+1, &fs->device_name);
|
---|
| 48 | if (retval)
|
---|
| 49 | goto errout;
|
---|
| 50 | strcpy(fs->device_name, src->device_name);
|
---|
| 51 |
|
---|
| 52 | retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
|
---|
| 53 | if (retval)
|
---|
| 54 | goto errout;
|
---|
| 55 | memcpy(fs->super, src->super, SUPERBLOCK_SIZE);
|
---|
| 56 |
|
---|
| 57 | retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
|
---|
| 58 | if (retval)
|
---|
| 59 | goto errout;
|
---|
| 60 | memcpy(fs->orig_super, src->orig_super, SUPERBLOCK_SIZE);
|
---|
| 61 |
|
---|
| 62 | retval = ext2fs_get_mem((size_t) fs->desc_blocks * fs->blocksize,
|
---|
| 63 | &fs->group_desc);
|
---|
| 64 | if (retval)
|
---|
| 65 | goto errout;
|
---|
| 66 | memcpy(fs->group_desc, src->group_desc,
|
---|
| 67 | (size_t) fs->desc_blocks * fs->blocksize);
|
---|
| 68 |
|
---|
| 69 | if (src->inode_map) {
|
---|
| 70 | retval = ext2fs_copy_bitmap(src->inode_map, &fs->inode_map);
|
---|
| 71 | if (retval)
|
---|
| 72 | goto errout;
|
---|
| 73 | }
|
---|
| 74 | if (src->block_map) {
|
---|
| 75 | retval = ext2fs_copy_bitmap(src->block_map, &fs->block_map);
|
---|
| 76 | if (retval)
|
---|
| 77 | goto errout;
|
---|
| 78 | }
|
---|
| 79 | if (src->badblocks) {
|
---|
| 80 | retval = ext2fs_badblocks_copy(src->badblocks, &fs->badblocks);
|
---|
| 81 | if (retval)
|
---|
| 82 | goto errout;
|
---|
| 83 | }
|
---|
| 84 | if (src->dblist) {
|
---|
| 85 | retval = ext2fs_copy_dblist(src->dblist, &fs->dblist);
|
---|
| 86 | if (retval)
|
---|
| 87 | goto errout;
|
---|
| 88 | }
|
---|
| 89 | *dest = fs;
|
---|
| 90 | return 0;
|
---|
| 91 | errout:
|
---|
| 92 | ext2fs_free(fs);
|
---|
| 93 | return retval;
|
---|
| 94 |
|
---|
| 95 | }
|
---|
| 96 |
|
---|