Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libphdi/libcdirectory/libcdirectory_directory.c
Line
Count
Source
1
/*
2
 * Directory functions
3
 *
4
 * Copyright (C) 2008-2026, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <memory.h>
24
#include <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( WINAPI )
34
#include <stddef.h>
35
#endif
36
37
#if defined( HAVE_SYS_STAT_H )
38
#include <sys/stat.h>
39
#endif
40
41
#if defined( WINAPI ) && !defined( __CYGWIN__ )
42
#include <direct.h>
43
#endif
44
45
#if defined( HAVE_DIRENT_H )
46
#include <dirent.h>
47
#endif
48
49
#if defined( HAVE_FCNTL_H )
50
#include <fcntl.h>
51
#endif
52
53
#if defined( HAVE_UNISTD_H )
54
#include <unistd.h>
55
#endif
56
57
#include "libcdirectory_definitions.h"
58
#include "libcdirectory_directory.h"
59
#include "libcdirectory_directory_entry.h"
60
#include "libcdirectory_libcerror.h"
61
#include "libcdirectory_system_string.h"
62
#include "libcdirectory_types.h"
63
#include "libcdirectory_wide_string.h"
64
65
/* Creates a directory
66
 * Make sure the value directory is referencing, is set to NULL
67
 * Returns 1 if successful or -1 on error
68
 */
69
int libcdirectory_directory_initialize(
70
     libcdirectory_directory_t **directory,
71
     libcerror_error_t **error )
72
0
{
73
0
  libcdirectory_internal_directory_t *internal_directory = NULL;
74
0
  static char *function                                  = "libcdirectory_directory_initialize";
75
76
0
  if( directory == NULL )
77
0
  {
78
0
    libcerror_error_set(
79
0
     error,
80
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
81
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
82
0
     "%s: invalid directory.",
83
0
     function );
84
85
0
    return( -1 );
86
0
  }
87
0
  if( *directory != NULL )
88
0
  {
89
0
    libcerror_error_set(
90
0
     error,
91
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
92
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
93
0
     "%s: invalid directory value already set.",
94
0
     function );
95
96
0
    return( -1 );
97
0
  }
98
0
  internal_directory = memory_allocate_structure(
99
0
                  libcdirectory_internal_directory_t );
100
101
0
  if( internal_directory == NULL )
102
0
  {
103
0
    libcerror_error_set(
104
0
     error,
105
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
106
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
107
0
     "%s: unable to create directory.",
108
0
     function );
109
110
0
    goto on_error;
111
0
  }
112
0
  if( memory_set(
113
0
       internal_directory,
114
0
       0,
115
0
       sizeof( libcdirectory_internal_directory_t ) ) == NULL )
116
0
  {
117
0
    libcerror_error_set(
118
0
     error,
119
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
120
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
121
0
     "%s: unable to clear directory.",
122
0
     function );
123
124
0
    goto on_error;
125
0
  }
126
#if defined( WINAPI )
127
  internal_directory->handle = INVALID_HANDLE_VALUE;
128
#endif
129
130
0
  *directory = (libcdirectory_directory_t *) internal_directory;
131
132
0
  return( 1 );
133
134
0
on_error:
135
0
  if( internal_directory != NULL )
136
0
  {
137
0
    memory_free(
138
0
     internal_directory );
139
0
  }
140
0
  return( -1 );
141
0
}
142
143
/* Frees a directory
144
 * Returns 1 if successful or -1 on error
145
 */
146
int libcdirectory_directory_free(
147
     libcdirectory_directory_t **directory,
148
     libcerror_error_t **error )
149
0
{
150
0
  libcdirectory_internal_directory_t *internal_directory = NULL;
151
0
  static char *function                                  = "libcdirectory_directory_free";
152
0
  int result                                             = 1;
153
154
0
  if( directory == NULL )
155
0
  {
156
0
    libcerror_error_set(
157
0
     error,
158
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
159
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
160
0
     "%s: invalid directory.",
161
0
     function );
162
163
0
    return( -1 );
164
0
  }
165
0
  if( *directory != NULL )
166
0
  {
167
0
    internal_directory = (libcdirectory_internal_directory_t *) *directory;
168
169
#if defined( WINAPI )
170
    if( internal_directory->handle != INVALID_HANDLE_VALUE )
171
#else
172
0
    if( internal_directory->stream != NULL )
173
0
#endif
174
0
    {
175
0
      if( libcdirectory_directory_close(
176
0
           *directory,
177
0
           error ) != 0 )
178
0
      {
179
0
        libcerror_error_set(
180
0
         error,
181
0
         LIBCERROR_ERROR_DOMAIN_IO,
182
0
         LIBCERROR_IO_ERROR_CLOSE_FAILED,
183
0
         "%s: unable to close directory.",
184
0
         function );
185
186
0
        result = -1;
187
0
      }
188
0
    }
189
#if defined( HAVE_DIRENT_H ) && !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE )
190
    if( internal_directory->path != NULL )
191
    {
192
      memory_free(
193
       internal_directory->path );
194
195
      internal_directory->path      = NULL;
196
      internal_directory->path_size = 0;
197
    }
198
#endif
199
0
    *directory = NULL;
200
201
0
    memory_free(
202
0
     internal_directory );
203
0
  }
204
0
  return( result );
205
0
}
206
207
#if defined( WINAPI ) && ( WINVER >= 0x0400 )
208
209
/* Opens a directory
210
 * This function uses the WINAPI function for Windows XP or later
211
 * Returns 1 if successful or -1 on error
212
 */
213
int libcdirectory_directory_open(
214
     libcdirectory_directory_t *directory,
215
     const char *directory_name,
216
     libcerror_error_t **error )
217
{
218
  libcdirectory_internal_directory_t *internal_directory                   = NULL;
219
  libcdirectory_internal_directory_entry_t *internal_first_directory_entry = NULL;
220
  system_character_t *system_directory_name                                = NULL;
221
  static char *function                                                    = "libcdirectory_directory_open";
222
  size_t directory_name_length                                             = 0;
223
  size_t system_directory_name_size                                        = 0;
224
  DWORD error_code                                                         = 0;
225
  DWORD file_attributes                                                    = 0;
226
227
  if( directory == NULL )
228
  {
229
    libcerror_error_set(
230
     error,
231
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
232
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
233
     "%s: invalid directory.",
234
     function );
235
236
    return( -1 );
237
  }
238
  internal_directory = (libcdirectory_internal_directory_t *) directory;
239
240
  if( directory_name == NULL )
241
  {
242
    libcerror_error_set(
243
     error,
244
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
245
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
246
     "%s: invalid directory name.",
247
     function );
248
249
    return( -1 );
250
  }
251
  if( internal_directory->handle != INVALID_HANDLE_VALUE )
252
  {
253
    libcerror_error_set(
254
     error,
255
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
256
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
257
     "%s: invalid directory - handle value already set.",
258
     function );
259
260
    return( -1 );
261
  }
262
  if( internal_directory->first_entry != NULL )
263
  {
264
    libcerror_error_set(
265
     error,
266
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
267
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
268
     "%s: invalid directory - first entry value already set.",
269
     function );
270
271
    return( -1 );
272
  }
273
  if( libcdirectory_directory_entry_initialize(
274
       &( internal_directory->first_entry ),
275
       error ) != 1 )
276
  {
277
    libcerror_error_set(
278
     error,
279
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
280
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
281
     "%s: unable to create first entry.",
282
     function );
283
284
    goto on_error;
285
  }
286
  if( internal_directory->first_entry == NULL )
287
  {
288
    libcerror_error_set(
289
     error,
290
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
291
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
292
     "%s: invalid directory - missing first entry.",
293
     function );
294
295
    goto on_error;
296
  }
297
  internal_first_directory_entry = (libcdirectory_internal_directory_entry_t *) internal_directory->first_entry;
298
299
  directory_name_length = narrow_string_length(
300
                           directory_name );
301
302
  if( libcdirectory_system_string_size_from_narrow_string(
303
       directory_name,
304
       directory_name_length + 1,
305
       &system_directory_name_size,
306
       error ) != 1 )
307
  {
308
    libcerror_error_set(
309
     error,
310
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
311
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
312
     "%s: unable to determine system character directory name size.",
313
     function );
314
315
    goto on_error;
316
  }
317
  /* Find files requires a search path, add a \ and * if necessary
318
   */
319
  if( ( directory_name_length < 2 )
320
   || ( directory_name[ directory_name_length - 1 ] != '\\' ) )
321
  {
322
    system_directory_name_size += 1;
323
  }
324
  system_directory_name_size += 1;
325
326
  system_directory_name = system_string_allocate(
327
                           system_directory_name_size );
328
329
  if( system_directory_name == NULL )
330
  {
331
    libcerror_error_set(
332
     error,
333
     LIBCERROR_ERROR_DOMAIN_MEMORY,
334
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
335
     "%s: unable to create system character directory name.",
336
     function );
337
338
    goto on_error;
339
  }
340
  if( libcdirectory_system_string_copy_from_narrow_string(
341
       system_directory_name,
342
       system_directory_name_size,
343
       directory_name,
344
       directory_name_length + 1,
345
       error ) != 1 )
346
  {
347
    libcerror_error_set(
348
     error,
349
     LIBCERROR_ERROR_DOMAIN_MEMORY,
350
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
351
     "%s: unable to set system character directory name.",
352
     function );
353
354
    goto on_error;
355
  }
356
  /* GetFileAttributes on a non-directory will be successful
357
   */
358
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
359
  file_attributes = GetFileAttributesW(
360
                     (LPCWSTR) system_directory_name );
361
#else
362
  file_attributes = GetFileAttributesA(
363
                     (LPCSTR) system_directory_name );
364
#endif
365
  if( ( file_attributes & FILE_ATTRIBUTE_DIRECTORY ) == 0 )
366
  {
367
    error_code = GetLastError();
368
369
    libcerror_system_set_error(
370
     error,
371
     LIBCERROR_ERROR_DOMAIN_IO,
372
     LIBCERROR_IO_ERROR_OPEN_FAILED,
373
     error_code,
374
     "%s: unsupported file type - not a directory.",
375
     function );
376
377
    goto on_error;
378
  }
379
  /* Find files requires a search path, add a \ and * if necessary
380
   */
381
  if( ( directory_name_length < 2 )
382
   || ( directory_name[ directory_name_length - 1 ] != '\\' ) )
383
  {
384
    system_directory_name[ system_directory_name_size - 3 ] = (system_character_t) '\\';
385
  }
386
  system_directory_name[ system_directory_name_size - 2 ] = (system_character_t) '*';
387
  system_directory_name[ system_directory_name_size - 1 ] = 0;
388
389
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
390
  /* Cannot use FindFirstFileA here because it requires a narrow version of WIN32_FIND_DATA
391
   */
392
  internal_directory->handle = FindFirstFileW(
393
                                (LPCWSTR) system_directory_name,
394
                                &( internal_first_directory_entry->find_data ) );
395
#else
396
  internal_directory->handle = FindFirstFileA(
397
                                (LPCSTR) system_directory_name,
398
                                &( internal_first_directory_entry->find_data ) );
399
#endif
400
  if( internal_directory->handle == INVALID_HANDLE_VALUE )
401
  {
402
    error_code = GetLastError();
403
404
    libcerror_system_set_error(
405
     error,
406
     LIBCERROR_ERROR_DOMAIN_IO,
407
     LIBCERROR_IO_ERROR_OPEN_FAILED,
408
     error_code,
409
     "%s: unable to open directory.",
410
     function );
411
412
    goto on_error;
413
  }
414
  memory_free(
415
   system_directory_name );
416
417
  return( 1 );
418
419
on_error:
420
  if( internal_directory->handle != INVALID_HANDLE_VALUE )
421
  {
422
    FindClose(
423
     internal_directory->handle );
424
425
    internal_directory->handle = INVALID_HANDLE_VALUE;
426
  }
427
  if( system_directory_name != NULL )
428
  {
429
    memory_free(
430
     system_directory_name );
431
  }
432
  if( internal_directory->first_entry != NULL )
433
  {
434
    libcdirectory_directory_entry_free(
435
     &( internal_directory->first_entry ),
436
     NULL );
437
  }
438
  return( -1 );
439
}
440
441
#elif defined( WINAPI )
442
443
/* TODO */
444
#error WINAPI open directory for Windows earlier than NT4 not implemented
445
446
#elif defined( HAVE_OPENDIR )
447
448
/* Opens a directory
449
 * This function uses the POSIX opendir function
450
 * Returns 1 if successful or -1 on error
451
 */
452
int libcdirectory_directory_open(
453
     libcdirectory_directory_t *directory,
454
     const char *directory_name,
455
     libcerror_error_t **error )
456
0
{
457
0
  libcdirectory_internal_directory_t *internal_directory = NULL;
458
0
  static char *function                                  = "libcdirectory_directory_open";
459
460
#if !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE )
461
  char *system_directory_name                            = NULL;
462
  size_t directory_name_length                           = 0;
463
  size_t system_directory_name_size                      = 0;
464
#endif
465
466
0
  if( directory == NULL )
467
0
  {
468
0
    libcerror_error_set(
469
0
     error,
470
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
471
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
472
0
     "%s: invalid directory.",
473
0
     function );
474
475
0
    return( -1 );
476
0
  }
477
0
  internal_directory = (libcdirectory_internal_directory_t *) directory;
478
479
0
  if( directory_name == NULL )
480
0
  {
481
0
    libcerror_error_set(
482
0
     error,
483
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
484
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
485
0
     "%s: invalid directory name.",
486
0
     function );
487
488
0
    return( -1 );
489
0
  }
490
0
  if( internal_directory->stream != NULL )
491
0
  {
492
0
    libcerror_error_set(
493
0
     error,
494
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
495
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
496
0
     "%s: invalid directory - stream value already set.",
497
0
     function );
498
499
0
    return( -1 );
500
0
  }
501
#if !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE )
502
  if( internal_directory->path != NULL )
503
  {
504
    libcerror_error_set(
505
     error,
506
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
507
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
508
     "%s: invalid directory - path value already set.",
509
     function );
510
511
    return( -1 );
512
  }
513
#endif
514
0
  internal_directory->stream = opendir(
515
0
                                directory_name );
516
517
0
  if( internal_directory->stream == NULL )
518
0
  {
519
0
    libcerror_system_set_error(
520
0
     error,
521
0
     LIBCERROR_ERROR_DOMAIN_IO,
522
0
     LIBCERROR_IO_ERROR_OPEN_FAILED,
523
0
     errno,
524
0
     "%s: unable to open directory.",
525
0
     function );
526
527
0
    goto on_error;
528
0
  }
529
#if !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE )
530
  directory_name_length = narrow_string_length(
531
                           directory_name );
532
533
  if( libcdirectory_system_string_size_from_narrow_string(
534
       directory_name,
535
       directory_name_length + 1,
536
       &system_directory_name_size,
537
       error ) != 1 )
538
  {
539
    libcerror_error_set(
540
     error,
541
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
542
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
543
     "%s: unable to determine system character directory name size.",
544
     function );
545
546
    goto on_error;
547
  }
548
  system_directory_name = system_string_allocate(
549
                           system_directory_name_size );
550
551
  if( system_directory_name == NULL )
552
  {
553
    libcerror_error_set(
554
     error,
555
     LIBCERROR_ERROR_DOMAIN_MEMORY,
556
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
557
     "%s: unable to create system character directory name.",
558
     function );
559
560
    goto on_error;
561
  }
562
  if( libcdirectory_system_string_copy_from_narrow_string(
563
       system_directory_name,
564
       system_directory_name_size,
565
       directory_name,
566
       directory_name_length + 1,
567
       error ) != 1 )
568
  {
569
    libcerror_error_set(
570
     error,
571
     LIBCERROR_ERROR_DOMAIN_MEMORY,
572
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
573
     "%s: unable to set system character directory name.",
574
     function );
575
576
    goto on_error;
577
  }
578
  if( internal_directory->path != NULL )
579
  {
580
    memory_free(
581
     internal_directory->path );
582
  }
583
  internal_directory->path      = system_directory_name;
584
  internal_directory->path_size = system_directory_name_size;
585
586
#endif /* !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE ) */
587
588
0
  return( 1 );
589
590
0
on_error:
591
0
  if( internal_directory->stream != NULL )
592
0
  {
593
0
    closedir(
594
0
     internal_directory->stream );
595
596
0
    internal_directory->stream = NULL;
597
0
  }
598
#if !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE )
599
  if( system_directory_name != NULL )
600
  {
601
    memory_free(
602
     system_directory_name );
603
  }
604
#endif
605
0
  return( -1 );
606
0
}
607
608
#else
609
#error Missing open directory function
610
#endif
611
612
#if defined( HAVE_WIDE_CHARACTER_TYPE )
613
614
#if defined( WINAPI ) && ( WINVER >= 0x0400 )
615
616
/* Opens a directory
617
 * This function uses the WINAPI function for Windows XP or later
618
 * Returns 1 if successful or -1 on error
619
 */
620
int libcdirectory_directory_open_wide(
621
     libcdirectory_directory_t *directory,
622
     const wchar_t *directory_name,
623
     libcerror_error_t **error )
624
{
625
  libcdirectory_internal_directory_t *internal_directory                   = NULL;
626
  libcdirectory_internal_directory_entry_t *internal_first_directory_entry = NULL;
627
  system_character_t *system_directory_name                                = NULL;
628
  static char *function                                                    = "libcdirectory_directory_open_wide";
629
  size_t directory_name_length                                             = 0;
630
  size_t system_directory_name_size                                        = 0;
631
  DWORD error_code                                                         = 0;
632
  DWORD file_attributes                                                    = 0;
633
634
  if( directory == NULL )
635
  {
636
    libcerror_error_set(
637
     error,
638
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
639
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
640
     "%s: invalid directory.",
641
     function );
642
643
    return( -1 );
644
  }
645
  internal_directory = (libcdirectory_internal_directory_t *) directory;
646
647
  if( directory_name == NULL )
648
  {
649
    libcerror_error_set(
650
     error,
651
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
652
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
653
     "%s: invalid directory name.",
654
     function );
655
656
    return( -1 );
657
  }
658
  if( internal_directory->handle != INVALID_HANDLE_VALUE )
659
  {
660
    libcerror_error_set(
661
     error,
662
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
663
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
664
     "%s: invalid directory - handle value already set.",
665
     function );
666
667
    return( -1 );
668
  }
669
  if( internal_directory->first_entry != NULL )
670
  {
671
    libcerror_error_set(
672
     error,
673
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
674
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
675
     "%s: invalid directory - first entry value already set.",
676
     function );
677
678
    return( -1 );
679
  }
680
  if( libcdirectory_directory_entry_initialize(
681
       &( internal_directory->first_entry ),
682
       error ) != 1 )
683
  {
684
    libcerror_error_set(
685
     error,
686
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
687
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
688
     "%s: unable to create first entry.",
689
     function );
690
691
    goto on_error;
692
  }
693
  if( internal_directory->first_entry == NULL )
694
  {
695
    libcerror_error_set(
696
     error,
697
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
698
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
699
     "%s: invalid directory - missing first entry.",
700
     function );
701
702
    goto on_error;
703
  }
704
  internal_first_directory_entry = (libcdirectory_internal_directory_entry_t *) internal_directory->first_entry;
705
706
  directory_name_length = wide_string_length(
707
                           directory_name );
708
709
  if( libcdirectory_system_string_size_from_wide_string(
710
       directory_name,
711
       directory_name_length + 1,
712
       &system_directory_name_size,
713
       error ) != 1 )
714
  {
715
    libcerror_error_set(
716
     error,
717
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
718
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
719
     "%s: unable to determine system character directory name size.",
720
     function );
721
722
    goto on_error;
723
  }
724
  /* Find files requires a search path, add a \ and * if necessary
725
   */
726
  if( ( directory_name_length < 2 )
727
   || ( directory_name[ directory_name_length - 1 ] != (wchar_t) '\\' ) )
728
  {
729
    system_directory_name_size += 1;
730
  }
731
  system_directory_name_size += 1;
732
733
  system_directory_name = system_string_allocate(
734
                           system_directory_name_size );
735
736
  if( system_directory_name == NULL )
737
  {
738
    libcerror_error_set(
739
     error,
740
     LIBCERROR_ERROR_DOMAIN_MEMORY,
741
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
742
     "%s: unable to create system character directory name.",
743
     function );
744
745
    goto on_error;
746
  }
747
  if( libcdirectory_system_string_copy_from_wide_string(
748
       system_directory_name,
749
       system_directory_name_size,
750
       directory_name,
751
       directory_name_length + 1,
752
       error ) != 1 )
753
  {
754
    libcerror_error_set(
755
     error,
756
     LIBCERROR_ERROR_DOMAIN_MEMORY,
757
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
758
     "%s: unable to set system character directory name.",
759
     function );
760
761
    goto on_error;
762
  }
763
  /* GetFileAttributes on a non-directory will be successful
764
   */
765
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
766
  file_attributes = GetFileAttributesW(
767
                     (LPCWSTR) system_directory_name );
768
#else
769
  file_attributes = GetFileAttributesA(
770
                     (LPCSTR) system_directory_name );
771
#endif
772
  if( ( file_attributes & FILE_ATTRIBUTE_DIRECTORY ) == 0 )
773
  {
774
    error_code = GetLastError();
775
776
    libcerror_system_set_error(
777
     error,
778
     LIBCERROR_ERROR_DOMAIN_IO,
779
     LIBCERROR_IO_ERROR_OPEN_FAILED,
780
     error_code,
781
     "%s: unsupported file type - not a directory.",
782
     function );
783
784
    goto on_error;
785
  }
786
  /* Find files requires a search path, add a \ and * if necessary
787
   */
788
  if( ( directory_name_length < 2 )
789
   || ( directory_name[ directory_name_length - 1 ] != (wchar_t) '\\' ) )
790
  {
791
    system_directory_name[ system_directory_name_size - 3 ] = (system_character_t) '\\';
792
  }
793
  system_directory_name[ system_directory_name_size - 2 ] = (system_character_t) '*';
794
  system_directory_name[ system_directory_name_size - 1 ] = 0;
795
796
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
797
  internal_directory->handle = FindFirstFileW(
798
                                (LPCWSTR) system_directory_name,
799
                                &( internal_first_directory_entry->find_data ) );
800
#else
801
  /* Cannot use FindFirstFileW here because it requires a wide version of WIN32_FIND_DATA
802
   */
803
  internal_directory->handle = FindFirstFileA(
804
                                (LPCSTR) system_directory_name,
805
                                &( internal_first_directory_entry->find_data ) );
806
#endif
807
  if( internal_directory->handle == INVALID_HANDLE_VALUE )
808
  {
809
    error_code = GetLastError();
810
811
    libcerror_system_set_error(
812
     error,
813
     LIBCERROR_ERROR_DOMAIN_IO,
814
     LIBCERROR_IO_ERROR_OPEN_FAILED,
815
     error_code,
816
     "%s: unable to open directory.",
817
     function );
818
819
    goto on_error;
820
  }
821
  memory_free(
822
   system_directory_name );
823
824
  return( 1 );
825
826
on_error:
827
  if( internal_directory->handle != INVALID_HANDLE_VALUE )
828
  {
829
    FindClose(
830
     internal_directory->handle );
831
832
    internal_directory->handle = INVALID_HANDLE_VALUE;
833
  }
834
  if( system_directory_name != NULL )
835
  {
836
    memory_free(
837
     system_directory_name );
838
  }
839
  if( internal_directory->first_entry != NULL )
840
  {
841
    libcdirectory_directory_entry_free(
842
     &( internal_directory->first_entry ),
843
     NULL );
844
  }
845
  return( -1 );
846
}
847
848
#elif defined( WINAPI )
849
850
/* TODO */
851
#error WINAPI open directory for Windows earlier than NT4 not implemented
852
853
#elif defined( HAVE_OPENDIR )
854
855
/* Opens a directory
856
 * This function uses the POSIX opendir function
857
 * Returns 1 if successful or -1 on error
858
 */
859
int libcdirectory_directory_open_wide(
860
     libcdirectory_directory_t *directory,
861
     const wchar_t *directory_name,
862
     libcerror_error_t **error )
863
{
864
  libcdirectory_internal_directory_t *internal_directory = NULL;
865
  static char *function                                  = "libcdirectory_directory_open_wide";
866
  char *system_directory_name                            = NULL;
867
  size_t directory_name_length                           = 0;
868
  size_t system_directory_name_size                      = 0;
869
870
  if( directory == NULL )
871
  {
872
    libcerror_error_set(
873
     error,
874
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
875
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
876
     "%s: invalid directory.",
877
     function );
878
879
    return( -1 );
880
  }
881
  internal_directory = (libcdirectory_internal_directory_t *) directory;
882
883
  if( directory_name == NULL )
884
  {
885
    libcerror_error_set(
886
     error,
887
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
888
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
889
     "%s: invalid directory name.",
890
     function );
891
892
    return( -1 );
893
  }
894
  if( internal_directory->stream != NULL )
895
  {
896
    libcerror_error_set(
897
     error,
898
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
899
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
900
     "%s: invalid directory - stream value already set.",
901
     function );
902
903
    return( -1 );
904
  }
905
#if !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE )
906
  if( internal_directory->path != NULL )
907
  {
908
    libcerror_error_set(
909
     error,
910
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
911
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
912
     "%s: invalid directory - path value already set.",
913
     function );
914
915
    return( -1 );
916
  }
917
#endif
918
  directory_name_length = wide_string_length(
919
                           directory_name );
920
921
  if( libcdirectory_system_string_size_from_wide_string(
922
       directory_name,
923
       directory_name_length + 1,
924
       &system_directory_name_size,
925
       error ) != 1 )
926
  {
927
    libcerror_error_set(
928
     error,
929
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
930
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
931
     "%s: unable to determine system character directory name size.",
932
     function );
933
934
    goto on_error;
935
  }
936
  system_directory_name = system_string_allocate(
937
                           system_directory_name_size );
938
939
  if( system_directory_name == NULL )
940
  {
941
    libcerror_error_set(
942
     error,
943
     LIBCERROR_ERROR_DOMAIN_MEMORY,
944
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
945
     "%s: unable to create system character directory name.",
946
     function );
947
948
    goto on_error;
949
  }
950
  if( libcdirectory_system_string_copy_from_wide_string(
951
       system_directory_name,
952
       system_directory_name_size,
953
       directory_name,
954
       directory_name_length + 1,
955
       error ) != 1 )
956
  {
957
    libcerror_error_set(
958
     error,
959
     LIBCERROR_ERROR_DOMAIN_MEMORY,
960
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
961
     "%s: unable to set system character directory name.",
962
     function );
963
964
    goto on_error;
965
  }
966
  internal_directory->stream = opendir(
967
                                system_directory_name );
968
969
  if( internal_directory->stream == NULL )
970
  {
971
    libcerror_system_set_error(
972
     error,
973
     LIBCERROR_ERROR_DOMAIN_IO,
974
     LIBCERROR_IO_ERROR_OPEN_FAILED,
975
     errno,
976
     "%s: unable to open directory.",
977
     function );
978
979
    goto on_error;
980
  }
981
#if !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE )
982
  internal_directory->path      = system_directory_name;
983
  internal_directory->path_size = system_directory_name_size;
984
#else
985
  memory_free(
986
   system_directory_name );
987
988
  system_directory_name = NULL;
989
#endif
990
  return( 1 );
991
992
on_error:
993
  if( internal_directory->stream != NULL )
994
  {
995
    closedir(
996
     internal_directory->stream );
997
998
    internal_directory->stream = NULL;
999
  }
1000
  if( system_directory_name != NULL )
1001
  {
1002
    memory_free(
1003
     system_directory_name );
1004
  }
1005
  return( -1 );
1006
}
1007
1008
#else
1009
#error Missing wide open directory function
1010
#endif
1011
1012
#endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
1013
1014
#if defined( WINAPI ) && ( WINVER >= 0x0400 )
1015
1016
/* Closes a directory
1017
 * This function uses the WINAPI function for Windows XP or later
1018
 * Returns 0 if successful or -1 on error
1019
 */
1020
int libcdirectory_directory_close(
1021
     libcdirectory_directory_t *directory,
1022
     libcerror_error_t **error )
1023
{
1024
  libcdirectory_internal_directory_t *internal_directory = NULL;
1025
  static char *function                                  = "libcdirectory_directory_close";
1026
  int result                                             = 0;
1027
  DWORD error_code                                       = 0;
1028
1029
  if( directory == NULL )
1030
  {
1031
    libcerror_error_set(
1032
     error,
1033
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1034
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1035
     "%s: invalid directory.",
1036
     function );
1037
1038
    return( -1 );
1039
  }
1040
  internal_directory = (libcdirectory_internal_directory_t *) directory;
1041
1042
  if( internal_directory->handle != INVALID_HANDLE_VALUE )
1043
  {
1044
    if( FindClose(
1045
         internal_directory->handle ) == 0 )
1046
    {
1047
      error_code = GetLastError();
1048
1049
      libcerror_system_set_error(
1050
       error,
1051
       LIBCERROR_ERROR_DOMAIN_IO,
1052
       LIBCERROR_IO_ERROR_CLOSE_FAILED,
1053
       error_code,
1054
       "%s: unable to close directory.",
1055
       function );
1056
1057
      result = -1;
1058
    }
1059
    internal_directory->handle = INVALID_HANDLE_VALUE;
1060
  }
1061
  if( internal_directory->first_entry != NULL )
1062
  {
1063
    if( libcdirectory_directory_entry_free(
1064
         &( internal_directory->first_entry ),
1065
         error ) != 1 )
1066
    {
1067
      libcerror_error_set(
1068
       error,
1069
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1070
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1071
       "%s: unable to free first entry.",
1072
       function );
1073
1074
      result = -1;
1075
    }
1076
  }
1077
  return( result );
1078
}
1079
1080
#elif defined( WINAPI )
1081
1082
/* TODO */
1083
#error WINAPI close directory for Windows earlier than NT4 not implemented
1084
1085
#elif defined( HAVE_CLOSEDIR )
1086
1087
/* Closes a directory
1088
 * This function uses the POSIX closedir function
1089
 * Returns 0 if successful or -1 on error
1090
 */
1091
int libcdirectory_directory_close(
1092
     libcdirectory_directory_t *directory,
1093
     libcerror_error_t **error )
1094
0
{
1095
0
  libcdirectory_internal_directory_t *internal_directory = NULL;
1096
0
  static char *function                                  = "libcdirectory_directory_close";
1097
0
  int result                                             = 0;
1098
1099
0
  if( directory == NULL )
1100
0
  {
1101
0
    libcerror_error_set(
1102
0
     error,
1103
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1104
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1105
0
     "%s: invalid directory.",
1106
0
     function );
1107
1108
0
    return( -1 );
1109
0
  }
1110
0
  internal_directory = (libcdirectory_internal_directory_t *) directory;
1111
1112
0
  if( internal_directory->stream != NULL )
1113
0
  {
1114
0
    if( closedir(
1115
0
         internal_directory->stream ) != 0 )
1116
0
    {
1117
0
      libcerror_system_set_error(
1118
0
       error,
1119
0
       LIBCERROR_ERROR_DOMAIN_IO,
1120
0
       LIBCERROR_IO_ERROR_CLOSE_FAILED,
1121
0
       errno,
1122
0
       "%s: unable to close directory.",
1123
0
       function );
1124
1125
0
      result = -1;
1126
0
    }
1127
0
    internal_directory->stream = NULL;
1128
0
  }
1129
#if defined( HAVE_DIRENT_H ) && !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE )
1130
  if( internal_directory->path != NULL )
1131
  {
1132
    memory_free(
1133
     internal_directory->path );
1134
1135
    internal_directory->path      = NULL;
1136
    internal_directory->path_size = 0;
1137
  }
1138
#endif
1139
0
  return( result );
1140
0
}
1141
1142
#else
1143
#error Missing close directory function
1144
#endif
1145
1146
#if defined( WINAPI ) && ( WINVER >= 0x0400 )
1147
1148
/* Reads a directory
1149
 * This function uses the WINAPI function for Windows XP or later
1150
 * Returns 1 if successful, 0 if no such entry or -1 on error
1151
 */
1152
int libcdirectory_directory_read_entry(
1153
     libcdirectory_directory_t *directory,
1154
     libcdirectory_directory_entry_t *directory_entry,
1155
     libcerror_error_t **error )
1156
{
1157
  libcdirectory_internal_directory_t *internal_directory             = NULL;
1158
  libcdirectory_internal_directory_entry_t *internal_directory_entry = NULL;
1159
  static char *function                                              = "libcdirectory_directory_read_entry";
1160
  DWORD error_code                                                   = 0;
1161
1162
  if( directory == NULL )
1163
  {
1164
    libcerror_error_set(
1165
     error,
1166
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1167
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1168
     "%s: invalid directory.",
1169
     function );
1170
1171
    return( -1 );
1172
  }
1173
  internal_directory = (libcdirectory_internal_directory_t *) directory;
1174
1175
  if( directory_entry == NULL )
1176
  {
1177
    libcerror_error_set(
1178
     error,
1179
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1180
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1181
     "%s: invalid directory entry.",
1182
     function );
1183
1184
    return( -1 );
1185
  }
1186
  internal_directory_entry = (libcdirectory_internal_directory_entry_t *) directory_entry;
1187
1188
  if( internal_directory->handle == INVALID_HANDLE_VALUE )
1189
  {
1190
    libcerror_error_set(
1191
     error,
1192
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1193
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1194
     "%s: invalid directory - missing handle.",
1195
     function );
1196
1197
    return( -1 );
1198
  }
1199
  if( internal_directory->first_entry != NULL )
1200
  {
1201
    if( libcdirectory_directory_entry_copy(
1202
         directory_entry,
1203
         internal_directory->first_entry,
1204
         error ) != 1 )
1205
    {
1206
      libcerror_error_set(
1207
       error,
1208
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1209
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1210
       "%s: unable to copy first directory entry.",
1211
       function );
1212
1213
      return( -1 );
1214
    }
1215
    if( libcdirectory_directory_entry_free(
1216
         (libcdirectory_directory_t **) &( internal_directory->first_entry ),
1217
         error ) != 1 )
1218
    {
1219
      libcerror_error_set(
1220
       error,
1221
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1222
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1223
       "%s: unable to free first entry.",
1224
       function );
1225
1226
      return( -1 );
1227
    }
1228
  }
1229
  else
1230
  {
1231
#if defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_SYSTEM_CHARACTER )
1232
    if( internal_directory_entry->narrow_name != NULL )
1233
    {
1234
      memory_free(
1235
       internal_directory_entry->narrow_name );
1236
1237
      internal_directory_entry->narrow_name = NULL;
1238
      internal_directory_entry->name_size   = 0;
1239
    }
1240
#else
1241
    if( internal_directory_entry->wide_name != NULL )
1242
    {
1243
      memory_free(
1244
       internal_directory_entry->wide_name );
1245
1246
      internal_directory_entry->wide_name = NULL;
1247
      internal_directory_entry->name_size = 0;
1248
    }
1249
#endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
1250
1251
    if( FindNextFile(
1252
         internal_directory->handle,
1253
         &( internal_directory_entry->find_data ) ) == 0 )
1254
    {
1255
      error_code = GetLastError();
1256
1257
      if( error_code == ERROR_NO_MORE_FILES )
1258
      {
1259
        return( 0 );
1260
      }
1261
      else
1262
      {
1263
        libcerror_system_set_error(
1264
         error,
1265
         LIBCERROR_ERROR_DOMAIN_IO,
1266
         LIBCERROR_IO_ERROR_READ_FAILED,
1267
         error_code,
1268
         "%s: unable to read from directory.",
1269
         function );
1270
      }
1271
      return( -1 );
1272
    }
1273
  }
1274
  return( 1 );
1275
}
1276
1277
#elif defined( WINAPI )
1278
1279
/* TODO */
1280
#error WINAPI read directory entry for Windows earlier than NT4 not implemented
1281
1282
#elif defined( HAVE_READDIR ) || defined( HAVE_READDIR_R )
1283
1284
/* Reads a directory
1285
 * This function uses the POSIX readdir or readdir_r function
1286
 * Returns 1 if successful, 0 if no such entry or -1 on error
1287
 */
1288
int libcdirectory_directory_read_entry(
1289
     libcdirectory_directory_t *directory,
1290
     libcdirectory_directory_entry_t *directory_entry,
1291
     libcerror_error_t **error )
1292
0
{
1293
0
  libcdirectory_internal_directory_t *internal_directory             = NULL;
1294
0
  libcdirectory_internal_directory_entry_t *internal_directory_entry = NULL;
1295
0
  struct dirent *result_directory_entry                              = NULL;
1296
0
  static char *function                                              = "libcdirectory_directory_read_entry";
1297
0
  int result                                                         = 0;
1298
1299
0
  size_t directory_entry_name_length                                 = 0;
1300
0
  size_t directory_entry_record_size                                 = 0;
1301
1302
#if !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE )
1303
  struct stat file_statistics;
1304
1305
  system_character_t *system_directory_entry_path                    = NULL;
1306
  size_t system_directory_entry_path_index                           = 0;
1307
  size_t system_directory_entry_path_size                            = 0;
1308
#endif
1309
1310
0
  if( directory == NULL )
1311
0
  {
1312
0
    libcerror_error_set(
1313
0
     error,
1314
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1315
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1316
0
     "%s: invalid directory.",
1317
0
     function );
1318
1319
0
    return( -1 );
1320
0
  }
1321
0
  internal_directory = (libcdirectory_internal_directory_t *) directory;
1322
1323
0
  if( directory_entry == NULL )
1324
0
  {
1325
0
    libcerror_error_set(
1326
0
     error,
1327
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1328
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1329
0
     "%s: invalid directory entry.",
1330
0
     function );
1331
1332
0
    return( -1 );
1333
0
  }
1334
0
  internal_directory_entry = (libcdirectory_internal_directory_entry_t *) directory_entry;
1335
1336
0
  if( internal_directory->stream == NULL )
1337
0
  {
1338
0
    libcerror_error_set(
1339
0
     error,
1340
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1341
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1342
0
     "%s: invalid directory - missing stream.",
1343
0
     function );
1344
1345
0
    return( -1 );
1346
0
  }
1347
#if !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE )
1348
  if( internal_directory->path == NULL )
1349
  {
1350
    libcerror_error_set(
1351
     error,
1352
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1353
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1354
     "%s: invalid directory - missing path.",
1355
     function );
1356
1357
    return( -1 );
1358
  }
1359
#endif
1360
#if defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_SYSTEM_CHARACTER )
1361
  if( internal_directory_entry->narrow_name != NULL )
1362
  {
1363
    memory_free(
1364
     internal_directory_entry->narrow_name );
1365
1366
    internal_directory_entry->narrow_name = NULL;
1367
    internal_directory_entry->name_size   = 0;
1368
  }
1369
#else
1370
0
  if( internal_directory_entry->wide_name != NULL )
1371
0
  {
1372
0
    memory_free(
1373
0
     internal_directory_entry->wide_name );
1374
1375
0
    internal_directory_entry->wide_name = NULL;
1376
0
    internal_directory_entry->name_size = 0;
1377
0
  }
1378
0
#endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
1379
1380
#if defined( HAVE_READDIR ) && ( !defined( HAVE_READDIR_R ) || defined( HAVE_DEPRECATED_READDIR_R ) )
1381
1382
  errno = 0;
1383
1384
  result_directory_entry = readdir(
1385
                            internal_directory->stream );
1386
1387
  if( ( result_directory_entry == NULL )
1388
    && ( errno != 0 ) )
1389
  {
1390
    result = -1;
1391
  }
1392
#else
1393
0
  result = readdir_r(
1394
0
            internal_directory->stream,
1395
0
            &( internal_directory_entry->entry ),
1396
0
            &result_directory_entry );
1397
1398
0
#endif /* defined( HAVE_READDIR ) && ( !defined( HAVE_READDIR_R ) || defined( HAVE_DEPRECATED_READDIR_R ) ) */
1399
1400
0
  if( result != 0 )
1401
0
  {
1402
0
    libcerror_system_set_error(
1403
0
     error,
1404
0
     LIBCERROR_ERROR_DOMAIN_IO,
1405
0
     LIBCERROR_IO_ERROR_READ_FAILED,
1406
0
     errno,
1407
0
     "%s: unable to read from directory.",
1408
0
     function );
1409
1410
0
    goto on_error;
1411
0
  }
1412
0
  if( result_directory_entry == NULL )
1413
0
  {
1414
0
    return( 0 );
1415
0
  }
1416
#if defined( HAVE_READDIR ) && ( !defined( HAVE_READDIR_R ) || defined( HAVE_DEPRECATED_READDIR_R ) )
1417
1418
  /* Note the size of a directory entry record in the stream buffer can be smaller
1419
   * or (due to alignment padding) larger than sizeof( struct dirent )
1420
   */
1421
  directory_entry_name_length = narrow_string_length(
1422
                                 (char *) result_directory_entry->d_name );
1423
1424
  if( directory_entry_name_length >= sizeof( internal_directory_entry->entry.d_name ) )
1425
  {
1426
    libcerror_error_set(
1427
     error,
1428
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1429
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1430
     "%s: unsupported directory entry record.",
1431
     function );
1432
1433
    goto on_error;
1434
  }
1435
  directory_entry_record_size = offsetof( struct dirent, d_name ) + directory_entry_name_length + 1;
1436
1437
#if defined( HAVE_STRUCT_DIRENT_D_RECLEN )
1438
  /* Note that MinGW defines d_reclen as 0
1439
   */
1440
  if( result_directory_entry->d_reclen != 0 )
1441
  {
1442
    if( (size_t) result_directory_entry->d_reclen < directory_entry_record_size )
1443
    {
1444
      libcerror_error_set(
1445
       error,
1446
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1447
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1448
       "%s: unsupported directory entry record.",
1449
       function );
1450
1451
      goto on_error;
1452
    }
1453
    directory_entry_record_size = result_directory_entry->d_reclen;
1454
  }
1455
#endif /* defined( HAVE_STRUCT_DIRENT_D_RECLEN ) */
1456
1457
  if( directory_entry_record_size > sizeof( struct dirent ) )
1458
  {
1459
    directory_entry_record_size = sizeof( struct dirent );
1460
  }
1461
  if( memory_set(
1462
       &( internal_directory_entry->entry ),
1463
       0,
1464
       sizeof( struct dirent ) ) == NULL )
1465
  {
1466
    libcerror_error_set(
1467
     error,
1468
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1469
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
1470
     "%s: unable to clear directory entry.",
1471
     function );
1472
1473
    goto on_error;
1474
  }
1475
  if( memory_copy(
1476
       &( internal_directory_entry->entry ),
1477
       result_directory_entry,
1478
       directory_entry_record_size ) == NULL )
1479
  {
1480
    libcerror_error_set(
1481
     error,
1482
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1483
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1484
     "%s: unable to copy directory entry.",
1485
     function );
1486
1487
    goto on_error;
1488
  }
1489
#endif /* defined( HAVE_READDIR ) && ( !defined( HAVE_READDIR_R ) || defined( HAVE_DEPRECATED_READDIR_R ) ) */
1490
1491
#if !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE )
1492
  directory_entry_name_length = narrow_string_length(
1493
                                 (char *) internal_directory_entry->entry.d_name );
1494
1495
  if( ( directory_entry_name_length == 1 )
1496
   && ( internal_directory_entry->entry.d_name[ 0 ] == '.' ) )
1497
  {
1498
    file_statistics.st_mode = S_IFDIR;
1499
  }
1500
  else if( ( directory_entry_name_length == 2 )
1501
        && ( internal_directory_entry->entry.d_name[ 0 ] == '.' )
1502
        && ( internal_directory_entry->entry.d_name[ 1 ] == '.' ) )
1503
  {
1504
    file_statistics.st_mode = S_IFDIR;
1505
  }
1506
  else
1507
  {
1508
    system_directory_entry_path_size = internal_directory->path_size + directory_entry_name_length + 1;
1509
1510
    system_directory_entry_path = system_string_allocate(
1511
                                   system_directory_entry_path_size );
1512
1513
    if( system_directory_entry_path == NULL )
1514
    {
1515
      libcerror_error_set(
1516
       error,
1517
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1518
       LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
1519
       "%s: unable to create system character directory entry path.",
1520
       function );
1521
1522
      goto on_error;
1523
    }
1524
    if( system_string_copy(
1525
         system_directory_entry_path,
1526
         internal_directory->path,
1527
         internal_directory->path_size - 1 ) == NULL )
1528
    {
1529
      libcerror_error_set(
1530
       error,
1531
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1532
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1533
       "%s: unable to copy directory path to system character directory path.",
1534
       function );
1535
1536
      goto on_error;
1537
    }
1538
    system_directory_entry_path_index = internal_directory->path_size - 1;
1539
1540
    system_directory_entry_path[ system_directory_entry_path_index++ ] = (system_character_t) '/';
1541
1542
    if( libcdirectory_system_string_copy_from_narrow_string(
1543
         &( system_directory_entry_path[ system_directory_entry_path_index ] ),
1544
         system_directory_entry_path_size - system_directory_entry_path_index,
1545
         (char *) internal_directory_entry->entry.d_name,
1546
         directory_entry_name_length + 1,
1547
         error ) != 1 )
1548
    {
1549
      libcerror_error_set(
1550
       error,
1551
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1552
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1553
       "%s: unable to copy directory entry name to system character directory path.",
1554
       function );
1555
1556
      goto on_error;
1557
    }
1558
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1559
    if( _wstat(
1560
         system_directory_entry_path,
1561
         &file_statistics ) != 0 )
1562
#elif defined( HAVE_LSTAT )
1563
    /* Note that lstat is used and not stat to prevent symbolic links from
1564
     * being resolved and to allow the entry type to remain a symbolic link
1565
     */
1566
    if( lstat(
1567
         system_directory_entry_path,
1568
         &file_statistics ) != 0 )
1569
#else
1570
    if( stat(
1571
         system_directory_entry_path,
1572
         &file_statistics ) != 0 )
1573
#endif
1574
    {
1575
      libcerror_error_set(
1576
       error,
1577
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1578
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1579
       "%s: unable to retrieve directory entry file statistics.",
1580
       function );
1581
1582
      goto on_error;
1583
    }
1584
    memory_free(
1585
     system_directory_entry_path );
1586
1587
    system_directory_entry_path = NULL;
1588
  }
1589
  internal_directory_entry->st_mode = file_statistics.st_mode;
1590
1591
#endif /* !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE ) */
1592
1593
0
  return( 1 );
1594
1595
0
on_error:
1596
#if !defined( LIBCDIRECTORY_HAVE_DIRENT_D_TYPE )
1597
  if( system_directory_entry_path != NULL )
1598
  {
1599
    memory_free(
1600
     system_directory_entry_path );
1601
  }
1602
#endif
1603
0
  return( -1 );
1604
0
}
1605
1606
#else
1607
#error Missing read directory entry function
1608
#endif
1609
1610
/* Determines if a directory has a specific entry
1611
 * Returns 1 if the directory has the corresponding entry, 0 if not or -1 on error
1612
 */
1613
int libcdirectory_directory_has_entry(
1614
     libcdirectory_directory_t *directory,
1615
     libcdirectory_directory_entry_t *directory_entry,
1616
     const char *entry_name,
1617
     size_t entry_name_length,
1618
     uint8_t entry_type,
1619
     uint8_t compare_flags,
1620
     libcerror_error_t **error )
1621
0
{
1622
0
  libcdirectory_directory_entry_t *search_directory_entry = NULL;
1623
0
  char *search_directory_entry_name                       = NULL;
1624
0
  static char *function                                   = "libcdirectory_directory_has_entry";
1625
0
  size_t search_directory_entry_name_length               = 0;
1626
0
  uint8_t search_directory_entry_type                     = 0;
1627
0
  int entry_found                                         = 0;
1628
0
  int match                                               = 0;
1629
0
  int result                                              = 0;
1630
1631
0
  if( directory == NULL )
1632
0
  {
1633
0
    libcerror_error_set(
1634
0
     error,
1635
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1636
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1637
0
     "%s: invalid directory.",
1638
0
     function );
1639
1640
0
    return( -1 );
1641
0
  }
1642
0
  if( directory_entry == NULL )
1643
0
  {
1644
0
    libcerror_error_set(
1645
0
     error,
1646
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1647
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1648
0
     "%s: invalid directory entry.",
1649
0
     function );
1650
1651
0
    return( -1 );
1652
0
  }
1653
0
  if( entry_name == NULL )
1654
0
  {
1655
0
    libcerror_error_set(
1656
0
     error,
1657
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1658
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1659
0
     "%s: invalid entry name.",
1660
0
     function );
1661
1662
0
    return( -1 );
1663
0
  }
1664
0
  if( entry_name_length > (size_t) SSIZE_MAX )
1665
0
  {
1666
0
    libcerror_error_set(
1667
0
     error,
1668
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1669
0
     LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
1670
0
     "%s: invalid entry name length value exceeds maximum.",
1671
0
     function );
1672
1673
0
    return( -1 );
1674
0
  }
1675
0
  if( ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_DEVICE )
1676
0
   && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_DIRECTORY )
1677
0
   && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_FILE )
1678
0
   && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_LINK )
1679
0
   && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_PIPE )
1680
0
   && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_SOCKET )
1681
0
   && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_UNDEFINED ) )
1682
0
  {
1683
0
    libcerror_error_set(
1684
0
     error,
1685
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1686
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1687
0
     "%s: unsupported entry type.",
1688
0
     function );
1689
1690
0
    return( -1 );
1691
0
  }
1692
0
  if( ( compare_flags & ~( LIBCDIRECTORY_COMPARE_FLAG_NO_CASE ) ) != 0 )
1693
0
  {
1694
0
    libcerror_error_set(
1695
0
     error,
1696
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1697
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1698
0
     "%s: unsupported compare flags.",
1699
0
     function );
1700
1701
0
    return( -1 );
1702
0
  }
1703
0
  if( libcdirectory_directory_entry_initialize(
1704
0
       &search_directory_entry,
1705
0
       error ) != 1 )
1706
0
  {
1707
0
    libcerror_error_set(
1708
0
     error,
1709
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1710
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1711
0
     "%s: unable to create search directory entry.",
1712
0
     function );
1713
1714
0
    goto on_error;
1715
0
  }
1716
0
  entry_found = 0;
1717
1718
0
  do
1719
0
  {
1720
0
    result = libcdirectory_directory_read_entry(
1721
0
        directory,
1722
0
        search_directory_entry,
1723
0
        error );
1724
1725
0
    if( result == -1 )
1726
0
    {
1727
0
      libcerror_error_set(
1728
0
       error,
1729
0
       LIBCERROR_ERROR_DOMAIN_IO,
1730
0
       LIBCERROR_IO_ERROR_READ_FAILED,
1731
0
       "%s: unable to read directory entry.",
1732
0
       function );
1733
1734
0
      goto on_error;
1735
0
    }
1736
0
    else if( result == 0 )
1737
0
    {
1738
0
      break;
1739
0
    }
1740
0
    if( libcdirectory_directory_entry_get_type(
1741
0
         search_directory_entry,
1742
0
         &search_directory_entry_type,
1743
0
         error ) != 1 )
1744
0
    {
1745
0
      libcerror_error_set(
1746
0
       error,
1747
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1748
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1749
0
       "%s: unable to retrieve directory entry type.",
1750
0
       function );
1751
1752
0
      goto on_error;
1753
0
    }
1754
0
    if( search_directory_entry_type == entry_type )
1755
0
    {
1756
0
      if( libcdirectory_directory_entry_get_name(
1757
0
           search_directory_entry,
1758
0
           &search_directory_entry_name,
1759
0
           error ) != 1 )
1760
0
      {
1761
0
        libcerror_error_set(
1762
0
         error,
1763
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1764
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1765
0
         "%s: unable to retrieve directory entry name.",
1766
0
         function );
1767
1768
0
        goto on_error;
1769
0
      }
1770
0
      search_directory_entry_name_length = narrow_string_length(
1771
0
                                            search_directory_entry_name );
1772
1773
0
      if( search_directory_entry_name_length == entry_name_length )
1774
0
      {
1775
        /* If there is an exact match we're done searching
1776
         */
1777
0
        match = narrow_string_compare(
1778
0
           search_directory_entry_name,
1779
0
           entry_name,
1780
0
           entry_name_length );
1781
1782
0
        if( match == 0 )
1783
0
        {
1784
0
          if( libcdirectory_directory_entry_copy(
1785
0
               directory_entry,
1786
0
               search_directory_entry,
1787
0
               error ) != 1 )
1788
0
          {
1789
0
            libcerror_error_set(
1790
0
             error,
1791
0
             LIBCERROR_ERROR_DOMAIN_MEMORY,
1792
0
             LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1793
0
             "%s: unable to copy search directory entry.",
1794
0
             function );
1795
1796
0
            goto on_error;
1797
0
          }
1798
0
          entry_found = 1;
1799
1800
0
          break;
1801
0
        }
1802
        /* Ignore successive caseless matches
1803
         */
1804
0
        if( ( ( compare_flags & LIBCDIRECTORY_COMPARE_FLAG_NO_CASE ) != 0 )
1805
0
         && ( entry_found == 0 ) )
1806
0
        {
1807
0
          match = narrow_string_compare_no_case(
1808
0
             search_directory_entry_name,
1809
0
             entry_name,
1810
0
             entry_name_length );
1811
1812
0
          if( match == 0 )
1813
0
          {
1814
0
            if( libcdirectory_directory_entry_copy(
1815
0
                 directory_entry,
1816
0
                 search_directory_entry,
1817
0
                 error ) != 1 )
1818
0
            {
1819
0
              libcerror_error_set(
1820
0
               error,
1821
0
               LIBCERROR_ERROR_DOMAIN_MEMORY,
1822
0
               LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1823
0
               "%s: unable to copy search directory entry.",
1824
0
               function );
1825
1826
0
              goto on_error;
1827
0
            }
1828
0
            entry_found = 1;
1829
0
          }
1830
0
        }
1831
0
      }
1832
0
    }
1833
0
  }
1834
0
  while( result != 0 );
1835
1836
0
  if( libcdirectory_directory_entry_free(
1837
0
       &search_directory_entry,
1838
0
       error ) != 1 )
1839
0
  {
1840
0
    libcerror_error_set(
1841
0
     error,
1842
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1843
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1844
0
     "%s: unable to free search directory entry.",
1845
0
     function );
1846
1847
0
    goto on_error;
1848
0
  }
1849
0
  return( entry_found );
1850
1851
0
on_error:
1852
0
  if( search_directory_entry != NULL )
1853
0
  {
1854
0
    libcdirectory_directory_entry_free(
1855
0
     &search_directory_entry,
1856
     NULL );
1857
0
  }
1858
0
  return( -1 );
1859
0
}
1860
1861
#if defined( HAVE_WIDE_CHARACTER_TYPE )
1862
1863
/* Determines if a directory has a specific entry
1864
 * Returns 1 if the directory has the corresponding entry, 0 if not or -1 on error
1865
 */
1866
int libcdirectory_directory_has_entry_wide(
1867
     libcdirectory_directory_t *directory,
1868
     libcdirectory_directory_entry_t *directory_entry,
1869
     const wchar_t *entry_name,
1870
     size_t entry_name_length,
1871
     uint8_t entry_type,
1872
     uint8_t compare_flags,
1873
     libcerror_error_t **error )
1874
{
1875
  libcdirectory_directory_entry_t *search_directory_entry = NULL;
1876
  wchar_t *search_directory_entry_name                    = NULL;
1877
  static char *function                                   = "libcdirectory_directory_has_entry_wide";
1878
  size_t search_directory_entry_name_length               = 0;
1879
  uint8_t search_directory_entry_type                     = 0;
1880
  int entry_found                                         = 0;
1881
  int match                                               = 0;
1882
  int result                                              = 0;
1883
1884
  if( directory == NULL )
1885
  {
1886
    libcerror_error_set(
1887
     error,
1888
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1889
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1890
     "%s: invalid directory.",
1891
     function );
1892
1893
    return( -1 );
1894
  }
1895
  if( directory_entry == NULL )
1896
  {
1897
    libcerror_error_set(
1898
     error,
1899
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1900
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1901
     "%s: invalid directory entry.",
1902
     function );
1903
1904
    return( -1 );
1905
  }
1906
  if( entry_name == NULL )
1907
  {
1908
    libcerror_error_set(
1909
     error,
1910
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1911
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1912
     "%s: invalid entry name.",
1913
     function );
1914
1915
    return( -1 );
1916
  }
1917
  if( entry_name_length > (size_t) SSIZE_MAX )
1918
  {
1919
    libcerror_error_set(
1920
     error,
1921
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1922
     LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
1923
     "%s: invalid entry name length value exceeds maximum.",
1924
     function );
1925
1926
    return( -1 );
1927
  }
1928
  if( ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_DEVICE )
1929
   && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_DIRECTORY )
1930
   && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_FILE )
1931
   && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_LINK )
1932
   && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_PIPE )
1933
   && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_SOCKET )
1934
   && ( entry_type != LIBCDIRECTORY_ENTRY_TYPE_UNDEFINED ) )
1935
  {
1936
    libcerror_error_set(
1937
     error,
1938
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1939
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1940
     "%s: unsupported entry type.",
1941
     function );
1942
1943
    return( -1 );
1944
  }
1945
  if( ( compare_flags & ~( LIBCDIRECTORY_COMPARE_FLAG_NO_CASE ) ) != 0 )
1946
  {
1947
    libcerror_error_set(
1948
     error,
1949
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1950
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1951
     "%s: unsupported compare flags.",
1952
     function );
1953
1954
    return( -1 );
1955
  }
1956
  if( libcdirectory_directory_entry_initialize(
1957
       &search_directory_entry,
1958
       error ) != 1 )
1959
  {
1960
    libcerror_error_set(
1961
     error,
1962
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1963
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1964
     "%s: unable to create search directory entry.",
1965
     function );
1966
1967
    goto on_error;
1968
  }
1969
  entry_found = 0;
1970
1971
  do
1972
  {
1973
    result = libcdirectory_directory_read_entry(
1974
        directory,
1975
        search_directory_entry,
1976
        error );
1977
1978
    if( result == -1 )
1979
    {
1980
      libcerror_error_set(
1981
       error,
1982
       LIBCERROR_ERROR_DOMAIN_IO,
1983
       LIBCERROR_IO_ERROR_READ_FAILED,
1984
       "%s: unable to read directory entry.",
1985
       function );
1986
1987
      goto on_error;
1988
    }
1989
    else if( result == 0 )
1990
    {
1991
      break;
1992
    }
1993
    if( libcdirectory_directory_entry_get_type(
1994
         search_directory_entry,
1995
         &search_directory_entry_type,
1996
         error ) != 1 )
1997
    {
1998
      libcerror_error_set(
1999
       error,
2000
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2001
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2002
       "%s: unable to retrieve directory entry type.",
2003
       function );
2004
2005
      goto on_error;
2006
    }
2007
    if( search_directory_entry_type == entry_type )
2008
    {
2009
      if( libcdirectory_directory_entry_get_name_wide(
2010
           search_directory_entry,
2011
           &search_directory_entry_name,
2012
           error ) != 1 )
2013
      {
2014
        libcerror_error_set(
2015
         error,
2016
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
2017
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2018
         "%s: unable to retrieve directory entry name.",
2019
         function );
2020
2021
        goto on_error;
2022
      }
2023
      search_directory_entry_name_length = wide_string_length(
2024
                                            search_directory_entry_name );
2025
2026
      if( search_directory_entry_name_length == entry_name_length )
2027
      {
2028
        /* If there is an exact match we're done searching
2029
         */
2030
        match = wide_string_compare(
2031
           search_directory_entry_name,
2032
           entry_name,
2033
           entry_name_length );
2034
2035
        if( match == 0 )
2036
        {
2037
          if( libcdirectory_directory_entry_copy(
2038
               directory_entry,
2039
               search_directory_entry,
2040
               error ) != 1 )
2041
          {
2042
            libcerror_error_set(
2043
             error,
2044
             LIBCERROR_ERROR_DOMAIN_MEMORY,
2045
             LIBCERROR_MEMORY_ERROR_COPY_FAILED,
2046
             "%s: unable to copy search directory entry.",
2047
             function );
2048
2049
            goto on_error;
2050
          }
2051
          entry_found = 1;
2052
2053
          break;
2054
        }
2055
        /* Ignore successive caseless matches
2056
         */
2057
        if( ( ( compare_flags & LIBCDIRECTORY_COMPARE_FLAG_NO_CASE ) != 0 )
2058
         && ( entry_found == 0 ) )
2059
        {
2060
          match = wide_string_compare_no_case(
2061
             search_directory_entry_name,
2062
             entry_name,
2063
             entry_name_length );
2064
2065
          if( match == 0 )
2066
          {
2067
            if( libcdirectory_directory_entry_copy(
2068
                 directory_entry,
2069
                 search_directory_entry,
2070
                 error ) != 1 )
2071
            {
2072
              libcerror_error_set(
2073
               error,
2074
               LIBCERROR_ERROR_DOMAIN_MEMORY,
2075
               LIBCERROR_MEMORY_ERROR_COPY_FAILED,
2076
               "%s: unable to copy search directory entry.",
2077
               function );
2078
2079
              goto on_error;
2080
            }
2081
            entry_found = 1;
2082
          }
2083
        }
2084
      }
2085
    }
2086
  }
2087
  while( result != 0 );
2088
2089
  if( libcdirectory_directory_entry_free(
2090
       &search_directory_entry,
2091
       error ) != 1 )
2092
  {
2093
    libcerror_error_set(
2094
     error,
2095
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2096
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
2097
     "%s: unable to free search directory entry.",
2098
     function );
2099
2100
    goto on_error;
2101
  }
2102
  return( entry_found );
2103
2104
on_error:
2105
  if( search_directory_entry != NULL )
2106
  {
2107
    libcdirectory_directory_entry_free(
2108
     &search_directory_entry,
2109
     NULL );
2110
  }
2111
  return( -1 );
2112
}
2113
2114
#endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
2115