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