From e29c883005c8fab1dbfc53c8ec49751f9fe5e5dd Mon Sep 17 00:00:00 2001 From: Igor Klopov Date: Fri, 14 Jul 2017 16:22:40 +0300 Subject: [PATCH] add 'snapshot' entry to readdir('/') results --- prelude/bootstrap.js | 33 ++++++++++++++++--- test/test-50-observe-snapshot-root-2/main.js | 29 ++++++++++++++++ .../test-x-index.js | 27 +++++++++++++++ 3 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 test/test-50-observe-snapshot-root-2/main.js create mode 100644 test/test-50-observe-snapshot-root-2/test-x-index.js diff --git a/prelude/bootstrap.js b/prelude/bootstrap.js index d4880e037..04425f004 100644 --- a/prelude/bootstrap.js +++ b/prelude/bootstrap.js @@ -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 @@ -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) { @@ -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]; @@ -701,18 +720,22 @@ 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)) { @@ -720,7 +743,7 @@ function payloadFileSync (pointer) { } var callback = dezalgo(maybeCallback(arguments)); - readdirFromSnapshot(path, callback); + readdirFromSnapshot(path, isRoot, callback); }; // /////////////////////////////////////////////////////////////// diff --git a/test/test-50-observe-snapshot-root-2/main.js b/test/test-50-observe-snapshot-root-2/main.js new file mode 100644 index 000000000..ff8d74d1f --- /dev/null +++ b/test/test-50-observe-snapshot-root-2/main.js @@ -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); diff --git a/test/test-50-observe-snapshot-root-2/test-x-index.js b/test/test-50-observe-snapshot-root-2/test-x-index.js new file mode 100644 index 000000000..82de1a903 --- /dev/null +++ b/test/test-50-observe-snapshot-root-2/test-x-index.js @@ -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'); + } + }); + } +}