1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "dtc.h"
22
23#include <dirent.h>
24#include <sys/stat.h>
25
26static struct node *read_fstree(const char *dirname)
27{
28 DIR *d;
29 struct dirent *de;
30 struct stat st;
31 struct node *tree;
32
33 d = opendir(dirname);
34 if (!d)
35 die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno));
36
37 tree = build_node(NULL, NULL);
38
39 while ((de = readdir(d)) != NULL) {
40 char *tmpnam;
41
42 if (streq(de->d_name, ".")
43 || streq(de->d_name, ".."))
44 continue;
45
46 tmpnam = join_path(dirname, de->d_name);
47
48 if (lstat(tmpnam, &st) < 0)
49 die("stat(%s): %s\n", tmpnam, strerror(errno));
50
51 if (S_ISREG(st.st_mode)) {
52 struct property *prop;
53 FILE *pfile;
54
55 pfile = fopen(tmpnam, "r");
56 if (! pfile) {
57 fprintf(stderr,
58 "WARNING: Cannot open %s: %s\n",
59 tmpnam, strerror(errno));
60 } else {
61 prop = build_property(xstrdup(de->d_name),
62 data_copy_file(pfile,
63 st.st_size));
64 add_property(tree, prop);
65 fclose(pfile);
66 }
67 } else if (S_ISDIR(st.st_mode)) {
68 struct node *newchild;
69
70 newchild = read_fstree(tmpnam);
71 newchild = name_node(newchild, xstrdup(de->d_name));
72 add_child(tree, newchild);
73 }
74
75 free(tmpnam);
76 }
77
78 closedir(d);
79 return tree;
80}
81
82struct boot_info *dt_from_fs(const char *dirname)
83{
84 struct node *tree;
85
86 tree = read_fstree(dirname);
87 tree = name_node(tree, "");
88
89 return build_boot_info(NULL, tree, guess_boot_cpuid(tree));
90}
91
92