addon-sdk/source/lib/sdk/fs/path.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright Joyent, Inc. and other Node contributors.
michael@0 2 //
michael@0 3 // Permission is hereby granted, free of charge, to any person obtaining a
michael@0 4 // copy of this software and associated documentation files (the
michael@0 5 // "Software"), to deal in the Software without restriction, including
michael@0 6 // without limitation the rights to use, copy, modify, merge, publish,
michael@0 7 // distribute, sublicense, and/or sell copies of the Software, and to permit
michael@0 8 // persons to whom the Software is furnished to do so, subject to the
michael@0 9 // following conditions:
michael@0 10 //
michael@0 11 // The above copyright notice and this permission notice shall be included
michael@0 12 // in all copies or substantial portions of the Software.
michael@0 13 //
michael@0 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
michael@0 15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
michael@0 16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
michael@0 17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
michael@0 18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
michael@0 19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
michael@0 20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
michael@0 21
michael@0 22 // Adapted version of:
michael@0 23 // https://github.com/joyent/node/blob/v0.11.3/lib/path.js
michael@0 24
michael@0 25 // Shim process global from node.
michael@0 26 var process = Object.create(require('../system'));
michael@0 27 process.cwd = process.pathFor.bind(process, 'CurProcD');
michael@0 28
michael@0 29 // Update original check in node `process.platform === 'win32'` since in SDK it's `winnt`.
michael@0 30 var isWindows = process.platform.indexOf('win') === 0;
michael@0 31
michael@0 32
michael@0 33
michael@0 34 // resolves . and .. elements in a path array with directory names there
michael@0 35 // must be no slashes, empty elements, or device names (c:\) in the array
michael@0 36 // (so also no leading and trailing slashes - it does not distinguish
michael@0 37 // relative and absolute paths)
michael@0 38 function normalizeArray(parts, allowAboveRoot) {
michael@0 39 // if the path tries to go above the root, `up` ends up > 0
michael@0 40 var up = 0;
michael@0 41 for (var i = parts.length - 1; i >= 0; i--) {
michael@0 42 var last = parts[i];
michael@0 43 if (last === '.') {
michael@0 44 parts.splice(i, 1);
michael@0 45 } else if (last === '..') {
michael@0 46 parts.splice(i, 1);
michael@0 47 up++;
michael@0 48 } else if (up) {
michael@0 49 parts.splice(i, 1);
michael@0 50 up--;
michael@0 51 }
michael@0 52 }
michael@0 53
michael@0 54 // if the path is allowed to go above the root, restore leading ..s
michael@0 55 if (allowAboveRoot) {
michael@0 56 for (; up--; up) {
michael@0 57 parts.unshift('..');
michael@0 58 }
michael@0 59 }
michael@0 60
michael@0 61 return parts;
michael@0 62 }
michael@0 63
michael@0 64
michael@0 65 if (isWindows) {
michael@0 66 // Regex to split a windows path into three parts: [*, device, slash,
michael@0 67 // tail] windows-only
michael@0 68 var splitDeviceRe =
michael@0 69 /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/;
michael@0 70
michael@0 71 // Regex to split the tail part of the above into [*, dir, basename, ext]
michael@0 72 var splitTailRe =
michael@0 73 /^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/;
michael@0 74
michael@0 75 // Function to split a filename into [root, dir, basename, ext]
michael@0 76 // windows version
michael@0 77 var splitPath = function(filename) {
michael@0 78 // Separate device+slash from tail
michael@0 79 var result = splitDeviceRe.exec(filename),
michael@0 80 device = (result[1] || '') + (result[2] || ''),
michael@0 81 tail = result[3] || '';
michael@0 82 // Split the tail into dir, basename and extension
michael@0 83 var result2 = splitTailRe.exec(tail),
michael@0 84 dir = result2[1],
michael@0 85 basename = result2[2],
michael@0 86 ext = result2[3];
michael@0 87 return [device, dir, basename, ext];
michael@0 88 };
michael@0 89
michael@0 90 var normalizeUNCRoot = function(device) {
michael@0 91 return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\');
michael@0 92 };
michael@0 93
michael@0 94 // path.resolve([from ...], to)
michael@0 95 // windows version
michael@0 96 exports.resolve = function() {
michael@0 97 var resolvedDevice = '',
michael@0 98 resolvedTail = '',
michael@0 99 resolvedAbsolute = false;
michael@0 100
michael@0 101 for (var i = arguments.length - 1; i >= -1; i--) {
michael@0 102 var path;
michael@0 103 if (i >= 0) {
michael@0 104 path = arguments[i];
michael@0 105 } else if (!resolvedDevice) {
michael@0 106 path = process.cwd();
michael@0 107 } else {
michael@0 108 // Windows has the concept of drive-specific current working
michael@0 109 // directories. If we've resolved a drive letter but not yet an
michael@0 110 // absolute path, get cwd for that drive. We're sure the device is not
michael@0 111 // an unc path at this points, because unc paths are always absolute.
michael@0 112 path = process.env['=' + resolvedDevice];
michael@0 113 // Verify that a drive-local cwd was found and that it actually points
michael@0 114 // to our drive. If not, default to the drive's root.
michael@0 115 if (!path || path.substr(0, 3).toLowerCase() !==
michael@0 116 resolvedDevice.toLowerCase() + '\\') {
michael@0 117 path = resolvedDevice + '\\';
michael@0 118 }
michael@0 119 }
michael@0 120
michael@0 121 // Skip empty and invalid entries
michael@0 122 if (typeof path !== 'string') {
michael@0 123 throw new TypeError('Arguments to path.resolve must be strings');
michael@0 124 } else if (!path) {
michael@0 125 continue;
michael@0 126 }
michael@0 127
michael@0 128 var result = splitDeviceRe.exec(path),
michael@0 129 device = result[1] || '',
michael@0 130 isUnc = device && device.charAt(1) !== ':',
michael@0 131 isAbsolute = exports.isAbsolute(path),
michael@0 132 tail = result[3];
michael@0 133
michael@0 134 if (device &&
michael@0 135 resolvedDevice &&
michael@0 136 device.toLowerCase() !== resolvedDevice.toLowerCase()) {
michael@0 137 // This path points to another device so it is not applicable
michael@0 138 continue;
michael@0 139 }
michael@0 140
michael@0 141 if (!resolvedDevice) {
michael@0 142 resolvedDevice = device;
michael@0 143 }
michael@0 144 if (!resolvedAbsolute) {
michael@0 145 resolvedTail = tail + '\\' + resolvedTail;
michael@0 146 resolvedAbsolute = isAbsolute;
michael@0 147 }
michael@0 148
michael@0 149 if (resolvedDevice && resolvedAbsolute) {
michael@0 150 break;
michael@0 151 }
michael@0 152 }
michael@0 153
michael@0 154 // Convert slashes to backslashes when `resolvedDevice` points to an UNC
michael@0 155 // root. Also squash multiple slashes into a single one where appropriate.
michael@0 156 if (isUnc) {
michael@0 157 resolvedDevice = normalizeUNCRoot(resolvedDevice);
michael@0 158 }
michael@0 159
michael@0 160 // At this point the path should be resolved to a full absolute path,
michael@0 161 // but handle relative paths to be safe (might happen when process.cwd()
michael@0 162 // fails)
michael@0 163
michael@0 164 // Normalize the tail path
michael@0 165
michael@0 166 function f(p) {
michael@0 167 return !!p;
michael@0 168 }
michael@0 169
michael@0 170 resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/).filter(f),
michael@0 171 !resolvedAbsolute).join('\\');
michael@0 172
michael@0 173 return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
michael@0 174 '.';
michael@0 175 };
michael@0 176
michael@0 177 // windows version
michael@0 178 exports.normalize = function(path) {
michael@0 179 var result = splitDeviceRe.exec(path),
michael@0 180 device = result[1] || '',
michael@0 181 isUnc = device && device.charAt(1) !== ':',
michael@0 182 isAbsolute = exports.isAbsolute(path),
michael@0 183 tail = result[3],
michael@0 184 trailingSlash = /[\\\/]$/.test(tail);
michael@0 185
michael@0 186 // If device is a drive letter, we'll normalize to lower case.
michael@0 187 if (device && device.charAt(1) === ':') {
michael@0 188 device = device[0].toLowerCase() + device.substr(1);
michael@0 189 }
michael@0 190
michael@0 191 // Normalize the tail path
michael@0 192 tail = normalizeArray(tail.split(/[\\\/]+/).filter(function(p) {
michael@0 193 return !!p;
michael@0 194 }), !isAbsolute).join('\\');
michael@0 195
michael@0 196 if (!tail && !isAbsolute) {
michael@0 197 tail = '.';
michael@0 198 }
michael@0 199 if (tail && trailingSlash) {
michael@0 200 tail += '\\';
michael@0 201 }
michael@0 202
michael@0 203 // Convert slashes to backslashes when `device` points to an UNC root.
michael@0 204 // Also squash multiple slashes into a single one where appropriate.
michael@0 205 if (isUnc) {
michael@0 206 device = normalizeUNCRoot(device);
michael@0 207 }
michael@0 208
michael@0 209 return device + (isAbsolute ? '\\' : '') + tail;
michael@0 210 };
michael@0 211
michael@0 212 // windows version
michael@0 213 exports.isAbsolute = function(path) {
michael@0 214 var result = splitDeviceRe.exec(path),
michael@0 215 device = result[1] || '',
michael@0 216 isUnc = device && device.charAt(1) !== ':';
michael@0 217 // UNC paths are always absolute
michael@0 218 return !!result[2] || isUnc;
michael@0 219 };
michael@0 220
michael@0 221 // windows version
michael@0 222 exports.join = function() {
michael@0 223 function f(p) {
michael@0 224 if (typeof p !== 'string') {
michael@0 225 throw new TypeError('Arguments to path.join must be strings');
michael@0 226 }
michael@0 227 return p;
michael@0 228 }
michael@0 229
michael@0 230 var paths = Array.prototype.filter.call(arguments, f);
michael@0 231 var joined = paths.join('\\');
michael@0 232
michael@0 233 // Make sure that the joined path doesn't start with two slashes, because
michael@0 234 // normalize() will mistake it for an UNC path then.
michael@0 235 //
michael@0 236 // This step is skipped when it is very clear that the user actually
michael@0 237 // intended to point at an UNC path. This is assumed when the first
michael@0 238 // non-empty string arguments starts with exactly two slashes followed by
michael@0 239 // at least one more non-slash character.
michael@0 240 //
michael@0 241 // Note that for normalize() to treat a path as an UNC path it needs to
michael@0 242 // have at least 2 components, so we don't filter for that here.
michael@0 243 // This means that the user can use join to construct UNC paths from
michael@0 244 // a server name and a share name; for example:
michael@0 245 // path.join('//server', 'share') -> '\\\\server\\share\')
michael@0 246 if (!/^[\\\/]{2}[^\\\/]/.test(paths[0])) {
michael@0 247 joined = joined.replace(/^[\\\/]{2,}/, '\\');
michael@0 248 }
michael@0 249
michael@0 250 return exports.normalize(joined);
michael@0 251 };
michael@0 252
michael@0 253 // path.relative(from, to)
michael@0 254 // it will solve the relative path from 'from' to 'to', for instance:
michael@0 255 // from = 'C:\\orandea\\test\\aaa'
michael@0 256 // to = 'C:\\orandea\\impl\\bbb'
michael@0 257 // The output of the function should be: '..\\..\\impl\\bbb'
michael@0 258 // windows version
michael@0 259 exports.relative = function(from, to) {
michael@0 260 from = exports.resolve(from);
michael@0 261 to = exports.resolve(to);
michael@0 262
michael@0 263 // windows is not case sensitive
michael@0 264 var lowerFrom = from.toLowerCase();
michael@0 265 var lowerTo = to.toLowerCase();
michael@0 266
michael@0 267 function trim(arr) {
michael@0 268 var start = 0;
michael@0 269 for (; start < arr.length; start++) {
michael@0 270 if (arr[start] !== '') break;
michael@0 271 }
michael@0 272
michael@0 273 var end = arr.length - 1;
michael@0 274 for (; end >= 0; end--) {
michael@0 275 if (arr[end] !== '') break;
michael@0 276 }
michael@0 277
michael@0 278 if (start > end) return [];
michael@0 279 return arr.slice(start, end - start + 1);
michael@0 280 }
michael@0 281
michael@0 282 var toParts = trim(to.split('\\'));
michael@0 283
michael@0 284 var lowerFromParts = trim(lowerFrom.split('\\'));
michael@0 285 var lowerToParts = trim(lowerTo.split('\\'));
michael@0 286
michael@0 287 var length = Math.min(lowerFromParts.length, lowerToParts.length);
michael@0 288 var samePartsLength = length;
michael@0 289 for (var i = 0; i < length; i++) {
michael@0 290 if (lowerFromParts[i] !== lowerToParts[i]) {
michael@0 291 samePartsLength = i;
michael@0 292 break;
michael@0 293 }
michael@0 294 }
michael@0 295
michael@0 296 if (samePartsLength == 0) {
michael@0 297 return to;
michael@0 298 }
michael@0 299
michael@0 300 var outputParts = [];
michael@0 301 for (var i = samePartsLength; i < lowerFromParts.length; i++) {
michael@0 302 outputParts.push('..');
michael@0 303 }
michael@0 304
michael@0 305 outputParts = outputParts.concat(toParts.slice(samePartsLength));
michael@0 306
michael@0 307 return outputParts.join('\\');
michael@0 308 };
michael@0 309
michael@0 310 exports.sep = '\\';
michael@0 311 exports.delimiter = ';';
michael@0 312
michael@0 313 } else /* posix */ {
michael@0 314
michael@0 315 // Split a filename into [root, dir, basename, ext], unix version
michael@0 316 // 'root' is just a slash, or nothing.
michael@0 317 var splitPathRe =
michael@0 318 /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
michael@0 319 var splitPath = function(filename) {
michael@0 320 return splitPathRe.exec(filename).slice(1);
michael@0 321 };
michael@0 322
michael@0 323 // path.resolve([from ...], to)
michael@0 324 // posix version
michael@0 325 exports.resolve = function() {
michael@0 326 var resolvedPath = '',
michael@0 327 resolvedAbsolute = false;
michael@0 328
michael@0 329 for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
michael@0 330 var path = (i >= 0) ? arguments[i] : process.cwd();
michael@0 331
michael@0 332 // Skip empty and invalid entries
michael@0 333 if (typeof path !== 'string') {
michael@0 334 throw new TypeError('Arguments to path.resolve must be strings');
michael@0 335 } else if (!path) {
michael@0 336 continue;
michael@0 337 }
michael@0 338
michael@0 339 resolvedPath = path + '/' + resolvedPath;
michael@0 340 resolvedAbsolute = path.charAt(0) === '/';
michael@0 341 }
michael@0 342
michael@0 343 // At this point the path should be resolved to a full absolute path, but
michael@0 344 // handle relative paths to be safe (might happen when process.cwd() fails)
michael@0 345
michael@0 346 // Normalize the path
michael@0 347 resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) {
michael@0 348 return !!p;
michael@0 349 }), !resolvedAbsolute).join('/');
michael@0 350
michael@0 351 return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
michael@0 352 };
michael@0 353
michael@0 354 // path.normalize(path)
michael@0 355 // posix version
michael@0 356 exports.normalize = function(path) {
michael@0 357 var isAbsolute = exports.isAbsolute(path),
michael@0 358 trailingSlash = path.substr(-1) === '/';
michael@0 359
michael@0 360 // Normalize the path
michael@0 361 path = normalizeArray(path.split('/').filter(function(p) {
michael@0 362 return !!p;
michael@0 363 }), !isAbsolute).join('/');
michael@0 364
michael@0 365 if (!path && !isAbsolute) {
michael@0 366 path = '.';
michael@0 367 }
michael@0 368 if (path && trailingSlash) {
michael@0 369 path += '/';
michael@0 370 }
michael@0 371
michael@0 372 return (isAbsolute ? '/' : '') + path;
michael@0 373 };
michael@0 374
michael@0 375 // posix version
michael@0 376 exports.isAbsolute = function(path) {
michael@0 377 return path.charAt(0) === '/';
michael@0 378 };
michael@0 379
michael@0 380 // posix version
michael@0 381 exports.join = function() {
michael@0 382 var paths = Array.prototype.slice.call(arguments, 0);
michael@0 383 return exports.normalize(paths.filter(function(p, index) {
michael@0 384 if (typeof p !== 'string') {
michael@0 385 throw new TypeError('Arguments to path.join must be strings');
michael@0 386 }
michael@0 387 return p;
michael@0 388 }).join('/'));
michael@0 389 };
michael@0 390
michael@0 391
michael@0 392 // path.relative(from, to)
michael@0 393 // posix version
michael@0 394 exports.relative = function(from, to) {
michael@0 395 from = exports.resolve(from).substr(1);
michael@0 396 to = exports.resolve(to).substr(1);
michael@0 397
michael@0 398 function trim(arr) {
michael@0 399 var start = 0;
michael@0 400 for (; start < arr.length; start++) {
michael@0 401 if (arr[start] !== '') break;
michael@0 402 }
michael@0 403
michael@0 404 var end = arr.length - 1;
michael@0 405 for (; end >= 0; end--) {
michael@0 406 if (arr[end] !== '') break;
michael@0 407 }
michael@0 408
michael@0 409 if (start > end) return [];
michael@0 410 return arr.slice(start, end - start + 1);
michael@0 411 }
michael@0 412
michael@0 413 var fromParts = trim(from.split('/'));
michael@0 414 var toParts = trim(to.split('/'));
michael@0 415
michael@0 416 var length = Math.min(fromParts.length, toParts.length);
michael@0 417 var samePartsLength = length;
michael@0 418 for (var i = 0; i < length; i++) {
michael@0 419 if (fromParts[i] !== toParts[i]) {
michael@0 420 samePartsLength = i;
michael@0 421 break;
michael@0 422 }
michael@0 423 }
michael@0 424
michael@0 425 var outputParts = [];
michael@0 426 for (var i = samePartsLength; i < fromParts.length; i++) {
michael@0 427 outputParts.push('..');
michael@0 428 }
michael@0 429
michael@0 430 outputParts = outputParts.concat(toParts.slice(samePartsLength));
michael@0 431
michael@0 432 return outputParts.join('/');
michael@0 433 };
michael@0 434
michael@0 435 exports.sep = '/';
michael@0 436 exports.delimiter = ':';
michael@0 437 }
michael@0 438
michael@0 439 exports.dirname = function(path) {
michael@0 440 var result = splitPath(path),
michael@0 441 root = result[0],
michael@0 442 dir = result[1];
michael@0 443
michael@0 444 if (!root && !dir) {
michael@0 445 // No dirname whatsoever
michael@0 446 return '.';
michael@0 447 }
michael@0 448
michael@0 449 if (dir) {
michael@0 450 // It has a dirname, strip trailing slash
michael@0 451 dir = dir.substr(0, dir.length - 1);
michael@0 452 }
michael@0 453
michael@0 454 return root + dir;
michael@0 455 };
michael@0 456
michael@0 457
michael@0 458 exports.basename = function(path, ext) {
michael@0 459 var f = splitPath(path)[2];
michael@0 460 // TODO: make this comparison case-insensitive on windows?
michael@0 461 if (ext && f.substr(-1 * ext.length) === ext) {
michael@0 462 f = f.substr(0, f.length - ext.length);
michael@0 463 }
michael@0 464 return f;
michael@0 465 };
michael@0 466
michael@0 467
michael@0 468 exports.extname = function(path) {
michael@0 469 return splitPath(path)[3];
michael@0 470 };
michael@0 471
michael@0 472 if (isWindows) {
michael@0 473 exports._makeLong = function(path) {
michael@0 474 // Note: this will *probably* throw somewhere.
michael@0 475 if (typeof path !== 'string')
michael@0 476 return path;
michael@0 477
michael@0 478 if (!path) {
michael@0 479 return '';
michael@0 480 }
michael@0 481
michael@0 482 var resolvedPath = exports.resolve(path);
michael@0 483
michael@0 484 if (/^[a-zA-Z]\:\\/.test(resolvedPath)) {
michael@0 485 // path is local filesystem path, which needs to be converted
michael@0 486 // to long UNC path.
michael@0 487 return '\\\\?\\' + resolvedPath;
michael@0 488 } else if (/^\\\\[^?.]/.test(resolvedPath)) {
michael@0 489 // path is network UNC path, which needs to be converted
michael@0 490 // to long UNC path.
michael@0 491 return '\\\\?\\UNC\\' + resolvedPath.substring(2);
michael@0 492 }
michael@0 493
michael@0 494 return path;
michael@0 495 };
michael@0 496 } else {
michael@0 497 exports._makeLong = function(path) {
michael@0 498 return path;
michael@0 499 };
michael@0 500 }

mercurial