diff --git a/src/components/commandline.js b/src/components/commandline.js index 8b0f03f..6c0ab15 100644 --- a/src/components/commandline.js +++ b/src/components/commandline.js @@ -110,7 +110,7 @@ slCommandLine.prototype = { slConfiguration.scriptFile = cmdLine.resolveFile(slConfiguration.args[0]); } catch(e) { - slConfiguration.scriptFile = getMozFile(slConfiguration.args[0], cmdLine.workingDirectory) + slConfiguration.scriptFile = getAbsMozFile(slConfiguration.args[0], cmdLine.workingDirectory) } } else { diff --git a/src/modules/addon-sdk/sdk/io/file.js b/src/modules/addon-sdk/sdk/io/file.js index 96f4fd5..911c3ff 100644 --- a/src/modules/addon-sdk/sdk/io/file.js +++ b/src/modules/addon-sdk/sdk/io/file.js @@ -28,24 +28,35 @@ var dirsvc = Cc["@mozilla.org/file/directory_service;1"] var currentWorkingDirectory = dirsvc.get("CurWorkD", Ci.nsIFile); var _separator = '/'; +var isWin = false; -if (currentWorkingDirectory.path.charAt(0) != '/') { - _separator = '\\'; +if (currentWorkingDirectory instanceof Ci.nsILocalFileWin) { + _separator = '\\'; + isWin = true; } function MozFile(path) { - var file = currentWorkingDirectory.clone(); - try { - // if path is a relative path, there won't have exception + let isAbs = false; + if (isWin) { + path = path.replace(/\//g, "\\"); + isAbs = (/^([a-z]:)/i.test(path)); + } + else { + isAbs = (/^\//i.test(path)); + } + + let file; + if (isAbs) { + file = Cc['@mozilla.org/file/local;1'] + .createInstance(Ci.nsILocalFile); + file.initWithPath(path); + } + else { + file = currentWorkingDirectory.clone(); file.appendRelativePath(path); - return file; } - catch(e) { } - // the given path is really an absolute path - file = Cc['@mozilla.org/file/local;1'] - .createInstance(Ci.nsILocalFile); - file.initWithPath(path); + return file; } @@ -200,12 +211,12 @@ exports.join = function join(base) { exports.split = function split(path) { var f; - if (_separator == '\\') + if (isWin) // normalize path with / and replace redondant separator by a single separator - f = path.replace('/\\+/',"/"); + f = path.replace(/\\+/g,"/"); else // replace redondant separators by a single separator - f = path.replace('/\/+/',"/"); + f = path.replace(/\/+/g,"/"); return f.replace(/\/$/, "").split("/") } @@ -247,7 +258,8 @@ exports.basename = function basename(path) { }; exports.absolute = function base(path) { - var f = exports.split(MozFile(path).path); + var mzf = MozFile(path).path; + var f = exports.split(mzf); var p = []; f.forEach(function(element){ if (element == '.') @@ -260,9 +272,12 @@ exports.absolute = function base(path) { }) if (!p.length) return ''; - p[0] = _separator+p[0]; - if (p.length > 1) - return exports.join.apply(exports, p) + if (_separator != '\\') + p[0] = _separator+p[0]; + if (p.length > 1) { + var ret = exports.join.apply(exports, p); + return ret; + } return p[0]; } @@ -497,14 +512,12 @@ exports.isAbsolute = function isAbsolute(path) { if (path == "") return false; - var file = currentWorkingDirectory.clone(); - try { - // if path is a relative path, there won't have exception - file.appendRelativePath(path); - return false; + if (isWin) { + return (/^([a-z]:\\)/i.test(path.replace(/\//g, "\\"))); + } + else { + return (/^\//i.test(path)); } - catch(e) { } - return true; } exports.isExecutable = function isExecutable(path) { diff --git a/src/modules/phantom.jsm b/src/modules/phantom.jsm index c8c3b28..ab12ef3 100644 --- a/src/modules/phantom.jsm +++ b/src/modules/phantom.jsm @@ -120,12 +120,7 @@ var phantom = { return libPath.path; }, set libraryPath (path) { - libPath = Components.classes['@mozilla.org/file/local;1'] - .createInstance(Components.interfaces.nsILocalFile); - if (libPath instanceof Components.interfaces.nsILocalFileWin) - libPath.initWithPath(path.replace('/', '\\')); - else - libPath.initWithPath(path); + libPath = getMozFile(path); }, /** @@ -133,10 +128,10 @@ var phantom = { */ injectJs: function(filename) { // resolve the filename against the current working directory - let f = getMozFile(filename, Services.dirsvc.get("CurWorkD", Components.interfaces.nsIFile)); + let f = getAbsMozFile(filename, Services.dirsvc.get("CurWorkD", Components.interfaces.nsIFile)); if (!f.exists()) { // filename resolved against the libraryPath property - f = getMozFile(filename, libPath); + f = getAbsMozFile(filename, libPath); if (!f.exists()) { dump("Can't open '"+filename+"'\n"); return false; diff --git a/src/modules/slErrorLogger.jsm b/src/modules/slErrorLogger.jsm index babcba6..2b324fb 100644 --- a/src/modules/slErrorLogger.jsm +++ b/src/modules/slErrorLogger.jsm @@ -32,7 +32,7 @@ function initErrorLogger(fileName, workingDir) { uconv.charset = "UTF-8"; // open the log file - let file = getMozFile(fileName, workingDir); + let file = getAbsMozFile(fileName, workingDir); let openFlags = parseInt("0x1A"); let permFlags = parseInt("0644", 8); outputStream = Cc['@mozilla.org/network/file-output-stream;1']. diff --git a/src/modules/slLauncher.jsm b/src/modules/slLauncher.jsm index 1f837ec..ac593d0 100644 --- a/src/modules/slLauncher.jsm +++ b/src/modules/slLauncher.jsm @@ -119,10 +119,9 @@ var slLauncher = { } function getFile(path, isDir) { - let file = Cc['@mozilla.org/file/local;1'] - .createInstance(Ci.nsILocalFile); + let file; try { - file.initWithPath(path); + file = getMozFile(path); } catch(e){ throw Error("Modules path "+path+" is not a valid path"); @@ -142,20 +141,12 @@ function getFile(path, isDir) { */ function isFile(filename, base) { try { - // see if we have an absolute path let file; if (base) { - file = base.clone(); - if (file instanceof Ci.nsILocalFileWin) { - file.appendRelativePath(filename.replace("/", "\\")); - } - else - file.appendRelativePath(filename); + file = getAbsMozFile(filename, base); } else { - file = Cc['@mozilla.org/file/local;1'] - .createInstance(Ci.nsILocalFile); - file.initWithPath(filename); + file = getMozFile(filename); } if (file.exists()) { return file.path; @@ -320,7 +311,6 @@ function prepareLoader(fileURI, dirFile) { // let's resolve other id module as usual id = Loader.resolve(id, requirer); - if (id.indexOf('sdk/') === 0 && requirer.indexOf('slimer-sdk/') === 0) { return id; diff --git a/src/modules/slUtils.jsm b/src/modules/slUtils.jsm index 40a9ba8..c4f948f 100644 --- a/src/modules/slUtils.jsm +++ b/src/modules/slUtils.jsm @@ -3,8 +3,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ "use strict"; -var EXPORTED_SYMBOLS = ["dumpex", "dumpStack", "dumpo", - "getMozFile", "readSyncStringFromFile", "readChromeFile", +var EXPORTED_SYMBOLS = ["dumpex", "dumpStack", "dumpo", "getMozFile", + "getAbsMozFile", "readSyncStringFromFile", "readChromeFile", "getWebpageFromContentWindow", "getWebpageFromDocShell", "getBrowserFromContentWindow", "getBrowserFromDocShell", "createSimpleEnumerator", "slUtils"]; @@ -18,6 +18,9 @@ const scriptableStream = Cc["@mozilla.org/scriptableinputstream;1"].getService(C Cu.import("resource://gre/modules/Services.jsm"); +const isWin = (Services.dirsvc.get("CurWorkD", Ci.nsIFile) instanceof Ci.nsILocalFileWin); + + function dumpo(obj, indent) { if (typeof obj != 'object') { dump(""+obj+"\n") @@ -63,10 +66,14 @@ function dumpStack(aStack) { } /** + * create an nsIFile object containing the given path. If the path + * is a relative path, the nsIFile object will contain the path resolved against + * the given base path. * @param string path * @param nsIFile basepath + * @return nsIFile */ -function getMozFile(path, basepath) { +function getAbsMozFile(path, basepath) { var file = basepath.clone(); var pathElements = path.split(/[\\\/]/); var first = pathElements[0]; @@ -76,12 +83,18 @@ function getMozFile(path, basepath) { return file; } - if (first.match(/\:$/) || first == '') { + if ( (isWin && first.match(/\:$/)) || (!isWin && first == '')) { + // this is an absolute path file = Cc['@mozilla.org/file/local;1'] .createInstance(Ci.nsILocalFile); - file.initWithPath(path); + if (isWin) { + file.initWithPath(path.replace(/\//g, "\\")); + } + else + file.initWithPath(path); return file; } + while(pathElements.length) { first = pathElements.shift(); if (first == '.' || first == '') @@ -96,6 +109,31 @@ function getMozFile(path, basepath) { return file; } +/** + * create an nsIFile object containing the given path. + * @param string path an absolute path + * @return nsIFile + */ +function getMozFile(path) { + let isAbs; + if (isWin) { + path = path.replace(/\//g, "\\"); + isAbs = (/^([a-z]:)/i.test(path)); + } + else { + isAbs = (/^\//i.test(path)); + } + + if (!isAbs) { + throw new Error("getMozFile - the path is not an absolute path: "+path); + } + + let file = Cc['@mozilla.org/file/local;1'] + .createInstance(Ci.nsILocalFile); + file.initWithPath(path); + return file; +} + function readSyncStringFromFile (file) { let fstream = Cc["@mozilla.org/network/file-input-stream;1"]. diff --git a/src/modules/slimer-sdk/webpage.js b/src/modules/slimer-sdk/webpage.js index fc7a7f4..6918f46 100644 --- a/src/modules/slimer-sdk/webpage.js +++ b/src/modules/slimer-sdk/webpage.js @@ -1072,12 +1072,7 @@ function _create(parentWebpageInfo) { }, set libraryPath (path) { - libPath = Cc['@mozilla.org/file/local;1'] - .createInstance(Ci.nsILocalFile); - if (libPath instanceof Ci.nsILocalFileWin) - libPath.initWithPath(path.replace('/', '\\')); - else - libPath.initWithPath(path); + libPath = getMozFile(path); }, /** @@ -1093,10 +1088,10 @@ function _create(parentWebpageInfo) { if (!browser) { throw new Error("WebPage not opened"); } - let f = getMozFile(filename, Services.dirsvc.get("CurWorkD", Ci.nsIFile)); + let f = getAbsMozFile(filename, Services.dirsvc.get("CurWorkD", Ci.nsIFile)); if (!f.exists()) { // filename resolved against the libraryPath property - f = getMozFile(filename, libPath); + f = getAbsMozFile(filename, libPath); if (!f.exists()) { dump("Can't open '"+filename+"'\n"); return false; @@ -1323,9 +1318,7 @@ function _create(parentWebpageInfo) { browser.uploadFiles = []; files.forEach(function(file) { try { - let selectedFile = Cc['@mozilla.org/file/local;1'] - .createInstance(Ci.nsILocalFile); - selectedFile.initWithPath(file); + let selectedFile = getMozFile(file); if (selectedFile.exists()) { browser.uploadFiles.push(selectedFile); }