Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfshfs/libfshfs/libfshfs_btree_node_cache.c
Line
Count
Source
1
/*
2
 * B-tree node cache functions
3
 *
4
 * Copyright (C) 2009-2026, 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.7k
{
39
15.7k
  static char *function = "libfshfs_btree_node_cache_initialize";
40
15.7k
  int cache_size        = 1;
41
15.7k
  int depth             = 0;
42
43
15.7k
  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.7k
  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.7k
  *btree_node_cache = memory_allocate_structure(
66
15.7k
                       libfshfs_btree_node_cache_t );
67
68
15.7k
  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.7k
  if( memory_set(
80
15.7k
       *btree_node_cache,
81
15.7k
       0,
82
15.7k
       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.7k
  for( depth = 0;
99
157k
       depth < 9;
100
141k
       depth++ )
101
141k
  {
102
141k
    if( libfcache_cache_initialize(
103
141k
         &( ( *btree_node_cache )->caches[ depth ] ),
104
141k
         cache_size,
105
141k
         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
141k
    cache_size = LIBFSHFS_MAXIMUM_CACHE_ENTRIES_BTREE_FILE_NODES * ( depth + 1 );
118
141k
  }
119
15.7k
  return( 1 );
120
121
0
on_error:
122
0
  if( *btree_node_cache != NULL )
123
0
  {
124
0
    for( depth = 0;
125
0
         depth < 9;
126
0
         depth++ )
127
0
    {
128
0
      if( ( *btree_node_cache )->caches[ depth ] != NULL )
129
0
      {
130
0
        libfcache_cache_free(
131
0
         &( ( *btree_node_cache )->caches[ depth ] ),
132
0
         NULL );
133
0
      }
134
0
    }
135
0
    memory_free(
136
0
     *btree_node_cache );
137
138
0
    *btree_node_cache = NULL;
139
0
  }
140
0
  return( -1 );
141
15.7k
}
142
143
/* Frees a B-tree node cache
144
 * Returns 1 if successful or -1 on error
145
 */
146
int libfshfs_btree_node_cache_free(
147
     libfshfs_btree_node_cache_t **btree_node_cache,
148
     libcerror_error_t **error )
149
15.7k
{
150
15.7k
  static char *function = "libfshfs_btree_node_cache_free";
151
15.7k
  int depth             = 0;
152
15.7k
  int result            = 1;
153
154
15.7k
  if( btree_node_cache == NULL )
155
0
  {
156
0
    libcerror_error_set(
157
0
     error,
158
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
159
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
160
0
     "%s: invalid B-tree node cache.",
161
0
     function );
162
163
0
    return( -1 );
164
0
  }
165
15.7k
  if( *btree_node_cache != NULL )
166
15.7k
  {
167
15.7k
    for( depth = 0;
168
157k
         depth < 9;
169
141k
         depth++ )
170
141k
    {
171
141k
      if( libfcache_cache_free(
172
141k
           &( ( *btree_node_cache )->caches[ depth ] ),
173
141k
           error ) != 1 )
174
0
      {
175
0
        libcerror_error_set(
176
0
         error,
177
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
178
0
         LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
179
0
         "%s: unable to free cache: %d.",
180
0
         function,
181
0
         depth );
182
183
0
        result = -1;
184
0
      }
185
141k
    }
186
15.7k
    memory_free(
187
15.7k
     *btree_node_cache );
188
189
    *btree_node_cache = NULL;
190
15.7k
  }
191
15.7k
  return( result );
192
15.7k
}
193