瀏覽代碼

Refactor for elevated IPC

master
pb 2 年之前
父節點
當前提交
d8611a53b9
共有 1 個檔案被更改,包括 19 行新增7 行删除
  1. 19
    7
      win_verse.js

+ 19
- 7
win_verse.js 查看文件

@@ -4,6 +4,11 @@ const fs = require('fs')
var cli = require('./cliverse')
var nodeShellExec = cli.nodeShellExec;

function elevatedRunIPCWriteMessage( target, m ) {
fs.writeFileSync(target, ', ' + JSON.stringify( m ), { 'flag': 'a+' })
}


var __isElevated = null;
var shell_verse = {
// getCommonTask is agnostic of whether we are running in an elevated shell or not. It runs in either case.
@@ -16,15 +21,16 @@ var shell_verse = {
, elevatedRunner( taskToRun, inBatch ){
// PB : TODO -- Should be called only when we are in an elevated shell that was already requested from an unelevated shell with a batch of tasks.
try {
var runlogjson = `${selectedinstance.root}/.elxr/run-${runtimestamp}/run.log`
var __runasresult = null;
return taskToRun().then((r)=>{
// PB : TODO -- Every elevation should have its own messaging file. Async writes from multiple processes are a problem here...
fs.writeFileSync('run.log', ', ' + JSON.stringify( { info : taskToRun.info, success: true }), { 'flag': 'a+' })
elevatedRunIPCWriteMessage( runlogjson, ', ' + JSON.stringify( { info : taskToRun.info, success: true }) )
if(!inBatch) fs.writeFileSync('run.done', 'success') // PB : TODO -- This should be done conditionally if we are running inproc.
return __runasresult = r;
})
.catch((e) => {
fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' })
elevatedRunIPCWriteMessage( runlogjson, ', ' + JSON.stringify(e ) )
if(!inBatch)fs.writeFileSync('run.done', 'failure')
console.error(e)
})
@@ -154,13 +160,12 @@ var shell_verse = {
}

, requestElevation(elevatedRunner, taskToRun) {
// PB : TODO -- Multiple parallel request elevations should be queued into a promise.
// PB : TODO -- Multiple parallel request elevations should be queued into a batch and serialized as a single promise.

var processedArgs = taskToRun.processedArgs, selectedinstance = taskToRun.selectedinstance , statuslog = taskToRun.statuslog
// Wait for the runas to complete before we read it.
try {
fs.unlinkSync('run.done')
fs.unlinkSync('run.log')
fs.unlinkSync('run.done') // Need a unique file for aech elevated run.
}
catch (e) { } //Ignore

@@ -173,6 +178,9 @@ var shell_verse = {
var args = [`${selectedinstance.root}/.elxr/run-${taskToRun.runtimestamp}/windowselevate.hta`].concat(processedArgs._)
namedArgs.length > 0 ? args = args.concat(namedArgs.join(' ')) : null;
args.push('--runas=self');
var elevatedruntimestamp = (new Date()).getTime()
args.push(`--runtimestamp=${elevatedruntimestamp}`);

// args.push('--nodepath=' + r.messages[r.messages.length - 1])
// if (!processedArgs.node_env) args.push('--node_env=' + ENV.NODE_ENV)
// if (processedArgs.debug) args.push('--debug=true') // Enable to debug elevated..
@@ -193,7 +201,11 @@ var shell_verse = {
// runas returned.
try {
// PB : TODO -- Log is comma prefixed. Needs to be proper JSON.
var runaslog = JSON.parse('[ { "success" : true, "result" : "started"}' + fs.readFileSync('run.log', { flags: 'a+' }) + ']');
var runlogjson = `${selectedinstance.root}/.elxr/run-${elevatedruntimestamp}/run.log`
var runaslog = JSON.parse('[' + fs.readFileSync(runlogjson, { flags: 'a+' }) + ']');
try { fs.unlinkSync(runlogjson) } catch(e){ } // PB : TODO -- Have a unique file for each elevated run.
// console.log( "runaslog : " + runaslog.length )
// Assemble elevated run results into the main run log
runaslog.forEach((logEntry) => {
statuslog.statuslog(logEntry.success ? null : logEntry, logEntry)
logEntry.success ? (console.log(['success :' + logEntry.result]), console.log((logEntry.messages || []).join(' '))) : (console.error(['error :' + logEntry.result]), console.error((logEntry.messages || []).join(' ')))
@@ -207,7 +219,7 @@ var shell_verse = {
})
.catch(err => console.error('Elevation failed : ' + err));
})
}
}
, iswin(){ return true}
, islin(){ return false}
}

Loading…
取消
儲存