2013-11-10 01:05:58 +01:00
|
|
|
/**
|
2014-05-02 15:35:51 +02:00
|
|
|
* Copyright 2013, 2014 IBM Corp.
|
2013-11-10 01:05:58 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
var when = require('when');
|
2014-01-16 06:59:13 +01:00
|
|
|
var nodeFn = require('when/node/function');
|
|
|
|
var keys = require('when/keys');
|
2013-11-10 01:05:58 +01:00
|
|
|
var fspath = require("path");
|
2014-01-16 06:59:13 +01:00
|
|
|
var mkdirp = require("mkdirp");
|
|
|
|
|
2015-02-03 23:02:26 +01:00
|
|
|
var log = require("../log");
|
|
|
|
|
2014-01-16 06:59:13 +01:00
|
|
|
var promiseDir = nodeFn.lift(mkdirp);
|
2013-11-10 01:05:58 +01:00
|
|
|
|
|
|
|
var settings;
|
|
|
|
var flowsFile;
|
2014-03-31 14:38:35 +02:00
|
|
|
var flowsFullPath;
|
2015-02-04 14:45:45 +01:00
|
|
|
var flowsFileBackup;
|
2013-11-10 01:05:58 +01:00
|
|
|
var credentialsFile;
|
2015-02-04 16:41:18 +01:00
|
|
|
var credentialsFileBackup;
|
2014-05-02 15:35:51 +02:00
|
|
|
var oldCredentialsFile;
|
2015-03-14 00:37:59 +01:00
|
|
|
var sessionsFile;
|
2013-11-10 01:05:58 +01:00
|
|
|
var libDir;
|
|
|
|
var libFlowsDir;
|
2014-08-28 01:35:07 +02:00
|
|
|
var globalSettingsFile;
|
2013-11-10 01:05:58 +01:00
|
|
|
|
|
|
|
function getFileMeta(root,path) {
|
|
|
|
var fn = fspath.join(root,path);
|
|
|
|
var fd = fs.openSync(fn,"r");
|
|
|
|
var size = fs.fstatSync(fd).size;
|
|
|
|
var meta = {};
|
|
|
|
var read = 0;
|
|
|
|
var length = 10;
|
|
|
|
var remaining = "";
|
|
|
|
var buffer = Buffer(length);
|
|
|
|
while(read < size) {
|
|
|
|
read+=fs.readSync(fd,buffer,0,length);
|
|
|
|
var data = remaining+buffer.toString();
|
|
|
|
var parts = data.split("\n");
|
|
|
|
remaining = parts.splice(-1);
|
|
|
|
for (var i=0;i<parts.length;i+=1) {
|
|
|
|
var match = /^\/\/ (\w+): (.*)/.exec(parts[i]);
|
|
|
|
if (match) {
|
|
|
|
meta[match[1]] = match[2];
|
|
|
|
} else {
|
|
|
|
read = size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fs.closeSync(fd);
|
|
|
|
return meta;
|
|
|
|
}
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
function getFileBody(root,path) {
|
|
|
|
var body = "";
|
|
|
|
var fn = fspath.join(root,path);
|
|
|
|
var fd = fs.openSync(fn,"r");
|
|
|
|
var size = fs.fstatSync(fd).size;
|
|
|
|
var scanning = true;
|
|
|
|
var read = 0;
|
|
|
|
var length = 50;
|
|
|
|
var remaining = "";
|
|
|
|
var buffer = Buffer(length);
|
|
|
|
while(read < size) {
|
|
|
|
var thisRead = fs.readSync(fd,buffer,0,length);
|
|
|
|
read += thisRead;
|
|
|
|
if (scanning) {
|
2013-11-10 23:19:01 +01:00
|
|
|
var data = remaining+buffer.slice(0,thisRead).toString();
|
2013-11-10 01:05:58 +01:00
|
|
|
var parts = data.split("\n");
|
|
|
|
remaining = parts.splice(-1)[0];
|
|
|
|
for (var i=0;i<parts.length;i+=1) {
|
|
|
|
if (! /^\/\/ \w+: /.test(parts[i])) {
|
|
|
|
scanning = false;
|
|
|
|
body += parts[i]+"\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (! /^\/\/ \w+: /.test(remaining)) {
|
|
|
|
scanning = false;
|
|
|
|
}
|
|
|
|
if (!scanning) {
|
|
|
|
body += remaining;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
body += buffer.slice(0,thisRead).toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fs.closeSync(fd);
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
2014-10-31 12:40:10 +01:00
|
|
|
/**
|
|
|
|
* Write content to a file using UTF8 encoding.
|
|
|
|
* This forces a fsync before completing to ensure
|
|
|
|
* the write hits disk.
|
|
|
|
*/
|
|
|
|
function writeFile(path,content) {
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
var stream = fs.createWriteStream(path);
|
|
|
|
stream.on('open',function(fd) {
|
|
|
|
stream.end(content,'utf8',function() {
|
|
|
|
fs.fsync(fd,resolve);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
stream.on('error',function(err) {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
var localfilesystem = {
|
|
|
|
init: function(_settings) {
|
|
|
|
settings = _settings;
|
2015-02-25 15:23:59 +01:00
|
|
|
|
|
|
|
var promises = [];
|
|
|
|
|
|
|
|
if (!settings.userDir) {
|
|
|
|
if (fs.existsSync(fspath.join(process.env.NODE_RED_HOME,".config.json"))) {
|
|
|
|
settings.userDir = process.env.NODE_RED_HOME;
|
|
|
|
} else {
|
2015-03-04 22:47:38 +01:00
|
|
|
settings.userDir = fspath.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || process.env.NODE_RED_HOME,".node-red");
|
2015-02-25 15:23:59 +01:00
|
|
|
promises.push(promiseDir(settings.userDir));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
if (settings.flowFile) {
|
|
|
|
flowsFile = settings.flowFile;
|
2015-02-26 12:30:20 +01:00
|
|
|
|
|
|
|
if (flowsFile[0] == "/") {
|
|
|
|
// Absolute path
|
|
|
|
flowsFullPath = flowsFile;
|
|
|
|
} else if (flowsFile.substring(0,2) === "./") {
|
|
|
|
// Relative to cwd
|
|
|
|
flowsFullPath = fspath.join(process.cwd(),flowsFile);
|
|
|
|
} else {
|
|
|
|
if (fs.existsSync(fspath.join(process.cwd(),flowsFile))) {
|
|
|
|
// Found in cwd
|
|
|
|
flowsFullPath = fspath.join(process.cwd(),flowsFile);
|
|
|
|
} else {
|
|
|
|
// Use userDir
|
|
|
|
flowsFullPath = fspath.join(settings.userDir,flowsFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
} else {
|
|
|
|
flowsFile = 'flows_'+require('os').hostname()+'.json';
|
2015-02-25 15:23:59 +01:00
|
|
|
flowsFullPath = fspath.join(settings.userDir,flowsFile);
|
2013-11-10 01:05:58 +01:00
|
|
|
}
|
2015-02-04 14:45:45 +01:00
|
|
|
var ffExt = fspath.extname(flowsFullPath);
|
|
|
|
var ffName = fspath.basename(flowsFullPath);
|
|
|
|
var ffBase = fspath.basename(flowsFullPath,ffExt);
|
|
|
|
var ffDir = fspath.dirname(flowsFullPath);
|
|
|
|
|
2015-02-25 15:23:59 +01:00
|
|
|
credentialsFile = fspath.join(settings.userDir,ffBase+"_cred"+ffExt);
|
|
|
|
credentialsFileBackup = fspath.join(settings.userDir,"."+ffBase+"_cred"+ffExt+".backup");
|
2015-02-26 12:30:20 +01:00
|
|
|
|
2015-02-25 15:23:59 +01:00
|
|
|
oldCredentialsFile = fspath.join(settings.userDir,"credentials.json");
|
2015-02-04 14:45:45 +01:00
|
|
|
|
|
|
|
flowsFileBackup = fspath.join(ffDir,"."+ffName+".backup");
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2015-03-14 00:37:59 +01:00
|
|
|
sessionsFile = fspath.join(settings.userDir,".sessions.json");
|
|
|
|
|
2015-02-25 15:23:59 +01:00
|
|
|
libDir = fspath.join(settings.userDir,"lib");
|
2013-11-10 01:05:58 +01:00
|
|
|
libFlowsDir = fspath.join(libDir,"flows");
|
2014-08-28 01:35:07 +02:00
|
|
|
|
2015-02-25 15:23:59 +01:00
|
|
|
globalSettingsFile = fspath.join(settings.userDir,".config.json");
|
|
|
|
|
|
|
|
promises.push(promiseDir(libFlowsDir));
|
2014-08-28 01:35:07 +02:00
|
|
|
|
2015-02-25 15:23:59 +01:00
|
|
|
return when.all(promises);
|
2013-11-10 01:05:58 +01:00
|
|
|
},
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
getFlows: function() {
|
2015-02-26 12:30:20 +01:00
|
|
|
return when.promise(function(resolve) {
|
|
|
|
log.info("User Directory : "+settings.userDir);
|
|
|
|
log.info("Flows file : "+flowsFullPath);
|
|
|
|
fs.exists(flowsFullPath, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
resolve(nodeFn.call(fs.readFile,flowsFullPath,'utf8').then(function(data) {
|
|
|
|
return JSON.parse(data);
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
log.info("Creating new flows file");
|
|
|
|
resolve([]);
|
|
|
|
}
|
|
|
|
});
|
2013-11-10 01:05:58 +01:00
|
|
|
});
|
|
|
|
},
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
saveFlows: function(flows) {
|
2014-05-07 01:33:50 +02:00
|
|
|
if (fs.existsSync(flowsFullPath)) {
|
2015-02-04 14:45:45 +01:00
|
|
|
fs.renameSync(flowsFullPath,flowsFileBackup);
|
2014-05-07 01:33:50 +02:00
|
|
|
}
|
2014-06-07 23:33:29 +02:00
|
|
|
|
|
|
|
var flowData;
|
|
|
|
|
|
|
|
if (settings.flowFilePretty) {
|
|
|
|
flowData = JSON.stringify(flows,null,4);
|
|
|
|
} else {
|
|
|
|
flowData = JSON.stringify(flows);
|
|
|
|
}
|
2014-10-31 12:40:10 +01:00
|
|
|
return writeFile(flowsFullPath, flowData);
|
2013-11-10 01:05:58 +01:00
|
|
|
},
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
getCredentials: function() {
|
2015-02-26 12:30:20 +01:00
|
|
|
return when.promise(function(resolve) {
|
|
|
|
fs.exists(credentialsFile, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
resolve(nodeFn.call(fs.readFile, credentialsFile, 'utf8').then(function(data) {
|
|
|
|
return JSON.parse(data)
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
fs.exists(oldCredentialsFile, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
resolve(nodeFn.call(fs.readFile, oldCredentialsFile, 'utf8').then(function(data) {
|
|
|
|
return JSON.parse(data)
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
resolve({});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-11-10 01:05:58 +01:00
|
|
|
});
|
|
|
|
},
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
saveCredentials: function(credentials) {
|
2015-02-04 14:45:45 +01:00
|
|
|
if (fs.existsSync(credentialsFile)) {
|
|
|
|
fs.renameSync(credentialsFile,credentialsFileBackup);
|
|
|
|
}
|
2014-06-07 23:33:29 +02:00
|
|
|
var credentialData;
|
|
|
|
if (settings.flowFilePretty) {
|
|
|
|
credentialData = JSON.stringify(credentials,null,4);
|
|
|
|
} else {
|
|
|
|
credentialData = JSON.stringify(credentials);
|
|
|
|
}
|
2014-10-31 12:40:10 +01:00
|
|
|
return writeFile(credentialsFile, credentialData);
|
2013-11-10 01:05:58 +01:00
|
|
|
},
|
2014-08-28 01:35:07 +02:00
|
|
|
|
|
|
|
getSettings: function() {
|
|
|
|
if (fs.existsSync(globalSettingsFile)) {
|
|
|
|
return nodeFn.call(fs.readFile,globalSettingsFile,'utf8').then(function(data) {
|
2014-09-22 22:35:30 +02:00
|
|
|
if (data) {
|
2014-09-23 18:09:23 +02:00
|
|
|
try {
|
|
|
|
return JSON.parse(data);
|
|
|
|
} catch(err) {
|
2015-02-03 23:02:26 +01:00
|
|
|
log.info("Corrupted config detected - resetting");
|
2014-09-23 18:09:23 +02:00
|
|
|
return {};
|
|
|
|
}
|
2014-09-22 22:35:30 +02:00
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
2014-08-28 01:35:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return when.resolve({});
|
|
|
|
},
|
|
|
|
saveSettings: function(settings) {
|
2014-10-31 12:40:10 +01:00
|
|
|
return writeFile(globalSettingsFile,JSON.stringify(settings,null,1));
|
2014-08-28 01:35:07 +02:00
|
|
|
},
|
2015-03-14 00:37:59 +01:00
|
|
|
getSessions: function() {
|
|
|
|
if (fs.existsSync(sessionsFile)) {
|
|
|
|
return nodeFn.call(fs.readFile,sessionsFile,'utf8').then(function(data) {
|
|
|
|
if (data) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(data);
|
|
|
|
} catch(err) {
|
|
|
|
log.info("Corrupted sessions file - resetting");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return when.resolve({});
|
|
|
|
},
|
|
|
|
saveSessions: function(sessions) {
|
|
|
|
return writeFile(sessionsFile,JSON.stringify(sessions));
|
|
|
|
},
|
2014-08-28 01:35:07 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
getLibraryEntry: function(type,path) {
|
|
|
|
var root = fspath.join(libDir,type);
|
|
|
|
var rootPath = fspath.join(libDir,type,path);
|
2014-01-16 06:59:13 +01:00
|
|
|
return promiseDir(root).then(function () {
|
|
|
|
return nodeFn.call(fs.lstat, rootPath).then(function(stats) {
|
2014-07-02 00:46:25 +02:00
|
|
|
if (stats.isFile()) {
|
|
|
|
return getFileBody(root,path);
|
|
|
|
}
|
2013-11-10 01:05:58 +01:00
|
|
|
if (path.substr(-1) == '/') {
|
|
|
|
path = path.substr(0,path.length-1);
|
|
|
|
}
|
2014-01-16 06:59:13 +01:00
|
|
|
return nodeFn.call(fs.readdir, rootPath).then(function(fns) {
|
2013-11-10 01:05:58 +01:00
|
|
|
var dirs = [];
|
|
|
|
var files = [];
|
|
|
|
fns.sort().filter(function(fn) {
|
|
|
|
var fullPath = fspath.join(path,fn);
|
|
|
|
var absoluteFullPath = fspath.join(root,fullPath);
|
|
|
|
if (fn[0] != ".") {
|
|
|
|
var stats = fs.lstatSync(absoluteFullPath);
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
dirs.push(fn);
|
|
|
|
} else {
|
|
|
|
var meta = getFileMeta(root,fullPath);
|
|
|
|
meta.fn = fn;
|
|
|
|
files.push(meta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-01-16 06:59:13 +01:00
|
|
|
return dirs.concat(files);
|
2013-11-10 01:05:58 +01:00
|
|
|
});
|
2015-04-04 00:05:56 +02:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
if (type === "flows" && !/\.json$/.test(path)) {
|
|
|
|
return localfilesystem.getLibraryEntry(type,path+".json")
|
|
|
|
.otherwise(function(e) {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
2014-01-16 06:59:13 +01:00
|
|
|
});
|
2013-11-10 01:05:58 +01:00
|
|
|
});
|
|
|
|
},
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
saveLibraryEntry: function(type,path,meta,body) {
|
2014-01-16 06:59:13 +01:00
|
|
|
var fn = fspath.join(libDir, type, path);
|
2013-11-10 01:05:58 +01:00
|
|
|
var headers = "";
|
|
|
|
for (var i in meta) {
|
2014-07-02 00:46:25 +02:00
|
|
|
if (meta.hasOwnProperty(i)) {
|
|
|
|
headers += "// "+i+": "+meta[i]+"\n";
|
|
|
|
}
|
2013-11-10 01:05:58 +01:00
|
|
|
}
|
2014-01-16 06:59:13 +01:00
|
|
|
return promiseDir(fspath.dirname(fn)).then(function () {
|
2014-10-31 12:40:10 +01:00
|
|
|
writeFile(fn,headers+body);
|
2013-11-10 01:05:58 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = localfilesystem;
|