comparison ffstools/tiffs-mkfs/input.c @ 705:12ae93940467

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