Debian Codegraph @

libbfio/libbfio_file.c

raw symbols SCIP
1 /*
2 * File 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 <memory.h>
24 #include <types.h>
25
26 #include "libbfio_definitions.h"
27 #include "libbfio_file.h"
28 #include "libbfio_file_io_handle.h"
29 #include "libbfio_handle.h"
30 #include "libbfio_libcerror.h"
31 #include "libbfio_libcpath.h"
32 #include "libbfio_types.h"
33
34 /* Creates a file handle
35 * Make sure the value handle is referencing, is set to NULL
36 * Returns 1 if successful or -1 on error
37 */
38 int libbfio_file_initialize(
39 libbfio_handle_t **handle,
40 libcerror_error_t **error )
41 {
42 libbfio_file_io_handle_t *file_io_handle = NULL;
43 static char *function = "libbfio_file_initialize";
44
45 if( handle == NULL )
46 {
47 libcerror_error_set(
48 error,
49 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
50 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
51 "%s: invalid handle.",
52 function );
53
54 return( -1 );
55 }
56 if( *handle != NULL )
57 {
58 libcerror_error_set(
59 error,
60 LIBCERROR_ERROR_DOMAIN_RUNTIME,
61 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
62 "%s: invalid handle value already set.",
63 function );
64
65 return( -1 );
66 }
67 if( libbfio_file_io_handle_initialize(
68 &file_io_handle,
69 error ) != 1 )
70 {
71 libcerror_error_set(
72 error,
73 LIBCERROR_ERROR_DOMAIN_RUNTIME,
74 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
75 "%s: unable to create file IO handle.",
76 function );
77
78 goto on_error;
79 }
80 if( libbfio_handle_initialize(
81 handle,
82 (intptr_t *) file_io_handle,
83 (int (*)(intptr_t **, libcerror_error_t **)) libbfio_file_io_handle_free,
84 (int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) libbfio_file_io_handle_clone,
85 (int (*)(intptr_t *, int, libcerror_error_t **)) libbfio_file_io_handle_open,
86 (int (*)(intptr_t *, libcerror_error_t **)) libbfio_file_io_handle_close,
87 (ssize_t (*)(intptr_t *, uint8_t *, size_t, libcerror_error_t **)) libbfio_file_io_handle_read_buffer,
88 (ssize_t (*)(intptr_t *, const uint8_t *, size_t, libcerror_error_t **)) libbfio_file_io_handle_write_buffer,
89 (off64_t (*)(intptr_t *, off64_t, int, libcerror_error_t **)) libbfio_file_io_handle_seek_offset,
90 (int (*)(intptr_t *, libcerror_error_t **)) libbfio_file_io_handle_exists,
91 (int (*)(intptr_t *, libcerror_error_t **)) libbfio_file_io_handle_is_open,
92 (int (*)(intptr_t *, size64_t *, libcerror_error_t **)) libbfio_file_io_handle_get_size,
93 LIBBFIO_FLAG_IO_HANDLE_MANAGED | LIBBFIO_FLAG_IO_HANDLE_CLONE_BY_FUNCTION,
94 error ) != 1 )
95 {
96 libcerror_error_set(
97 error,
98 LIBCERROR_ERROR_DOMAIN_RUNTIME,
99 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
100 "%s: unable to create handle.",
101 function );
102
103 goto on_error;
104 }
105 return( 1 );
106
107 on_error:
108 if( file_io_handle != NULL )
109 {
110 libbfio_file_io_handle_free(
111 &file_io_handle,
112 NULL );
113 }
114 return( -1 );
115 }
116
117 /* Retrieves the name size of the file handle
118 * The name size includes the end of string character
119 * Returns 1 if succesful or -1 on error
120 */
121 int libbfio_file_get_name_size(
122 libbfio_handle_t *handle,
123 size_t *name_size,
124 libcerror_error_t **error )
125 {
126 libbfio_internal_handle_t *internal_handle = NULL;
127 static char *function = "libbfio_file_get_name_size";
128
129 if( handle == NULL )
130 {
131 libcerror_error_set(
132 error,
133 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
134 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
135 "%s: invalid handle.",
136 function );
137
138 return( -1 );
139 }
140 internal_handle = (libbfio_internal_handle_t *) handle;
141
142 if( libbfio_file_io_handle_get_name_size(
143 (libbfio_file_io_handle_t *) internal_handle->io_handle,
144 name_size,
145 error ) != 1 )
146 {
147 libcerror_error_set(
148 error,
149 LIBCERROR_ERROR_DOMAIN_RUNTIME,
150 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
151 "%s: unable to retrieve name size.",
152 function );
153
154 return( -1 );
155 }
156 return( 1 );
157 }
158
159 /* Retrieves the name of the file handle
160 * The name size should include the end of string character
161 * Returns 1 if succesful or -1 on error
162 */
163 int libbfio_file_get_name(
164 libbfio_handle_t *handle,
165 char *name,
166 size_t name_size,
167 libcerror_error_t **error )
168 {
169 libbfio_internal_handle_t *internal_handle = NULL;
170 static char *function = "libbfio_file_get_name";
171
172 if( handle == NULL )
173 {
174 libcerror_error_set(
175 error,
176 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
177 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
178 "%s: invalid handle.",
179 function );
180
181 return( -1 );
182 }
183 internal_handle = (libbfio_internal_handle_t *) handle;
184
185 if( libbfio_file_io_handle_get_name(
186 (libbfio_file_io_handle_t *) internal_handle->io_handle,
187 name,
188 name_size,
189 error ) != 1 )
190 {
191 libcerror_error_set(
192 error,
193 LIBCERROR_ERROR_DOMAIN_RUNTIME,
194 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
195 "%s: unable to retrieve name.",
196 function );
197
198 return( -1 );
199 }
200 return( 1 );
201 }
202
203 /* Sets the name for the file handle
204 * Returns 1 if succesful or -1 on error
205 */
206 int libbfio_file_set_name(
207 libbfio_handle_t *handle,
208 const char *name,
209 size_t name_length,
210 libcerror_error_t **error )
211 {
212 libbfio_internal_handle_t *internal_handle = NULL;
213 char *full_name = NULL;
214 static char *function = "libbfio_file_set_name";
215 size_t full_name_size = 0;
216
217 if( handle == NULL )
218 {
219 libcerror_error_set(
220 error,
221 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
222 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
223 "%s: invalid handle.",
224 function );
225
226 return( -1 );
227 }
228 internal_handle = (libbfio_internal_handle_t *) handle;
229
230 if( libcpath_path_get_full_path(
231 name,
232 name_length,
233 &full_name,
234 &full_name_size,
235 error ) != 1 )
236 {
237 libcerror_error_set(
238 error,
239 LIBCERROR_ERROR_DOMAIN_RUNTIME,
240 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
241 "%s: unable to determine full path.",
242 function );
243
244 goto on_error;
245 }
246 if( libbfio_file_io_handle_set_name(
247 (libbfio_file_io_handle_t *) internal_handle->io_handle,
248 full_name,
249 full_name_size - 1,
250 error ) != 1 )
251 {
252 libcerror_error_set(
253 error,
254 LIBCERROR_ERROR_DOMAIN_RUNTIME,
255 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
256 "%s: unable to set name.",
257 function );
258
259 goto on_error;
260 }
261 if( ( full_name != NULL )
262 && ( full_name != name ) )
263 {
264 memory_free(
265 full_name );
266 }
267 return( 1 );
268
269 on_error:
270 if( ( full_name != NULL )
271 && ( full_name != name ) )
272 {
273 memory_free(
274 full_name );
275 }
276 return( -1 );
277 }
278
279 #if defined( HAVE_WIDE_CHARACTER_TYPE )
280
281 /* Retrieves the name size of the file handle
282 * The name size includes the end of string character
283 * Returns 1 if succesful or -1 on error
284 */
285 int libbfio_file_get_name_size_wide(
286 libbfio_handle_t *handle,
287 size_t *name_size,
288 libcerror_error_t **error )
289 {
290 libbfio_internal_handle_t *internal_handle = NULL;
291 static char *function = "libbfio_file_get_name_size_wide";
292
293 if( handle == NULL )
294 {
295 libcerror_error_set(
296 error,
297 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
298 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
299 "%s: invalid handle.",
300 function );
301
302 return( -1 );
303 }
304 internal_handle = (libbfio_internal_handle_t *) handle;
305
306 if( libbfio_file_io_handle_get_name_size_wide(
307 (libbfio_file_io_handle_t *) internal_handle->io_handle,
308 name_size,
309 error ) != 1 )
310 {
311 libcerror_error_set(
312 error,
313 LIBCERROR_ERROR_DOMAIN_RUNTIME,
314 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
315 "%s: unable to retrieve name size.",
316 function );
317
318 return( -1 );
319 }
320 return( 1 );
321 }
322
323 /* Retrieves the name of the file handle
324 * The name size should include the end of string character
325 * Returns 1 if succesful or -1 on error
326 */
327 int libbfio_file_get_name_wide(
328 libbfio_handle_t *handle,
329 wchar_t *name,
330 size_t name_size,
331 libcerror_error_t **error )
332 {
333 libbfio_internal_handle_t *internal_handle = NULL;
334 static char *function = "libbfio_file_get_name_wide";
335
336 if( handle == NULL )
337 {
338 libcerror_error_set(
339 error,
340 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
341 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
342 "%s: invalid handle.",
343 function );
344
345 return( -1 );
346 }
347 internal_handle = (libbfio_internal_handle_t *) handle;
348
349 if( libbfio_file_io_handle_get_name_wide(
350 (libbfio_file_io_handle_t *) internal_handle->io_handle,
351 name,
352 name_size,
353 error ) != 1 )
354 {
355 libcerror_error_set(
356 error,
357 LIBCERROR_ERROR_DOMAIN_RUNTIME,
358 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
359 "%s: unable to retrieve name.",
360 function );
361
362 return( -1 );
363 }
364 return( 1 );
365 }
366
367 /* Sets the name for the file handle
368 * Returns 1 if succesful or -1 on error
369 */
370 int libbfio_file_set_name_wide(
371 libbfio_handle_t *handle,
372 const wchar_t *name,
373 size_t name_length,
374 libcerror_error_t **error )
375 {
376 libbfio_internal_handle_t *internal_handle = NULL;
377 wchar_t *full_name = NULL;
378 static char *function = "libbfio_file_set_name_wide";
379 size_t full_name_size = 0;
380
381 if( handle == NULL )
382 {
383 libcerror_error_set(
384 error,
385 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
386 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
387 "%s: invalid handle.",
388 function );
389
390 return( -1 );
391 }
392 internal_handle = (libbfio_internal_handle_t *) handle;
393
394 if( libcpath_path_get_full_path_wide(
395 name,
396 name_length,
397 &full_name,
398 &full_name_size,
399 error ) != 1 )
400 {
401 libcerror_error_set(
402 error,
403 LIBCERROR_ERROR_DOMAIN_RUNTIME,
404 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
405 "%s: unable to determine full path.",
406 function );
407
408 goto on_error;
409 }
410 if( libbfio_file_io_handle_set_name_wide(
411 (libbfio_file_io_handle_t *) internal_handle->io_handle,
412 full_name,
413 full_name_size - 1,
414 error ) != 1 )
415 {
416 libcerror_error_set(
417 error,
418 LIBCERROR_ERROR_DOMAIN_RUNTIME,
419 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
420 "%s: unable to set name.",
421 function );
422
423 goto on_error;
424 }
425 if( ( full_name != NULL )
426 && ( full_name != name ) )
427 {
428 memory_free(
429 full_name );
430 }
431 return( 1 );
432
433 on_error:
434 if( ( full_name != NULL )
435 && ( full_name != name ) )
436 {
437 memory_free(
438 full_name );
439 }
440 return( -1 );
441 }
442
443 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
444