Skip to content

Commit

Permalink
Fixes path issues on Windows
Browse files Browse the repository at this point in the history
Path manipulated by scripts should be converted in
good path format on Windows.

Closes laurentj#65
  • Loading branch information
laurentj committed Jul 31, 2013
1 parent bce5b38 commit b711683
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/components/commandline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
63 changes: 38 additions & 25 deletions src/modules/addon-sdk/sdk/io/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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("/")
}
Expand Down Expand Up @@ -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 == '.')
Expand All @@ -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];
}

Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 3 additions & 8 deletions src/modules/phantom.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,18 @@ 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);
},

/**
* injects an external script into the SlimerJS sandbox runtime
*/
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;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/slErrorLogger.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -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'].
Expand Down
18 changes: 4 additions & 14 deletions src/modules/slLauncher.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
48 changes: 43 additions & 5 deletions src/modules/slUtils.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand All @@ -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")
Expand Down Expand Up @@ -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];
Expand All @@ -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 == '')
Expand All @@ -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"].
Expand Down
15 changes: 4 additions & 11 deletions src/modules/slimer-sdk/webpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit b711683

Please sign in to comment.