2009-08-11 17:37:13 +02:00
|
|
|
From 447a7055bbff636821fb9a559e9b8d30b1ab9391 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Ulrich Hecht <uli@suse.de>
|
|
|
|
Date: Tue, 14 Apr 2009 16:28:45 +0200
|
|
|
|
Subject: [PATCH 09/33] qemu-cvs-newpath
|
|
|
|
|
|
|
|
fast path mangling patch by Kirill A. Shutemov
|
|
|
|
|
|
|
|
Signed-off-by: Ulrich Hecht <uli@suse.de>
|
|
|
|
---
|
|
|
|
linux-user/path.c | 190 ++++++++++++++++-------------------------------------
|
|
|
|
1 files changed, 56 insertions(+), 134 deletions(-)
|
|
|
|
|
|
|
|
diff --git a/linux-user/path.c b/linux-user/path.c
|
|
|
|
index 06b1f5f..aedff50 100644
|
|
|
|
--- a/linux-user/path.c
|
|
|
|
+++ b/linux-user/path.c
|
2007-07-10 18:55:54 +02:00
|
|
|
@@ -1,159 +1,81 @@
|
2007-06-11 18:17:36 +02:00
|
|
|
/* Code to mangle pathnames into those matching a given prefix.
|
|
|
|
eg. open("/lib/foo.so") => open("/usr/gnemul/i386-linux/lib/foo.so");
|
|
|
|
-
|
|
|
|
- The assumption is that this area does not change.
|
|
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
|
|
-#include <dirent.h>
|
|
|
|
+#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
-#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
-#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "qemu.h"
|
|
|
|
|
|
|
|
-struct pathelem
|
|
|
|
-{
|
|
|
|
- /* Name of this, eg. lib */
|
|
|
|
- char *name;
|
|
|
|
- /* Full path name, eg. /usr/gnemul/x86-linux/lib. */
|
|
|
|
- char *pathname;
|
|
|
|
- struct pathelem *parent;
|
|
|
|
- /* Children */
|
|
|
|
- unsigned int num_entries;
|
|
|
|
- struct pathelem *entries[0];
|
|
|
|
+struct path_list_head {
|
|
|
|
+ struct path_list_head *next;
|
|
|
|
+ char* path;
|
|
|
|
};
|
|
|
|
|
|
|
|
-static struct pathelem *base;
|
|
|
|
-
|
|
|
|
-/* First N chars of S1 match S2, and S2 is N chars long. */
|
|
|
|
-static int strneq(const char *s1, unsigned int n, const char *s2)
|
|
|
|
-{
|
|
|
|
- unsigned int i;
|
|
|
|
-
|
|
|
|
- for (i = 0; i < n; i++)
|
|
|
|
- if (s1[i] != s2[i])
|
|
|
|
- return 0;
|
|
|
|
- return s2[i] == 0;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-static struct pathelem *add_entry(struct pathelem *root, const char *name);
|
|
|
|
-
|
|
|
|
-static struct pathelem *new_entry(const char *root,
|
|
|
|
- struct pathelem *parent,
|
|
|
|
- const char *name)
|
|
|
|
-{
|
|
|
|
- struct pathelem *new = malloc(sizeof(*new));
|
|
|
|
- new->name = strdup(name);
|
|
|
|
- asprintf(&new->pathname, "%s/%s", root, name);
|
|
|
|
- new->num_entries = 0;
|
|
|
|
- return new;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-#define streq(a,b) (strcmp((a), (b)) == 0)
|
|
|
|
-
|
|
|
|
-static struct pathelem *add_dir_maybe(struct pathelem *path)
|
|
|
|
-{
|
|
|
|
- DIR *dir;
|
|
|
|
-
|
|
|
|
- if ((dir = opendir(path->pathname)) != NULL) {
|
|
|
|
- struct dirent *dirent;
|
|
|
|
-
|
|
|
|
- while ((dirent = readdir(dir)) != NULL) {
|
|
|
|
- if (!streq(dirent->d_name,".") && !streq(dirent->d_name,"..")){
|
|
|
|
- path = add_entry(path, dirent->d_name);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- closedir(dir);
|
|
|
|
- }
|
|
|
|
- return path;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-static struct pathelem *add_entry(struct pathelem *root, const char *name)
|
|
|
|
-{
|
|
|
|
- root->num_entries++;
|
|
|
|
-
|
|
|
|
- root = realloc(root, sizeof(*root)
|
|
|
|
- + sizeof(root->entries[0])*root->num_entries);
|
|
|
|
-
|
|
|
|
- root->entries[root->num_entries-1] = new_entry(root->pathname, root, name);
|
|
|
|
- root->entries[root->num_entries-1]
|
|
|
|
- = add_dir_maybe(root->entries[root->num_entries-1]);
|
|
|
|
- return root;
|
|
|
|
-}
|
|
|
|
-
|
2008-07-25 04:42:31 +02:00
|
|
|
-/* This needs to be done after tree is stabilized (ie. no more reallocs!). */
|
2007-06-11 18:17:36 +02:00
|
|
|
-static void set_parents(struct pathelem *child, struct pathelem *parent)
|
|
|
|
-{
|
|
|
|
- unsigned int i;
|
|
|
|
-
|
|
|
|
- child->parent = parent;
|
|
|
|
- for (i = 0; i < child->num_entries; i++)
|
|
|
|
- set_parents(child->entries[i], child);
|
|
|
|
-}
|
2007-07-10 18:55:54 +02:00
|
|
|
-
|
2007-06-11 18:17:36 +02:00
|
|
|
-/* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
|
|
|
|
-static const char *
|
|
|
|
-follow_path(const struct pathelem *cursor, const char *name)
|
|
|
|
-{
|
|
|
|
- unsigned int i, namelen;
|
|
|
|
-
|
|
|
|
- name += strspn(name, "/");
|
|
|
|
- namelen = strcspn(name, "/");
|
|
|
|
-
|
|
|
|
- if (namelen == 0)
|
|
|
|
- return cursor->pathname;
|
|
|
|
-
|
|
|
|
- if (strneq(name, namelen, ".."))
|
|
|
|
- return follow_path(cursor->parent, name + namelen);
|
|
|
|
-
|
|
|
|
- if (strneq(name, namelen, "."))
|
|
|
|
- return follow_path(cursor, name + namelen);
|
|
|
|
-
|
|
|
|
- for (i = 0; i < cursor->num_entries; i++)
|
|
|
|
- if (strneq(name, namelen, cursor->entries[i]->name))
|
|
|
|
- return follow_path(cursor->entries[i], name + namelen);
|
|
|
|
-
|
|
|
|
- /* Not found */
|
|
|
|
- return NULL;
|
2007-07-10 18:55:54 +02:00
|
|
|
-}
|
|
|
|
+static struct path_list_head* list_head;
|
|
|
|
|
|
|
|
void init_paths(const char *prefix)
|
|
|
|
{
|
|
|
|
- char pref_buf[PATH_MAX];
|
|
|
|
-
|
|
|
|
- if (prefix[0] == '\0' ||
|
|
|
|
- !strcmp(prefix, "/"))
|
|
|
|
+ if (prefix[0] != '/' ||
|
|
|
|
+ prefix[0] == '\0' ||
|
|
|
|
+ !strcmp(prefix, "/"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
- if (prefix[0] != '/') {
|
|
|
|
- char *cwd = get_current_dir_name();
|
|
|
|
- if (!cwd)
|
|
|
|
- abort();
|
|
|
|
- strcpy(pref_buf, cwd);
|
|
|
|
- strcat(pref_buf, "/");
|
|
|
|
- strcat(pref_buf, prefix);
|
|
|
|
- free(cwd);
|
|
|
|
- } else
|
|
|
|
- strcpy(pref_buf,prefix + 1);
|
2009-08-11 17:37:13 +02:00
|
|
|
+ list_head = malloc(sizeof(struct path_list_head));
|
|
|
|
|
2007-07-10 18:55:54 +02:00
|
|
|
- base = new_entry("", NULL, pref_buf);
|
|
|
|
- base = add_dir_maybe(base);
|
|
|
|
- if (base->num_entries == 0) {
|
|
|
|
- free (base);
|
|
|
|
- base = NULL;
|
|
|
|
- } else {
|
|
|
|
- set_parents(base, base);
|
|
|
|
- }
|
2007-06-11 18:17:36 +02:00
|
|
|
+ /* first element of list is prefix */
|
|
|
|
+ list_head->path = strdup(prefix);
|
|
|
|
+ list_head->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look for path in emulation dir, otherwise return name. */
|
|
|
|
const char *path(const char *name)
|
|
|
|
{
|
|
|
|
+ struct path_list_head *list = list_head;
|
|
|
|
+ int path_length = strlen(list_head->path) + strlen(name) + 1;
|
|
|
|
+ char *newname = malloc(path_length);
|
|
|
|
+ struct stat buf;
|
|
|
|
+ const char * result = name;
|
|
|
|
+
|
|
|
|
/* Only do absolute paths: quick and dirty, but should mostly be OK.
|
|
|
|
Could do relative by tracking cwd. */
|
2009-04-01 18:57:34 +02:00
|
|
|
- if (!base || !name || name[0] != '/')
|
2007-06-11 18:17:36 +02:00
|
|
|
- return name;
|
2009-08-11 17:37:13 +02:00
|
|
|
-
|
|
|
|
- return follow_path(base, name) ?: name;
|
2007-06-11 18:17:36 +02:00
|
|
|
+ if (!list_head || result[0] != '/')
|
|
|
|
+ goto exit;
|
|
|
|
+
|
|
|
|
+ strncpy(newname, list_head->path, path_length);
|
|
|
|
+ strncat(newname, name, path_length);
|
2009-08-11 17:37:13 +02:00
|
|
|
+
|
2007-06-11 18:17:36 +02:00
|
|
|
+ /* look for place where path should be present */
|
|
|
|
+ while ( list->next && (strcmp(list->next->path, newname) < 0) )
|
|
|
|
+ list = list->next;
|
|
|
|
+
|
|
|
|
+ /* if there is no path in list */
|
|
|
|
+ if ( !list->next || strcmp(list->next->path, newname) ) {
|
|
|
|
+ /* add element to list if path exist in emulation dir */
|
|
|
|
+ if ( !stat(newname, &buf) )
|
|
|
|
+ {
|
|
|
|
+ struct path_list_head *new;
|
|
|
|
+
|
|
|
|
+ new = malloc(sizeof(struct path_list_head));
|
|
|
|
+ new->path = strdup(newname);
|
|
|
|
+ new->next = list->next;
|
|
|
|
+ list->next = new;
|
|
|
|
+ result = new->path;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else if ( stat(list->next->path, &buf) ) {
|
|
|
|
+ /* remove element from list if path doesn't exist in emulation dir */
|
|
|
|
+ struct path_list_head* tmp;
|
|
|
|
+
|
|
|
|
+ tmp = list->next;
|
|
|
|
+ list->next = tmp->next;
|
|
|
|
+ free(tmp->path);
|
|
|
|
+ free(tmp);
|
|
|
|
+ } else
|
|
|
|
+ result = list->next->path;
|
|
|
|
+
|
|
|
|
+exit:
|
|
|
|
+ free(newname);
|
|
|
|
+ return result;
|
|
|
|
}
|
2009-08-11 17:37:13 +02:00
|
|
|
--
|
|
|
|
1.6.2.1
|
|
|
|
|