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