embark/lib/core/fs.js

213 lines
4.6 KiB
JavaScript
Raw Normal View History

const parseJson = require('parse-json');
const os = require('os');
let path = require('path');
2017-03-29 17:50:05 +00:00
let fs = require('fs-extra');
let utils = require('../utils/utils.js');
require('colors');
2017-02-19 16:37:54 +00:00
2018-10-10 19:26:01 +00:00
const pathConfigs = {
DAPP_PATH: process.env.DAPP_PATH,
EMBARK_PATH: process.env.EMBARK_PATH
};
function restrictPath(receiver, binding, count, args) {
const dapp = dappPath();
2018-10-17 18:49:16 +00:00
const embark = embarkPath();
2018-10-09 20:53:58 +00:00
const allowedRoots = [
dapp,
2018-10-17 18:49:16 +00:00
embark,
2018-10-09 20:53:58 +00:00
os.tmpdir()
];
let allInsideRestricted = true;
for(let i = 0; i < count; i++) {
let resolved = path.resolve(dapp, args[i]);
2018-10-09 20:53:58 +00:00
allInsideRestricted = allowedRoots.some(p => { return resolved.indexOf(p) === 0; });
if(!allInsideRestricted) break;
}
if(allInsideRestricted) return receiver.apply(binding, args);
throw new Error('EPERM: Operation not permitted');
}
2017-02-19 16:37:54 +00:00
function mkdirpSync() {
return restrictPath(fs.mkdirpSync, fs.mkdirpSync, 1, arguments);
2017-02-19 16:37:54 +00:00
}
2018-04-17 20:34:37 +00:00
function mkdirp() {
return restrictPath(fs.mkdirp, fs.mkdirp, 1, arguments);
2018-04-17 20:34:37 +00:00
}
2018-09-13 18:30:15 +00:00
function readdir() {
return restrictPath(fs.readdir, fs.readdir, 1, arguments);
2018-09-13 18:30:15 +00:00
}
function stat() {
return restrictPath(fs.stat, fs.stat, 1, arguments);
2018-09-13 18:30:15 +00:00
}
function remove() {
return restrictPath(fs.remove, fs.remove, 1, arguments);
2018-09-13 18:30:15 +00:00
}
2018-05-08 13:02:46 +00:00
function copy() {
return restrictPath(fs.copy, fs.copy, 2, arguments);
2018-05-08 13:02:46 +00:00
}
2017-02-19 16:37:54 +00:00
function copySync() {
return restrictPath(fs.copySync, fs.copySync, 2, arguments);
2017-02-19 16:37:54 +00:00
}
function move(){
return restrictPath(fs.move, fs.move, 2, arguments);
}
2018-08-12 19:08:37 +00:00
function moveSync() {
return restrictPath(fs.moveSync, fs.moveSync, 2, arguments);
2018-08-12 19:08:37 +00:00
}
function appendFileSync() {
return restrictPath(fs.appendFileSync, fs.writeFileSync, 1, arguments);
}
function writeFile() {
return restrictPath(fs.writeFile, fs.writeFileSync, 1, arguments);
}
2017-02-19 17:51:32 +00:00
function writeFileSync() {
return restrictPath(fs.writeFileSync, fs.writeFileSync, 1, arguments);
2017-02-19 17:51:32 +00:00
}
function readFile() {
return restrictPath(fs.readFile, fs.readFile, 1, arguments);
}
2017-02-19 16:37:54 +00:00
function readFileSync() {
return restrictPath(fs.readFileSync, fs.readFileSync, 1, arguments);
2017-02-19 16:37:54 +00:00
}
2018-08-30 12:13:37 +00:00
function readdirSync() {
return restrictPath(fs.readdirSync, fs.readdirSync, 1, arguments);
2018-08-30 12:13:37 +00:00
}
function statSync() {
return restrictPath(fs.statSync, fs.statSync, 1, arguments);
2018-08-30 12:13:37 +00:00
}
2017-02-19 16:37:54 +00:00
function readJSONSync() {
let content = readFileSync.apply(readFileSync, arguments);
try {
return parseJson(content);
} catch(e) {
console.error("error: ".red + arguments[0].green.underline + " " + e.message.green);
process.exit(0);
}
2017-02-19 16:37:54 +00:00
}
function writeJSONSync() {
return restrictPath(fs.writeJSONSync, fs.writeJSONSync, 1, arguments);
2017-02-19 16:37:54 +00:00
}
2018-05-08 13:36:50 +00:00
function writeJson() {
return restrictPath(fs.writeJson, fs.writeJson, 1, arguments);
2018-05-08 13:36:50 +00:00
}
2017-03-30 11:12:39 +00:00
function existsSync() {
return restrictPath(fs.existsSync, fs.existsSync, 1, arguments);
}
2018-05-08 12:54:10 +00:00
function access() {
return restrictPath(fs.access, fs.access, 1, arguments);
2018-05-08 12:54:10 +00:00
}
2018-01-11 14:22:58 +00:00
function removeSync() {
return restrictPath(fs.removeSync, fs.removeSync, 1, arguments);
2018-01-11 14:22:58 +00:00
}
2018-08-17 22:55:39 +00:00
function anchoredPath(envAnchor, ...args) {
2018-10-10 19:26:01 +00:00
let anchor = pathConfigs[envAnchor];
if (!pathConfigs[envAnchor]) {
2018-08-17 22:55:39 +00:00
console.error(`process.env.${envAnchor} was not set`.bold.red);
process.exit(1);
}
2018-08-17 22:55:39 +00:00
return utils.joinPath(anchor, ...args);
}
function embarkPath() {
return anchoredPath('EMBARK_PATH', ...arguments);
}
2018-04-02 19:30:16 +00:00
function dappPath() {
2018-08-17 22:55:39 +00:00
return anchoredPath('DAPP_PATH', ...arguments);
}
2018-09-10 06:53:12 +00:00
function pkgPath() {
return anchoredPath('PKG_PATH', ...arguments);
}
2018-04-17 20:34:37 +00:00
function createWriteStream() {
return restrictPath(fs.createWriteStream, fs.createWriteStream, 1, arguments);
2018-04-17 20:34:37 +00:00
}
2018-07-06 08:38:09 +00:00
function tmpDir() {
let os = require('os');
return utils.joinPath(os.tmpdir(), ...arguments);
}
function copyPreserve(sourceFilePath, targetFilePath) {
const path = require('path');
let ext = 1;
let preserved = targetFilePath;
while (fs.existsSync(preserved)) {
let extname = path.extname(targetFilePath);
preserved = utils.joinPath(
path.dirname(targetFilePath),
`${path.basename(targetFilePath, extname)}.${ext}${extname}`
);
ext++;
}
if (preserved !== targetFilePath) {
fs.copySync(targetFilePath, preserved);
}
fs.copySync(sourceFilePath, targetFilePath);
}
function outputFileSync(){
return fs.outputFileSync.apply(fs.outputFile, arguments);
}
2017-02-19 16:37:54 +00:00
module.exports = {
access,
appendFileSync,
2018-05-08 13:02:46 +00:00
copy,
copySync,
createWriteStream,
embarkPath,
existsSync,
mkdirp,
mkdirpSync,
move,
2018-08-12 19:08:37 +00:00
moveSync,
2018-10-17 18:49:16 +00:00
outputFileSync,
readFile,
readFileSync,
readJSONSync,
readdir,
2018-08-30 12:13:37 +00:00
readdirSync,
remove,
removeSync,
stat,
2018-08-30 12:13:37 +00:00
statSync,
tmpDir,
writeFile,
writeFileSync,
writeJSONSync,
copyPreserve,
dappPath,
2018-09-10 06:53:12 +00:00
pkgPath,
outputFileSync,
writeJson
2017-02-19 16:37:54 +00:00
};