Whamcloud - gitweb
Shorten compile commands run by the build system
[tools/e2fsprogs.git] / lib / ext2fs / freefs.c
1 /*
2  * freefs.c --- free an ext2 filesystem
3  *
4  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
13 #include <stdio.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17
18 #include "ext2_fs.h"
19 #include "ext2fsP.h"
20
21 static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache);
22
23 void ext2fs_free(ext2_filsys fs)
24 {
25         if (!fs || (fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS))
26                 return;
27         if (fs->image_io != fs->io) {
28                 if (fs->image_io)
29                         io_channel_close(fs->image_io);
30         }
31         if (fs->io) {
32                 io_channel_close(fs->io);
33         }
34         if (fs->device_name)
35                 ext2fs_free_mem(&fs->device_name);
36         if (fs->super)
37                 ext2fs_free_mem(&fs->super);
38         if (fs->orig_super)
39                 ext2fs_free_mem(&fs->orig_super);
40         if (fs->group_desc)
41                 ext2fs_free_mem(&fs->group_desc);
42         if (fs->block_map)
43                 ext2fs_free_block_bitmap(fs->block_map);
44         if (fs->inode_map)
45                 ext2fs_free_inode_bitmap(fs->inode_map);
46
47         if (fs->badblocks)
48                 ext2fs_badblocks_list_free(fs->badblocks);
49         fs->badblocks = 0;
50
51         if (fs->dblist)
52                 ext2fs_free_dblist(fs->dblist);
53
54         if (fs->icache)
55                 ext2fs_free_inode_cache(fs->icache);
56
57         fs->magic = 0;
58
59         ext2fs_free_mem(&fs);
60 }
61
62 /*
63  * Free the inode cache structure
64  */
65 static void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
66 {
67         if (--icache->refcount)
68                 return;
69         if (icache->buffer)
70                 ext2fs_free_mem(&icache->buffer);
71         if (icache->cache)
72                 ext2fs_free_mem(&icache->cache);
73         icache->buffer_blk = 0;
74         ext2fs_free_mem(&icache);
75 }
76
77 /*
78  * This procedure frees a badblocks list.
79  */
80 void ext2fs_u32_list_free(ext2_u32_list bb)
81 {
82         if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
83                 return;
84
85         if (bb->list)
86                 ext2fs_free_mem(&bb->list);
87         bb->list = 0;
88         ext2fs_free_mem(&bb);
89 }
90
91 void ext2fs_badblocks_list_free(ext2_badblocks_list bb)
92 {
93         ext2fs_u32_list_free((ext2_u32_list) bb);
94 }
95
96
97 /*
98  * Free a directory block list
99  */
100 void ext2fs_free_dblist(ext2_dblist dblist)
101 {
102         if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
103                 return;
104
105         if (dblist->list)
106                 ext2fs_free_mem(&dblist->list);
107         dblist->list = 0;
108         if (dblist->fs && dblist->fs->dblist == dblist)
109                 dblist->fs->dblist = 0;
110         dblist->magic = 0;
111         ext2fs_free_mem(&dblist);
112 }
113