File:  [Qemu by Fabrice Bellard] / qemu / path.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 18:23:57 2018 UTC (3 years, 7 months ago) by root
Branches: qemu, MAIN
CVS tags: qemu0150, qemu0141, qemu0140, qemu0130, HEAD
qemu 0.13.0

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

unix.superglobalmegacorp.com