Merge pull request #2371 from btsimonh/projects-flowsFile-absolute

flows file absolute path plus non-existent folder creation.
This commit is contained in:
Nick O'Leary 2019-11-21 10:44:20 +00:00 committed by GitHub
commit 83c1e44925
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 14 deletions

View File

@ -56,8 +56,9 @@ function init(_settings, _runtime) {
if (settings.flowFile) {
flowsFile = settings.flowFile;
// handle Unix and Windows "C:\"
if ((flowsFile[0] == "/") || (flowsFile[1] == ":")) {
// handle Unix and Windows "C:\" and Windows "\\" for UNC.
if (fspath.isAbsolute(flowsFile)) {
//if (((flowsFile[0] == "\\") && (flowsFile[1] == "\\")) || (flowsFile[0] == "/") || (flowsFile[1] == ":")) {
// Absolute path
flowsFullPath = flowsFile;
} else if (flowsFile.substring(0,2) === "./") {

View File

@ -15,6 +15,7 @@
**/
var fs = require('fs-extra');
var fspath = require('path');
var when = require('when');
var nodeFn = require('when/node/function');
@ -79,25 +80,31 @@ module.exports = {
* the write hits disk.
*/
writeFile: function(path,content,backupPath) {
if (backupPath) {
if (backupPath) {
if (fs.existsSync(path)) {
fs.renameSync(path,backupPath);
}
}
return when.promise(function(resolve,reject) {
var stream = fs.createWriteStream(path);
stream.on('open',function(fd) {
stream.write(content,'utf8',function() {
fs.fsync(fd,function(err) {
if (err) {
log.warn(log._("storage.localfilesystem.fsync-fail",{path: path, message: err.toString()}));
}
stream.end(resolve);
fs.ensureDir(fspath.dirname(path), (err)=>{
if (err) {
reject(err);
return;
}
var stream = fs.createWriteStream(path);
stream.on('open',function(fd) {
stream.write(content,'utf8',function() {
fs.fsync(fd,function(err) {
if (err) {
log.warn(log._("storage.localfilesystem.fsync-fail",{path: path, message: err.toString()}));
}
stream.end(resolve);
});
});
});
});
stream.on('error',function(err) {
reject(err);
stream.on('error',function(err) {
reject(err);
});
});
});
},

View File

@ -19,6 +19,7 @@ var fs = require('fs-extra');
var path = require('path');
var sinon = require('sinon');
var NR_TEST_UTILS = require("nr-test-utils");
var process = require("process");
var localfilesystem = NR_TEST_UTILS.require("@node-red/runtime/lib/storage/localfilesystem");
var log = NR_TEST_UTILS.require("@node-red/util").log;
@ -474,4 +475,44 @@ describe('storage/localfilesystem', function() {
done(err);
});
});
it('should handle flow file in random unc path and non-existent subfolder',function(done) {
// only test on win32
if (process.platform !== 'win32') {
console.log('skipped test as not win32');
done();
return;
}
// get a real windows path
var flowFile = path.win32.resolve(userDir+'/some/random/path');
var rootdir = path.win32.resolve(userDir+'/some');
// make it into a local UNC path
flowFile = flowFile.replace('C:\\', '\\\\localhost\\c$\\');
localfilesystem.init({userDir:userDir, flowFile:flowFile}, mockRuntime).then(function() {
fs.existsSync(flowFile).should.be.false();
localfilesystem.saveFlows(testFlow).then(function() {
fs.existsSync(flowFile).should.be.true();
localfilesystem.getFlows().then(function(flows) {
flows.should.eql(testFlow);
// cleanup
fs.removeSync(rootdir);
done();
}).catch(function(err) {
// cleanup
fs.removeSync(rootdir);
done(err);
});
}).catch(function(err) {
// cleanup
fs.removeSync(rootdir);
done(err);
});
}).catch(function(err) {
// cleanup
fs.removeSync(rootdir);
done(err);
});
});
});