config/pathsub.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5 /*
michael@0 6 ** Pathname subroutines.
michael@0 7 **
michael@0 8 ** Brendan Eich, 8/29/95
michael@0 9 */
michael@0 10 #include <assert.h>
michael@0 11 #include <sys/types.h>
michael@0 12 #include <dirent.h>
michael@0 13 #include <errno.h>
michael@0 14 #include <stdarg.h>
michael@0 15 #include <stdio.h>
michael@0 16 #include <stdlib.h>
michael@0 17 #include <string.h>
michael@0 18 #include <unistd.h>
michael@0 19 #include <sys/stat.h>
michael@0 20 #include "pathsub.h"
michael@0 21
michael@0 22 #ifdef USE_REENTRANT_LIBC
michael@0 23 #include <libc_r.h>
michael@0 24 #endif
michael@0 25
michael@0 26 #ifdef SUNOS4
michael@0 27 #include "sunos4.h"
michael@0 28 #endif
michael@0 29
michael@0 30 #ifndef D_INO
michael@0 31 #define D_INO d_ino
michael@0 32 #endif
michael@0 33
michael@0 34 char *program;
michael@0 35
michael@0 36 void
michael@0 37 fail(char *format, ...)
michael@0 38 {
michael@0 39 int error;
michael@0 40 va_list ap;
michael@0 41
michael@0 42 #ifdef USE_REENTRANT_LIBC
michael@0 43 R_STRERROR_INIT_R();
michael@0 44 #endif
michael@0 45
michael@0 46 error = errno;
michael@0 47 fprintf(stderr, "%s: ", program);
michael@0 48 va_start(ap, format);
michael@0 49 vfprintf(stderr, format, ap);
michael@0 50 va_end(ap);
michael@0 51 if (error) {
michael@0 52
michael@0 53 #ifdef USE_REENTRANT_LIBC
michael@0 54 R_STRERROR_R(errno);
michael@0 55 fprintf(stderr, ": %s", r_strerror_r);
michael@0 56 #else
michael@0 57 fprintf(stderr, ": %s", strerror(errno));
michael@0 58 #endif
michael@0 59 }
michael@0 60
michael@0 61 putc('\n', stderr);
michael@0 62 exit(1);
michael@0 63 }
michael@0 64
michael@0 65 char *
michael@0 66 getcomponent(char *path, char *name)
michael@0 67 {
michael@0 68 if (*path == '\0')
michael@0 69 return 0;
michael@0 70 if (*path == '/') {
michael@0 71 *name++ = '/';
michael@0 72 } else {
michael@0 73 do {
michael@0 74 *name++ = *path++;
michael@0 75 } while (*path != '/' && *path != '\0');
michael@0 76 }
michael@0 77 *name = '\0';
michael@0 78 while (*path == '/')
michael@0 79 path++;
michael@0 80 return path;
michael@0 81 }
michael@0 82
michael@0 83 #ifdef LAME_READDIR
michael@0 84 #include <sys/param.h>
michael@0 85 /*
michael@0 86 ** The static buffer in Unixware's readdir is too small.
michael@0 87 */
michael@0 88 struct dirent *readdir(DIR *d)
michael@0 89 {
michael@0 90 static struct dirent *buf = NULL;
michael@0 91
michael@0 92 if(buf == NULL)
michael@0 93 buf = (struct dirent *) malloc(sizeof(struct dirent) + MAXPATHLEN);
michael@0 94 return(readdir_r(d, buf));
michael@0 95 }
michael@0 96 #endif
michael@0 97
michael@0 98 char *
michael@0 99 ino2name(ino_t ino)
michael@0 100 {
michael@0 101 DIR *dp;
michael@0 102 struct dirent *ep;
michael@0 103 char *name;
michael@0 104
michael@0 105 dp = opendir("..");
michael@0 106 if (!dp)
michael@0 107 fail("cannot read parent directory");
michael@0 108 for (;;) {
michael@0 109 if (!(ep = readdir(dp)))
michael@0 110 fail("cannot find current directory");
michael@0 111 if (ep->D_INO == ino)
michael@0 112 break;
michael@0 113 }
michael@0 114 name = xstrdup(ep->d_name);
michael@0 115 closedir(dp);
michael@0 116 return name;
michael@0 117 }
michael@0 118
michael@0 119 void *
michael@0 120 xmalloc(size_t size)
michael@0 121 {
michael@0 122 void *p = malloc(size);
michael@0 123 if (!p)
michael@0 124 fail("cannot allocate %u bytes", size);
michael@0 125 return p;
michael@0 126 }
michael@0 127
michael@0 128 char *
michael@0 129 xstrdup(char *s)
michael@0 130 {
michael@0 131 return strcpy(xmalloc(strlen(s) + 1), s);
michael@0 132 }
michael@0 133
michael@0 134 char *
michael@0 135 xbasename(char *path)
michael@0 136 {
michael@0 137 char *cp;
michael@0 138
michael@0 139 while ((cp = strrchr(path, '/')) && cp[1] == '\0')
michael@0 140 *cp = '\0';
michael@0 141 if (!cp) return path;
michael@0 142 return cp + 1;
michael@0 143 }
michael@0 144
michael@0 145 void
michael@0 146 xchdir(char *dir)
michael@0 147 {
michael@0 148 if (chdir(dir) < 0)
michael@0 149 fail("cannot change directory to %s", dir);
michael@0 150 }
michael@0 151
michael@0 152 int
michael@0 153 relatepaths(char *from, char *to, char *outpath)
michael@0 154 {
michael@0 155 char *cp, *cp2;
michael@0 156 int len;
michael@0 157 char buf[NAME_MAX];
michael@0 158
michael@0 159 assert(*from == '/' && *to == '/');
michael@0 160 for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++)
michael@0 161 if (*cp == '\0')
michael@0 162 break;
michael@0 163 while (cp[-1] != '/')
michael@0 164 cp--, cp2--;
michael@0 165 if (cp - 1 == to) {
michael@0 166 /* closest common ancestor is /, so use full pathname */
michael@0 167 len = strlen(strcpy(outpath, to));
michael@0 168 if (outpath[len] != '/') {
michael@0 169 outpath[len++] = '/';
michael@0 170 outpath[len] = '\0';
michael@0 171 }
michael@0 172 } else {
michael@0 173 len = 0;
michael@0 174 while ((cp2 = getcomponent(cp2, buf)) != 0) {
michael@0 175 strcpy(outpath + len, "../");
michael@0 176 len += 3;
michael@0 177 }
michael@0 178 while ((cp = getcomponent(cp, buf)) != 0) {
michael@0 179 sprintf(outpath + len, "%s/", buf);
michael@0 180 len += strlen(outpath + len);
michael@0 181 }
michael@0 182 }
michael@0 183 return len;
michael@0 184 }
michael@0 185
michael@0 186 void
michael@0 187 reversepath(char *inpath, char *name, int len, char *outpath)
michael@0 188 {
michael@0 189 char *cp, *cp2;
michael@0 190 char buf[NAME_MAX];
michael@0 191 struct stat sb;
michael@0 192
michael@0 193 cp = strcpy(outpath + PATH_MAX - (len + 1), name);
michael@0 194 cp2 = inpath;
michael@0 195 while ((cp2 = getcomponent(cp2, buf)) != 0) {
michael@0 196 if (strcmp(buf, ".") == 0)
michael@0 197 continue;
michael@0 198 if (strcmp(buf, "..") == 0) {
michael@0 199 if (stat(".", &sb) < 0)
michael@0 200 fail("cannot stat current directory");
michael@0 201 name = ino2name(sb.st_ino);
michael@0 202 len = strlen(name);
michael@0 203 cp -= len + 1;
michael@0 204 strcpy(cp, name);
michael@0 205 cp[len] = '/';
michael@0 206 free(name);
michael@0 207 xchdir("..");
michael@0 208 } else {
michael@0 209 cp -= 3;
michael@0 210 strncpy(cp, "../", 3);
michael@0 211 xchdir(buf);
michael@0 212 }
michael@0 213 }
michael@0 214 strcpy(outpath, cp);
michael@0 215 }

mercurial