/src/libqcow/libbfio/libbfio_file.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * File 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 "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  | 0  | { | 
42  | 0  |   libbfio_file_io_handle_t *file_io_handle = NULL;  | 
43  | 0  |   static char *function                    = "libbfio_file_initialize";  | 
44  |  | 
  | 
45  | 0  |   if( handle == 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 handle.",  | 
52  | 0  |      function );  | 
53  |  | 
  | 
54  | 0  |     return( -1 );  | 
55  | 0  |   }  | 
56  | 0  |   if( *handle != 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 handle value already set.",  | 
63  | 0  |      function );  | 
64  |  | 
  | 
65  | 0  |     return( -1 );  | 
66  | 0  |   }  | 
67  | 0  |   if( libbfio_file_io_handle_initialize(  | 
68  | 0  |        &file_io_handle,  | 
69  | 0  |        error ) != 1 )  | 
70  | 0  |   { | 
71  | 0  |     libcerror_error_set(  | 
72  | 0  |      error,  | 
73  | 0  |      LIBCERROR_ERROR_DOMAIN_RUNTIME,  | 
74  | 0  |      LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,  | 
75  | 0  |      "%s: unable to create file IO handle.",  | 
76  | 0  |      function );  | 
77  |  | 
  | 
78  | 0  |     goto on_error;  | 
79  | 0  |   }  | 
80  | 0  |   if( libbfio_handle_initialize(  | 
81  | 0  |        handle,  | 
82  | 0  |        (intptr_t *) file_io_handle,  | 
83  | 0  |        (int (*)(intptr_t **, libcerror_error_t **)) libbfio_file_io_handle_free,  | 
84  | 0  |        (int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) libbfio_file_io_handle_clone,  | 
85  | 0  |        (int (*)(intptr_t *, int, libcerror_error_t **)) libbfio_file_io_handle_open,  | 
86  | 0  |        (int (*)(intptr_t *, libcerror_error_t **)) libbfio_file_io_handle_close,  | 
87  | 0  |        (ssize_t (*)(intptr_t *, uint8_t *, size_t, libcerror_error_t **)) libbfio_file_io_handle_read_buffer,  | 
88  | 0  |        (ssize_t (*)(intptr_t *, const uint8_t *, size_t, libcerror_error_t **)) libbfio_file_io_handle_write_buffer,  | 
89  | 0  |        (off64_t (*)(intptr_t *, off64_t, int, libcerror_error_t **)) libbfio_file_io_handle_seek_offset,  | 
90  | 0  |        (int (*)(intptr_t *, libcerror_error_t **)) libbfio_file_io_handle_exists,  | 
91  | 0  |        (int (*)(intptr_t *, libcerror_error_t **)) libbfio_file_io_handle_is_open,  | 
92  | 0  |        (int (*)(intptr_t *, size64_t *, libcerror_error_t **)) libbfio_file_io_handle_get_size,  | 
93  | 0  |        LIBBFIO_FLAG_IO_HANDLE_MANAGED | LIBBFIO_FLAG_IO_HANDLE_CLONE_BY_FUNCTION,  | 
94  | 0  |        error ) != 1 )  | 
95  | 0  |   { | 
96  | 0  |     libcerror_error_set(  | 
97  | 0  |      error,  | 
98  | 0  |      LIBCERROR_ERROR_DOMAIN_RUNTIME,  | 
99  | 0  |      LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,  | 
100  | 0  |      "%s: unable to create handle.",  | 
101  | 0  |      function );  | 
102  |  | 
  | 
103  | 0  |     goto on_error;  | 
104  | 0  |   }  | 
105  | 0  |   return( 1 );  | 
106  |  |  | 
107  | 0  | on_error:  | 
108  | 0  |   if( file_io_handle != NULL )  | 
109  | 0  |   { | 
110  | 0  |     libbfio_file_io_handle_free(  | 
111  | 0  |      &file_io_handle,  | 
112  | 0  |      NULL );  | 
113  | 0  |   }  | 
114  | 0  |   return( -1 );  | 
115  | 0  | }  | 
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  | 0  | { | 
126  | 0  |   libbfio_internal_handle_t *internal_handle = NULL;  | 
127  | 0  |   static char *function                      = "libbfio_file_get_name_size";  | 
128  |  | 
  | 
129  | 0  |   if( handle == NULL )  | 
130  | 0  |   { | 
131  | 0  |     libcerror_error_set(  | 
132  | 0  |      error,  | 
133  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
134  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
135  | 0  |      "%s: invalid handle.",  | 
136  | 0  |      function );  | 
137  |  | 
  | 
138  | 0  |     return( -1 );  | 
139  | 0  |   }  | 
140  | 0  |   internal_handle = (libbfio_internal_handle_t *) handle;  | 
141  |  | 
  | 
142  | 0  |   if( libbfio_file_io_handle_get_name_size(  | 
143  | 0  |        (libbfio_file_io_handle_t *) internal_handle->io_handle,  | 
144  | 0  |        name_size,  | 
145  | 0  |        error ) != 1 )  | 
146  | 0  |   { | 
147  | 0  |     libcerror_error_set(  | 
148  | 0  |      error,  | 
149  | 0  |      LIBCERROR_ERROR_DOMAIN_RUNTIME,  | 
150  | 0  |      LIBCERROR_RUNTIME_ERROR_GET_FAILED,  | 
151  | 0  |      "%s: unable to retrieve name size.",  | 
152  | 0  |      function );  | 
153  |  | 
  | 
154  | 0  |     return( -1 );  | 
155  | 0  |   }  | 
156  | 0  |   return( 1 );  | 
157  | 0  | }  | 
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  | 0  | { | 
169  | 0  |   libbfio_internal_handle_t *internal_handle = NULL;  | 
170  | 0  |   static char *function                      = "libbfio_file_get_name";  | 
171  |  | 
  | 
172  | 0  |   if( handle == NULL )  | 
173  | 0  |   { | 
174  | 0  |     libcerror_error_set(  | 
175  | 0  |      error,  | 
176  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
177  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
178  | 0  |      "%s: invalid handle.",  | 
179  | 0  |      function );  | 
180  |  | 
  | 
181  | 0  |     return( -1 );  | 
182  | 0  |   }  | 
183  | 0  |   internal_handle = (libbfio_internal_handle_t *) handle;  | 
184  |  | 
  | 
185  | 0  |   if( libbfio_file_io_handle_get_name(  | 
186  | 0  |        (libbfio_file_io_handle_t *) internal_handle->io_handle,  | 
187  | 0  |        name,  | 
188  | 0  |        name_size,  | 
189  | 0  |        error ) != 1 )  | 
190  | 0  |   { | 
191  | 0  |     libcerror_error_set(  | 
192  | 0  |      error,  | 
193  | 0  |      LIBCERROR_ERROR_DOMAIN_RUNTIME,  | 
194  | 0  |      LIBCERROR_RUNTIME_ERROR_GET_FAILED,  | 
195  | 0  |      "%s: unable to retrieve name.",  | 
196  | 0  |      function );  | 
197  |  | 
  | 
198  | 0  |     return( -1 );  | 
199  | 0  |   }  | 
200  | 0  |   return( 1 );  | 
201  | 0  | }  | 
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  | 0  | { | 
212  | 0  |   libbfio_internal_handle_t *internal_handle = NULL;  | 
213  | 0  |   char *full_name                            = NULL;  | 
214  | 0  |   static char *function                      = "libbfio_file_set_name";  | 
215  | 0  |   size_t full_name_size                      = 0;  | 
216  |  | 
  | 
217  | 0  |   if( handle == NULL )  | 
218  | 0  |   { | 
219  | 0  |     libcerror_error_set(  | 
220  | 0  |      error,  | 
221  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
222  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
223  | 0  |      "%s: invalid handle.",  | 
224  | 0  |      function );  | 
225  |  | 
  | 
226  | 0  |     return( -1 );  | 
227  | 0  |   }  | 
228  | 0  |   internal_handle = (libbfio_internal_handle_t *) handle;  | 
229  |  | 
  | 
230  | 0  |   if( libcpath_path_get_full_path(  | 
231  | 0  |        name,  | 
232  | 0  |        name_length,  | 
233  | 0  |        &full_name,  | 
234  | 0  |        &full_name_size,  | 
235  | 0  |        error ) != 1 )  | 
236  | 0  |   { | 
237  | 0  |     libcerror_error_set(  | 
238  | 0  |      error,  | 
239  | 0  |      LIBCERROR_ERROR_DOMAIN_RUNTIME,  | 
240  | 0  |      LIBCERROR_RUNTIME_ERROR_SET_FAILED,  | 
241  | 0  |      "%s: unable to determine full path.",  | 
242  | 0  |      function );  | 
243  |  | 
  | 
244  | 0  |     goto on_error;  | 
245  | 0  |   }  | 
246  | 0  |   if( libbfio_file_io_handle_set_name(  | 
247  | 0  |        (libbfio_file_io_handle_t *) internal_handle->io_handle,  | 
248  | 0  |        full_name,  | 
249  | 0  |        full_name_size - 1,  | 
250  | 0  |        error ) != 1 )  | 
251  | 0  |   { | 
252  | 0  |     libcerror_error_set(  | 
253  | 0  |      error,  | 
254  | 0  |      LIBCERROR_ERROR_DOMAIN_RUNTIME,  | 
255  | 0  |      LIBCERROR_RUNTIME_ERROR_SET_FAILED,  | 
256  | 0  |      "%s: unable to set name.",  | 
257  | 0  |      function );  | 
258  |  | 
  | 
259  | 0  |     goto on_error;  | 
260  | 0  |   }  | 
261  | 0  |   if( ( full_name != NULL )  | 
262  | 0  |    && ( full_name != name ) )  | 
263  | 0  |   { | 
264  | 0  |     memory_free(  | 
265  | 0  |      full_name );  | 
266  | 0  |   }  | 
267  | 0  |   return( 1 );  | 
268  |  |  | 
269  | 0  | on_error:  | 
270  | 0  |   if( ( full_name != NULL )  | 
271  | 0  |    && ( full_name != name ) )  | 
272  | 0  |   { | 
273  | 0  |     memory_free(  | 
274  | 0  |      full_name );  | 
275  | 0  |   }  | 
276  | 0  |   return( -1 );  | 
277  | 0  | }  | 
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  |  |  |