Statistics
| Branch: | Revision:

root / linux-user / path.c @ 17759187

History | View | Annotate | Download (3.8 kB)

1 32ce6337 bellard
/* Code to mangle pathnames into those matching a given prefix.
2 32ce6337 bellard
   eg. open("/lib/foo.so") => open("/usr/gnemul/i386-linux/lib/foo.so");
3 32ce6337 bellard

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