FreeCalypso > hg > freecalypso-tools
annotate ffstools/tiffs-mkfs/input.c @ 894:7ade15d4e0cb
fc-buzplay: BU refactoring, first step
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 03 Apr 2022 08:11:05 +0000 |
parents | 12ae93940467 |
children |
rev | line source |
---|---|
705
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 #include <sys/types.h> |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 #include <sys/param.h> |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 #include <sys/stat.h> |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 #include <dirent.h> |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <stdio.h> |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdint.h> |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdlib.h> |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <string.h> |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <strings.h> |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <unistd.h> |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include "struct.h" |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include "globals.h" |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 void |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 read_dir_level(dto, srcpath, depth) |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 struct tree_object *dto; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 char *srcpath; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 { |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 DIR *rdd; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 struct dirent *dirent; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 char hostpath_child[MAXPATHLEN]; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 struct stat hst; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 struct tree_object *cto; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 unsigned nchildren; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 dto->is_dir = 1; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 rdd = opendir(srcpath); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 if (!rdd) { |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 perror(srcpath); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 exit(1); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 } |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 nchildren = 0; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 while (dirent = readdir(rdd)) { |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 if (dirent->d_name[0] == '.') |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 continue; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 if (strlen(dirent->d_name) > MAX_FN_COMPONENT) { |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 fprintf(stderr, |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 "error: \"%s\" in %s exceeds the FFS component name limit\n", |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 dirent->d_name, srcpath); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 exit(1); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 } |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 if (nchildren >= MAX_DIR_ENTRIES) { |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 fprintf(stderr, "error: %s has too many children\n", |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 srcpath); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 exit(1); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 } |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 cto = malloc(sizeof(struct tree_object)); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 if (!cto) { |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 perror("malloc of struct tree_object"); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 exit(1); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 } |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 strcpy(cto->name, dirent->d_name); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 dto->u.d.children[nchildren++] = cto; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 if (strlen(srcpath) + strlen(dirent->d_name) + 2 > |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 sizeof hostpath_child) { |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 fprintf(stderr, |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 "error: host side pathname buffer overflow\n"); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 exit(1); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 } |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 sprintf(hostpath_child, "%s/%s", srcpath, dirent->d_name); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 if (lstat(hostpath_child, &hst) < 0) { |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 perror(hostpath_child); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 exit(1); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 } |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 switch (hst.st_mode & S_IFMT) { |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 case S_IFREG: |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 cto->is_dir = 0; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 strcpy(cto->u.f.host_pathname, hostpath_child); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 break; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 case S_IFDIR: |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 if (depth >= MAX_DIR_NEST-1) { |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 fprintf(stderr, |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 "error: directory nesting too deep at %s\n", |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 hostpath_child); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 exit(1); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 } |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 read_dir_level(cto, hostpath_child, depth + 1); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 break; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 default: |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 fprintf(stderr, |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 "error: %s is neither a regular file nor a directory\n", |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 hostpath_child); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 exit(1); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 } |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 } |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 closedir(rdd); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 dto->u.d.nchildren = nchildren; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 } |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 static int |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 compare_func(p1, p2) |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 struct tree_object **p1, **p2; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 { |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 return strcmp((*p1)->name, (*p2)->name); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 } |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 void |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 sort_dir_level(dto) |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 struct tree_object *dto; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 { |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 unsigned n; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 struct tree_object *cto; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 if (dto->u.d.nchildren > 1) |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 qsort(dto->u.d.children, dto->u.d.nchildren, |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 sizeof(struct tree_object *), compare_func); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 for (n = 0; n < dto->u.d.nchildren; n++) { |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 cto = dto->u.d.children[n]; |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 if (cto->is_dir) |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 sort_dir_level(cto); |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 } |
12ae93940467
tiffs-mkfs program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
112 } |