Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvsbsdl/libfcache/libfcache_cache.c
Line
Count
Source
1
/*
2
 * The cache functions
3
 *
4
 * Copyright (C) 2010-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 "libfcache_cache.h"
27
#include "libfcache_cache_value.h"
28
#include "libfcache_definitions.h"
29
#include "libfcache_libcdata.h"
30
#include "libfcache_libcerror.h"
31
#include "libfcache_types.h"
32
33
/* Creates a cache
34
 * Make sure the value cache is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libfcache_cache_initialize(
38
     libfcache_cache_t **cache,
39
     int maximum_cache_entries,
40
     libcerror_error_t **error )
41
392k
{
42
392k
  libfcache_internal_cache_t *internal_cache = NULL;
43
392k
  static char *function                      = "libfcache_cache_initialize";
44
45
392k
  if( cache == NULL )
46
0
  {
47
0
    libcerror_error_set(
48
0
     error,
49
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
50
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
51
0
     "%s: invalid cache.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
392k
  if( *cache != NULL )
57
0
  {
58
0
    libcerror_error_set(
59
0
     error,
60
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
61
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
62
0
     "%s: invalid cache value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
392k
  if( maximum_cache_entries <= 0 )
68
0
  {
69
0
    libcerror_error_set(
70
0
     error,
71
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
72
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
73
0
     "%s: invalid maximum cache entries value zero or less.",
74
0
     function );
75
76
0
    return( -1 );
77
0
  }
78
392k
  internal_cache = memory_allocate_structure(
79
392k
                    libfcache_internal_cache_t );
80
81
392k
  if( internal_cache == NULL )
82
0
  {
83
0
    libcerror_error_set(
84
0
     error,
85
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
86
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
87
0
     "%s: unable to create cache.",
88
0
     function );
89
90
0
    goto on_error;
91
0
  }
92
392k
  if( memory_set(
93
392k
       internal_cache,
94
392k
       0,
95
392k
       sizeof( libfcache_internal_cache_t ) ) == NULL )
96
0
  {
97
0
    libcerror_error_set(
98
0
     error,
99
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
100
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
101
0
     "%s: unable to clear cache.",
102
0
     function );
103
104
0
    memory_free(
105
0
     internal_cache );
106
107
0
    return( -1 );
108
0
  }
109
392k
  if( libcdata_array_initialize(
110
392k
       &( internal_cache->entries_array ),
111
392k
       maximum_cache_entries,
112
392k
       error ) != 1 )
113
0
  {
114
0
    libcerror_error_set(
115
0
     error,
116
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
117
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
118
0
     "%s: unable to create entries array.",
119
0
     function );
120
121
0
    goto on_error;
122
0
  }
123
392k
  if( libcdata_list_initialize(
124
392k
       &( internal_cache->entries_list ),
125
392k
       error ) != 1 )
126
0
  {
127
0
    libcerror_error_set(
128
0
     error,
129
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
130
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
131
0
     "%s: unable to create entries list.",
132
0
     function );
133
134
0
    goto on_error;
135
0
  }
136
392k
  *cache = (libfcache_cache_t *) internal_cache;
137
138
392k
  return( 1 );
139
140
0
on_error:
141
0
  if( internal_cache != NULL )
142
0
  {
143
0
    if( internal_cache->entries_array != NULL )
144
0
    {
145
0
      libcdata_array_free(
146
0
       &( internal_cache->entries_array ),
147
0
       NULL,
148
0
       NULL );
149
0
    }
150
0
    memory_free(
151
0
     internal_cache );
152
0
  }
153
0
  return( -1 );
154
392k
}
155
156
/* Frees a cache
157
 * Returns 1 if successful or -1 on error
158
 */
159
int libfcache_cache_free(
160
     libfcache_cache_t **cache,
161
     libcerror_error_t **error )
162
392k
{
163
392k
  libfcache_internal_cache_t *internal_cache = NULL;
164
392k
  static char *function                      = "libfcache_cache_free";
165
392k
  int result                                 = 1;
166
167
392k
  if( cache == NULL )
168
0
  {
169
0
    libcerror_error_set(
170
0
     error,
171
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
172
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
173
0
     "%s: invalid cache.",
174
0
     function );
175
176
0
    return( -1 );
177
0
  }
178
392k
  if( *cache != NULL )
179
392k
  {
180
392k
    internal_cache = (libfcache_internal_cache_t *) *cache;
181
392k
    *cache         = NULL;
182
183
392k
    if( libcdata_list_free(
184
392k
         &( internal_cache->entries_list ),
185
392k
         NULL,
186
392k
         error ) != 1 )
187
0
    {
188
0
      libcerror_error_set(
189
0
       error,
190
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
191
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
192
0
       "%s: unable to free the entries list.",
193
0
       function );
194
195
0
      result = -1;
196
0
    }
197
392k
    if( libcdata_array_free(
198
392k
         &( internal_cache->entries_array ),
199
392k
         (int (*)(intptr_t **, libcerror_error_t **)) &libfcache_cache_value_free,
200
392k
         error ) != 1 )
201
0
    {
202
0
      libcerror_error_set(
203
0
       error,
204
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
205
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
206
0
       "%s: unable to free the entries array.",
207
0
       function );
208
209
0
      result = -1;
210
0
    }
211
392k
    memory_free(
212
392k
     internal_cache );
213
392k
  }
214
392k
  return( result );
215
392k
}
216
217
/* Empties the cache
218
 * Returns 1 if successful or -1 on error
219
 */
220
int libfcache_cache_empty(
221
     libfcache_cache_t *cache,
222
     libcerror_error_t **error )
223
14.5k
{
224
14.5k
  libfcache_internal_cache_t *internal_cache = NULL;
225
14.5k
  static char *function                      = "libfcache_cache_empty";
226
227
14.5k
  if( cache == NULL )
228
0
  {
229
0
    libcerror_error_set(
230
0
     error,
231
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
232
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
233
0
     "%s: invalid cache.",
234
0
     function );
235
236
0
    return( -1 );
237
0
  }
238
14.5k
  internal_cache = (libfcache_internal_cache_t *) cache;
239
240
14.5k
  if( libcdata_list_empty(
241
14.5k
       internal_cache->entries_list,
242
14.5k
       NULL,
243
14.5k
       error ) != 1 )
244
0
  {
245
0
    libcerror_error_set(
246
0
     error,
247
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
248
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
249
0
     "%s: unable to empty entries list.",
250
0
     function );
251
252
0
    return( -1 );
253
0
  }
254
14.5k
  if( libcdata_array_clear(
255
14.5k
       internal_cache->entries_array,
256
14.5k
       (int (*)(intptr_t **, libcerror_error_t **)) &libfcache_cache_value_free,
257
14.5k
       error ) != 1 )
258
0
  {
259
0
    libcerror_error_set(
260
0
     error,
261
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
262
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
263
0
     "%s: unable to clear entries array.",
264
0
     function );
265
266
0
    return( -1 );
267
0
  }
268
14.5k
  internal_cache->number_of_cache_values = 0;
269
270
14.5k
  return( 1 );
271
14.5k
}
272
273
/* Clones (duplicates) the cache, not the cache values
274
 * Returns 1 if successful or -1 on error
275
 */
276
int libfcache_cache_clone(
277
     libfcache_cache_t **destination_cache,
278
     libfcache_cache_t *source_cache,
279
     libcerror_error_t **error )
280
10
{
281
10
  libfcache_internal_cache_t *internal_source_cache = NULL;
282
10
  static char *function                             = "libfcache_cache_clone";
283
10
  int number_of_cache_entries                       = 0;
284
285
10
  if( destination_cache == NULL )
286
0
  {
287
0
    libcerror_error_set(
288
0
     error,
289
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
290
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
291
0
     "%s: invalid destination cache.",
292
0
     function );
293
294
0
    return( -1 );
295
0
  }
296
10
  if( *destination_cache != NULL )
297
0
  {
298
0
    libcerror_error_set(
299
0
     error,
300
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
301
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
302
0
     "%s: destination cache already set.",
303
0
     function );
304
305
0
    return( -1 );
306
0
  }
307
10
  if( source_cache == NULL )
308
0
  {
309
0
    *destination_cache = NULL;
310
311
0
    return( 1 );
312
0
  }
313
10
  internal_source_cache = (libfcache_internal_cache_t *) source_cache;
314
315
10
  if( libcdata_array_get_number_of_entries(
316
10
       internal_source_cache->entries_array,
317
10
       &number_of_cache_entries,
318
10
       error ) != 1 )
319
0
  {
320
0
    libcerror_error_set(
321
0
     error,
322
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
323
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
324
0
     "%s: unable to retrieve number of cache entries from source entries array.",
325
0
     function );
326
327
0
    return( -1 );
328
0
  }
329
10
  if( libfcache_cache_initialize(
330
10
       destination_cache,
331
10
       number_of_cache_entries,
332
10
       error ) != 1 )
333
0
  {
334
0
    libcerror_error_set(
335
0
     error,
336
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
337
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
338
0
     "%s: unable to create destination cache.",
339
0
     function );
340
341
0
    return( -1 );
342
0
  }
343
10
  return( 1 );
344
10
}
345
346
/* Resizes the cache
347
 * Returns 1 if successful or -1 on error
348
 */
349
int libfcache_cache_resize(
350
     libfcache_cache_t *cache,
351
     int maximum_cache_entries,
352
     libcerror_error_t **error )
353
0
{
354
0
  libfcache_internal_cache_t *internal_cache = NULL;
355
0
  static char *function                      = "libfcache_cache_resize";
356
357
0
  if( cache == NULL )
358
0
  {
359
0
    libcerror_error_set(
360
0
     error,
361
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
362
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
363
0
     "%s: invalid cache.",
364
0
     function );
365
366
0
    return( -1 );
367
0
  }
368
0
  internal_cache = (libfcache_internal_cache_t *) cache;
369
370
0
  if( libcdata_array_resize(
371
0
       internal_cache->entries_array,
372
0
       maximum_cache_entries,
373
0
       (int (*)(intptr_t **, libcerror_error_t **)) &libfcache_cache_value_free,
374
0
       error ) != 1 )
375
0
  {
376
0
    libcerror_error_set(
377
0
     error,
378
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
379
0
     LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
380
0
     "%s: unable to resize entries array.",
381
0
     function );
382
383
0
    return( -1 );
384
0
  }
385
0
  return( 1 );
386
0
}
387
388
/* Retrieves the number of entries of the cache
389
 * Returns 1 if successful or -1 on error
390
 */
391
int libfcache_cache_get_number_of_entries(
392
     libfcache_cache_t *cache,
393
     int *number_of_entries,
394
     libcerror_error_t **error )
395
92.7k
{
396
92.7k
  libfcache_internal_cache_t *internal_cache = NULL;
397
92.7k
  static char *function                      = "libfcache_cache_get_number_of_entries";
398
399
92.7k
  if( cache == NULL )
400
0
  {
401
0
    libcerror_error_set(
402
0
     error,
403
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
404
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
405
0
     "%s: invalid cache.",
406
0
     function );
407
408
0
    return( -1 );
409
0
  }
410
92.7k
  internal_cache = (libfcache_internal_cache_t *) cache;
411
412
92.7k
  if( libcdata_array_get_number_of_entries(
413
92.7k
       internal_cache->entries_array,
414
92.7k
       number_of_entries,
415
92.7k
       error ) != 1 )
416
0
  {
417
0
    libcerror_error_set(
418
0
     error,
419
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
420
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
421
0
     "%s: unable to retrieve number of entries from entries array.",
422
0
     function );
423
424
0
    return( -1 );
425
0
  }
426
92.7k
  return( 1 );
427
92.7k
}
428
429
/* Retrieves the number of cache values
430
 * Returns 1 if successful or -1 on error
431
 */
432
int libfcache_cache_get_number_of_cache_values(
433
     libfcache_cache_t *cache,
434
     int *number_of_cache_values,
435
     libcerror_error_t **error )
436
0
{
437
0
  libfcache_internal_cache_t *internal_cache = NULL;
438
0
  static char *function                      = "libfcache_cache_get_number_of_cache_values";
439
440
0
  if( cache == NULL )
441
0
  {
442
0
    libcerror_error_set(
443
0
     error,
444
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
445
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
446
0
     "%s: invalid cache.",
447
0
     function );
448
449
0
    return( -1 );
450
0
  }
451
0
  internal_cache = (libfcache_internal_cache_t *) cache;
452
453
0
  if( libcdata_list_get_number_of_elements(
454
0
       internal_cache->entries_list,
455
0
       number_of_cache_values,
456
0
       error ) != 1 )
457
0
  {
458
0
    libcerror_error_set(
459
0
     error,
460
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
461
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
462
0
     "%s: unable to retrieve number of elements from entries list.",
463
0
     function );
464
465
0
    return( -1 );
466
0
  }
467
0
  return( 1 );
468
0
}
469
470
/* Clears the cache value for the specific index
471
 * Returns 1 if successful or -1 on error
472
 */
473
int libfcache_cache_clear_value_by_index(
474
     libfcache_cache_t *cache,
475
     int cache_entry_index,
476
     libcerror_error_t **error )
477
28.0k
{
478
28.0k
  libfcache_cache_value_t *cache_value       = NULL;
479
28.0k
  libfcache_internal_cache_t *internal_cache = NULL;
480
28.0k
  static char *function                      = "libfcache_cache_clear_value_by_index";
481
482
28.0k
  if( cache == NULL )
483
0
  {
484
0
    libcerror_error_set(
485
0
     error,
486
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
487
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
488
0
     "%s: invalid cache.",
489
0
     function );
490
491
0
    return( -1 );
492
0
  }
493
28.0k
  internal_cache = (libfcache_internal_cache_t *) cache;
494
495
28.0k
  if( libcdata_array_get_entry_by_index(
496
28.0k
       internal_cache->entries_array,
497
28.0k
       cache_entry_index,
498
28.0k
       (intptr_t **) &cache_value,
499
28.0k
       error ) != 1 )
500
0
  {
501
0
    libcerror_error_set(
502
0
     error,
503
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
504
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
505
0
     "%s: unable to retrieve cache value: %d from entries array.",
506
0
     function,
507
0
     cache_entry_index );
508
509
0
    return( -1 );
510
0
  }
511
28.0k
  if( libfcache_cache_value_clear(
512
28.0k
       cache_value,
513
28.0k
       error ) != 1 )
514
0
  {
515
0
    libcerror_error_set(
516
0
     error,
517
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
518
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
519
0
     "%s: unable to clear cache value.",
520
0
     function );
521
522
0
    return( -1 );
523
0
  }
524
28.0k
  return( 1 );
525
28.0k
}
526
527
/* Retrieves the cache value that matches the file index, offset and timestamp
528
 * Returns 1 if successful, 0 if no such value or -1 on error
529
 */
530
int libfcache_cache_get_value_by_identifier(
531
     libfcache_cache_t *cache,
532
     int file_index,
533
     off64_t offset,
534
     int64_t timestamp,
535
     libfcache_cache_value_t **cache_value,
536
     libcerror_error_t **error )
537
6.77M
{
538
6.77M
  libcdata_list_element_t *list_element      = NULL;
539
6.77M
  libfcache_cache_value_t *safe_cache_value  = NULL;
540
6.77M
  libfcache_internal_cache_t *internal_cache = NULL;
541
6.77M
  static char *function                      = "libfcache_cache_get_value_by_identifier";
542
6.77M
  off64_t cache_value_offset                 = 0;
543
6.77M
  int64_t cache_value_timestamp              = 0;
544
6.77M
  int cache_value_file_index                 = 0;
545
6.77M
  int number_of_cache_values                 = 0;
546
6.77M
  int result                                 = 0;
547
548
6.77M
  if( cache == NULL )
549
0
  {
550
0
    libcerror_error_set(
551
0
     error,
552
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
553
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
554
0
     "%s: invalid cache.",
555
0
     function );
556
557
0
    return( -1 );
558
0
  }
559
6.77M
  internal_cache = (libfcache_internal_cache_t *) cache;
560
561
6.77M
  if( cache_value == NULL )
562
0
  {
563
0
    libcerror_error_set(
564
0
     error,
565
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
566
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
567
0
     "%s: invalid cache value.",
568
0
     function );
569
570
0
    return( -1 );
571
0
  }
572
6.77M
  *cache_value = NULL;
573
574
6.77M
  if( libcdata_list_get_number_of_elements(
575
6.77M
       internal_cache->entries_list,
576
6.77M
       &number_of_cache_values,
577
6.77M
       error ) != 1 )
578
0
  {
579
0
    libcerror_error_set(
580
0
     error,
581
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
582
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
583
0
     "%s: unable to retrieve number of elements from entries list.",
584
0
     function );
585
586
0
    return( -1 );
587
0
  }
588
6.77M
  if( libcdata_list_get_first_element(
589
6.77M
       internal_cache->entries_list,
590
6.77M
       &list_element,
591
6.77M
       error ) != 1 )
592
0
  {
593
0
    libcerror_error_set(
594
0
     error,
595
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
596
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
597
0
     "%s: unable to retrieve first list element.",
598
0
     function );
599
600
0
    return( -1 );
601
0
  }
602
50.4M
  while( list_element != NULL )
603
48.2M
  {
604
48.2M
    if( libcdata_list_element_get_value(
605
48.2M
         list_element,
606
48.2M
         (intptr_t **) &safe_cache_value,
607
48.2M
         error ) != 1 )
608
0
    {
609
0
      libcerror_error_set(
610
0
       error,
611
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
612
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
613
0
       "%s: unable to retrieve cache value from list element.",
614
0
       function );
615
616
0
      return( -1 );
617
0
    }
618
48.2M
    if( libfcache_cache_value_get_identifier(
619
48.2M
         safe_cache_value,
620
48.2M
         &cache_value_file_index,
621
48.2M
         &cache_value_offset,
622
48.2M
         &cache_value_timestamp,
623
48.2M
         error ) != 1 )
624
0
    {
625
0
      libcerror_error_set(
626
0
       error,
627
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
628
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
629
0
       "%s: unable to retrieve cache value identifier.",
630
0
       function );
631
632
0
      return( -1 );
633
0
    }
634
48.2M
    if( ( cache_value_file_index == file_index )
635
48.2M
     && ( cache_value_offset == offset )
636
4.63M
     && ( cache_value_timestamp == timestamp ) )
637
4.62M
    {
638
4.62M
      result = 1;
639
640
4.62M
      break;
641
4.62M
    }
642
43.6M
    if( libcdata_list_element_get_next_element(
643
43.6M
         list_element,
644
43.6M
         &list_element,
645
43.6M
         error ) != 1 )
646
0
    {
647
0
      libcerror_error_set(
648
0
       error,
649
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
650
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
651
0
       "%s: unable to retrieve next list element.",
652
0
       function );
653
654
0
      return( -1 );
655
0
    }
656
43.6M
  }
657
6.77M
  if( ( result != 0 )
658
4.62M
   && ( number_of_cache_values > 0 ) )
659
4.62M
  {
660
4.62M
    if( libcdata_list_remove_element(
661
4.62M
         internal_cache->entries_list,
662
4.62M
         list_element,
663
4.62M
         error ) != 1 )
664
0
    {
665
0
      libcerror_error_set(
666
0
       error,
667
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
668
0
       LIBCERROR_RUNTIME_ERROR_REMOVE_FAILED,
669
0
       "%s: unable to remove list element.",
670
0
       function );
671
672
0
      return( -1 );
673
0
    }
674
4.62M
    if( libcdata_list_prepend_element(
675
4.62M
         internal_cache->entries_list,
676
4.62M
         list_element,
677
4.62M
         error ) != 1 )
678
0
    {
679
0
      libcerror_error_set(
680
0
       error,
681
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
682
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
683
0
       "%s: unable to prepend list element.",
684
0
       function );
685
686
0
      return( -1 );
687
0
    }
688
4.62M
    *cache_value = safe_cache_value;
689
4.62M
  }
690
6.77M
  return( result );
691
6.77M
}
692
693
/* Retrieves the cache value for the specific index
694
 * Returns 1 if successful or -1 on error
695
 */
696
int libfcache_cache_get_value_by_index(
697
     libfcache_cache_t *cache,
698
     int cache_entry_index,
699
     libfcache_cache_value_t **cache_value,
700
     libcerror_error_t **error )
701
92.7k
{
702
92.7k
  libfcache_internal_cache_t *internal_cache = NULL;
703
92.7k
  static char *function                      = "libfcache_cache_get_value_by_index";
704
705
92.7k
  if( cache == NULL )
706
0
  {
707
0
    libcerror_error_set(
708
0
     error,
709
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
710
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
711
0
     "%s: invalid cache.",
712
0
     function );
713
714
0
    return( -1 );
715
0
  }
716
92.7k
  internal_cache = (libfcache_internal_cache_t *) cache;
717
718
92.7k
  if( libcdata_array_get_entry_by_index(
719
92.7k
       internal_cache->entries_array,
720
92.7k
       cache_entry_index,
721
92.7k
       (intptr_t **) cache_value,
722
92.7k
       error ) != 1 )
723
0
  {
724
0
    libcerror_error_set(
725
0
     error,
726
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
727
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
728
0
     "%s: unable to retrieve cache value: %d from entries array.",
729
0
     function,
730
0
     cache_entry_index );
731
732
0
    return( -1 );
733
0
  }
734
92.7k
  return( 1 );
735
92.7k
}
736
737
/* Sets the cache value for the file index, offset and timestamp
738
 * Returns 1 if successful or -1 on error
739
 */
740
int libfcache_cache_set_value_by_identifier(
741
     libfcache_cache_t *cache,
742
     int file_index,
743
     off64_t offset,
744
     int64_t timestamp,
745
     intptr_t *value,
746
     int (*value_free_function)(
747
            intptr_t **value,
748
            libcerror_error_t **error ),
749
     uint8_t flags,
750
     libcerror_error_t **error )
751
2.17M
{
752
2.17M
  libcdata_list_element_t *list_element      = NULL;
753
2.17M
  libfcache_cache_value_t *cache_value       = NULL;
754
2.17M
  libfcache_cache_value_t *safe_cache_value  = NULL;
755
2.17M
  libfcache_internal_cache_t *internal_cache = NULL;
756
2.17M
  static char *function                      = "libfcache_cache_set_value_by_identifier";
757
2.17M
  int cache_entry_index                      = 0;
758
2.17M
  int number_of_cache_entries                = 0;
759
2.17M
  int number_of_cache_values                 = 0;
760
761
2.17M
  if( cache == NULL )
762
0
  {
763
0
    libcerror_error_set(
764
0
     error,
765
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
766
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
767
0
     "%s: invalid cache.",
768
0
     function );
769
770
0
    return( -1 );
771
0
  }
772
2.17M
  internal_cache = (libfcache_internal_cache_t *) cache;
773
774
2.17M
  if( libcdata_array_get_number_of_entries(
775
2.17M
       internal_cache->entries_array,
776
2.17M
       &number_of_cache_entries,
777
2.17M
       error ) != 1 )
778
0
  {
779
0
    libcerror_error_set(
780
0
     error,
781
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
782
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
783
0
     "%s: unable to retrieve number of cache entries from entries array.",
784
0
     function );
785
786
0
    goto on_error;
787
0
  }
788
2.17M
  if( libcdata_list_get_number_of_elements(
789
2.17M
       internal_cache->entries_list,
790
2.17M
       &number_of_cache_values,
791
2.17M
       error ) != 1 )
792
0
  {
793
0
    libcerror_error_set(
794
0
     error,
795
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
796
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
797
0
     "%s: unable to retrieve number of elements from entries list.",
798
0
     function );
799
800
0
    goto on_error;
801
0
  }
802
2.17M
  if( number_of_cache_values < number_of_cache_entries )
803
186k
  {
804
186k
    cache_entry_index = number_of_cache_values;
805
806
186k
    if( libcdata_array_get_entry_by_index(
807
186k
         internal_cache->entries_array,
808
186k
         cache_entry_index,
809
186k
         (intptr_t **) &cache_value,
810
186k
         error ) != 1 )
811
0
    {
812
0
      libcerror_error_set(
813
0
       error,
814
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
815
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
816
0
       "%s: unable to retrieve cache value: %d from entries array.",
817
0
       function,
818
0
       cache_entry_index );
819
820
0
      goto on_error;
821
0
    }
822
186k
    if( cache_value == NULL )
823
186k
    {
824
186k
      if( libfcache_cache_value_initialize(
825
186k
           &safe_cache_value,
826
186k
           error ) != 1 )
827
0
      {
828
0
        libcerror_error_set(
829
0
         error,
830
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
831
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
832
0
         "%s: unable to create cache value.",
833
0
         function );
834
835
0
        goto on_error;
836
0
      }
837
186k
      if( libfcache_cache_value_set_cache_index(
838
186k
           safe_cache_value,
839
186k
           cache_entry_index,
840
186k
           error ) != 1 )
841
0
      {
842
0
        libcerror_error_set(
843
0
         error,
844
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
845
0
         LIBCERROR_RUNTIME_ERROR_SET_FAILED,
846
0
         "%s: unable to set cache index in cache value.",
847
0
         function );
848
849
0
        goto on_error;
850
0
      }
851
186k
      if( libcdata_array_set_entry_by_index(
852
186k
           internal_cache->entries_array,
853
186k
           cache_entry_index,
854
186k
           (intptr_t *) safe_cache_value,
855
186k
           error ) != 1 )
856
0
      {
857
0
        libcerror_error_set(
858
0
         error,
859
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
860
0
         LIBCERROR_RUNTIME_ERROR_SET_FAILED,
861
0
         "%s: unable to set cache value: %d in entries array.",
862
0
         function,
863
0
         cache_entry_index );
864
865
0
        goto on_error;
866
0
      }
867
186k
      if( libcdata_list_prepend_value(
868
186k
           internal_cache->entries_list,
869
186k
           (intptr_t *) safe_cache_value,
870
186k
           error ) != 1 )
871
0
      {
872
0
        libcdata_array_set_entry_by_index(
873
0
         internal_cache->entries_array,
874
0
         cache_entry_index,
875
0
         NULL,
876
0
         NULL );
877
878
0
        libcerror_error_set(
879
0
         error,
880
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
881
0
         LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
882
0
         "%s: unable to prepend cache value: %d to entries list.",
883
0
         function,
884
0
         cache_entry_index );
885
886
0
        goto on_error;
887
0
      }
888
186k
      cache_value      = safe_cache_value;
889
186k
      safe_cache_value = NULL;
890
186k
    }
891
186k
  }
892
1.99M
  else
893
1.99M
  {
894
1.99M
    if( libcdata_list_get_last_element(
895
1.99M
         internal_cache->entries_list,
896
1.99M
         &list_element,
897
1.99M
         error ) != 1 )
898
0
    {
899
0
      libcerror_error_set(
900
0
       error,
901
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
902
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
903
0
       "%s: unable to retrieve last list element.",
904
0
       function );
905
906
0
      goto on_error;
907
0
    }
908
1.99M
    if( libcdata_list_element_get_value(
909
1.99M
         list_element,
910
1.99M
         (intptr_t **) &cache_value,
911
1.99M
         error ) != 1 )
912
0
    {
913
0
      libcerror_error_set(
914
0
       error,
915
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
916
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
917
0
       "%s: unable to retrieve cache value from list element.",
918
0
       function );
919
920
0
      goto on_error;
921
0
    }
922
1.99M
    if( libfcache_cache_value_get_cache_index(
923
1.99M
         cache_value,
924
1.99M
         &cache_entry_index,
925
1.99M
         error ) != 1 )
926
0
    {
927
0
      libcerror_error_set(
928
0
       error,
929
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
930
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
931
0
       "%s: unable to retrieve cache index from cache value.",
932
0
       function );
933
934
0
      goto on_error;
935
0
    }
936
1.99M
    if( libcdata_array_get_entry_by_index(
937
1.99M
         internal_cache->entries_array,
938
1.99M
         cache_entry_index,
939
1.99M
         (intptr_t **) &cache_value,
940
1.99M
         error ) != 1 )
941
0
    {
942
0
      libcerror_error_set(
943
0
       error,
944
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
945
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
946
0
       "%s: unable to retrieve cache value: %d from entries array.",
947
0
       function,
948
0
       cache_entry_index );
949
950
0
      goto on_error;
951
0
    }
952
1.99M
  }
953
2.17M
  if( libfcache_cache_value_set_identifier(
954
2.17M
       cache_value,
955
2.17M
       file_index,
956
2.17M
       offset,
957
2.17M
       timestamp,
958
2.17M
       error ) != 1 )
959
0
  {
960
0
    libcerror_error_set(
961
0
     error,
962
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
963
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
964
0
     "%s: unable to set identifier in cache value.",
965
0
     function );
966
967
0
    goto on_error;
968
0
  }
969
2.17M
  if( libfcache_cache_value_set_value(
970
2.17M
       cache_value,
971
2.17M
       value,
972
2.17M
       value_free_function,
973
2.17M
       flags,
974
2.17M
       error ) != 1 )
975
0
  {
976
0
    libcerror_error_set(
977
0
     error,
978
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
979
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
980
0
     "%s: unable to set value in cache value.",
981
0
     function );
982
983
0
    goto on_error;
984
0
  }
985
2.17M
  return( 1 );
986
987
0
on_error:
988
0
  if( safe_cache_value != NULL )
989
0
  {
990
0
    libfcache_cache_value_free(
991
0
     &safe_cache_value,
992
0
     NULL );
993
0
  }
994
0
  return( -1 );
995
2.17M
}
996
997
/* Sets the cache value for the specific index
998
 * Returns 1 if successful or -1 on error
999
 */
1000
int libfcache_cache_set_value_by_index(
1001
     libfcache_cache_t *cache,
1002
     int cache_entry_index,
1003
     int file_index,
1004
     off64_t offset,
1005
     int64_t timestamp,
1006
     intptr_t *value,
1007
     int (*value_free_function)(
1008
            intptr_t **value,
1009
            libcerror_error_t **error ),
1010
     uint8_t flags,
1011
     libcerror_error_t **error )
1012
32.0k
{
1013
32.0k
  libfcache_cache_value_t *cache_value       = NULL;
1014
32.0k
  libfcache_cache_value_t *safe_cache_value  = NULL;
1015
32.0k
  libfcache_internal_cache_t *internal_cache = NULL;
1016
32.0k
  static char *function                      = "libfcache_cache_set_value_by_index";
1017
1018
32.0k
  if( cache == NULL )
1019
0
  {
1020
0
    libcerror_error_set(
1021
0
     error,
1022
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1023
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1024
0
     "%s: invalid cache.",
1025
0
     function );
1026
1027
0
    return( -1 );
1028
0
  }
1029
32.0k
  internal_cache = (libfcache_internal_cache_t *) cache;
1030
1031
32.0k
  if( libcdata_array_get_entry_by_index(
1032
32.0k
       internal_cache->entries_array,
1033
32.0k
       cache_entry_index,
1034
32.0k
       (intptr_t **) &cache_value,
1035
32.0k
       error ) != 1 )
1036
0
  {
1037
0
    libcerror_error_set(
1038
0
     error,
1039
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1040
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1041
0
     "%s: unable to retrieve cache value: %d from entries array.",
1042
0
     function,
1043
0
     cache_entry_index );
1044
1045
0
    goto on_error;
1046
0
  }
1047
32.0k
  if( cache_value == NULL )
1048
25.8k
  {
1049
25.8k
    if( libfcache_cache_value_initialize(
1050
25.8k
         &safe_cache_value,
1051
25.8k
         error ) != 1 )
1052
0
    {
1053
0
      libcerror_error_set(
1054
0
       error,
1055
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1056
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1057
0
       "%s: unable to create cache value.",
1058
0
       function );
1059
1060
0
      goto on_error;
1061
0
    }
1062
25.8k
    if( libfcache_cache_value_set_cache_index(
1063
25.8k
         safe_cache_value,
1064
25.8k
         cache_entry_index,
1065
25.8k
         error ) != 1 )
1066
0
    {
1067
0
      libcerror_error_set(
1068
0
       error,
1069
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1070
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1071
0
       "%s: unable to set cache index in cache value.",
1072
0
       function );
1073
1074
0
      goto on_error;
1075
0
    }
1076
25.8k
    if( libcdata_array_set_entry_by_index(
1077
25.8k
         internal_cache->entries_array,
1078
25.8k
         cache_entry_index,
1079
25.8k
         (intptr_t *) safe_cache_value,
1080
25.8k
         error ) != 1 )
1081
0
    {
1082
0
      libcerror_error_set(
1083
0
       error,
1084
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1085
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1086
0
       "%s: unable to set cache value: %d in entries array.",
1087
0
       function,
1088
0
       cache_entry_index );
1089
1090
0
      goto on_error;
1091
0
    }
1092
25.8k
    if( libcdata_list_prepend_value(
1093
25.8k
         internal_cache->entries_list,
1094
25.8k
         (intptr_t *) safe_cache_value,
1095
25.8k
         error ) != 1 )
1096
0
    {
1097
0
      libcdata_array_set_entry_by_index(
1098
0
       internal_cache->entries_array,
1099
0
       cache_entry_index,
1100
0
       NULL,
1101
0
       NULL );
1102
1103
0
      libcerror_error_set(
1104
0
       error,
1105
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1106
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1107
0
       "%s: unable to prepend cache value: %d to entries list.",
1108
0
       function,
1109
0
       cache_entry_index );
1110
1111
0
      goto on_error;
1112
0
    }
1113
25.8k
    cache_value      = safe_cache_value;
1114
25.8k
    safe_cache_value = NULL;
1115
25.8k
  }
1116
32.0k
  if( libfcache_cache_value_set_value(
1117
32.0k
       cache_value,
1118
32.0k
       value,
1119
32.0k
       value_free_function,
1120
32.0k
       flags,
1121
32.0k
       error ) != 1 )
1122
0
  {
1123
0
    libcerror_error_set(
1124
0
     error,
1125
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1126
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1127
0
     "%s: unable to set value in cache value.",
1128
0
     function );
1129
1130
0
    goto on_error;
1131
0
  }
1132
32.0k
  if( libfcache_cache_value_set_identifier(
1133
32.0k
       cache_value,
1134
32.0k
       file_index,
1135
32.0k
       offset,
1136
32.0k
       timestamp,
1137
32.0k
       error ) != 1 )
1138
0
  {
1139
0
    libcerror_error_set(
1140
0
     error,
1141
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1142
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1143
0
     "%s: unable to set identifier in cache value.",
1144
0
     function );
1145
1146
0
    goto on_error;
1147
0
  }
1148
32.0k
  return( 1 );
1149
1150
0
on_error:
1151
0
  if( safe_cache_value != NULL )
1152
0
  {
1153
0
    libfcache_cache_value_free(
1154
0
     &safe_cache_value,
1155
     NULL );
1156
0
  }
1157
0
  return( -1 );
1158
32.0k
}
1159