forked from e2tools/e2tools
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsysXtract.c
1348 lines (1176 loc) · 33.4 KB
/
sysXtract.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* sysXtract
*
* Copyright 2017 nkk71 <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* $Header: /home/ksheff/src/e2tools/RCS/ls.c,v 0.8 2004/04/07 03:30:49 ksheff Exp $ */
/*
* ls.c --- list directories
*
* Copyright (C) 1997 Theodore Ts'o. This file may be redistributed
* under the terms of the GNU Public License.
*
* Modified by Keith W. Sheffield <[email protected]> for inclusion with e2tools
*/
/*
* $Log: ls.c,v $
* Revision 0.8 2004/04/07 03:30:49 ksheff
* Updated usage string.
*
* Revision 0.7 2004/04/07 02:41:50 ksheff
* Modified to bracket files with an inode number of 0 with >< characters.
*
* Revision 0.6 2004/04/06 20:27:26 ksheff
* Modified to print "No files found!" for empty directories, corrected the
* directory name display, and fixed REGEX_OPT masking.
*
* Revision 0.5 2002/06/05 23:14:11 ksheff
* Fixed short display with respect to displaying the contents of multiple
* directories.
*
* Revision 0.4 2002/06/05 23:07:34 ksheff
* Allowed for multiple directory and file specifications. Added the -f
* option for no sorting.
*
* Revision 0.3 2002/06/03 23:00:51 ksheff
* Removed display code from directory iterator. A list of displayable files
* is now generated. This list can be sorted and displayed in a variety of
* ways. The -a, -c, -i, -r, and -t options are now accepted.
*
* Revision 0.2 2002/03/05 12:12:52 ksheff
* Removed setting optind for SCO.
*
* Revision 0.1 2002/02/27 04:46:21 ksheff
* initial revision
*
*/
#include "e2tools.h"
#include "elist.h"
#include <regex.h>
#include <stdint.h>
#include <inttypes.h>
#include <pwd.h>
#include <grp.h>
#define VERSION_STR "0.8.1"
/*
* list directory
*/
#define LONG_OPT 0x0001
#define DELETED_OPT 0x0002
#define REGEX_OPT 0x0004
#define REVERSE_OPT 0x0008
#define HIDDEN_OPT 0x0010
#define CREATE_OPT 0x0020
#define INODE_OPT 0x0040
#define NUMERIC_OPT 0x0080
#define SHORT_OPT 0x0100
#define FS_CONFIG_OPT 0x0200
#define RECURSIVE_OPT 0x0400
#define EXCL_LNK_OPT 0x0800
#define EXCL_DIR_OPT 0x1000
#define EXCL_REG_OPT 0x2000
#define EXCL_NONCAPS_OPT 0x4000
#define DIRECTORY_TYPE -1
#define NORMAL_TYPE 0
struct list_dir_struct {
int options;
regex_t *reg;
elist_t *files;
};
typedef struct list_file_struct {
char *name;
char *full_name;
struct ext2_inode inode;
ext2_ino_t dir;
ext2_ino_t inode_num;
int entry;
long type;
} ls_file_t;
static const char *monstr[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
/* Things we need for selinux and capabilities */
#define XATTR_SELINUX_SUFFIX "selinux"
#define XATTR_CAPS_SUFFIX "capability"
#define VFS_CAP_U32 2
struct vfs_cap_data {
__le32 magic_etc;
struct {
__le32 permitted;
__le32 inheritable;
} data[VFS_CAP_U32];
};
/* global variables */
static ext2_filsys fs;
static int max_name_len = 0;
// for recursion and multiple dir specs
static elist_t *Dirs_To_List = NULL;
static char *Current_Path = NULL;
// path prefix for fs_config format
static char *fs_config_Path_Prefix = NULL;
// output files
static char *extract_path = NULL;
static FILE *fp_file_contexts = NULL;
static FILE *fp_C_fs_config = NULL;
static FILE *fp_X_fs_config = NULL;
static FILE *fp_D_listing = NULL;
static FILE *fp_symlinks = NULL;
static int g_count_dir = 0;
static int g_count_reg = 0;
static int g_count_lnk = 0;
static int g_count_unknown = 0;
static int g_verbose = 1;
/* forward function declarations */
static int list_dir_proc(ext2_ino_t dir, int entry, struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *private);
void free_ls_file_t(void *f);
void fprintf_long_disp(FILE *fp, ls_file_t *info, const char *selinux_context, struct vfs_cap_data *capabilities, char *symlink_dest, int options);
int full_name_sort(const void *n1, const void *n2);
long add_ls_file(char *name, int namelen, ext2_ino_t dir, ext2_ino_t ino, int entry, int type, struct list_dir_struct *ls);
elist_t *remove_ls_dups(elist_t *list);
void extract_file(ls_file_t *info, int options);
char *strdupcat_sanitized(const char *path, const char *file)
{
int path_len;
int file_len;
char *full_name;
char *p = path;
int needs_slash = 0;
if (!path && !file)
return NULL;
else if (!path)
return strdup(file);
// remove leading / and .
/*
* do not remove leading . that will just break hidden
* dirs in the base folder, instead fix the command
* line parsing when given paths
while (p[0] == '/' || p[0] == '.')
p++;
*/
path_len = strlen(p);
file_len = strlen(file);
if (path_len > 0 && p[path_len-1] != '/' && file[0] != '/')
needs_slash = 1;
full_name = malloc(path_len + needs_slash + file_len + 1);
if (!full_name) {
perror("malloc");
return NULL;
}
memcpy(full_name, p, path_len);
full_name[path_len] = '/'; // don't really need to if() it
memcpy(full_name + path_len + needs_slash, file, file_len + 1);
return full_name;
}
#if 1
/* ********************************************************************************** */
/* my_strcmp() from copy.c */
int my_strcmp(const void *n1, const void *n2)
{
char *s1 = *((char **)n1);
char *s2 = *((char **)n2);
return(strcmp(s1, s2));
}
/* change_cwd() from mkdir.c */
/* Name: change_cwd()
*
* Description:
*
* This function changes the current working directory
*
* Algorithm:
*
* Look up the inode number for the input string
* check to see if it's a directory
* Assign it as the current working directory if it is.
*
* Global Variables:
*
* None
*
* Arguments:
*
* ext2_filsys fs; The current filesystem
* ext_ino_t root; The root directory
* ext_ino_t *cwd; The current working directory
* char *dirname; The name of the directory we want to change to
*
* Return Values:
*
* 0 - changed to the directory successfully
* the error code of what went wrong
*
* Author: Keith W Sheffield
* Date: 02/21/2002
*
* Modification History:
*
* MM/DD/YY Name Description
*
*/
long
change_cwd(ext2_filsys fs, ext2_ino_t root, ext2_ino_t *cwd, char *dirname)
{
ext2_ino_t inode;
long retval;
if ((retval = ext2fs_namei(fs, root, *cwd, dirname, &inode)))
{
fprintf(stderr, "%s\n", error_message(retval));
return retval;
}
else if ((retval = ext2fs_check_directory(fs, inode)))
{
fprintf(stderr, "%s\n", error_message(retval));
return retval;
}
*cwd = inode;
return(0);
} /* end of change_cwd */
/* get_file_parts() from mv.c */
/* Name: get_file_parts()
*
* Description:
*
* This function returns each of the following file 'parts': directory name,
* base name, inode number of the directory
*
* Algorithm:
*
* Use the root directory as the current working directory
* Find the last / in the full pathname
* If none are found, set the basename to the full pathname,
* and the directory to NULL
* Otherwise,
* Separate the basename from the directory
* Change the working directory
* Set the return pointers.
*
* Global Variables:
*
* None.
*
* Arguments:
*
* ext2_filsys fs; the filesystem being used
* ext2_ino_t root; the root directory of the filesystem
* char *pathname; the full pathname of the file
* ext2_ino_t *dir_ino; The inode number of the directory
* char **dir_name; the directory the file is in
* char **base_name; The basename of the file
*
* Return Values:
*
* 0 - retrieved the information ok
* otherwise the error code of what went wrong
*
* Author: Keith W. Sheffield
* Date: 03/21/2002
*
* Modification History:
*
* MM/DD/YY Name Description
*
*/
long
get_file_parts(ext2_filsys fs, ext2_ino_t root, char *pathname,
ext2_ino_t *dir_ino, char **dir_name, char **base_name)
{
char *fname;
long retval;
/* move to the source directory */
*dir_name = pathname;
*dir_ino = root;
if (NULL == (fname = strrchr(pathname, '/')))
{
fname = pathname;
*dir_name = NULL;
}
else
{
*fname++ = '\0';
if ((*pathname != '\0' && strcmp(pathname, ".") != 0) &&
(retval = change_cwd(fs, root, dir_ino, pathname)))
{
fprintf(stderr, "Error changing to directory %s\n",
pathname);
return(retval);
}
}
*base_name = fname;
return(0);
} /* end of get_file_parts */
/* ********************************************************************************** */
#endif
/* Name: list_dir_proc()
*
* Description:
*
*
* Algorithm:
*
*
* Global Variables:
*
* None
*
* Arguments:
*
*
* Return Values:
*
*
* Author: Theodore Ts'o
* Date: 1997
*
* Modification History:
*
* MM/DD/YY Name Description
* 02/27/02 K.Sheffield Modified for use with e2tools
*/
static int list_dir_proc(ext2_ino_t dir, int entry, struct ext2_dir_entry *dirent,
int UNUSED_PARM(offset), int UNUSED_PARM(blocksize),
char UNUSED_PARM(*buf), void *private)
{
char name[EXT2_NAME_LEN];
struct list_dir_struct *ls = (struct list_dir_struct *) private;
int thislen;
thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ? (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
strncpy(name, dirent->name, thislen);
name[thislen] = '\0';
/* skip hidden files unless we ask for them */
if (0 == (ls->options & HIDDEN_OPT) && name[0] == '.')
return(0);
// always skip . and ..
if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
return(0);
if ((ls->options & REGEX_OPT) && regexec(ls->reg, name, 0, NULL, 0))
return(0);
return(add_ls_file(name, thislen, dir, dirent->inode, entry, NORMAL_TYPE, ls));
}
/* Name: add_ls_file()
*
* Description:
*
*
* Algorithm:
*
*
* Global Variables:
*
* None
*
* Arguments:
*
*
* Return Values:
*
*
* Author: K.Sheffield
* Date: 02/27/2002
*
* Modification History:
*
* MM/DD/YY Name Description
*/
long add_ls_file(char *name, int namelen, ext2_ino_t dir, ext2_ino_t ino,
int entry, int type, struct list_dir_struct *ls)
{
ls_file_t *file_info;
elist_t *flist;
if (NULL == (file_info = calloc(sizeof(ls_file_t),1))) {
perror("list_dir");
return(0);
}
file_info->dir = dir;
if (entry == DIRENT_DELETED_FILE)
file_info->inode_num = 0;
else
file_info->inode_num = ino;
file_info->entry = entry;
file_info->type = type;
if (file_info->inode_num) {
if (read_inode(fs, file_info->inode_num, &file_info->inode)) {
free(file_info);
return 0;
}
}
if (file_info->inode_num) {
if (LINUX_S_ISDIR(file_info->inode.i_mode))
g_count_dir++;
else if (LINUX_S_ISREG(file_info->inode.i_mode))
g_count_reg++;
else if (LINUX_S_ISLNK(file_info->inode.i_mode))
g_count_lnk++;
else
g_count_unknown++;
}
if (name) {
file_info->name = strdup(name);
if (type == DIRECTORY_TYPE)
file_info->full_name = strdupcat_sanitized("", name);
else
file_info->full_name = strdupcat_sanitized(Current_Path, name);
}
if (NULL == (flist = elist_insert(ls->files, file_info))) {
perror("list_dir");
free_ls_file_t(file_info);
return 0;
}
ls->files = flist;
if (max_name_len < namelen)
max_name_len = namelen;
if ((ls->options & RECURSIVE_OPT) && type == NORMAL_TYPE && LINUX_S_ISDIR(file_info->inode.i_mode) && !LINUX_S_ISLNK(file_info->inode.i_mode)) {
// if we're recursing append this dir to the list
Dirs_To_List = elist_append(Dirs_To_List, strdup(file_info->full_name));
}
return 0;
}
/* Name: free_ls_file_t()
*
* Description:
*
* This function is used to free an ls_file_t structure.
*
* Algorithm:
*
* Free the file's name if it is not NULL
* Free the ls_file_t structure
* Check to make sure the ls_file_t structure is not NULL
*
* Global Variables:
*
* None.
*
* Arguments:
*
* void *f; The structure to free
*
* Return Values:
*
* none
*
* Author: Keith W. Sheffield
* Date: 06/03/2002
*
* Modification History:
*
* MM/DD/YY Name Description
*
*/
void free_ls_file_t(void *f)
{
ls_file_t *n = (ls_file_t *) f;
if (n != NULL) {
if (n->name != NULL)
free(n->name);
if (n->full_name != NULL)
free(n->full_name);
free(n);
}
} /* end of free_ls_file_t */
int main(int argc, char *argv[])
{
ext2_ino_t root;
ext2_ino_t cwd;
ext2_ino_t inode=0;
int retval;
int c;
int flags;
struct list_dir_struct ls;
char *fs_name;
char *last_fs_name;
char *path = NULL;
char *dup_path = NULL;
char *dir_name;
char *base_name;
elist_t *files=NULL;
ls_file_t *cur_file;
memset(&ls, 0, sizeof(ls));
last_fs_name = NULL;
#ifdef HAVE_OPTRESET
optreset = 1; /* Makes BSD getopt happy */
#endif
// default for make_fs_config
ls.options |= FS_CONFIG_OPT;
while ((c = getopt (argc, argv, "C:D:d:F:L:p:qRS:X:x:")) != EOF) {
switch (c) {
case 'd':
fs_name = optarg;
if (NULL != (path = strchr(fs_name, ':')))
*path++ = '\0';
if ((retval = open_filesystem(fs_name, &fs, &root, 0))) {
return(1);
}
last_fs_name = fs_name;
break;
case 'R':
ls.options |= RECURSIVE_OPT;
break;
case 'p':
fs_config_Path_Prefix = optarg;
break;
case 'x':
while (optarg && optarg[0]) {
if (optarg[0] == 'L')
ls.options |= EXCL_LNK_OPT;
else if (optarg[0] == 'D')
ls.options |= EXCL_DIR_OPT;
else if (optarg[0] == 'R')
ls.options |= EXCL_REG_OPT;
else if (optarg[0] == 'N')
ls.options |= EXCL_NONCAPS_OPT;
optarg++;
}
break;
case 'S':
fp_file_contexts = fopen(optarg, "w");
break;
case 'C':
fp_C_fs_config = fopen(optarg, "w");
break;
case 'X':
fp_X_fs_config = fopen(optarg, "w");
break;
case 'L':
fp_symlinks = fopen(optarg, "w");
break;
case 'F':
extract_path = optarg;
break;
case 'D':
fp_D_listing = fopen(optarg, "w");
break;
case 'q':
g_verbose = 0;
break;
}
}
// show hidden files
ls.options |= HIDDEN_OPT;
// always use numeric format
ls.options |= NUMERIC_OPT;
if (argc <= optind) {
fputs("\n"
"Usage: sysXtract " VERSION_STR " [-qR][-xLDRN][-F dir][-D file][-L file][-p prefix][-S file][-C file][-X file] file\n\n"
" [-q] Quieter (don't continuously print progress while extracting files)\n"
" [-R] List subdirectories recursively\n"
" [-x LDRN] Exclude [L]symlinks [D]directories [R]regular files [N]files without capabilities\n"
" [-F OutputDir] Directory to extract regular files to\n"
" [-D dir_listing] File to output ls -lRZ style output\n"
" [-L symlink_list] File to output symlink informtion\n"
" [-p prefix] Prefix used for below file_contexts and fs_config entries\n"
" [-S file_contexts] File to output file_contexts information\n"
" [-C fs_config] File to output complete fs_config information\n"
" [-X xtra_fs_config] File to output capabilities-only fs_config\n"
" file ext4 image file to be processed\n"
"\n", stderr);
return(1);
}
fprintf(stderr, "\n");
fprintf(stderr, "sysXtract " VERSION_STR "\n");
/* sort the remaining command line arguments */
//qsort(argv+optind, argc-optind, sizeof(char *), my_strcmp);
for(c=optind;c<argc;c++) {
fs_name = argv[c];
if (NULL != (path = strchr(fs_name, ':')))
*path++ = '\0';
else if (last_fs_name != NULL) {
path = fs_name;
fs_name = last_fs_name;
}
if (Dirs_To_List) {
elist_free(Dirs_To_List, free);
Dirs_To_List = NULL;
}
// TODO: mkdir_recursive on base path if specified (make sure to exclude file is it's a file)
if (path)
Dirs_To_List = elist_append(Dirs_To_List, strdup(path));
else
Dirs_To_List = elist_append(Dirs_To_List, strdup(""));
elist_t *itr = Dirs_To_List;
while (itr && itr->data) {
path = itr->data;
/* keep a copy of the file path for later because get_file_parts() is
* destructive.
*/
if (dup_path) {
free(dup_path);
dup_path = NULL;
}
if (path)
dup_path = strdup(path);
if (last_fs_name == NULL || strcmp(last_fs_name, fs_name)) {
if (last_fs_name != NULL)
ext2fs_close(fs);
if ((retval = open_filesystem(fs_name, &fs, &root, 0))) {
return(1);
}
fprintf(stderr, "Reading '%s'\n", fs_name);
last_fs_name = fs_name;
}
dir_name = NULL;
cwd = root;
ls.options &= (~REGEX_OPT);
if (path != NULL && *path != '\0') {
if (get_file_parts(fs, root, path, &cwd, &dir_name, &base_name)) {
ext2fs_close(fs);
return(-1);
}
if (is_file_regexp(base_name)) {
if (NULL == (ls.reg = (regex_t *) make_regexp(base_name))) {
fprintf(stderr, "Error creating regular expression for %s\n", base_name);
return(1);
}
ls.options |= REGEX_OPT;
inode = cwd;
}
/* check to see if the file name exists in the current directory
*/
else if ((retval = ext2fs_namei(fs, cwd, cwd, base_name, &inode))) {
fputs(error_message(retval), stderr);
ext2fs_close(fs);
return(1);
}
}
else
inode = root;
if(!dir_name)
dir_name = ".";
if (!inode)
continue;
flags = DIRENT_FLAG_INCLUDE_EMPTY;
if (ls.options & DELETED_OPT)
flags |= DIRENT_FLAG_INCLUDE_REMOVED;
if ((retval = ext2fs_check_directory(fs, inode))) {
if (retval != EXT2_ET_NO_DIRECTORY) {
fputs(error_message(retval), stderr);
ext2fs_close(fs);
return(1);
}
if(add_ls_file(dir_name, 0, cwd, 0, 0, DIRECTORY_TYPE, &ls)) {
fputs(error_message(retval), stderr);
ext2fs_close(fs);
return(1);
}
if(add_ls_file(base_name, strlen(base_name), cwd, inode, 0, NORMAL_TYPE, &ls)) {
fputs(error_message(retval), stderr);
ext2fs_close(fs);
return(1);
}
}
else {
if (ls.options & REGEX_OPT || path == NULL || *path == '\0')
path = dir_name;
else if (dup_path)
path = dup_path;
// we need to set a global variable so it can be picked up by add_ls_file()
Current_Path = dup_path;
// if(add_ls_file((ls.options & REGEX_OPT) ? dir_name : path, 0, inode, 0,
if(add_ls_file(path, 0, inode, 0, 0, DIRECTORY_TYPE, &ls)) {
fputs(error_message(retval), stderr);
ext2fs_close(fs);
return(1);
}
retval = ext2fs_dir_iterate2(fs, inode, flags, 0, list_dir_proc, &ls);
if (retval) {
fputs(error_message(retval), stderr);
ext2fs_close(fs);
return(1);
}
}
itr = itr->next;
}
elist_free(Dirs_To_List, free);
Dirs_To_List = NULL;
}
fprintf(stderr, "Scan complete:\n");
fprintf(stderr, " %6d directories\n", g_count_dir);
fprintf(stderr, " %6d regular files\n", g_count_reg);
fprintf(stderr, " %6d symlinks\n", g_count_lnk);
if (g_count_unknown) {
fprintf(stderr, " %6d unknown entries! Please inform the developer!\n", g_count_unknown);
}
int total_count = g_count_dir + g_count_reg + g_count_lnk + g_count_unknown;
fprintf(stderr, "Sorting and removing duplicates\n");
elist_sort(ls.files, full_name_sort, ls.options & REVERSE_OPT);
ls.files = files = remove_ls_dups(ls.files);
if (files == NULL)
fprintf(stderr, "No files found!");
else {
if (!g_verbose && extract_path) {
fprintf(stderr, "Extracting %d entries please wait...", total_count);
}
while(files != NULL) {
cur_file = (ls_file_t *)files->data;
#if 0
// does not work properly (probably due to sort)
if (fp_D_listing && cur_file->type == DIRECTORY_TYPE) {
fprintf(fp_D_listing, "\n%s:\n", cur_file->full_name);
}
#endif
if (cur_file->type != DIRECTORY_TYPE) {
extract_file(cur_file, ls.options);
if (g_verbose && extract_path) {
fprintf(stderr, "\rExtracting files: %d remaining \r",--total_count);
}
}
//~ else
//~ printf("mkdir %s\n", cur_file->full_name);
files = files->next;
}
}
//fprintf(stderr, "\rExtracting files: 0 remaining \r");
fputc('\n', stderr);
elist_free(ls.files, free_ls_file_t);
if (fp_file_contexts)
fclose(fp_file_contexts);
if (fp_C_fs_config)
fclose(fp_C_fs_config);
if (fp_X_fs_config)
fclose(fp_X_fs_config);
if (fp_symlinks)
fclose(fp_symlinks);
if (fs)
ext2fs_close(fs);
fprintf(stderr, "Finished.\n\n");
return(0);
}
char *read_xattr(ext2_ino_t inode_num, const char *attribute)
{
char *ret = NULL;
struct ext2_xattr_handle *h;
char *buf = NULL;
size_t buflen;
errcode_t err;
unsigned int handle_flags = 0;
err = ext2fs_xattrs_open(fs, inode_num, &h);
if (err)
goto out2;
err = ext2fs_xattrs_flags(h, &handle_flags, NULL);
if (err)
goto out;
err = ext2fs_xattrs_read(h);
if (err)
goto out;
err = ext2fs_xattr_get(h, attribute, (void **)&buf, &buflen);
if (err)
goto out;
ret = malloc(buflen);
if (ret)
memcpy(ret, buf, buflen);
ext2fs_free_mem(&buf);
out:
ext2fs_xattrs_close(&h);
// Might not be an error just no capabilities
/* if (err)
com_err("xattrs_get", err, "while getting extended attribute"); */
out2:
return ret;
}
void fprintf_symlink_entry(FILE *fp, ls_file_t *info, char *symlink_dest)
{
// edify format:
// fprintf(fp, "symlink(\"%s\", \"/%s\");\n", symlink_dest, info->full_name);
fprintf(fp, "%-30s -> %s\n", info->full_name, symlink_dest);
}
void fprintf_file_contexts_entry(FILE *fp, ls_file_t *info, const char *selinux_context)
{
if (fs_config_Path_Prefix) {
if (fs_config_Path_Prefix[0] == '/')
fprintf(fp, "%s", fs_config_Path_Prefix);
else
fprintf(fp, "/%s", fs_config_Path_Prefix); // add leading / for file_contexts
}
else
fprintf(fp,"/"); // add leading / for file_contexts
fprintf(fp, "%-50s %s\n", info->full_name, selinux_context);
}
void fprintf_fs_config_entry(FILE *fp, ls_file_t *info, struct vfs_cap_data *capabilities)
{
// full_name uid gid mode [capabilities=caps]
if (fs_config_Path_Prefix) {
if (fs_config_Path_Prefix[0] == '/')
fprintf(fp, "%s", fs_config_Path_Prefix + 1); // strip leading / for fs_config
else
fprintf(fp, "%s", fs_config_Path_Prefix);
}
fprintf(fp, "%-50s ", info->full_name);
fprintf(fp, "%05d %05d ", info->inode.i_uid, info->inode.i_gid);
fprintf(fp, "%05o", info->inode.i_mode & 07777);
/* Capabilities (struct vfs_cap_data) are stored as follows:
* cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
* cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
* cap_data.data[0].inheritable = 0;
* cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
* cap_data.data[1].inheritable = 0;
*/
if (capabilities) {
uint64_t cap_data = ((uint64_t)capabilities->data[1].permitted << 32) + capabilities->data[0].permitted;
fprintf(fp, " capabilities=%lu", cap_data);
}
fprintf(fp, "\n");
}
int mkdir_recursive(const char *pathname, mode_t mode)
{
char buf[1024];
const char *slash;
const char *p = pathname;
int width;
int ret;
struct stat info;
while ((slash = strchr(p, '/')) != NULL) {
width = slash - pathname;
p = slash + 1;
if (width < 0)
break;
if (width == 0)
continue;
if ((unsigned int)width > sizeof(buf) - 1) {
fprintf(stderr, "path too long for mkdir_recursive\n");
return -1;
}
memcpy(buf, pathname, width);
buf[width] = 0;
if (stat(buf, &info) != 0) {
ret = mkdir(buf, mode);
if (ret && errno != EEXIST)
return ret;
}
}
ret = mkdir(pathname, mode);
if (ret && errno != EEXIST)
return ret;
return 0;
}
static char *get_inline_symlink(ext2_ino_t inode_num, struct ext2_inode *inode)
{
char *symlink_dest = NULL;
errcode_t retval;
char *buf = NULL;
size_t size;
retval = ext2fs_inline_data_size(fs, inode_num, &size);
if (retval)
goto out;
retval = ext2fs_get_memzero(size + 1, &buf);
if (retval)
goto out;