/src/libphdi/libcdirectory/libcdirectory_directory.c
Line | Count | Source |
1 | | /* |
2 | | * Directory functions |
3 | | * |
4 | | * Copyright (C) 2008-2025, 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 <narrow_string.h> |
25 | | #include <system_string.h> |
26 | | #include <types.h> |
27 | | #include <wide_string.h> |
28 | | |
29 | | #if defined( HAVE_ERRNO_H ) || defined( WINAPI ) |
30 | | #include <errno.h> |
31 | | #endif |
32 | | |
33 | | #if defined( HAVE_SYS_STAT_H ) |
34 | | #include <sys/stat.h> |
35 | | #endif |
36 | | |
37 | | #if defined( WINAPI ) && !defined( __CYGWIN__ ) |
38 | | #include <direct.h> |
39 | | #endif |
40 | | |
41 | | #if defined( HAVE_DIRENT_H ) |
42 | | #include <dirent.h> |
43 | | #endif |
44 | | |
45 | | #if defined( HAVE_UNISTD_H ) |
46 | | #include <unistd.h> |
47 | | #endif |
48 | | |
49 | | #include "libcdirectory_definitions.h" |
50 | | #include "libcdirectory_directory.h" |
51 | | #include "libcdirectory_directory_entry.h" |
52 | | #include "libcdirectory_libcerror.h" |
53 | | #include "libcdirectory_system_string.h" |
54 | | #include "libcdirectory_types.h" |
55 | | #include "libcdirectory_wide_string.h" |
56 | | |
57 | | /* Creates a directory |
58 | | * Make sure the value directory is referencing, is set to NULL |
59 | | * Returns 1 if successful or -1 on error |
60 | | */ |
61 | | int libcdirectory_directory_initialize( |
62 | | libcdirectory_directory_t **directory, |
63 | | libcerror_error_t **error ) |
64 | 0 | { |
65 | 0 | libcdirectory_internal_directory_t *internal_directory = NULL; |
66 | 0 | static char *function = "libcdirectory_directory_initialize"; |
67 | |
|
68 | 0 | if( directory == NULL ) |
69 | 0 | { |
70 | 0 | libcerror_error_set( |
71 | 0 | error, |
72 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
73 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
74 | 0 | "%s: invalid directory.", |
75 | 0 | function ); |
76 | |
|
77 | 0 | return( -1 ); |
78 | 0 | } |
79 | 0 | if( *directory != NULL ) |
80 | 0 | { |
81 | 0 | libcerror_error_set( |
82 | 0 | error, |
83 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
84 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
85 | 0 | "%s: invalid directory value already set.", |
86 | 0 | function ); |
87 | |
|
88 | 0 | return( -1 ); |
89 | 0 | } |
90 | 0 | internal_directory = memory_allocate_structure( |
91 | 0 | libcdirectory_internal_directory_t ); |
92 | |
|
93 | 0 | if( internal_directory == NULL ) |
94 | 0 | { |
95 | 0 | libcerror_error_set( |
96 | 0 | error, |
97 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
98 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
99 | 0 | "%s: unable to create directory.", |
100 | 0 | function ); |
101 | |
|
102 | 0 | goto on_error; |
103 | 0 | } |
104 | 0 | if( memory_set( |
105 | 0 | internal_directory, |
106 | 0 | 0, |
107 | 0 | sizeof( libcdirectory_internal_directory_t ) ) == NULL ) |
108 | 0 | { |
109 | 0 | libcerror_error_set( |
110 | 0 | error, |
111 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
112 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
113 | 0 | "%s: unable to clear directory.", |
114 | 0 | function ); |
115 | |
|
116 | 0 | goto on_error; |
117 | 0 | } |
118 | | #if defined( WINAPI ) |
119 | | internal_directory->handle = INVALID_HANDLE_VALUE; |
120 | | #endif |
121 | | |
122 | 0 | *directory = (libcdirectory_directory_t *) internal_directory; |
123 | |
|
124 | 0 | return( 1 ); |
125 | | |
126 | 0 | on_error: |
127 | 0 | if( internal_directory != NULL ) |
128 | 0 | { |
129 | 0 | memory_free( |
130 | 0 | internal_directory ); |
131 | 0 | } |
132 | 0 | return( -1 ); |
133 | 0 | } |
134 | | |
135 | | /* Frees a directory |
136 | | * Returns 1 if successful or -1 on error |
137 | | */ |
138 | | int libcdirectory_directory_free( |
139 | | libcdirectory_directory_t **directory, |
140 | | libcerror_error_t **error ) |
141 | 0 | { |
142 | 0 | libcdirectory_internal_directory_t *internal_directory = NULL; |
143 | 0 | static char *function = "libcdirectory_directory_free"; |
144 | 0 | int result = 1; |
145 | |
|
146 | 0 | if( directory == NULL ) |
147 | 0 | { |
148 | 0 | libcerror_error_set( |
149 | 0 | error, |
150 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
151 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
152 | 0 | "%s: invalid directory.", |
153 | 0 | function ); |
154 | |
|
155 | 0 | return( -1 ); |
156 | 0 | } |
157 | 0 | if( *directory != NULL ) |
158 | 0 | { |
159 | 0 | internal_directory = (libcdirectory_internal_directory_t *) *directory; |
160 | | |
161 | | #if defined( WINAPI ) |
162 | | if( internal_directory->handle != INVALID_HANDLE_VALUE ) |
163 | | #else |
164 | 0 | if( internal_directory->stream != NULL ) |
165 | 0 | #endif |
166 | 0 | { |
167 | 0 | if( libcdirectory_directory_close( |
168 | 0 | *directory, |
169 | 0 | error ) != 0 ) |
170 | 0 | { |
171 | 0 | libcerror_error_set( |
172 | 0 | error, |
173 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
174 | 0 | LIBCERROR_IO_ERROR_CLOSE_FAILED, |
175 | 0 | "%s: unable to close directory.", |
176 | 0 | function ); |
177 | |
|
178 | 0 | result = -1; |
179 | 0 | } |
180 | 0 | } |
181 | 0 | *directory = NULL; |
182 | |
|
183 | 0 | memory_free( |
184 | 0 | internal_directory ); |
185 | 0 | } |
186 | 0 | return( result ); |
187 | 0 | } |
188 | | |
189 | | #if defined( WINAPI ) && ( WINVER >= 0x0400 ) |
190 | | |
191 | | /* Opens a directory |
192 | | * This function uses the WINAPI function for Windows XP or later |
193 | | * Returns 1 if successful or -1 on error |
194 | | */ |
195 | | int libcdirectory_directory_open( |
196 | | libcdirectory_directory_t *directory, |
197 | | const char *directory_name, |
198 | | libcerror_error_t **error ) |
199 | | { |
200 | | libcdirectory_internal_directory_t *internal_directory = NULL; |
201 | | libcdirectory_internal_directory_entry_t *internal_first_directory_entry = NULL; |
202 | | system_character_t *system_directory_name = NULL; |
203 | | static char *function = "libcdirectory_directory_open"; |
204 | | size_t directory_name_length = 0; |
205 | | size_t system_directory_name_size = 0; |
206 | | DWORD error_code = 0; |
207 | | |
208 | | if( directory == NULL ) |
209 | | { |
210 | | libcerror_error_set( |
211 | | error, |
212 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
213 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
214 | | "%s: invalid directory.", |
215 | | function ); |
216 | | |
217 | | return( -1 ); |
218 | | } |
219 | | internal_directory = (libcdirectory_internal_directory_t *) directory; |
220 | | |
221 | | if( directory_name == NULL ) |
222 | | { |
223 | | libcerror_error_set( |
224 | | error, |
225 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
226 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
227 | | "%s: invalid directory name.", |
228 | | function ); |
229 | | |
230 | | return( -1 ); |
231 | | } |
232 | | if( internal_directory->handle != INVALID_HANDLE_VALUE ) |
233 | | { |
234 | | libcerror_error_set( |
235 | | error, |
236 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
237 | | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
238 | | "%s: invalid directory - handle value already set.", |
239 | | function ); |
240 | | |
241 | | return( -1 ); |
242 | | } |
243 | | if( internal_directory->first_entry != NULL ) |
244 | | { |
245 | | libcerror_error_set( |
246 | | error, |
247 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
248 | | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
249 | | "%s: invalid directory - first entry value already set.", |
250 | | function ); |
251 | | |
252 | | return( -1 ); |
253 | | } |
254 | | if( libcdirectory_directory_entry_initialize( |
255 | | &( internal_directory->first_entry ), |
256 | | error ) != 1 ) |
257 | | { |
258 | | libcerror_error_set( |
259 | | error, |
260 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
261 | | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
262 | | "%s: unable to create first entry.", |
263 | | function ); |
264 | | |
265 | | goto on_error; |
266 | | } |
267 | | if( internal_directory->first_entry == NULL ) |
268 | | { |
269 | | libcerror_error_set( |
270 | | error, |
271 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
272 | | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
273 | | "%s: invalid directory - missing first entry.", |
274 | | function ); |
275 | | |
276 | | goto on_error; |
277 | | } |
278 | | internal_first_directory_entry = (libcdirectory_internal_directory_entry_t *) internal_directory->first_entry; |
279 | | |
280 | | directory_name_length = narrow_string_length( |
281 | | directory_name ); |
282 | | |
283 | | if( libcdirectory_system_string_size_from_narrow_string( |
284 | | directory_name, |
285 | | directory_name_length + 1, |
286 | | &system_directory_name_size, |
287 | | error ) != 1 ) |
288 | | { |
289 | | libcerror_error_set( |
290 | | error, |
291 | | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
292 | | LIBCERROR_CONVERSION_ERROR_GENERIC, |
293 | | "%s: unable to determine system character directory name size.", |
294 | | function ); |
295 | | |
296 | | goto on_error; |
297 | | } |
298 | | /* Find files requires a search path, add a \ and * if necessary |
299 | | */ |
300 | | if( ( directory_name_length < 2 ) |
301 | | || ( directory_name[ directory_name_length - 1 ] != '\\' ) ) |
302 | | { |
303 | | system_directory_name_size += 1; |
304 | | } |
305 | | system_directory_name_size += 1; |
306 | | |
307 | | system_directory_name = system_string_allocate( |
308 | | system_directory_name_size ); |
309 | | |
310 | | if( system_directory_name == NULL ) |
311 | | { |
312 | | libcerror_error_set( |
313 | | error, |
314 | | LIBCERROR_ERROR_DOMAIN_MEMORY, |
315 | | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
316 | | "%s: unable to create system character directory name.", |
317 | | function ); |
318 | | |
319 | | goto on_error; |
320 | | } |
321 | | if( libcdirectory_system_string_copy_from_narrow_string( |
322 | | system_directory_name, |
323 | | system_directory_name_size, |
324 | | directory_name, |
325 | | directory_name_length + 1, |
326 | | error ) != 1 ) |
327 | | { |
328 | | libcerror_error_set( |
329 | | error, |
330 | | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
331 | | LIBCERROR_CONVERSION_ERROR_GENERIC, |
332 | | "%s: unable to set system character directory name.", |
333 | | function ); |
334 | | |
335 | | goto on_error; |
336 | | } |
337 | | /* Find files requires a search path, add a \ and * if necessary |
338 | | */ |
339 | | if( ( directory_name_length < 2 ) |
340 | | || ( directory_name[ directory_name_length - 1 ] != '\\' ) ) |
341 | | { |
342 | | system_directory_name[ system_directory_name_size - 3 ] = (system_character_t) '\\'; |
343 | | } |
344 | | system_directory_name[ system_directory_name_size - 2 ] = (system_character_t) '*'; |
345 | | system_directory_name[ system_directory_name_size - 1 ] = 0; |
346 | | |
347 | | #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) |
348 | | /* Cannot use FindFirstFileA here because it requires a narrow version of WIN32_FIND_DATA |
349 | | */ |
350 | | internal_directory->handle = FindFirstFileW( |
351 | | (LPCWSTR) system_directory_name, |
352 | | &( internal_first_directory_entry->find_data ) ); |
353 | | #else |
354 | | internal_directory->handle = FindFirstFileA( |
355 | | (LPCSTR) system_directory_name, |
356 | | &( internal_first_directory_entry->find_data ) ); |
357 | | #endif |
358 | | if( internal_directory->handle == INVALID_HANDLE_VALUE ) |
359 | | { |
360 | | error_code = GetLastError(); |
361 | | |
362 | | libcerror_system_set_error( |
363 | | error, |
364 | | LIBCERROR_ERROR_DOMAIN_IO, |
365 | | LIBCERROR_IO_ERROR_OPEN_FAILED, |
366 | | error_code, |
367 | | "%s: unable to open directory.", |
368 | | function ); |
369 | | |
370 | | goto on_error; |
371 | | } |
372 | | /* Note that FindFirstFile on a non-directory will be successful |
373 | | */ |
374 | | if( ( internal_first_directory_entry->find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == 0 ) |
375 | | { |
376 | | libcerror_system_set_error( |
377 | | error, |
378 | | LIBCERROR_ERROR_DOMAIN_IO, |
379 | | LIBCERROR_IO_ERROR_OPEN_FAILED, |
380 | | error_code, |
381 | | "%s: unsupported file type - not a directory.", |
382 | | function ); |
383 | | |
384 | | goto on_error; |
385 | | } |
386 | | memory_free( |
387 | | system_directory_name ); |
388 | | |
389 | | return( 1 ); |
390 | | |
391 | | on_error: |
392 | | if( system_directory_name != NULL ) |
393 | | { |
394 | | memory_free( |
395 | | system_directory_name ); |
396 | | } |
397 | | if( internal_directory->first_entry != NULL ) |
398 | | { |
399 | | libcdirectory_directory_entry_free( |
400 | | &( internal_directory->first_entry ), |
401 | | NULL ); |
402 | | } |
403 | | return( -1 ); |
404 | | } |
405 | | |
406 | | #elif defined( WINAPI ) |
407 | | |
408 | | /* TODO */ |
409 | | #error WINAPI open directory for Windows earlier than NT4 not implemented |
410 | | |
411 | | #elif defined( HAVE_OPENDIR ) |
412 | | |
413 | | /* Opens a directory |
414 | | * This function uses the POSIX opendir function |
415 | | * Returns 1 if successful or -1 on error |
416 | | */ |
417 | | int libcdirectory_directory_open( |
418 | | libcdirectory_directory_t *directory, |
419 | | const char *directory_name, |
420 | | libcerror_error_t **error ) |
421 | 0 | { |
422 | 0 | libcdirectory_internal_directory_t *internal_directory = NULL; |
423 | 0 | static char *function = "libcdirectory_directory_open"; |
424 | |
|
425 | 0 | if( directory == NULL ) |
426 | 0 | { |
427 | 0 | libcerror_error_set( |
428 | 0 | error, |
429 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
430 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
431 | 0 | "%s: invalid directory.", |
432 | 0 | function ); |
433 | |
|
434 | 0 | return( -1 ); |
435 | 0 | } |
436 | 0 | internal_directory = (libcdirectory_internal_directory_t *) directory; |
437 | | |
438 | 0 | if( directory_name == NULL ) |
439 | 0 | { |
440 | 0 | libcerror_error_set( |
441 | 0 | error, |
442 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
443 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
444 | 0 | "%s: invalid directory name.", |
445 | 0 | function ); |
446 | |
|
447 | 0 | return( -1 ); |
448 | 0 | } |
449 | 0 | if( internal_directory->stream != NULL ) |
450 | 0 | { |
451 | 0 | libcerror_error_set( |
452 | 0 | error, |
453 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
454 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
455 | 0 | "%s: invalid directory - stream value already set.", |
456 | 0 | function ); |
457 | |
|
458 | 0 | return( -1 ); |
459 | 0 | } |
460 | 0 | internal_directory->stream = opendir( |
461 | 0 | directory_name ); |
462 | |
|
463 | 0 | if( internal_directory->stream == NULL ) |
464 | 0 | { |
465 | 0 | libcerror_system_set_error( |
466 | 0 | error, |
467 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
468 | 0 | LIBCERROR_IO_ERROR_OPEN_FAILED, |
469 | 0 | errno, |
470 | 0 | "%s: unable to open directory.", |
471 | 0 | function ); |
472 | |
|
473 | 0 | return( -1 ); |
474 | 0 | } |
475 | 0 | return( 1 ); |
476 | 0 | } |
477 | | |
478 | | #else |
479 | | #error Missing open directory function |
480 | | #endif |
481 | | |
482 | | #if defined( HAVE_WIDE_CHARACTER_TYPE ) |
483 | | |
484 | | #if defined( WINAPI ) && ( WINVER >= 0x0400 ) |
485 | | |
486 | | /* Opens a directory |
487 | | * This function uses the WINAPI function for Windows XP or later |
488 | | * Returns 1 if successful or -1 on error |
489 | | */ |
490 | | int libcdirectory_directory_open_wide( |
491 | | libcdirectory_directory_t *directory, |
492 | | const wchar_t *directory_name, |
493 | | libcerror_error_t **error ) |
494 | | { |
495 | | libcdirectory_internal_directory_t *internal_directory = NULL; |
496 | | libcdirectory_internal_directory_entry_t *internal_first_directory_entry = NULL; |
497 | | system_character_t *system_directory_name = NULL; |
498 | | static char *function = "libcdirectory_directory_open_wide"; |
499 | | size_t directory_name_length = 0; |
500 | | size_t system_directory_name_size = 0; |
501 | | DWORD error_code = 0; |
502 | | |
503 | | if( directory == NULL ) |
504 | | { |
505 | | libcerror_error_set( |
506 | | error, |
507 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
508 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
509 | | "%s: invalid directory.", |
510 | | function ); |
511 | | |
512 | | return( -1 ); |
513 | | } |
514 | | internal_directory = (libcdirectory_internal_directory_t *) directory; |
515 | | |
516 | | if( directory_name == NULL ) |
517 | | { |
518 | | libcerror_error_set( |
519 | | error, |
520 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
521 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
522 | | "%s: invalid directory name.", |
523 | | function ); |
524 | | |
525 | | return( -1 ); |
526 | | } |
527 | | if( internal_directory->handle != INVALID_HANDLE_VALUE ) |
528 | | { |
529 | | libcerror_error_set( |
530 | | error, |
531 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
532 | | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
533 | | "%s: invalid directory - handle value already set.", |
534 | | function ); |
535 | | |
536 | | return( -1 ); |
537 | | } |
538 | | if( internal_directory->first_entry != NULL ) |
539 | | { |
540 | | libcerror_error_set( |
541 | | error, |
542 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
543 | | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
544 | | "%s: invalid directory - first entry value already set.", |
545 | | function ); |
546 | | |
547 | | return( -1 ); |
548 | | } |
549 | | if( libcdirectory_directory_entry_initialize( |
550 | | &( internal_directory->first_entry ), |
551 | | error ) != 1 ) |
552 | | { |
553 | | libcerror_error_set( |
554 | | error, |
555 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
556 | | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
557 | | "%s: unable to create first entry.", |
558 | | function ); |
559 | | |
560 | | goto on_error; |
561 | | } |
562 | | if( internal_directory->first_entry == NULL ) |
563 | | { |
564 | | libcerror_error_set( |
565 | | error, |
566 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
567 | | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
568 | | "%s: invalid directory - missing first entry.", |
569 | | function ); |
570 | | |
571 | | goto on_error; |
572 | | } |
573 | | internal_first_directory_entry = (libcdirectory_internal_directory_entry_t *) internal_directory->first_entry; |
574 | | |
575 | | directory_name_length = wide_string_length( |
576 | | directory_name ); |
577 | | |
578 | | if( libcdirectory_system_string_size_from_wide_string( |
579 | | directory_name, |
580 | | directory_name_length + 1, |
581 | | &system_directory_name_size, |
582 | | error ) != 1 ) |
583 | | { |
584 | | libcerror_error_set( |
585 | | error, |
586 | | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
587 | | LIBCERROR_CONVERSION_ERROR_GENERIC, |
588 | | "%s: unable to determine system character directory name size.", |
589 | | function ); |
590 | | |
591 | | goto on_error; |
592 | | } |
593 | | /* Find files requires a search path, add a \ and * if necessary |
594 | | */ |
595 | | if( ( directory_name_length < 2 ) |
596 | | || ( directory_name[ directory_name_length - 1 ] == (wchar_t) '\\' ) ) |
597 | | { |
598 | | system_directory_name_size += 1; |
599 | | } |
600 | | system_directory_name_size += 1; |
601 | | |
602 | | system_directory_name = system_string_allocate( |
603 | | system_directory_name_size ); |
604 | | |
605 | | if( system_directory_name == NULL ) |
606 | | { |
607 | | libcerror_error_set( |
608 | | error, |
609 | | LIBCERROR_ERROR_DOMAIN_MEMORY, |
610 | | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
611 | | "%s: unable to create system character directory name.", |
612 | | function ); |
613 | | |
614 | | goto on_error; |
615 | | } |
616 | | if( libcdirectory_system_string_copy_from_wide_string( |
617 | | system_directory_name, |
618 | | system_directory_name_size, |
619 | | directory_name, |
620 | | directory_name_length + 1, |
621 | | error ) != 1 ) |
622 | | { |
623 | | libcerror_error_set( |
624 | | error, |
625 | | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
626 | | LIBCERROR_CONVERSION_ERROR_GENERIC, |
627 | | "%s: unable to set system character directory name.", |
628 | | function ); |
629 | | |
630 | | goto on_error; |
631 | | } |
632 | | /* Find files requires a search path, add a \ and * if necessary |
633 | | */ |
634 | | if( ( directory_name_length < 2 ) |
635 | | || ( directory_name[ directory_name_length - 1 ] == (wchar_t) '\\' ) ) |
636 | | { |
637 | | system_directory_name[ system_directory_name_size - 3 ] = (system_character_t) '\\'; |
638 | | } |
639 | | system_directory_name[ system_directory_name_size - 2 ] = (system_character_t) '*'; |
640 | | system_directory_name[ system_directory_name_size - 1 ] = 0; |
641 | | |
642 | | #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) |
643 | | internal_directory->handle = FindFirstFileW( |
644 | | (LPCWSTR) system_directory_name, |
645 | | &( internal_first_directory_entry->find_data ) ); |
646 | | #else |
647 | | /* Cannot use FindFirstFileW here because it requires a wide version of WIN32_FIND_DATA |
648 | | */ |
649 | | internal_directory->handle = FindFirstFileA( |
650 | | (LPCSTR) system_directory_name, |
651 | | &( internal_first_directory_entry->find_data ) ); |
652 | | #endif |
653 | | if( internal_directory->handle == INVALID_HANDLE_VALUE ) |
654 | | { |
655 | | error_code = GetLastError(); |
656 | | |
657 | | libcerror_system_set_error( |
658 | | error, |
659 | | LIBCERROR_ERROR_DOMAIN_IO, |
660 | | LIBCERROR_IO_ERROR_OPEN_FAILED, |
661 | | error_code, |
662 | | "%s: unable to open directory.", |
663 | | function ); |
664 | | |
665 | | goto on_error; |
666 | | } |
667 | | /* Note that FindFirstFile on a non-directory will be successful |
668 | | */ |
669 | | if( ( internal_first_directory_entry->find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == 0 ) |
670 | | { |
671 | | libcerror_system_set_error( |
672 | | error, |
673 | | LIBCERROR_ERROR_DOMAIN_IO, |
674 | | LIBCERROR_IO_ERROR_OPEN_FAILED, |
675 | | error_code, |
676 | | "%s: unsupported file type - not a directory.", |
677 | | function ); |
678 | | |
679 | | goto on_error; |
680 | | } |
681 | | memory_free( |
682 | | system_directory_name ); |
683 | | |
684 | | return( 1 ); |
685 | | |
686 | | on_error: |
687 | | if( system_directory_name != NULL ) |
688 | | { |
689 | | memory_free( |
690 | | system_directory_name ); |
691 | | } |
692 | | if( internal_directory->first_entry != NULL ) |
693 | | { |
694 | | libcdirectory_directory_entry_free( |
695 | | &( internal_directory->first_entry ), |
696 | | NULL ); |
697 | | } |
698 | | return( -1 ); |
699 | | } |
700 | | |
701 | | #elif defined( WINAPI ) |
702 | | |
703 | | /* TODO */ |
704 | | #error WINAPI open directory for Windows earlier than NT4 not implemented |
705 | | |
706 | | #elif defined( HAVE_OPENDIR ) |
707 | | |
708 | | /* Opens a directory |
709 | | * This function uses the POSIX opendir function |
710 | | * Returns 1 if successful or -1 on error |
711 | | */ |
712 | | int libcdirectory_directory_open_wide( |
713 | | libcdirectory_directory_t *directory, |
714 | | const wchar_t *directory_name, |
715 | | libcerror_error_t **error ) |
716 | | { |
717 | | libcdirectory_internal_directory_t *internal_directory = NULL; |
718 | | static char *function = "libcdirectory_directory_open_wide"; |
719 | | char *system_directory_name = NULL; |
720 | | size_t directory_name_length = 0; |
721 | | size_t system_directory_name_size = 0; |
722 | | |
723 | | if( directory == NULL ) |
724 | | { |
725 | | libcerror_error_set( |
726 | | error, |
727 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
728 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
729 | | "%s: invalid directory.", |
730 | | function ); |
731 | | |
732 | | return( -1 ); |
733 | | } |
734 | | internal_directory = (libcdirectory_internal_directory_t *) directory; |
735 | | |
736 | | if( directory_name == NULL ) |
737 | | { |
738 | | libcerror_error_set( |
739 | | error, |
740 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
741 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
742 | | "%s: invalid directory name.", |
743 | | function ); |
744 | | |
745 | | return( -1 ); |
746 | | } |
747 | | if( internal_directory->stream != NULL ) |
748 | | { |
749 | | libcerror_error_set( |
750 | | error, |
751 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
752 | | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
753 | | "%s: invalid directory - stream value already set.", |
754 | | function ); |
755 | | |
756 | | return( -1 ); |
757 | | } |
758 | | directory_name_length = wide_string_length( |
759 | | directory_name ); |
760 | | |
761 | | if( libcdirectory_system_string_size_from_wide_string( |
762 | | directory_name, |
763 | | directory_name_length + 1, |
764 | | &system_directory_name_size, |
765 | | error ) != 1 ) |
766 | | { |
767 | | libcerror_error_set( |
768 | | error, |
769 | | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
770 | | LIBCERROR_CONVERSION_ERROR_GENERIC, |
771 | | "%s: unable to determine system character directory name size.", |
772 | | function ); |
773 | | |
774 | | goto on_error; |
775 | | } |
776 | | system_directory_name = system_string_allocate( |
777 | | system_directory_name_size ); |
778 | | |
779 | | if( system_directory_name == NULL ) |
780 | | { |
781 | | libcerror_error_set( |
782 | | error, |
783 | | LIBCERROR_ERROR_DOMAIN_MEMORY, |
784 | | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
785 | | "%s: unable to create system character directory name.", |
786 | | function ); |
787 | | |
788 | | goto on_error; |
789 | | } |
790 | | if( libcdirectory_system_string_copy_from_wide_string( |
791 | | system_directory_name, |
792 | | system_directory_name_size, |
793 | | directory_name, |
794 | | directory_name_length + 1, |
795 | | error ) != 1 ) |
796 | | { |
797 | | libcerror_error_set( |
798 | | error, |
799 | | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
800 | | LIBCERROR_CONVERSION_ERROR_GENERIC, |
801 | | "%s: unable to set system character directory name.", |
802 | | function ); |
803 | | |
804 | | goto on_error; |
805 | | } |
806 | | internal_directory->stream = opendir( |
807 | | system_directory_name ); |
808 | | |
809 | | if( internal_directory->stream == NULL ) |
810 | | { |
811 | | libcerror_system_set_error( |
812 | | error, |
813 | | LIBCERROR_ERROR_DOMAIN_IO, |
814 | | LIBCERROR_IO_ERROR_OPEN_FAILED, |
815 | | errno, |
816 | | "%s: unable to open directory.", |
817 | | function ); |
818 | | |
819 | | goto on_error; |
820 | | } |
821 | | memory_free( |
822 | | system_directory_name ); |
823 | | |
824 | | system_directory_name = NULL; |
825 | | |
826 | | return( 1 ); |
827 | | |
828 | | on_error: |
829 | | if( system_directory_name != NULL ) |
830 | | { |
831 | | memory_free( |
832 | | system_directory_name ); |
833 | | } |
834 | | return( -1 ); |
835 | | } |
836 | | |
837 | | #else |
838 | | #error Missing wide open directory function |
839 | | #endif |
840 | | |
841 | | #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */ |
842 | | |
843 | | #if defined( WINAPI ) && ( WINVER >= 0x0400 ) |
844 | | |
845 | | /* Closes a directory |
846 | | * This function uses the WINAPI function for Windows XP or later |
847 | | * Returns 0 if successful or -1 on error |
848 | | */ |
849 | | int libcdirectory_directory_close( |
850 | | libcdirectory_directory_t *directory, |
851 | | libcerror_error_t **error ) |
852 | | { |
853 | | libcdirectory_internal_directory_t *internal_directory = NULL; |
854 | | static char *function = "libcdirectory_directory_close"; |
855 | | int result = 0; |
856 | | DWORD error_code = 0; |
857 | | |
858 | | if( directory == NULL ) |
859 | | { |
860 | | libcerror_error_set( |
861 | | error, |
862 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
863 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
864 | | "%s: invalid directory.", |
865 | | function ); |
866 | | |
867 | | return( -1 ); |
868 | | } |
869 | | internal_directory = (libcdirectory_internal_directory_t *) directory; |
870 | | |
871 | | if( internal_directory->handle != INVALID_HANDLE_VALUE ) |
872 | | { |
873 | | if( FindClose( |
874 | | internal_directory->handle ) == 0 ) |
875 | | { |
876 | | error_code = GetLastError(); |
877 | | |
878 | | libcerror_system_set_error( |
879 | | error, |
880 | | LIBCERROR_ERROR_DOMAIN_IO, |
881 | | LIBCERROR_IO_ERROR_CLOSE_FAILED, |
882 | | error_code, |
883 | | "%s: unable to close directory.", |
884 | | function ); |
885 | | |
886 | | result = -1; |
887 | | } |
888 | | internal_directory->handle = INVALID_HANDLE_VALUE; |
889 | | } |
890 | | if( internal_directory->first_entry != NULL ) |
891 | | { |
892 | | if( libcdirectory_directory_entry_free( |
893 | | &( internal_directory->first_entry ), |
894 | | error ) != 1 ) |
895 | | { |
896 | | libcerror_error_set( |
897 | | error, |
898 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
899 | | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
900 | | "%s: unable to free first entry.", |
901 | | function ); |
902 | | |
903 | | result = -1; |
904 | | } |
905 | | } |
906 | | return( result ); |
907 | | } |
908 | | |
909 | | #elif defined( WINAPI ) |
910 | | |
911 | | /* TODO */ |
912 | | #error WINAPI close directory for Windows earlier than NT4 not implemented |
913 | | |
914 | | #elif defined( HAVE_CLOSEDIR ) |
915 | | |
916 | | /* Closes a directory |
917 | | * This function uses the POSIX closedir function |
918 | | * Returns 0 if successful or -1 on error |
919 | | */ |
920 | | int libcdirectory_directory_close( |
921 | | libcdirectory_directory_t *directory, |
922 | | libcerror_error_t **error ) |
923 | 0 | { |
924 | 0 | libcdirectory_internal_directory_t *internal_directory = NULL; |
925 | 0 | static char *function = "libcdirectory_directory_close"; |
926 | 0 | int result = 0; |
927 | |
|
928 | 0 | if( directory == NULL ) |
929 | 0 | { |
930 | 0 | libcerror_error_set( |
931 | 0 | error, |
932 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
933 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
934 | 0 | "%s: invalid directory.", |
935 | 0 | function ); |
936 | |
|
937 | 0 | return( -1 ); |
938 | 0 | } |
939 | 0 | internal_directory = (libcdirectory_internal_directory_t *) directory; |
940 | | |
941 | 0 | if( internal_directory->stream != NULL ) |
942 | 0 | { |
943 | 0 | if( closedir( |
944 | 0 | internal_directory->stream ) != 0 ) |
945 | 0 | { |
946 | 0 | libcerror_system_set_error( |
947 | 0 | error, |
948 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
949 | 0 | LIBCERROR_IO_ERROR_CLOSE_FAILED, |
950 | 0 | errno, |
951 | 0 | "%s: unable to close directory.", |
952 | 0 | function ); |
953 | |
|
954 | 0 | result = -1; |
955 | 0 | } |
956 | 0 | internal_directory->stream = NULL; |
957 | 0 | } |
958 | 0 | return( result ); |
959 | 0 | } |
960 | | |
961 | | #else |
962 | | #error Missing close directory function |
963 | | #endif |
964 | | |
965 | | #if defined( WINAPI ) && ( WINVER >= 0x0400 ) |
966 | | |
967 | | /* Reads a directory |
968 | | * This function uses the WINAPI function for Windows XP or later |
969 | | * Returns 1 if successful, 0 if no such entry or -1 on error |
970 | | */ |
971 | | int libcdirectory_directory_read_entry( |
972 | | libcdirectory_directory_t *directory, |
973 | | libcdirectory_directory_entry_t *directory_entry, |
974 | | libcerror_error_t **error ) |
975 | | { |
976 | | libcdirectory_internal_directory_t *internal_directory = NULL; |
977 | | libcdirectory_internal_directory_entry_t *internal_directory_entry = NULL; |
978 | | static char *function = "libcdirectory_directory_read_entry"; |
979 | | DWORD error_code = 0; |
980 | | |
981 | | if( directory == NULL ) |
982 | | { |
983 | | libcerror_error_set( |
984 | | error, |
985 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
986 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
987 | | "%s: invalid directory.", |
988 | | function ); |
989 | | |
990 | | return( -1 ); |
991 | | } |
992 | | internal_directory = (libcdirectory_internal_directory_t *) directory; |
993 | | |
994 | | if( directory_entry == NULL ) |
995 | | { |
996 | | libcerror_error_set( |
997 | | error, |
998 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
999 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1000 | | "%s: invalid directory entry.", |
1001 | | function ); |
1002 | | |
1003 | | return( -1 ); |
1004 | | } |
1005 | | internal_directory_entry = (libcdirectory_internal_directory_entry_t *) directory_entry; |
1006 | | |
1007 | | if( internal_directory->handle == INVALID_HANDLE_VALUE ) |
1008 | | { |
1009 | | libcerror_error_set( |
1010 | | error, |
1011 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1012 | | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
1013 | | "%s: invalid directory - missing handle.", |
1014 | | function ); |
1015 | | |
1016 | | return( -1 ); |
1017 | | } |
1018 | | if( internal_directory->first_entry != NULL ) |
1019 | | { |
1020 | | if( libcdirectory_directory_entry_copy( |
1021 | | directory_entry, |
1022 | | internal_directory->first_entry, |
1023 | | error ) != 1 ) |
1024 | | { |
1025 | | libcerror_error_set( |
1026 | | error, |
1027 | | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1028 | | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
1029 | | "%s: unable to copy first directory entry.", |
1030 | | function ); |
1031 | | |
1032 | | return( -1 ); |
1033 | | } |
1034 | | if( libcdirectory_directory_entry_free( |
1035 | | (libcdirectory_directory_t **) &( internal_directory->first_entry ), |
1036 | | error ) != 1 ) |
1037 | | { |
1038 | | libcerror_error_set( |
1039 | | error, |
1040 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1041 | | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
1042 | | "%s: unable to free first entry.", |
1043 | | function ); |
1044 | | |
1045 | | return( -1 ); |
1046 | | } |
1047 | | } |
1048 | | else |
1049 | | { |
1050 | | #if defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_SYSTEM_CHARACTER ) |
1051 | | if( internal_directory_entry->narrow_name != NULL ) |
1052 | | { |
1053 | | memory_free( |
1054 | | internal_directory_entry->narrow_name ); |
1055 | | |
1056 | | internal_directory_entry->narrow_name = NULL; |
1057 | | } |
1058 | | #else |
1059 | | if( internal_directory_entry->wide_name != NULL ) |
1060 | | { |
1061 | | memory_free( |
1062 | | internal_directory_entry->wide_name ); |
1063 | | |
1064 | | internal_directory_entry->wide_name = NULL; |
1065 | | } |
1066 | | #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_SYSTEM_CHARACTER ) */ |
1067 | | |
1068 | | if( FindNextFile( |
1069 | | internal_directory->handle, |
1070 | | &( internal_directory_entry->find_data ) ) == 0 ) |
1071 | | { |
1072 | | error_code = GetLastError(); |
1073 | | |
1074 | | if( error_code == ERROR_NO_MORE_FILES ) |
1075 | | { |
1076 | | return( 0 ); |
1077 | | } |
1078 | | else |
1079 | | { |
1080 | | libcerror_system_set_error( |
1081 | | error, |
1082 | | LIBCERROR_ERROR_DOMAIN_IO, |
1083 | | LIBCERROR_IO_ERROR_READ_FAILED, |
1084 | | error_code, |
1085 | | "%s: unable to read from directory.", |
1086 | | function ); |
1087 | | } |
1088 | | return( -1 ); |
1089 | | } |
1090 | | } |
1091 | | return( 1 ); |
1092 | | } |
1093 | | |
1094 | | #elif defined( WINAPI ) |
1095 | | |
1096 | | /* TODO */ |
1097 | | #error WINAPI read directory entry for Windows earlier than NT4 not implemented |
1098 | | |
1099 | | #elif defined( HAVE_READDIR_R ) |
1100 | | |
1101 | | /* Reads a directory |
1102 | | * This function uses the POSIX readdir_r function |
1103 | | * Returns 1 if successful, 0 if no such entry or -1 on error |
1104 | | */ |
1105 | | int libcdirectory_directory_read_entry( |
1106 | | libcdirectory_directory_t *directory, |
1107 | | libcdirectory_directory_entry_t *directory_entry, |
1108 | | libcerror_error_t **error ) |
1109 | 0 | { |
1110 | 0 | libcdirectory_internal_directory_t *internal_directory = NULL; |
1111 | 0 | libcdirectory_internal_directory_entry_t *internal_directory_entry = NULL; |
1112 | 0 | struct dirent *result_directory_entry = NULL; |
1113 | 0 | static char *function = "libcdirectory_directory_read_entry"; |
1114 | 0 | int result = 0; |
1115 | |
|
1116 | 0 | if( directory == NULL ) |
1117 | 0 | { |
1118 | 0 | libcerror_error_set( |
1119 | 0 | error, |
1120 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1121 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1122 | 0 | "%s: invalid directory.", |
1123 | 0 | function ); |
1124 | |
|
1125 | 0 | return( -1 ); |
1126 | 0 | } |
1127 | 0 | internal_directory = (libcdirectory_internal_directory_t *) directory; |
1128 | | |
1129 | 0 | if( directory_entry == NULL ) |
1130 | 0 | { |
1131 | 0 | libcerror_error_set( |
1132 | 0 | error, |
1133 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1134 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1135 | 0 | "%s: invalid directory entry.", |
1136 | 0 | function ); |
1137 | |
|
1138 | 0 | return( -1 ); |
1139 | 0 | } |
1140 | 0 | internal_directory_entry = (libcdirectory_internal_directory_entry_t *) directory_entry; |
1141 | |
|
1142 | 0 | if( internal_directory->stream == NULL ) |
1143 | 0 | { |
1144 | 0 | libcerror_error_set( |
1145 | 0 | error, |
1146 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1147 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
1148 | 0 | "%s: invalid directory - missing stream.", |
1149 | 0 | function ); |
1150 | |
|
1151 | 0 | return( -1 ); |
1152 | 0 | } |
1153 | | #if defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_SYSTEM_CHARACTER ) |
1154 | | if( internal_directory_entry->narrow_name != NULL ) |
1155 | | { |
1156 | | memory_free( |
1157 | | internal_directory_entry->narrow_name ); |
1158 | | |
1159 | | internal_directory_entry->narrow_name = NULL; |
1160 | | } |
1161 | | #else |
1162 | 0 | if( internal_directory_entry->wide_name != NULL ) |
1163 | 0 | { |
1164 | 0 | memory_free( |
1165 | 0 | internal_directory_entry->wide_name ); |
1166 | |
|
1167 | 0 | internal_directory_entry->wide_name = NULL; |
1168 | 0 | } |
1169 | 0 | #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_SYSTEM_CHARACTER ) */ |
1170 | |
|
1171 | 0 | result = readdir_r( |
1172 | 0 | internal_directory->stream, |
1173 | 0 | &( internal_directory_entry->entry ), |
1174 | 0 | &result_directory_entry ); |
1175 | |
|
1176 | 0 | if( result != 0 ) |
1177 | 0 | { |
1178 | 0 | libcerror_system_set_error( |
1179 | 0 | error, |
1180 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
1181 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
1182 | 0 | errno, |
1183 | 0 | "%s: unable to read from directory.", |
1184 | 0 | function ); |
1185 | |
|
1186 | 0 | return( -1 ); |
1187 | 0 | } |
1188 | 0 | if( result_directory_entry == NULL ) |
1189 | 0 | { |
1190 | 0 | return( 0 ); |
1191 | 0 | } |
1192 | 0 | return( 1 ); |
1193 | 0 | } |
1194 | | |
1195 | | #else |
1196 | | #error Missing read directory entry function |
1197 | | #endif |
1198 | | |
1199 | | /* Determines if a directory has a specific entry |
1200 | | * Returns 1 if the directory has the corresponding entry, 0 if not or -1 on error |
1201 | | */ |
1202 | | int libcdirectory_directory_has_entry( |
1203 | | libcdirectory_directory_t *directory, |
1204 | | libcdirectory_directory_entry_t *directory_entry, |
1205 | | const char *entry_name, |
1206 | | size_t entry_name_length, |
1207 | | uint8_t entry_type, |
1208 | | uint8_t compare_flags, |
1209 | | libcerror_error_t **error ) |
1210 | 0 | { |
1211 | 0 | libcdirectory_directory_entry_t *search_directory_entry = NULL; |
1212 | 0 | char *search_directory_entry_name = NULL; |
1213 | 0 | static char *function = "libcdirectory_directory_read_entry"; |
1214 | 0 | size_t search_directory_entry_name_length = 0; |
1215 | 0 | uint8_t search_directory_entry_type = 0; |
1216 | 0 | int entry_found = 0; |
1217 | 0 | int match = 0; |
1218 | 0 | int result = 0; |
1219 | |
|
1220 | 0 | if( directory == NULL ) |
1221 | 0 | { |
1222 | 0 | libcerror_error_set( |
1223 | 0 | error, |
1224 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1225 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1226 | 0 | "%s: invalid directory.", |
1227 | 0 | function ); |
1228 | |
|
1229 | 0 | return( -1 ); |
1230 | 0 | } |
1231 | 0 | if( directory_entry == NULL ) |
1232 | 0 | { |
1233 | 0 | libcerror_error_set( |
1234 | 0 | error, |
1235 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1236 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1237 | 0 | "%s: invalid directory entry.", |
1238 | 0 | function ); |
1239 | |
|
1240 | 0 | return( -1 ); |
1241 | 0 | } |
1242 | 0 | if( entry_name == NULL ) |
1243 | 0 | { |
1244 | 0 | libcerror_error_set( |
1245 | 0 | error, |
1246 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1247 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1248 | 0 | "%s: invalid entry name.", |
1249 | 0 | function ); |
1250 | |
|
1251 | 0 | return( -1 ); |
1252 | 0 | } |
1253 | 0 | if( entry_name_length > (size_t) SSIZE_MAX ) |
1254 | 0 | { |
1255 | 0 | libcerror_error_set( |
1256 | 0 | error, |
1257 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1258 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1259 | 0 | "%s: invalid entry name length value exceeds maximum.", |
1260 | 0 | function ); |
1261 | |
|
1262 | 0 | return( -1 ); |
1263 | 0 | } |
1264 | 0 | if( ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_DEVICE ) |
1265 | 0 | && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_DIRECTORY ) |
1266 | 0 | && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_FILE ) |
1267 | 0 | && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_LINK ) |
1268 | 0 | && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_PIPE ) |
1269 | 0 | && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_SOCKET ) |
1270 | 0 | && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_UNDEFINED ) ) |
1271 | 0 | { |
1272 | 0 | libcerror_error_set( |
1273 | 0 | error, |
1274 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1275 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
1276 | 0 | "%s: unsupported entry type.", |
1277 | 0 | function ); |
1278 | |
|
1279 | 0 | return( -1 ); |
1280 | 0 | } |
1281 | 0 | if( ( compare_flags & ~( LIBCDIRECTORY_COMPARE_FLAG_NO_CASE ) ) != 0 ) |
1282 | 0 | { |
1283 | 0 | libcerror_error_set( |
1284 | 0 | error, |
1285 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1286 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
1287 | 0 | "%s: unsupported compare flags.", |
1288 | 0 | function ); |
1289 | |
|
1290 | 0 | return( -1 ); |
1291 | 0 | } |
1292 | 0 | if( libcdirectory_directory_entry_initialize( |
1293 | 0 | &search_directory_entry, |
1294 | 0 | error ) != 1 ) |
1295 | 0 | { |
1296 | 0 | libcerror_error_set( |
1297 | 0 | error, |
1298 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1299 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
1300 | 0 | "%s: unable to create search directory entry.", |
1301 | 0 | function ); |
1302 | |
|
1303 | 0 | goto on_error; |
1304 | 0 | } |
1305 | 0 | entry_found = 0; |
1306 | |
|
1307 | 0 | do |
1308 | 0 | { |
1309 | 0 | result = libcdirectory_directory_read_entry( |
1310 | 0 | directory, |
1311 | 0 | search_directory_entry, |
1312 | 0 | error ); |
1313 | |
|
1314 | 0 | if( result == -1 ) |
1315 | 0 | { |
1316 | 0 | libcerror_error_set( |
1317 | 0 | error, |
1318 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
1319 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
1320 | 0 | "%s: unable to read directory entry.", |
1321 | 0 | function ); |
1322 | |
|
1323 | 0 | goto on_error; |
1324 | 0 | } |
1325 | 0 | else if( result == 0 ) |
1326 | 0 | { |
1327 | 0 | break; |
1328 | 0 | } |
1329 | 0 | if( libcdirectory_directory_entry_get_type( |
1330 | 0 | search_directory_entry, |
1331 | 0 | &search_directory_entry_type, |
1332 | 0 | error ) != 1 ) |
1333 | 0 | { |
1334 | 0 | libcerror_error_set( |
1335 | 0 | error, |
1336 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1337 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
1338 | 0 | "%s: unable to retrieve directory entry type.", |
1339 | 0 | function ); |
1340 | |
|
1341 | 0 | goto on_error; |
1342 | 0 | } |
1343 | 0 | if( search_directory_entry_type == entry_type ) |
1344 | 0 | { |
1345 | 0 | if( libcdirectory_directory_entry_get_name( |
1346 | 0 | search_directory_entry, |
1347 | 0 | &search_directory_entry_name, |
1348 | 0 | error ) != 1 ) |
1349 | 0 | { |
1350 | 0 | libcerror_error_set( |
1351 | 0 | error, |
1352 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1353 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
1354 | 0 | "%s: unable to retrieve directory entry name.", |
1355 | 0 | function ); |
1356 | |
|
1357 | 0 | goto on_error; |
1358 | 0 | } |
1359 | 0 | search_directory_entry_name_length = narrow_string_length( |
1360 | 0 | search_directory_entry_name ); |
1361 | |
|
1362 | 0 | if( search_directory_entry_name_length == entry_name_length ) |
1363 | 0 | { |
1364 | | /* If there is an exact match we're done searching |
1365 | | */ |
1366 | 0 | match = narrow_string_compare( |
1367 | 0 | search_directory_entry_name, |
1368 | 0 | entry_name, |
1369 | 0 | entry_name_length ); |
1370 | |
|
1371 | 0 | if( match == 0 ) |
1372 | 0 | { |
1373 | 0 | if( libcdirectory_directory_entry_copy( |
1374 | 0 | directory_entry, |
1375 | 0 | search_directory_entry, |
1376 | 0 | error ) != 1 ) |
1377 | 0 | { |
1378 | 0 | libcerror_error_set( |
1379 | 0 | error, |
1380 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1381 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
1382 | 0 | "%s: unable to copy search directory entry.", |
1383 | 0 | function ); |
1384 | |
|
1385 | 0 | goto on_error; |
1386 | 0 | } |
1387 | 0 | entry_found = 1; |
1388 | |
|
1389 | 0 | break; |
1390 | 0 | } |
1391 | | /* Ignore successive caseless matches |
1392 | | */ |
1393 | 0 | if( ( ( compare_flags & LIBCDIRECTORY_COMPARE_FLAG_NO_CASE ) != 0 ) |
1394 | 0 | && ( entry_found == 0 ) ) |
1395 | 0 | { |
1396 | 0 | match = narrow_string_compare_no_case( |
1397 | 0 | search_directory_entry_name, |
1398 | 0 | entry_name, |
1399 | 0 | entry_name_length ); |
1400 | |
|
1401 | 0 | if( match == 0 ) |
1402 | 0 | { |
1403 | 0 | if( libcdirectory_directory_entry_copy( |
1404 | 0 | directory_entry, |
1405 | 0 | search_directory_entry, |
1406 | 0 | error ) != 1 ) |
1407 | 0 | { |
1408 | 0 | libcerror_error_set( |
1409 | 0 | error, |
1410 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1411 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
1412 | 0 | "%s: unable to copy search directory entry.", |
1413 | 0 | function ); |
1414 | |
|
1415 | 0 | goto on_error; |
1416 | 0 | } |
1417 | 0 | entry_found = 1; |
1418 | 0 | } |
1419 | 0 | } |
1420 | 0 | } |
1421 | 0 | } |
1422 | 0 | } |
1423 | 0 | while( result != 0 ); |
1424 | | |
1425 | 0 | if( libcdirectory_directory_entry_free( |
1426 | 0 | &search_directory_entry, |
1427 | 0 | error ) != 1 ) |
1428 | 0 | { |
1429 | 0 | libcerror_error_set( |
1430 | 0 | error, |
1431 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1432 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
1433 | 0 | "%s: unable to free search directory entry.", |
1434 | 0 | function ); |
1435 | |
|
1436 | 0 | goto on_error; |
1437 | 0 | } |
1438 | 0 | return( entry_found ); |
1439 | | |
1440 | 0 | on_error: |
1441 | 0 | if( search_directory_entry != NULL ) |
1442 | 0 | { |
1443 | 0 | libcdirectory_directory_entry_free( |
1444 | 0 | &search_directory_entry, |
1445 | | NULL ); |
1446 | 0 | } |
1447 | 0 | return( -1 ); |
1448 | 0 | } |
1449 | | |
1450 | | #if defined( HAVE_WIDE_CHARACTER_TYPE ) |
1451 | | |
1452 | | /* Determines if a directory has a specific entry |
1453 | | * Returns 1 if the directory has the corresponding entry, 0 if not or -1 on error |
1454 | | */ |
1455 | | int libcdirectory_directory_has_entry_wide( |
1456 | | libcdirectory_directory_t *directory, |
1457 | | libcdirectory_directory_entry_t *directory_entry, |
1458 | | const wchar_t *entry_name, |
1459 | | size_t entry_name_length, |
1460 | | uint8_t entry_type, |
1461 | | uint8_t compare_flags, |
1462 | | libcerror_error_t **error ) |
1463 | | { |
1464 | | libcdirectory_directory_entry_t *search_directory_entry = NULL; |
1465 | | wchar_t *search_directory_entry_name = NULL; |
1466 | | static char *function = "libcdirectory_directory_read_entry_wide"; |
1467 | | size_t search_directory_entry_name_length = 0; |
1468 | | uint8_t search_directory_entry_type = 0; |
1469 | | int entry_found = 0; |
1470 | | int match = 0; |
1471 | | int result = 0; |
1472 | | |
1473 | | if( directory == NULL ) |
1474 | | { |
1475 | | libcerror_error_set( |
1476 | | error, |
1477 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1478 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1479 | | "%s: invalid directory.", |
1480 | | function ); |
1481 | | |
1482 | | return( -1 ); |
1483 | | } |
1484 | | if( directory_entry == NULL ) |
1485 | | { |
1486 | | libcerror_error_set( |
1487 | | error, |
1488 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1489 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1490 | | "%s: invalid directory entry.", |
1491 | | function ); |
1492 | | |
1493 | | return( -1 ); |
1494 | | } |
1495 | | if( entry_name == NULL ) |
1496 | | { |
1497 | | libcerror_error_set( |
1498 | | error, |
1499 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1500 | | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1501 | | "%s: invalid entry name.", |
1502 | | function ); |
1503 | | |
1504 | | return( -1 ); |
1505 | | } |
1506 | | if( entry_name_length > (size_t) SSIZE_MAX ) |
1507 | | { |
1508 | | libcerror_error_set( |
1509 | | error, |
1510 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1511 | | LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1512 | | "%s: invalid entry name length value exceeds maximum.", |
1513 | | function ); |
1514 | | |
1515 | | return( -1 ); |
1516 | | } |
1517 | | if( ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_DEVICE ) |
1518 | | && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_DIRECTORY ) |
1519 | | && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_FILE ) |
1520 | | && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_LINK ) |
1521 | | && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_PIPE ) |
1522 | | && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_SOCKET ) |
1523 | | && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_UNDEFINED ) ) |
1524 | | { |
1525 | | libcerror_error_set( |
1526 | | error, |
1527 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1528 | | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
1529 | | "%s: unsupported entry type.", |
1530 | | function ); |
1531 | | |
1532 | | return( -1 ); |
1533 | | } |
1534 | | if( ( compare_flags & ~( LIBCDIRECTORY_COMPARE_FLAG_NO_CASE ) ) != 0 ) |
1535 | | { |
1536 | | libcerror_error_set( |
1537 | | error, |
1538 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1539 | | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
1540 | | "%s: unsupported compare flags.", |
1541 | | function ); |
1542 | | |
1543 | | return( -1 ); |
1544 | | } |
1545 | | if( libcdirectory_directory_entry_initialize( |
1546 | | &search_directory_entry, |
1547 | | error ) != 1 ) |
1548 | | { |
1549 | | libcerror_error_set( |
1550 | | error, |
1551 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1552 | | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
1553 | | "%s: unable to create search directory entry.", |
1554 | | function ); |
1555 | | |
1556 | | goto on_error; |
1557 | | } |
1558 | | entry_found = 0; |
1559 | | |
1560 | | do |
1561 | | { |
1562 | | result = libcdirectory_directory_read_entry( |
1563 | | directory, |
1564 | | search_directory_entry, |
1565 | | error ); |
1566 | | |
1567 | | if( result == -1 ) |
1568 | | { |
1569 | | libcerror_error_set( |
1570 | | error, |
1571 | | LIBCERROR_ERROR_DOMAIN_IO, |
1572 | | LIBCERROR_IO_ERROR_READ_FAILED, |
1573 | | "%s: unable to read directory entry.", |
1574 | | function ); |
1575 | | |
1576 | | goto on_error; |
1577 | | } |
1578 | | else if( result == 0 ) |
1579 | | { |
1580 | | break; |
1581 | | } |
1582 | | if( libcdirectory_directory_entry_get_type( |
1583 | | search_directory_entry, |
1584 | | &search_directory_entry_type, |
1585 | | error ) != 1 ) |
1586 | | { |
1587 | | libcerror_error_set( |
1588 | | error, |
1589 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1590 | | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
1591 | | "%s: unable to retrieve directory entry type.", |
1592 | | function ); |
1593 | | |
1594 | | goto on_error; |
1595 | | } |
1596 | | if( search_directory_entry_type == entry_type ) |
1597 | | { |
1598 | | if( libcdirectory_directory_entry_get_name_wide( |
1599 | | search_directory_entry, |
1600 | | &search_directory_entry_name, |
1601 | | error ) != 1 ) |
1602 | | { |
1603 | | libcerror_error_set( |
1604 | | error, |
1605 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1606 | | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
1607 | | "%s: unable to retrieve directory entry name.", |
1608 | | function ); |
1609 | | |
1610 | | goto on_error; |
1611 | | } |
1612 | | search_directory_entry_name_length = wide_string_length( |
1613 | | search_directory_entry_name ); |
1614 | | |
1615 | | if( search_directory_entry_name_length == entry_name_length ) |
1616 | | { |
1617 | | /* If there is an exact match we're done searching |
1618 | | */ |
1619 | | match = wide_string_compare( |
1620 | | search_directory_entry_name, |
1621 | | entry_name, |
1622 | | entry_name_length ); |
1623 | | |
1624 | | if( match == 0 ) |
1625 | | { |
1626 | | if( libcdirectory_directory_entry_copy( |
1627 | | directory_entry, |
1628 | | search_directory_entry, |
1629 | | error ) != 1 ) |
1630 | | { |
1631 | | libcerror_error_set( |
1632 | | error, |
1633 | | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1634 | | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
1635 | | "%s: unable to copy search directory entry.", |
1636 | | function ); |
1637 | | |
1638 | | goto on_error; |
1639 | | } |
1640 | | entry_found = 1; |
1641 | | |
1642 | | break; |
1643 | | } |
1644 | | /* Ignore successive caseless matches |
1645 | | */ |
1646 | | if( ( ( compare_flags & LIBCDIRECTORY_COMPARE_FLAG_NO_CASE ) != 0 ) |
1647 | | && ( entry_found == 0 ) ) |
1648 | | { |
1649 | | match = wide_string_compare_no_case( |
1650 | | search_directory_entry_name, |
1651 | | entry_name, |
1652 | | entry_name_length ); |
1653 | | |
1654 | | if( match == 0 ) |
1655 | | { |
1656 | | if( libcdirectory_directory_entry_copy( |
1657 | | directory_entry, |
1658 | | search_directory_entry, |
1659 | | error ) != 1 ) |
1660 | | { |
1661 | | libcerror_error_set( |
1662 | | error, |
1663 | | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1664 | | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
1665 | | "%s: unable to copy search directory entry.", |
1666 | | function ); |
1667 | | |
1668 | | goto on_error; |
1669 | | } |
1670 | | entry_found = 1; |
1671 | | } |
1672 | | } |
1673 | | } |
1674 | | } |
1675 | | } |
1676 | | while( result != 0 ); |
1677 | | |
1678 | | if( libcdirectory_directory_entry_free( |
1679 | | &search_directory_entry, |
1680 | | error ) != 1 ) |
1681 | | { |
1682 | | libcerror_error_set( |
1683 | | error, |
1684 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1685 | | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
1686 | | "%s: unable to free search directory entry.", |
1687 | | function ); |
1688 | | |
1689 | | goto on_error; |
1690 | | } |
1691 | | return( entry_found ); |
1692 | | |
1693 | | on_error: |
1694 | | if( search_directory_entry != NULL ) |
1695 | | { |
1696 | | libcdirectory_directory_entry_free( |
1697 | | &search_directory_entry, |
1698 | | NULL ); |
1699 | | } |
1700 | | return( -1 ); |
1701 | | } |
1702 | | |
1703 | | #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */ |
1704 | | |