1
2
3
4
5
6#include "qemu/osdep.h"
7#include <sys/param.h>
8#include <dirent.h>
9#include "qemu/cutils.h"
10#include "qemu/path.h"
11
12struct pathelem
13{
14
15 char *name;
16
17 char *pathname;
18 struct pathelem *parent;
19
20 unsigned int num_entries;
21 struct pathelem *entries[0];
22};
23
24static struct pathelem *base;
25
26
27static int strneq(const char *s1, unsigned int n, const char *s2)
28{
29 unsigned int i;
30
31 for (i = 0; i < n; i++)
32 if (s1[i] != s2[i])
33 return 0;
34 return s2[i] == 0;
35}
36
37static struct pathelem *add_entry(struct pathelem *root, const char *name,
38 unsigned type);
39
40static struct pathelem *new_entry(const char *root,
41 struct pathelem *parent,
42 const char *name)
43{
44 struct pathelem *new = g_malloc(sizeof(*new));
45 new->name = g_strdup(name);
46 new->pathname = g_strdup_printf("%s/%s", root, name);
47 new->num_entries = 0;
48 return new;
49}
50
51#define streq(a,b) (strcmp((a), (b)) == 0)
52
53
54#if defined(DT_DIR) && defined(DT_UNKNOWN) && defined(DT_LNK)
55# define dirent_type(dirent) ((dirent)->d_type)
56# define is_dir_maybe(type) \
57 ((type) == DT_DIR || (type) == DT_UNKNOWN || (type) == DT_LNK)
58#else
59# define dirent_type(dirent) (1)
60# define is_dir_maybe(type) (type)
61#endif
62
63static struct pathelem *add_dir_maybe(struct pathelem *path)
64{
65 DIR *dir;
66
67 if ((dir = opendir(path->pathname)) != NULL) {
68 struct dirent *dirent;
69
70 while ((dirent = readdir(dir)) != NULL) {
71 if (!streq(dirent->d_name,".") && !streq(dirent->d_name,"..")){
72 path = add_entry(path, dirent->d_name, dirent_type(dirent));
73 }
74 }
75 closedir(dir);
76 }
77 return path;
78}
79
80static struct pathelem *add_entry(struct pathelem *root, const char *name,
81 unsigned type)
82{
83 struct pathelem **e;
84
85 root->num_entries++;
86
87 root = g_realloc(root, sizeof(*root)
88 + sizeof(root->entries[0])*root->num_entries);
89 e = &root->entries[root->num_entries-1];
90
91 *e = new_entry(root->pathname, root, name);
92 if (is_dir_maybe(type)) {
93 *e = add_dir_maybe(*e);
94 }
95
96 return root;
97}
98
99
100static void set_parents(struct pathelem *child, struct pathelem *parent)
101{
102 unsigned int i;
103
104 child->parent = parent;
105 for (i = 0; i < child->num_entries; i++)
106 set_parents(child->entries[i], child);
107}
108
109
110static const char *
111follow_path(const struct pathelem *cursor, const char *name)
112{
113 unsigned int i, namelen;
114
115 name += strspn(name, "/");
116 namelen = strcspn(name, "/");
117
118 if (namelen == 0)
119 return cursor->pathname;
120
121 if (strneq(name, namelen, ".."))
122 return follow_path(cursor->parent, name + namelen);
123
124 if (strneq(name, namelen, "."))
125 return follow_path(cursor, name + namelen);
126
127 for (i = 0; i < cursor->num_entries; i++)
128 if (strneq(name, namelen, cursor->entries[i]->name))
129 return follow_path(cursor->entries[i], name + namelen);
130
131
132 return NULL;
133}
134
135void init_paths(const char *prefix)
136{
137 char pref_buf[PATH_MAX];
138
139 if (prefix[0] == '\0' ||
140 !strcmp(prefix, "/"))
141 return;
142
143 if (prefix[0] != '/') {
144 char *cwd = getcwd(NULL, 0);
145 size_t pref_buf_len = sizeof(pref_buf);
146
147 if (!cwd)
148 abort();
149 pstrcpy(pref_buf, sizeof(pref_buf), cwd);
150 pstrcat(pref_buf, pref_buf_len, "/");
151 pstrcat(pref_buf, pref_buf_len, prefix);
152 free(cwd);
153 } else
154 pstrcpy(pref_buf, sizeof(pref_buf), prefix + 1);
155
156 base = new_entry("", NULL, pref_buf);
157 base = add_dir_maybe(base);
158 if (base->num_entries == 0) {
159 g_free(base->pathname);
160 g_free(base->name);
161 g_free(base);
162 base = NULL;
163 } else {
164 set_parents(base, base);
165 }
166}
167
168
169const char *path(const char *name)
170{
171
172
173 if (!base || !name || name[0] != '/')
174 return name;
175
176 return follow_path(base, name) ?: name;
177}
178