toolkit/components/osfile/modules/ospath_unix.jsm

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /**
michael@0 6 * Handling native paths.
michael@0 7 *
michael@0 8 * This module contains a number of functions destined to simplify
michael@0 9 * working with native paths through a cross-platform API. Functions
michael@0 10 * of this module will only work with the following assumptions:
michael@0 11 *
michael@0 12 * - paths are valid;
michael@0 13 * - paths are defined with one of the grammars that this module can
michael@0 14 * parse (see later);
michael@0 15 * - all path concatenations go through function |join|.
michael@0 16 */
michael@0 17
michael@0 18 "use strict";
michael@0 19
michael@0 20 // Boilerplate used to be able to import this module both from the main
michael@0 21 // thread and from worker threads.
michael@0 22 if (typeof Components != "undefined") {
michael@0 23 Components.utils.importGlobalProperties(["URL"]);
michael@0 24 // Global definition of |exports|, to keep everybody happy.
michael@0 25 // In non-main thread, |exports| is provided by the module
michael@0 26 // loader.
michael@0 27 this.exports = {};
michael@0 28 } else if (typeof "module" == "undefined" || typeof "exports" == "undefined") {
michael@0 29 throw new Error("Please load this module using require()");
michael@0 30 }
michael@0 31
michael@0 32 let EXPORTED_SYMBOLS = [
michael@0 33 "basename",
michael@0 34 "dirname",
michael@0 35 "join",
michael@0 36 "normalize",
michael@0 37 "split",
michael@0 38 "toFileURI",
michael@0 39 "fromFileURI",
michael@0 40 ];
michael@0 41
michael@0 42 /**
michael@0 43 * Return the final part of the path.
michael@0 44 * The final part of the path is everything after the last "/".
michael@0 45 */
michael@0 46 let basename = function(path) {
michael@0 47 return path.slice(path.lastIndexOf("/") + 1);
michael@0 48 };
michael@0 49 exports.basename = basename;
michael@0 50
michael@0 51 /**
michael@0 52 * Return the directory part of the path.
michael@0 53 * The directory part of the path is everything before the last
michael@0 54 * "/". If the last few characters of this part are also "/",
michael@0 55 * they are ignored.
michael@0 56 *
michael@0 57 * If the path contains no directory, return ".".
michael@0 58 */
michael@0 59 let dirname = function(path) {
michael@0 60 let index = path.lastIndexOf("/");
michael@0 61 if (index == -1) {
michael@0 62 return ".";
michael@0 63 }
michael@0 64 while (index >= 0 && path[index] == "/") {
michael@0 65 --index;
michael@0 66 }
michael@0 67 return path.slice(0, index + 1);
michael@0 68 };
michael@0 69 exports.dirname = dirname;
michael@0 70
michael@0 71 /**
michael@0 72 * Join path components.
michael@0 73 * This is the recommended manner of getting the path of a file/subdirectory
michael@0 74 * in a directory.
michael@0 75 *
michael@0 76 * Example: Obtaining $TMP/foo/bar in an OS-independent manner
michael@0 77 * var tmpDir = OS.Constants.Path.tmpDir;
michael@0 78 * var path = OS.Path.join(tmpDir, "foo", "bar");
michael@0 79 *
michael@0 80 * Under Unix, this will return "/tmp/foo/bar".
michael@0 81 */
michael@0 82 let join = function(...path) {
michael@0 83 // If there is a path that starts with a "/", eliminate everything before
michael@0 84 let paths = [];
michael@0 85 for (let subpath of path) {
michael@0 86 if (subpath == null) {
michael@0 87 throw new TypeError("invalid path component");
michael@0 88 }
michael@0 89 if (subpath.length != 0 && subpath[0] == "/") {
michael@0 90 paths = [subpath];
michael@0 91 } else {
michael@0 92 paths.push(subpath);
michael@0 93 }
michael@0 94 }
michael@0 95 return paths.join("/");
michael@0 96 };
michael@0 97 exports.join = join;
michael@0 98
michael@0 99 /**
michael@0 100 * Normalize a path by removing any unneeded ".", "..", "//".
michael@0 101 */
michael@0 102 let normalize = function(path) {
michael@0 103 let stack = [];
michael@0 104 let absolute;
michael@0 105 if (path.length >= 0 && path[0] == "/") {
michael@0 106 absolute = true;
michael@0 107 } else {
michael@0 108 absolute = false;
michael@0 109 }
michael@0 110 path.split("/").forEach(function(v) {
michael@0 111 switch (v) {
michael@0 112 case "": case ".":// fallthrough
michael@0 113 break;
michael@0 114 case "..":
michael@0 115 if (stack.length == 0) {
michael@0 116 if (absolute) {
michael@0 117 throw new Error("Path is ill-formed: attempting to go past root");
michael@0 118 } else {
michael@0 119 stack.push("..");
michael@0 120 }
michael@0 121 } else {
michael@0 122 if (stack[stack.length - 1] == "..") {
michael@0 123 stack.push("..");
michael@0 124 } else {
michael@0 125 stack.pop();
michael@0 126 }
michael@0 127 }
michael@0 128 break;
michael@0 129 default:
michael@0 130 stack.push(v);
michael@0 131 }
michael@0 132 });
michael@0 133 let string = stack.join("/");
michael@0 134 return absolute ? "/" + string : string;
michael@0 135 };
michael@0 136 exports.normalize = normalize;
michael@0 137
michael@0 138 /**
michael@0 139 * Return the components of a path.
michael@0 140 * You should generally apply this function to a normalized path.
michael@0 141 *
michael@0 142 * @return {{
michael@0 143 * {bool} absolute |true| if the path is absolute, |false| otherwise
michael@0 144 * {array} components the string components of the path
michael@0 145 * }}
michael@0 146 *
michael@0 147 * Other implementations may add additional OS-specific informations.
michael@0 148 */
michael@0 149 let split = function(path) {
michael@0 150 return {
michael@0 151 absolute: path.length && path[0] == "/",
michael@0 152 components: path.split("/")
michael@0 153 };
michael@0 154 };
michael@0 155 exports.split = split;
michael@0 156
michael@0 157 /**
michael@0 158 * Returns the file:// URI file path of the given local file path.
michael@0 159 */
michael@0 160 // The case of %3b is designed to match Services.io, but fundamentally doesn't matter.
michael@0 161 let toFileURIExtraEncodings = {';': '%3b', '?': '%3F', "'": '%27', '#': '%23'};
michael@0 162 let toFileURI = function toFileURI(path) {
michael@0 163 let uri = encodeURI(this.normalize(path));
michael@0 164
michael@0 165 // add a prefix, and encodeURI doesn't escape a few characters that we do
michael@0 166 // want to escape, so fix that up
michael@0 167 let prefix = "file://";
michael@0 168 uri = prefix + uri.replace(/[;?'#]/g, match => toFileURIExtraEncodings[match]);
michael@0 169
michael@0 170 return uri;
michael@0 171 };
michael@0 172 exports.toFileURI = toFileURI;
michael@0 173
michael@0 174 /**
michael@0 175 * Returns the local file path from a given file URI.
michael@0 176 */
michael@0 177 let fromFileURI = function fromFileURI(uri) {
michael@0 178 let url = new URL(uri);
michael@0 179 if (url.protocol != 'file:') {
michael@0 180 throw new Error("fromFileURI expects a file URI");
michael@0 181 }
michael@0 182 let path = this.normalize(decodeURIComponent(url.pathname));
michael@0 183 return path;
michael@0 184 };
michael@0 185 exports.fromFileURI = fromFileURI;
michael@0 186
michael@0 187
michael@0 188 //////////// Boilerplate
michael@0 189 if (typeof Components != "undefined") {
michael@0 190 this.EXPORTED_SYMBOLS = EXPORTED_SYMBOLS;
michael@0 191 for (let symbol of EXPORTED_SYMBOLS) {
michael@0 192 this[symbol] = exports[symbol];
michael@0 193 }
michael@0 194 }

mercurial