root / bsd-user / path.c @ 2c277908
History | View | Annotate | Download (4.2 kB)
1 | 84778508 | blueswir1 | /* Code to mangle pathnames into those matching a given prefix.
|
---|---|---|---|
2 | 84778508 | blueswir1 | eg. open("/lib/foo.so") => open("/usr/gnemul/i386-linux/lib/foo.so");
|
3 | 84778508 | blueswir1 | |
4 | 84778508 | blueswir1 | The assumption is that this area does not change.
|
5 | 84778508 | blueswir1 | */
|
6 | 84778508 | blueswir1 | #include <sys/types.h> |
7 | 84778508 | blueswir1 | #include <sys/param.h> |
8 | 84778508 | blueswir1 | #include <dirent.h> |
9 | 84778508 | blueswir1 | #include <unistd.h> |
10 | 84778508 | blueswir1 | #include <stdlib.h> |
11 | 84778508 | blueswir1 | #include <string.h> |
12 | 84778508 | blueswir1 | #include <errno.h> |
13 | 84778508 | blueswir1 | #include <stdio.h> |
14 | 84778508 | blueswir1 | #include "qemu.h" |
15 | 84778508 | blueswir1 | #include "qemu-common.h" |
16 | 84778508 | blueswir1 | |
17 | 84778508 | blueswir1 | struct pathelem
|
18 | 84778508 | blueswir1 | { |
19 | 84778508 | blueswir1 | /* Name of this, eg. lib */
|
20 | 84778508 | blueswir1 | char *name;
|
21 | 84778508 | blueswir1 | /* Full path name, eg. /usr/gnemul/x86-linux/lib. */
|
22 | 84778508 | blueswir1 | char *pathname;
|
23 | 84778508 | blueswir1 | struct pathelem *parent;
|
24 | 84778508 | blueswir1 | /* Children */
|
25 | 84778508 | blueswir1 | unsigned int num_entries; |
26 | 84778508 | blueswir1 | struct pathelem *entries[0]; |
27 | 84778508 | blueswir1 | }; |
28 | 84778508 | blueswir1 | |
29 | 84778508 | blueswir1 | static struct pathelem *base; |
30 | 84778508 | blueswir1 | |
31 | 84778508 | blueswir1 | /* First N chars of S1 match S2, and S2 is N chars long. */
|
32 | 84778508 | blueswir1 | static int strneq(const char *s1, unsigned int n, const char *s2) |
33 | 84778508 | blueswir1 | { |
34 | 84778508 | blueswir1 | unsigned int i; |
35 | 84778508 | blueswir1 | |
36 | 84778508 | blueswir1 | for (i = 0; i < n; i++) |
37 | 84778508 | blueswir1 | if (s1[i] != s2[i])
|
38 | 84778508 | blueswir1 | return 0; |
39 | 84778508 | blueswir1 | return s2[i] == 0; |
40 | 84778508 | blueswir1 | } |
41 | 84778508 | blueswir1 | |
42 | 84778508 | blueswir1 | static struct pathelem *add_entry(struct pathelem *root, const char *name); |
43 | 84778508 | blueswir1 | |
44 | 84778508 | blueswir1 | static struct pathelem *new_entry(const char *root, |
45 | 84778508 | blueswir1 | struct pathelem *parent,
|
46 | 84778508 | blueswir1 | const char *name) |
47 | 84778508 | blueswir1 | { |
48 | 84778508 | blueswir1 | struct pathelem *new = malloc(sizeof(*new)); |
49 | 84778508 | blueswir1 | new->name = strdup(name); |
50 | 84778508 | blueswir1 | asprintf(&new->pathname, "%s/%s", root, name);
|
51 | 84778508 | blueswir1 | new->num_entries = 0;
|
52 | 84778508 | blueswir1 | return new;
|
53 | 84778508 | blueswir1 | } |
54 | 84778508 | blueswir1 | |
55 | 84778508 | blueswir1 | #define streq(a,b) (strcmp((a), (b)) == 0) |
56 | 84778508 | blueswir1 | |
57 | 84778508 | blueswir1 | static struct pathelem *add_dir_maybe(struct pathelem *path) |
58 | 84778508 | blueswir1 | { |
59 | 84778508 | blueswir1 | DIR *dir; |
60 | 84778508 | blueswir1 | |
61 | 84778508 | blueswir1 | if ((dir = opendir(path->pathname)) != NULL) { |
62 | 84778508 | blueswir1 | struct dirent *dirent;
|
63 | 84778508 | blueswir1 | |
64 | 84778508 | blueswir1 | while ((dirent = readdir(dir)) != NULL) { |
65 | 84778508 | blueswir1 | if (!streq(dirent->d_name,".") && !streq(dirent->d_name,"..")){ |
66 | 84778508 | blueswir1 | path = add_entry(path, dirent->d_name); |
67 | 84778508 | blueswir1 | } |
68 | 84778508 | blueswir1 | } |
69 | 84778508 | blueswir1 | closedir(dir); |
70 | 84778508 | blueswir1 | } |
71 | 84778508 | blueswir1 | return path;
|
72 | 84778508 | blueswir1 | } |
73 | 84778508 | blueswir1 | |
74 | 84778508 | blueswir1 | static struct pathelem *add_entry(struct pathelem *root, const char *name) |
75 | 84778508 | blueswir1 | { |
76 | 84778508 | blueswir1 | root->num_entries++; |
77 | 84778508 | blueswir1 | |
78 | 84778508 | blueswir1 | root = realloc(root, sizeof(*root)
|
79 | 84778508 | blueswir1 | + sizeof(root->entries[0])*root->num_entries); |
80 | 84778508 | blueswir1 | |
81 | 84778508 | blueswir1 | root->entries[root->num_entries-1] = new_entry(root->pathname, root, name);
|
82 | 84778508 | blueswir1 | root->entries[root->num_entries-1]
|
83 | 84778508 | blueswir1 | = add_dir_maybe(root->entries[root->num_entries-1]);
|
84 | 84778508 | blueswir1 | return root;
|
85 | 84778508 | blueswir1 | } |
86 | 84778508 | blueswir1 | |
87 | 84778508 | blueswir1 | /* This needs to be done after tree is stabilized (ie. no more reallocs!). */
|
88 | 84778508 | blueswir1 | static void set_parents(struct pathelem *child, struct pathelem *parent) |
89 | 84778508 | blueswir1 | { |
90 | 84778508 | blueswir1 | unsigned int i; |
91 | 84778508 | blueswir1 | |
92 | 84778508 | blueswir1 | child->parent = parent; |
93 | 84778508 | blueswir1 | for (i = 0; i < child->num_entries; i++) |
94 | 84778508 | blueswir1 | set_parents(child->entries[i], child); |
95 | 84778508 | blueswir1 | } |
96 | 84778508 | blueswir1 | |
97 | 84778508 | blueswir1 | /* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
|
98 | 84778508 | blueswir1 | static const char * |
99 | 84778508 | blueswir1 | follow_path(const struct pathelem *cursor, const char *name) |
100 | 84778508 | blueswir1 | { |
101 | 84778508 | blueswir1 | unsigned int i, namelen; |
102 | 84778508 | blueswir1 | |
103 | 84778508 | blueswir1 | name += strspn(name, "/");
|
104 | 84778508 | blueswir1 | namelen = strcspn(name, "/");
|
105 | 84778508 | blueswir1 | |
106 | 84778508 | blueswir1 | if (namelen == 0) |
107 | 84778508 | blueswir1 | return cursor->pathname;
|
108 | 84778508 | blueswir1 | |
109 | 84778508 | blueswir1 | if (strneq(name, namelen, "..")) |
110 | 84778508 | blueswir1 | return follow_path(cursor->parent, name + namelen);
|
111 | 84778508 | blueswir1 | |
112 | 84778508 | blueswir1 | if (strneq(name, namelen, ".")) |
113 | 84778508 | blueswir1 | return follow_path(cursor, name + namelen);
|
114 | 84778508 | blueswir1 | |
115 | 84778508 | blueswir1 | for (i = 0; i < cursor->num_entries; i++) |
116 | 84778508 | blueswir1 | if (strneq(name, namelen, cursor->entries[i]->name))
|
117 | 84778508 | blueswir1 | return follow_path(cursor->entries[i], name + namelen);
|
118 | 84778508 | blueswir1 | |
119 | 84778508 | blueswir1 | /* Not found */
|
120 | 84778508 | blueswir1 | return NULL; |
121 | 84778508 | blueswir1 | } |
122 | 84778508 | blueswir1 | |
123 | 84778508 | blueswir1 | void init_paths(const char *prefix) |
124 | 84778508 | blueswir1 | { |
125 | 84778508 | blueswir1 | char pref_buf[PATH_MAX];
|
126 | 84778508 | blueswir1 | |
127 | 84778508 | blueswir1 | if (prefix[0] == '\0' || |
128 | 84778508 | blueswir1 | !strcmp(prefix, "/"))
|
129 | 84778508 | blueswir1 | return;
|
130 | 84778508 | blueswir1 | |
131 | 84778508 | blueswir1 | if (prefix[0] != '/') { |
132 | 84778508 | blueswir1 | char *cwd = getcwd(NULL, 0); |
133 | 84778508 | blueswir1 | size_t pref_buf_len = sizeof(pref_buf);
|
134 | 84778508 | blueswir1 | |
135 | 84778508 | blueswir1 | if (!cwd)
|
136 | 84778508 | blueswir1 | abort(); |
137 | 84778508 | blueswir1 | pstrcpy(pref_buf, sizeof(pref_buf), cwd);
|
138 | 84778508 | blueswir1 | pstrcat(pref_buf, pref_buf_len, "/");
|
139 | 84778508 | blueswir1 | pstrcat(pref_buf, pref_buf_len, prefix); |
140 | 84778508 | blueswir1 | free(cwd); |
141 | 84778508 | blueswir1 | } else
|
142 | 84778508 | blueswir1 | pstrcpy(pref_buf, sizeof(pref_buf), prefix + 1); |
143 | 84778508 | blueswir1 | |
144 | 84778508 | blueswir1 | base = new_entry("", NULL, pref_buf); |
145 | 84778508 | blueswir1 | base = add_dir_maybe(base); |
146 | 84778508 | blueswir1 | if (base->num_entries == 0) { |
147 | 84778508 | blueswir1 | free (base); |
148 | 84778508 | blueswir1 | base = NULL;
|
149 | 84778508 | blueswir1 | } else {
|
150 | 84778508 | blueswir1 | set_parents(base, base); |
151 | 84778508 | blueswir1 | } |
152 | 84778508 | blueswir1 | } |
153 | 84778508 | blueswir1 | |
154 | 84778508 | blueswir1 | /* Look for path in emulation dir, otherwise return name. */
|
155 | 84778508 | blueswir1 | const char *path(const char *name) |
156 | 84778508 | blueswir1 | { |
157 | 84778508 | blueswir1 | /* Only do absolute paths: quick and dirty, but should mostly be OK.
|
158 | 84778508 | blueswir1 | Could do relative by tracking cwd. */
|
159 | 84778508 | blueswir1 | if (!base || name[0] != '/') |
160 | 84778508 | blueswir1 | return name;
|
161 | 84778508 | blueswir1 | |
162 | 84778508 | blueswir1 | return follow_path(base, name) ?: name;
|
163 | 84778508 | blueswir1 | } |