Debian Codegraph @

libbfio/libbfio_file_pool.c

raw symbols SCIP
1 /*
2 * File pool functions
3 *
4 * Copyright (C) 2009-2024, Joachim Metz <[email protected]>
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 <narrow_string.h>
24 #include <types.h>
25 #include <wide_string.h>
26
27 #include "libbfio_definitions.h"
28 #include "libbfio_file.h"
29 #include "libbfio_file_pool.h"
30 #include "libbfio_handle.h"
31 #include "libbfio_libcerror.h"
32 #include "libbfio_pool.h"
33 #include "libbfio_types.h"
34
35 /* Opens a handle in the pool
36 * Returns 1 if successful or -1 on error
37 */
38 int libbfio_file_pool_open(
39 libbfio_pool_t *pool,
40 int entry,
41 const char *name,
42 int access_flags,
43 libcerror_error_t **error )
44 {
45 libbfio_handle_t *file_io_handle = NULL;
46 static char *function = "libbfio_file_pool_open";
47 size_t name_length = 0;
48
49 if( pool == NULL )
50 {
51 libcerror_error_set(
52 error,
53 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
54 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
55 "%s: invalid pool.",
56 function );
57
58 return( -1 );
59 }
60 if( name == NULL )
61 {
62 libcerror_error_set(
63 error,
64 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
65 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
66 "%s: invalid name.",
67 function );
68
69 return( -1 );
70 }
71 if( libbfio_file_initialize(
72 &file_io_handle,
73 error ) != 1 )
74 {
75 libcerror_error_set(
76 error,
77 LIBCERROR_ERROR_DOMAIN_RUNTIME,
78 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
79 "%s: unable to create file IO handle.",
80 function );
81
82 goto on_error;
83 }
84 name_length = narrow_string_length(
85 name );
86
87 if( libbfio_file_set_name(
88 file_io_handle,
89 name,
90 name_length + 1,
91 error ) != 1 )
92 {
93 libcerror_error_set(
94 error,
95 LIBCERROR_ERROR_DOMAIN_RUNTIME,
96 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
97 "%s: unable to set name in file IO handle.",
98 function );
99
100 goto on_error;
101 }
102 if( libbfio_handle_open(
103 file_io_handle,
104 access_flags,
105 error ) != 1 )
106 {
107 libcerror_error_set(
108 error,
109 LIBCERROR_ERROR_DOMAIN_IO,
110 LIBCERROR_IO_ERROR_OPEN_FAILED,
111 "%s: unable to open file IO handle.",
112 function );
113
114 goto on_error;
115 }
116 if( libbfio_pool_set_handle(
117 pool,
118 entry,
119 file_io_handle,
120 access_flags,
121 error ) != 1 )
122 {
123 libcerror_error_set(
124 error,
125 LIBCERROR_ERROR_DOMAIN_RUNTIME,
126 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
127 "%s: unable to set file IO handle: %d in pool.",
128 function,
129 entry );
130
131 goto on_error;
132 }
133 return( 1 );
134
135 on_error:
136 if( file_io_handle != NULL )
137 {
138 libbfio_handle_free(
139 &file_io_handle,
140 NULL );
141 }
142 return( -1 );
143 }
144
145 #if defined( HAVE_WIDE_CHARACTER_TYPE )
146
147 /* Opens a handle in the pool
148 * Returns 1 if successful or -1 on error
149 */
150 int libbfio_file_pool_open_wide(
151 libbfio_pool_t *pool,
152 int entry,
153 const wchar_t *name,
154 int access_flags,
155 libcerror_error_t **error )
156 {
157 libbfio_handle_t *file_io_handle = NULL;
158 static char *function = "libbfio_file_pool_open_wide";
159 size_t name_length = 0;
160
161 if( pool == NULL )
162 {
163 libcerror_error_set(
164 error,
165 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
166 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
167 "%s: invalid pool.",
168 function );
169
170 return( -1 );
171 }
172 if( name == NULL )
173 {
174 libcerror_error_set(
175 error,
176 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
177 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
178 "%s: invalid name.",
179 function );
180
181 return( -1 );
182 }
183 if( libbfio_file_initialize(
184 &file_io_handle,
185 error ) != 1 )
186 {
187 libcerror_error_set(
188 error,
189 LIBCERROR_ERROR_DOMAIN_RUNTIME,
190 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
191 "%s: unable to create file IO handle.",
192 function );
193
194 goto on_error;
195 }
196 name_length = wide_string_length(
197 name );
198
199 if( libbfio_file_set_name_wide(
200 file_io_handle,
201 name,
202 name_length + 1,
203 error ) != 1 )
204 {
205 libcerror_error_set(
206 error,
207 LIBCERROR_ERROR_DOMAIN_RUNTIME,
208 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
209 "%s: unable to set name in file IO handle.",
210 function );
211
212 goto on_error;
213 }
214 if( libbfio_handle_open(
215 file_io_handle,
216 access_flags,
217 error ) != 1 )
218 {
219 libcerror_error_set(
220 error,
221 LIBCERROR_ERROR_DOMAIN_IO,
222 LIBCERROR_IO_ERROR_OPEN_FAILED,
223 "%s: unable to open file IO handle.",
224 function );
225
226 goto on_error;
227 }
228 if( libbfio_pool_set_handle(
229 pool,
230 entry,
231 file_io_handle,
232 access_flags,
233 error ) != 1 )
234 {
235 libcerror_error_set(
236 error,
237 LIBCERROR_ERROR_DOMAIN_RUNTIME,
238 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
239 "%s: unable to set file IO handle: %d in pool.",
240 function,
241 entry );
242
243 goto on_error;
244 }
245 return( 1 );
246
247 on_error:
248 if( file_io_handle != NULL )
249 {
250 libbfio_handle_free(
251 &file_io_handle,
252 NULL );
253 }
254 return( -1 );
255 }
256
257 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
258
259 /* Appends file handles to the pool for the corresponding filenames
260 * Returns 1 if successful or -1 on error
261 */
262 int libbfio_file_pool_append_handles_for_names(
263 libbfio_pool_t *pool,
264 char * const names[],
265 int number_of_names,
266 int access_flags,
267 libcerror_error_t **error )
268 {
269 libbfio_handle_t *file_io_handle = NULL;
270 static char *function = "libbfio_file_pool_append_handles_for_names";
271 size_t name_length = 0;
272 int pool_entry = 0;
273 int name_iterator = 0;
274
275 if( pool == NULL )
276 {
277 libcerror_error_set(
278 error,
279 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
280 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
281 "%s: invalid pool.",
282 function );
283
284 return( -1 );
285 }
286 if( names == NULL )
287 {
288 libcerror_error_set(
289 error,
290 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
291 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
292 "%s: invalid names.",
293 function );
294
295 return( -1 );
296 }
297 if( number_of_names <= 0 )
298 {
299 libcerror_error_set(
300 error,
301 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
302 LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
303 "%s: invalid number of names zero or less.",
304 function );
305
306 return( -1 );
307 }
308 for( name_iterator = 0;
309 name_iterator < number_of_names;
310 name_iterator++ )
311 {
312 name_length = narrow_string_length(
313 names[ name_iterator ] );
314
315 if( libbfio_file_initialize(
316 &file_io_handle,
317 error ) != 1 )
318 {
319 libcerror_error_set(
320 error,
321 LIBCERROR_ERROR_DOMAIN_RUNTIME,
322 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
323 "%s: unable to create file IO handle: %d.",
324 function,
325 name_iterator );
326
327 return( -1 );
328 }
329 if( libbfio_file_set_name(
330 file_io_handle,
331 names[ name_iterator ],
332 name_length + 1,
333 error ) != 1 )
334 {
335 libcerror_error_set(
336 error,
337 LIBCERROR_ERROR_DOMAIN_RUNTIME,
338 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
339 "%s: unable to set name in file IO handle: %d.",
340 function,
341 name_iterator );
342
343 libbfio_handle_free(
344 &file_io_handle,
345 NULL );
346
347 return( -1 );
348 }
349 if( libbfio_pool_append_handle(
350 pool,
351 &pool_entry,
352 file_io_handle,
353 access_flags,
354 error ) != 1 )
355 {
356 libcerror_error_set(
357 error,
358 LIBCERROR_ERROR_DOMAIN_RUNTIME,
359 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
360 "%s: unable to append file IO handle: %d to pool.",
361 function,
362 name_iterator );
363
364 libbfio_handle_free(
365 &file_io_handle,
366 NULL );
367
368 return( -1 );
369 }
370 file_io_handle = NULL;
371 }
372 return( 1 );
373 }
374
375 #if defined( HAVE_WIDE_CHARACTER_TYPE )
376
377 /* Appends file handles to the pool for the corresponding filenames
378 * Returns 1 if successful or -1 on error
379 */
380 int libbfio_file_pool_append_handles_for_names_wide(
381 libbfio_pool_t *pool,
382 wchar_t * const names[],
383 int number_of_names,
384 int access_flags,
385 libcerror_error_t **error )
386 {
387 libbfio_handle_t *file_io_handle = NULL;
388 static char *function = "libbfio_file_pool_append_handles_for_names_wide";
389 size_t name_length = 0;
390 int pool_entry = 0;
391 int name_iterator = 0;
392
393 if( pool == NULL )
394 {
395 libcerror_error_set(
396 error,
397 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
398 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
399 "%s: invalid pool.",
400 function );
401
402 return( -1 );
403 }
404 if( names == NULL )
405 {
406 libcerror_error_set(
407 error,
408 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
409 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
410 "%s: invalid names.",
411 function );
412
413 return( -1 );
414 }
415 if( number_of_names <= 0 )
416 {
417 libcerror_error_set(
418 error,
419 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
420 LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
421 "%s: invalid number of names zero or less.",
422 function );
423
424 return( -1 );
425 }
426 for( name_iterator = 0;
427 name_iterator < number_of_names;
428 name_iterator++ )
429 {
430 name_length = wide_string_length(
431 names[ name_iterator ] );
432
433 if( libbfio_file_initialize(
434 &file_io_handle,
435 error ) != 1 )
436 {
437 libcerror_error_set(
438 error,
439 LIBCERROR_ERROR_DOMAIN_RUNTIME,
440 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
441 "%s: unable to create file IO handle: %d.",
442 function,
443 name_iterator );
444
445 return( -1 );
446 }
447 if( libbfio_file_set_name_wide(
448 file_io_handle,
449 names[ name_iterator ],
450 name_length + 1,
451 error ) != 1 )
452 {
453 libcerror_error_set(
454 error,
455 LIBCERROR_ERROR_DOMAIN_RUNTIME,
456 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
457 "%s: unable to set name in file IO handle: %d.",
458 function,
459 name_iterator );
460
461 libbfio_handle_free(
462 &file_io_handle,
463 NULL );
464
465 return( -1 );
466 }
467 if( libbfio_pool_append_handle(
468 pool,
469 &pool_entry,
470 file_io_handle,
471 access_flags,
472 error ) != 1 )
473 {
474 libcerror_error_set(
475 error,
476 LIBCERROR_ERROR_DOMAIN_RUNTIME,
477 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
478 "%s: unable to append file IO handle: %d to pool.",
479 function,
480 name_iterator );
481
482 libbfio_handle_free(
483 &file_io_handle,
484 NULL );
485
486 return( -1 );
487 }
488 file_io_handle = NULL;
489 }
490 return( 1 );
491 }
492
493 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
494