Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
add 'snapshot' entry to readdir('/') results
Browse files Browse the repository at this point in the history
  • Loading branch information
igorklopov committed Jul 14, 2017
1 parent 70c4f28 commit e29c883
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
33 changes: 28 additions & 5 deletions prelude/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ console.log(translateNth(["", "a+"], 0, "d:\\snapshot\\countly\\plugins-ext\\123
// PROJECT /////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////

function isRootPath (p) {
return require('path').dirname(p) === p;
}

function projectToFilesystem (f) {
var xpdn = require('path').dirname(
EXECPATH
Expand Down Expand Up @@ -673,6 +677,20 @@ function payloadFileSync (pointer) {
// readdir ///////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////

function readdirRoot (path, cb) {
if (cb) {
ancestor.readdir(path, function (error, entries) {
if (error) return cb(error);
entries.push('snapshot');
cb(null, entries);
});
} else {
var entries = ancestor.readdirSync(path);
entries.push('snapshot');
return entries;
}
}

function readdirFromSnapshotSub (entityLinks, path, cb) {
if (cb) {
payloadFile(entityLinks, function (error, buffer) {
Expand All @@ -685,8 +703,9 @@ function payloadFileSync (pointer) {
}
}

function readdirFromSnapshot (path_, cb) {
function readdirFromSnapshot (path_, isRoot, cb) {
var cb2 = cb || rethrow;
if (isRoot) return readdirRoot(path_, cb);
var path = normalizePath(path_);
// console.log("readdirFromSnapshot", path);
var entity = VIRTUAL_FILESYSTEM[path];
Expand All @@ -701,26 +720,30 @@ function payloadFileSync (pointer) {
}

fs.readdirSync = function (path) {
if (!insideSnapshot(path)) {
var isRoot = isRootPath(path);

if (!insideSnapshot(path) && !isRoot) {
return ancestor.readdirSync.apply(fs, arguments);
}
if (insideMountpoint(path)) {
return ancestor.readdirSync.apply(fs, translateNth(arguments, 0, path));
}

return readdirFromSnapshot(path);
return readdirFromSnapshot(path, isRoot);
};

fs.readdir = function (path) {
if (!insideSnapshot(path)) {
var isRoot = isRootPath(path);

if (!insideSnapshot(path) && !isRoot) {
return ancestor.readdir.apply(fs, arguments);
}
if (insideMountpoint(path)) {
return ancestor.readdir.apply(fs, translateNth(arguments, 0, path));
}

var callback = dezalgo(maybeCallback(arguments));
readdirFromSnapshot(path, callback);
readdirFromSnapshot(path, isRoot, callback);
};

// ///////////////////////////////////////////////////////////////
Expand Down
29 changes: 29 additions & 0 deletions test/test-50-observe-snapshot-root-2/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env node

'use strict';

const path = require('path');
const assert = require('assert');
const utils = require('../utils.js');

assert(!module.parent);
assert(__dirname === process.cwd());

const target = process.argv[2] || 'host';
const input = './test-x-index.js';
const output = './test-output.exe';

let right;

utils.pkg.sync([
'--target', target,
'--output', output, input
]);

right = utils.spawn.sync(
'./' + path.basename(output), [],
{ cwd: path.dirname(output) }
);

assert.equal(right, 'ok\n');
utils.vacuum.sync(output);
27 changes: 27 additions & 0 deletions test/test-50-observe-snapshot-root-2/test-x-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node

'use strict';

var fs = require('fs');
var path = require('path');

if (process.platform === 'win32') {
var root = path.parse(process.argv[1]).root; // D:\\
if (fs.readdirSync(root).indexOf('snapshot') >= 0) {
fs.readdir(root, function (error, entries) {
if (error) throw error;
if (entries.indexOf('snapshot')) {
console.log('ok');
}
});
}
} else {
if (fs.readdirSync('/').indexOf('snapshot') >= 0) {
fs.readdir('/', function (error, entries) {
if (error) throw error;
if (entries.indexOf('snapshot')) {
console.log('ok');
}
});
}
}

0 comments on commit e29c883

Please sign in to comment.