iobroker.javascript
Version:
Javascript/Coffescript Script Engine for ioBroker
209 lines (204 loc) • 7.02 kB
JavaScript
const nodeFS = require('fs');
const path = require('path');
const ProtectFs = function (log) {
function checkObjectsJson(file) {
if (path.normalize(file).replace(/\\/g, '/').indexOf('-data/objects.json') !== -1) {
if (log) {
log.error('May not read ' + file);
} else {
console.error('May not read ' + file);
}
throw new Error('Permission denied');
}
}
this.readFile = function () {
checkObjectsJson(arguments[0]);
return nodeFS.readFile.apply(this, arguments);
};
this.readFileSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.readFileSync.apply(this, arguments);
};
this.writeFile = function () {
checkObjectsJson(arguments[0]);
return nodeFS.writeFile.apply(this, arguments);
};
this.writeFileSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.writeFileSync.apply(this, arguments);
};
this.unlink = function () {
checkObjectsJson(arguments[0]);
return nodeFS.unlink.apply(this, arguments);
};
this.unlinkSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.unlinkSync.apply(this, arguments);
};
this.appendFile = function () {
checkObjectsJson(arguments[0]);
return nodeFS.appendFile.apply(this, arguments);
};
this.appendFileSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.appendFileSync.apply(this, arguments);
};
this.chmod = function () {
checkObjectsJson(arguments[0]);
return nodeFS.chmod.apply(this, arguments);
};
this.chmodSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.chmodSync.apply(this, arguments);
};
this.chown = function () {
checkObjectsJson(arguments[0]);
return nodeFS.chmodSync.apply(this, arguments);
};
this.chownSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.chownSync.apply(this, arguments);
};
this.copyFile = function () {
checkObjectsJson(arguments[0]);
checkObjectsJson(arguments[1]);
if (nodeFS.copyFile) {
return nodeFS.copyFile.apply(this, arguments);
} else {
const cb = arguments[2];
return nodeFS.readFile(arguments[0], (err, data) => {
if (err) {
cb && cb(err);
} else {
nodeFS.writeFile(arguments[1], data, err => cb && cb(err));
}
});
}
};
this.copyFileSync = function () {
checkObjectsJson(arguments[0]);
checkObjectsJson(arguments[1]);
if (nodeFS.copyFileSync) {
return nodeFS.copyFileSync.apply(this, arguments);
} else {
return nodeFS.writeFileSync(arguments[1], nodeFS.readFileSync(arguments[0]));
}
};
this.rename = function () {
checkObjectsJson(arguments[0]);
checkObjectsJson(arguments[1]);
if (nodeFS.rename) {
return nodeFS.rename.apply(this, arguments);
} else {
const cb = arguments[2];
return nodeFS.readFile(arguments[0], (err, data) => {
if (err) {
cb && cb(err);
} else {
nodeFS.writeFile(arguments[1], data, err => cb && cb(err));
}
});
}
};
this.renameSync = function () {
checkObjectsJson(arguments[0]);
checkObjectsJson(arguments[1]);
if (nodeFS.renameSync) {
return nodeFS.renameSync.apply(this, arguments);
} else {
return nodeFS.writeFileSync(arguments[1], nodeFS.readFileSync(arguments[0]));
}
};
this.open = function () {
checkObjectsJson(arguments[0]);
return nodeFS.open.apply(this, arguments);
};
this.openSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.openSync.apply(this, arguments);
};
this.rename = function () {
checkObjectsJson(arguments[0]);
checkObjectsJson(arguments[1]);
return nodeFS.rename.apply(this, arguments);
};
this.renameSync = function () {
checkObjectsJson(arguments[0]);
checkObjectsJson(arguments[1]);
return nodeFS.renameSync.apply(this, arguments);
};
this.truncate = function () {
checkObjectsJson(arguments[0]);
return nodeFS.truncate.apply(this, arguments);
};
this.truncateSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.truncateSync.apply(this, arguments);
};
this.exists = function () {
checkObjectsJson(arguments[0]);
return nodeFS.exists.apply(this, arguments);
};
this.existsSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.existsSync.apply(this, arguments);
};
this.stat = function () {
checkObjectsJson(arguments[0]);
return nodeFS.stat.apply(this, arguments);
};
this.statSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.statSync.apply(this, arguments);
};
this.readdir = function () {
checkObjectsJson(arguments[0]);
return nodeFS.readdir.apply(this, arguments);
};
this.readdirSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.readdirSync.apply(this, arguments);
};
this.createReadStream = function () {
checkObjectsJson(arguments[0]);
return nodeFS.createReadStream.apply(this, arguments);
};
this.createWriteStream = function () {
checkObjectsJson(arguments[0]);
return nodeFS.createWriteStream.apply(this, arguments);
};
this.lstat = function () {
checkObjectsJson(arguments[0]);
return nodeFS.lstat.apply(this, arguments);
};
this.lstatSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.lstatSync.apply(this, arguments);
};
this.mkdir = function () {
checkObjectsJson(arguments[0]);
return nodeFS.mkdir.apply(this, arguments);
};
this.mkdirSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.mkdirSync.apply(this, arguments);
};
this.rmdir = function () {
checkObjectsJson(arguments[0]);
return nodeFS.rmdir.apply(this, arguments);
};
this.rmdirSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.rmdirSync.apply(this, arguments);
};
this.truncate = function () {
checkObjectsJson(arguments[0]);
return nodeFS.truncate.apply(this, arguments);
};
this.truncateSync = function () {
checkObjectsJson(arguments[0]);
return nodeFS.truncateSync.apply(this, arguments);
};
return this;
};
module.exports = ProtectFs;