michael@0: // Copyright Joyent, Inc. and other Node contributors. michael@0: // michael@0: // Permission is hereby granted, free of charge, to any person obtaining a michael@0: // copy of this software and associated documentation files (the michael@0: // "Software"), to deal in the Software without restriction, including michael@0: // without limitation the rights to use, copy, modify, merge, publish, michael@0: // distribute, sublicense, and/or sell copies of the Software, and to permit michael@0: // persons to whom the Software is furnished to do so, subject to the michael@0: // following conditions: michael@0: // michael@0: // The above copyright notice and this permission notice shall be included michael@0: // in all copies or substantial portions of the Software. michael@0: // michael@0: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS michael@0: // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF michael@0: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN michael@0: // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, michael@0: // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR michael@0: // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE michael@0: // USE OR OTHER DEALINGS IN THE SOFTWARE. michael@0: michael@0: // Adapted version of: michael@0: // https://github.com/joyent/node/blob/v0.11.3/lib/path.js michael@0: michael@0: // Shim process global from node. michael@0: var process = Object.create(require('../system')); michael@0: process.cwd = process.pathFor.bind(process, 'CurProcD'); michael@0: michael@0: // Update original check in node `process.platform === 'win32'` since in SDK it's `winnt`. michael@0: var isWindows = process.platform.indexOf('win') === 0; michael@0: michael@0: michael@0: michael@0: // resolves . and .. elements in a path array with directory names there michael@0: // must be no slashes, empty elements, or device names (c:\) in the array michael@0: // (so also no leading and trailing slashes - it does not distinguish michael@0: // relative and absolute paths) michael@0: function normalizeArray(parts, allowAboveRoot) { michael@0: // if the path tries to go above the root, `up` ends up > 0 michael@0: var up = 0; michael@0: for (var i = parts.length - 1; i >= 0; i--) { michael@0: var last = parts[i]; michael@0: if (last === '.') { michael@0: parts.splice(i, 1); michael@0: } else if (last === '..') { michael@0: parts.splice(i, 1); michael@0: up++; michael@0: } else if (up) { michael@0: parts.splice(i, 1); michael@0: up--; michael@0: } michael@0: } michael@0: michael@0: // if the path is allowed to go above the root, restore leading ..s michael@0: if (allowAboveRoot) { michael@0: for (; up--; up) { michael@0: parts.unshift('..'); michael@0: } michael@0: } michael@0: michael@0: return parts; michael@0: } michael@0: michael@0: michael@0: if (isWindows) { michael@0: // Regex to split a windows path into three parts: [*, device, slash, michael@0: // tail] windows-only michael@0: var splitDeviceRe = michael@0: /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/; michael@0: michael@0: // Regex to split the tail part of the above into [*, dir, basename, ext] michael@0: var splitTailRe = michael@0: /^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/; michael@0: michael@0: // Function to split a filename into [root, dir, basename, ext] michael@0: // windows version michael@0: var splitPath = function(filename) { michael@0: // Separate device+slash from tail michael@0: var result = splitDeviceRe.exec(filename), michael@0: device = (result[1] || '') + (result[2] || ''), michael@0: tail = result[3] || ''; michael@0: // Split the tail into dir, basename and extension michael@0: var result2 = splitTailRe.exec(tail), michael@0: dir = result2[1], michael@0: basename = result2[2], michael@0: ext = result2[3]; michael@0: return [device, dir, basename, ext]; michael@0: }; michael@0: michael@0: var normalizeUNCRoot = function(device) { michael@0: return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\'); michael@0: }; michael@0: michael@0: // path.resolve([from ...], to) michael@0: // windows version michael@0: exports.resolve = function() { michael@0: var resolvedDevice = '', michael@0: resolvedTail = '', michael@0: resolvedAbsolute = false; michael@0: michael@0: for (var i = arguments.length - 1; i >= -1; i--) { michael@0: var path; michael@0: if (i >= 0) { michael@0: path = arguments[i]; michael@0: } else if (!resolvedDevice) { michael@0: path = process.cwd(); michael@0: } else { michael@0: // Windows has the concept of drive-specific current working michael@0: // directories. If we've resolved a drive letter but not yet an michael@0: // absolute path, get cwd for that drive. We're sure the device is not michael@0: // an unc path at this points, because unc paths are always absolute. michael@0: path = process.env['=' + resolvedDevice]; michael@0: // Verify that a drive-local cwd was found and that it actually points michael@0: // to our drive. If not, default to the drive's root. michael@0: if (!path || path.substr(0, 3).toLowerCase() !== michael@0: resolvedDevice.toLowerCase() + '\\') { michael@0: path = resolvedDevice + '\\'; michael@0: } michael@0: } michael@0: michael@0: // Skip empty and invalid entries michael@0: if (typeof path !== 'string') { michael@0: throw new TypeError('Arguments to path.resolve must be strings'); michael@0: } else if (!path) { michael@0: continue; michael@0: } michael@0: michael@0: var result = splitDeviceRe.exec(path), michael@0: device = result[1] || '', michael@0: isUnc = device && device.charAt(1) !== ':', michael@0: isAbsolute = exports.isAbsolute(path), michael@0: tail = result[3]; michael@0: michael@0: if (device && michael@0: resolvedDevice && michael@0: device.toLowerCase() !== resolvedDevice.toLowerCase()) { michael@0: // This path points to another device so it is not applicable michael@0: continue; michael@0: } michael@0: michael@0: if (!resolvedDevice) { michael@0: resolvedDevice = device; michael@0: } michael@0: if (!resolvedAbsolute) { michael@0: resolvedTail = tail + '\\' + resolvedTail; michael@0: resolvedAbsolute = isAbsolute; michael@0: } michael@0: michael@0: if (resolvedDevice && resolvedAbsolute) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: // Convert slashes to backslashes when `resolvedDevice` points to an UNC michael@0: // root. Also squash multiple slashes into a single one where appropriate. michael@0: if (isUnc) { michael@0: resolvedDevice = normalizeUNCRoot(resolvedDevice); michael@0: } michael@0: michael@0: // At this point the path should be resolved to a full absolute path, michael@0: // but handle relative paths to be safe (might happen when process.cwd() michael@0: // fails) michael@0: michael@0: // Normalize the tail path michael@0: michael@0: function f(p) { michael@0: return !!p; michael@0: } michael@0: michael@0: resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/).filter(f), michael@0: !resolvedAbsolute).join('\\'); michael@0: michael@0: return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) || michael@0: '.'; michael@0: }; michael@0: michael@0: // windows version michael@0: exports.normalize = function(path) { michael@0: var result = splitDeviceRe.exec(path), michael@0: device = result[1] || '', michael@0: isUnc = device && device.charAt(1) !== ':', michael@0: isAbsolute = exports.isAbsolute(path), michael@0: tail = result[3], michael@0: trailingSlash = /[\\\/]$/.test(tail); michael@0: michael@0: // If device is a drive letter, we'll normalize to lower case. michael@0: if (device && device.charAt(1) === ':') { michael@0: device = device[0].toLowerCase() + device.substr(1); michael@0: } michael@0: michael@0: // Normalize the tail path michael@0: tail = normalizeArray(tail.split(/[\\\/]+/).filter(function(p) { michael@0: return !!p; michael@0: }), !isAbsolute).join('\\'); michael@0: michael@0: if (!tail && !isAbsolute) { michael@0: tail = '.'; michael@0: } michael@0: if (tail && trailingSlash) { michael@0: tail += '\\'; michael@0: } michael@0: michael@0: // Convert slashes to backslashes when `device` points to an UNC root. michael@0: // Also squash multiple slashes into a single one where appropriate. michael@0: if (isUnc) { michael@0: device = normalizeUNCRoot(device); michael@0: } michael@0: michael@0: return device + (isAbsolute ? '\\' : '') + tail; michael@0: }; michael@0: michael@0: // windows version michael@0: exports.isAbsolute = function(path) { michael@0: var result = splitDeviceRe.exec(path), michael@0: device = result[1] || '', michael@0: isUnc = device && device.charAt(1) !== ':'; michael@0: // UNC paths are always absolute michael@0: return !!result[2] || isUnc; michael@0: }; michael@0: michael@0: // windows version michael@0: exports.join = function() { michael@0: function f(p) { michael@0: if (typeof p !== 'string') { michael@0: throw new TypeError('Arguments to path.join must be strings'); michael@0: } michael@0: return p; michael@0: } michael@0: michael@0: var paths = Array.prototype.filter.call(arguments, f); michael@0: var joined = paths.join('\\'); michael@0: michael@0: // Make sure that the joined path doesn't start with two slashes, because michael@0: // normalize() will mistake it for an UNC path then. michael@0: // michael@0: // This step is skipped when it is very clear that the user actually michael@0: // intended to point at an UNC path. This is assumed when the first michael@0: // non-empty string arguments starts with exactly two slashes followed by michael@0: // at least one more non-slash character. michael@0: // michael@0: // Note that for normalize() to treat a path as an UNC path it needs to michael@0: // have at least 2 components, so we don't filter for that here. michael@0: // This means that the user can use join to construct UNC paths from michael@0: // a server name and a share name; for example: michael@0: // path.join('//server', 'share') -> '\\\\server\\share\') michael@0: if (!/^[\\\/]{2}[^\\\/]/.test(paths[0])) { michael@0: joined = joined.replace(/^[\\\/]{2,}/, '\\'); michael@0: } michael@0: michael@0: return exports.normalize(joined); michael@0: }; michael@0: michael@0: // path.relative(from, to) michael@0: // it will solve the relative path from 'from' to 'to', for instance: michael@0: // from = 'C:\\orandea\\test\\aaa' michael@0: // to = 'C:\\orandea\\impl\\bbb' michael@0: // The output of the function should be: '..\\..\\impl\\bbb' michael@0: // windows version michael@0: exports.relative = function(from, to) { michael@0: from = exports.resolve(from); michael@0: to = exports.resolve(to); michael@0: michael@0: // windows is not case sensitive michael@0: var lowerFrom = from.toLowerCase(); michael@0: var lowerTo = to.toLowerCase(); michael@0: michael@0: function trim(arr) { michael@0: var start = 0; michael@0: for (; start < arr.length; start++) { michael@0: if (arr[start] !== '') break; michael@0: } michael@0: michael@0: var end = arr.length - 1; michael@0: for (; end >= 0; end--) { michael@0: if (arr[end] !== '') break; michael@0: } michael@0: michael@0: if (start > end) return []; michael@0: return arr.slice(start, end - start + 1); michael@0: } michael@0: michael@0: var toParts = trim(to.split('\\')); michael@0: michael@0: var lowerFromParts = trim(lowerFrom.split('\\')); michael@0: var lowerToParts = trim(lowerTo.split('\\')); michael@0: michael@0: var length = Math.min(lowerFromParts.length, lowerToParts.length); michael@0: var samePartsLength = length; michael@0: for (var i = 0; i < length; i++) { michael@0: if (lowerFromParts[i] !== lowerToParts[i]) { michael@0: samePartsLength = i; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (samePartsLength == 0) { michael@0: return to; michael@0: } michael@0: michael@0: var outputParts = []; michael@0: for (var i = samePartsLength; i < lowerFromParts.length; i++) { michael@0: outputParts.push('..'); michael@0: } michael@0: michael@0: outputParts = outputParts.concat(toParts.slice(samePartsLength)); michael@0: michael@0: return outputParts.join('\\'); michael@0: }; michael@0: michael@0: exports.sep = '\\'; michael@0: exports.delimiter = ';'; michael@0: michael@0: } else /* posix */ { michael@0: michael@0: // Split a filename into [root, dir, basename, ext], unix version michael@0: // 'root' is just a slash, or nothing. michael@0: var splitPathRe = michael@0: /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; michael@0: var splitPath = function(filename) { michael@0: return splitPathRe.exec(filename).slice(1); michael@0: }; michael@0: michael@0: // path.resolve([from ...], to) michael@0: // posix version michael@0: exports.resolve = function() { michael@0: var resolvedPath = '', michael@0: resolvedAbsolute = false; michael@0: michael@0: for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) { michael@0: var path = (i >= 0) ? arguments[i] : process.cwd(); michael@0: michael@0: // Skip empty and invalid entries michael@0: if (typeof path !== 'string') { michael@0: throw new TypeError('Arguments to path.resolve must be strings'); michael@0: } else if (!path) { michael@0: continue; michael@0: } michael@0: michael@0: resolvedPath = path + '/' + resolvedPath; michael@0: resolvedAbsolute = path.charAt(0) === '/'; michael@0: } michael@0: michael@0: // At this point the path should be resolved to a full absolute path, but michael@0: // handle relative paths to be safe (might happen when process.cwd() fails) michael@0: michael@0: // Normalize the path michael@0: resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) { michael@0: return !!p; michael@0: }), !resolvedAbsolute).join('/'); michael@0: michael@0: return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.'; michael@0: }; michael@0: michael@0: // path.normalize(path) michael@0: // posix version michael@0: exports.normalize = function(path) { michael@0: var isAbsolute = exports.isAbsolute(path), michael@0: trailingSlash = path.substr(-1) === '/'; michael@0: michael@0: // Normalize the path michael@0: path = normalizeArray(path.split('/').filter(function(p) { michael@0: return !!p; michael@0: }), !isAbsolute).join('/'); michael@0: michael@0: if (!path && !isAbsolute) { michael@0: path = '.'; michael@0: } michael@0: if (path && trailingSlash) { michael@0: path += '/'; michael@0: } michael@0: michael@0: return (isAbsolute ? '/' : '') + path; michael@0: }; michael@0: michael@0: // posix version michael@0: exports.isAbsolute = function(path) { michael@0: return path.charAt(0) === '/'; michael@0: }; michael@0: michael@0: // posix version michael@0: exports.join = function() { michael@0: var paths = Array.prototype.slice.call(arguments, 0); michael@0: return exports.normalize(paths.filter(function(p, index) { michael@0: if (typeof p !== 'string') { michael@0: throw new TypeError('Arguments to path.join must be strings'); michael@0: } michael@0: return p; michael@0: }).join('/')); michael@0: }; michael@0: michael@0: michael@0: // path.relative(from, to) michael@0: // posix version michael@0: exports.relative = function(from, to) { michael@0: from = exports.resolve(from).substr(1); michael@0: to = exports.resolve(to).substr(1); michael@0: michael@0: function trim(arr) { michael@0: var start = 0; michael@0: for (; start < arr.length; start++) { michael@0: if (arr[start] !== '') break; michael@0: } michael@0: michael@0: var end = arr.length - 1; michael@0: for (; end >= 0; end--) { michael@0: if (arr[end] !== '') break; michael@0: } michael@0: michael@0: if (start > end) return []; michael@0: return arr.slice(start, end - start + 1); michael@0: } michael@0: michael@0: var fromParts = trim(from.split('/')); michael@0: var toParts = trim(to.split('/')); michael@0: michael@0: var length = Math.min(fromParts.length, toParts.length); michael@0: var samePartsLength = length; michael@0: for (var i = 0; i < length; i++) { michael@0: if (fromParts[i] !== toParts[i]) { michael@0: samePartsLength = i; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: var outputParts = []; michael@0: for (var i = samePartsLength; i < fromParts.length; i++) { michael@0: outputParts.push('..'); michael@0: } michael@0: michael@0: outputParts = outputParts.concat(toParts.slice(samePartsLength)); michael@0: michael@0: return outputParts.join('/'); michael@0: }; michael@0: michael@0: exports.sep = '/'; michael@0: exports.delimiter = ':'; michael@0: } michael@0: michael@0: exports.dirname = function(path) { michael@0: var result = splitPath(path), michael@0: root = result[0], michael@0: dir = result[1]; michael@0: michael@0: if (!root && !dir) { michael@0: // No dirname whatsoever michael@0: return '.'; michael@0: } michael@0: michael@0: if (dir) { michael@0: // It has a dirname, strip trailing slash michael@0: dir = dir.substr(0, dir.length - 1); michael@0: } michael@0: michael@0: return root + dir; michael@0: }; michael@0: michael@0: michael@0: exports.basename = function(path, ext) { michael@0: var f = splitPath(path)[2]; michael@0: // TODO: make this comparison case-insensitive on windows? michael@0: if (ext && f.substr(-1 * ext.length) === ext) { michael@0: f = f.substr(0, f.length - ext.length); michael@0: } michael@0: return f; michael@0: }; michael@0: michael@0: michael@0: exports.extname = function(path) { michael@0: return splitPath(path)[3]; michael@0: }; michael@0: michael@0: if (isWindows) { michael@0: exports._makeLong = function(path) { michael@0: // Note: this will *probably* throw somewhere. michael@0: if (typeof path !== 'string') michael@0: return path; michael@0: michael@0: if (!path) { michael@0: return ''; michael@0: } michael@0: michael@0: var resolvedPath = exports.resolve(path); michael@0: michael@0: if (/^[a-zA-Z]\:\\/.test(resolvedPath)) { michael@0: // path is local filesystem path, which needs to be converted michael@0: // to long UNC path. michael@0: return '\\\\?\\' + resolvedPath; michael@0: } else if (/^\\\\[^?.]/.test(resolvedPath)) { michael@0: // path is network UNC path, which needs to be converted michael@0: // to long UNC path. michael@0: return '\\\\?\\UNC\\' + resolvedPath.substring(2); michael@0: } michael@0: michael@0: return path; michael@0: }; michael@0: } else { michael@0: exports._makeLong = function(path) { michael@0: return path; michael@0: }; michael@0: }