/src/libfshfs/libfshfs/libfshfs_btree_node_cache.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * B-tree node cache functions |
3 | | * |
4 | | * Copyright (C) 2009-2024, Joachim Metz <joachim.metz@gmail.com> |
5 | | * |
6 | | * Refer to AUTHORS for acknowledgements. |
7 | | * |
8 | | * This program is free software: you can redistribute it and/or modify |
9 | | * it under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation, either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include <common.h> |
23 | | #include <memory.h> |
24 | | #include <types.h> |
25 | | |
26 | | #include "libfshfs_btree_node_cache.h" |
27 | | #include "libfshfs_definitions.h" |
28 | | #include "libfshfs_libcerror.h" |
29 | | #include "libfshfs_libfcache.h" |
30 | | |
31 | | /* Creates a B-tree node cache |
32 | | * Make sure the value btree_node_cache is referencing, is set to NULL |
33 | | * Returns 1 if successful or -1 on error |
34 | | */ |
35 | | int libfshfs_btree_node_cache_initialize( |
36 | | libfshfs_btree_node_cache_t **btree_node_cache, |
37 | | libcerror_error_t **error ) |
38 | 15.2k | { |
39 | 15.2k | static char *function = "libfshfs_btree_node_cache_initialize"; |
40 | 15.2k | int cache_size = 1; |
41 | 15.2k | int depth = 0; |
42 | | |
43 | 15.2k | if( btree_node_cache == NULL ) |
44 | 0 | { |
45 | 0 | libcerror_error_set( |
46 | 0 | error, |
47 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
48 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
49 | 0 | "%s: invalid B-tree node cache.", |
50 | 0 | function ); |
51 | |
|
52 | 0 | return( -1 ); |
53 | 0 | } |
54 | 15.2k | if( *btree_node_cache != NULL ) |
55 | 0 | { |
56 | 0 | libcerror_error_set( |
57 | 0 | error, |
58 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
59 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
60 | 0 | "%s: invalid B-tree node cache value already set.", |
61 | 0 | function ); |
62 | |
|
63 | 0 | return( -1 ); |
64 | 0 | } |
65 | 15.2k | *btree_node_cache = memory_allocate_structure( |
66 | 15.2k | libfshfs_btree_node_cache_t ); |
67 | | |
68 | 15.2k | if( *btree_node_cache == NULL ) |
69 | 0 | { |
70 | 0 | libcerror_error_set( |
71 | 0 | error, |
72 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
73 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
74 | 0 | "%s: unable to create B-tree node cache.", |
75 | 0 | function ); |
76 | |
|
77 | 0 | goto on_error; |
78 | 0 | } |
79 | 15.2k | if( memory_set( |
80 | 15.2k | *btree_node_cache, |
81 | 15.2k | 0, |
82 | 15.2k | sizeof( libfshfs_btree_node_cache_t ) ) == NULL ) |
83 | 0 | { |
84 | 0 | libcerror_error_set( |
85 | 0 | error, |
86 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
87 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
88 | 0 | "%s: unable to clear B-tree node cache.", |
89 | 0 | function ); |
90 | |
|
91 | 0 | memory_free( |
92 | 0 | *btree_node_cache ); |
93 | |
|
94 | 0 | *btree_node_cache = NULL; |
95 | |
|
96 | 0 | return( -1 ); |
97 | 0 | } |
98 | 15.2k | for( depth = 0; |
99 | 152k | depth < 9; |
100 | 137k | depth++ ) |
101 | 137k | { |
102 | 137k | if( libfcache_cache_initialize( |
103 | 137k | &( ( *btree_node_cache )->caches[ depth ] ), |
104 | 137k | cache_size, |
105 | 137k | error ) != 1 ) |
106 | 0 | { |
107 | 0 | libcerror_error_set( |
108 | 0 | error, |
109 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
110 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
111 | 0 | "%s: unable to create cache: %d.", |
112 | 0 | function, |
113 | 0 | depth ); |
114 | |
|
115 | 0 | goto on_error; |
116 | 0 | } |
117 | 137k | cache_size = LIBFSHFS_MAXIMUM_CACHE_ENTRIES_BTREE_FILE_NODES * ( depth + 1 ); |
118 | 137k | } |
119 | 15.2k | return( 1 ); |
120 | | |
121 | 0 | on_error: |
122 | 0 | if( *btree_node_cache != NULL ) |
123 | 0 | { |
124 | 0 | memory_free( |
125 | 0 | *btree_node_cache ); |
126 | |
|
127 | 0 | *btree_node_cache = NULL; |
128 | 0 | } |
129 | 0 | return( -1 ); |
130 | 15.2k | } |
131 | | |
132 | | /* Frees a B-tree node cache |
133 | | * Returns 1 if successful or -1 on error |
134 | | */ |
135 | | int libfshfs_btree_node_cache_free( |
136 | | libfshfs_btree_node_cache_t **btree_node_cache, |
137 | | libcerror_error_t **error ) |
138 | 15.2k | { |
139 | 15.2k | static char *function = "libfshfs_btree_node_cache_free"; |
140 | 15.2k | int depth = 0; |
141 | 15.2k | int result = 1; |
142 | | |
143 | 15.2k | if( btree_node_cache == NULL ) |
144 | 0 | { |
145 | 0 | libcerror_error_set( |
146 | 0 | error, |
147 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
148 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
149 | 0 | "%s: invalid B-tree node cache.", |
150 | 0 | function ); |
151 | |
|
152 | 0 | return( -1 ); |
153 | 0 | } |
154 | 15.2k | if( *btree_node_cache != NULL ) |
155 | 15.2k | { |
156 | 15.2k | for( depth = 0; |
157 | 152k | depth < 9; |
158 | 137k | depth++ ) |
159 | 137k | { |
160 | 137k | if( libfcache_cache_free( |
161 | 137k | &( ( *btree_node_cache )->caches[ depth ] ), |
162 | 137k | error ) != 1 ) |
163 | 0 | { |
164 | 0 | libcerror_error_set( |
165 | 0 | error, |
166 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
167 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
168 | 0 | "%s: unable to free cache: %d.", |
169 | 0 | function, |
170 | 0 | depth ); |
171 | |
|
172 | 0 | result = -1; |
173 | 0 | } |
174 | 137k | } |
175 | 15.2k | memory_free( |
176 | 15.2k | *btree_node_cache ); |
177 | | |
178 | 15.2k | *btree_node_cache = NULL; |
179 | 15.2k | } |
180 | 15.2k | return( result ); |
181 | 15.2k | } |
182 | | |