Statistics
| Branch: | Revision:

root / linux-user / path.c @ e6e5906b

History | View | Annotate | Download (3.6 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 32ce6337 bellard
/* This needs to be done after tree is stabalized (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
void init_paths(const char *prefix)
96 32ce6337 bellard
{
97 32ce6337 bellard
    if (prefix[0] != '/' ||
98 32ce6337 bellard
        prefix[0] == '\0' ||
99 32ce6337 bellard
        !strcmp(prefix, "/"))
100 32ce6337 bellard
        return;
101 32ce6337 bellard
102 32ce6337 bellard
    base = new_entry("", NULL, prefix+1);
103 32ce6337 bellard
    base = add_dir_maybe(base);
104 6f28fb86 bellard
    if (base->num_entries == 0) {
105 6f28fb86 bellard
        free (base);
106 6f28fb86 bellard
        base = NULL;
107 6f28fb86 bellard
    } else {
108 6f28fb86 bellard
        set_parents(base, base);
109 6f28fb86 bellard
    }
110 32ce6337 bellard
}
111 32ce6337 bellard
112 32ce6337 bellard
/* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
113 32ce6337 bellard
static const char *
114 32ce6337 bellard
follow_path(const struct pathelem *cursor, const char *name)
115 32ce6337 bellard
{
116 32ce6337 bellard
    unsigned int i, namelen;
117 32ce6337 bellard
118 32ce6337 bellard
    name += strspn(name, "/");
119 32ce6337 bellard
    namelen = strcspn(name, "/");
120 32ce6337 bellard
121 32ce6337 bellard
    if (namelen == 0)
122 32ce6337 bellard
        return cursor->pathname;
123 32ce6337 bellard
124 32ce6337 bellard
    if (strneq(name, namelen, ".."))
125 32ce6337 bellard
        return follow_path(cursor->parent, name + namelen);
126 32ce6337 bellard
127 32ce6337 bellard
    if (strneq(name, namelen, "."))
128 32ce6337 bellard
        return follow_path(cursor, name + namelen);
129 32ce6337 bellard
130 32ce6337 bellard
    for (i = 0; i < cursor->num_entries; i++)
131 32ce6337 bellard
        if (strneq(name, namelen, cursor->entries[i]->name))
132 32ce6337 bellard
            return follow_path(cursor->entries[i], name + namelen);
133 32ce6337 bellard
134 32ce6337 bellard
    /* Not found */
135 32ce6337 bellard
    return NULL;
136 32ce6337 bellard
}
137 32ce6337 bellard
138 32ce6337 bellard
/* Look for path in emulation dir, otherwise return name. */
139 32ce6337 bellard
const char *path(const char *name)
140 32ce6337 bellard
{
141 32ce6337 bellard
    /* Only do absolute paths: quick and dirty, but should mostly be OK.
142 32ce6337 bellard
       Could do relative by tracking cwd. */
143 32ce6337 bellard
    if (!base || name[0] != '/')
144 32ce6337 bellard
        return name;
145 32ce6337 bellard
146 32ce6337 bellard
    return follow_path(base, name) ?: name;
147 32ce6337 bellard
}