diff --git a/file-layout.txt b/file-layout.txt index 72e2583..de413c3 100644 --- a/file-layout.txt +++ b/file-layout.txt @@ -11,11 +11,14 @@ Service Only These files should not contain RPC. This is to remove the likelyhood of rpc blocking. Since the main app - is not able to respond to its own RPC + is not able to respond to its own RPC. + Files may contain DB code. ./svc/main.php ./svc/db-reads.php ./svc/db-writes.php + ./svc/file-line-reader.php - might contain some generally useful file access, may want to extract + Service Child Process Only These files can have both database and RPC. This is because the child has been forked from main and is a legitimate @@ -30,8 +33,9 @@ general nature. ./shd/common.php (some old code needs to be removed) - ./lib/time.php - ./inc/paths.php (this will be upgraded and likely split into path-files specific to each section) + ./shd/backup-funcs.php + ./inc/paths.php (auto-generated file: this will be upgraded and likely split into path-files specific to each section) + Web-server Files ---------------- @@ -39,12 +43,13 @@ These files specialise in handling the GUI, browser interaction using JS and HTML and session life-cycle. No DB code allowed here. All calls but go via RPC to main.php. + ./phpinfo.php + ./post_handler.php + ./inc/bu_list_content.php ./inc/secure.php ./inc/pagemap.php ./index.php - ./phpinfo.php - ./post_handler.php ./inc/new-file-bu.php ./inc/login.php ./inc/edit-sh-bu.php @@ -52,7 +57,9 @@ Libraries (included as required) - ./lib/sysVcom.php + + ./lib/sysVcom.php + ./lib/time.php diff --git a/html/inc/pagemap.php b/html/inc/pagemap.php index 2155037..38e3d19 100644 --- a/html/inc/pagemap.php +++ b/html/inc/pagemap.php @@ -14,8 +14,12 @@ function invokeCommand( $cmd, $postvars ){ - if( $cmd=='backup-list-state' ){ - //include_once __DIR__ . "/bu_list_content.php"; + if( $cmd=='bu-test-file-lines' ){ + sendHtmlOk_WithData( rpc( 'f:bu-test-file-lines', $postvars['buid'], $postvars['from-line-num'] ) ); + return true; + } + + if( $cmd=='backup-list-state' ){ sendHtmlOk_WithData( [ 'state', rpc( 'f:getbu_list_state' ) ] ); return true; } @@ -58,7 +62,9 @@ if( $cmd=='btn_run_bu' ){ - $res = rpc( 'f:kickoff-backup', $postvars['id'], $postvars['isTest'] ); + error_log( "test is " . $postvars['isTest'] ); + + $res = rpc( 'f:kickoff-backup', $postvars['id'], 'true'===$postvars['isTest'] ); //originally when this was handled by a bash script, the results would have been complex. The return //values could have been error, done or running. diff --git a/html/inc/secure.php b/html/inc/secure.php index 9995011..7e1b91a 100644 --- a/html/inc/secure.php +++ b/html/inc/secure.php @@ -38,7 +38,7 @@ exit; } - include_once "../shd/common.php"; + include_once __DIR__."/../shd/common.php"; include_once "login.php"; if(authenticate( $_POST['username'], $_POST['password'], $_SESSION['svr_nonce'] ) ){ @@ -65,7 +65,7 @@ // likely that the page was just refreshed // --- Send back the login page --- -include_once "../shd/common.php"; +include_once __DIR__."/../shd/common.php"; $_SESSION['svr_nonce'] = generateNonce(); include_once "login.php"; diff --git a/html/js/main.js b/html/js/main.js index 5e0a3d7..cd576a9 100644 --- a/html/js/main.js +++ b/html/js/main.js @@ -180,13 +180,19 @@ return; case 'btn_run_bu': - if(instrArr.length==2 && instrArr[1]=='noTest'){ + bIsBuTest = (instrArr.length==2 && instrArr[1]=='noTest'); + if( bIsBuTest ){ map_params.set('isTest', 'false' ); }else{ map_params.set('isTest', 'true' ); console.log("running a test backup"); + } exec_instruction( instr, map_params ); + + if(!bIsBuTest){ + monitorBuOutput( true, id, -1 ); + } return; case 'delete-bu': @@ -295,44 +301,46 @@ return; } - switch(instr){ - case 'btn_run_bu': + bu_result = data[1][0]; //was it ok or err? - if(data[1][0]!='ok:'){ + switch(instr){ + case 'btn_run_bu': + + if(bu_result!='ok:'){ //latest msg contains similar format as used by the server rpc layer - console.log("odd message: " + data[1][0] ); + console.log("odd message: " + bu_result ); console.log( data ); return; } - bu_status = data[1][1]; + bu_status = data[1][1]; //extra info - if(bu_status=='done'){ - // this comes from the test run, although I should really explicitly send back the 'test' flag + if(bu_status=='running'){ + + // is it a test run? - by rights, I should send back the 'test' flag let elem = document.getElementById( "ta_testoutput" ); if(elem==null){ - console.log( data ); - return; + //not a test - set the tick + let elem = document.getElementById( "td_run_" + id ); + elem.innerHTML = ""; + + //ensure the state map is also adjusted + for( j=0; j"; - - //ensure the state map is also adjusted - for( j=0; j { + monitorBuOutput(true, buid, 0); + }, 1000); + return ; + } + + console.log("Fetch output from line: " + from_line_num ); + + let e_ta_testoutput = document.getElementById( "ta_testoutput" ); + if(e_ta_testoutput==null){ + // page gone (should terminate server open file?) + return; + } + + let map_params = new Map(); + map_params.set('action', 'bu-test-file-lines' ); + map_params.set('buid', buid ); + map_params.set('from-line-num', from_line_num ); + + postData( "post_handler.php", function( success, data, m ){ + //console.log( data ); + let cnt=0; + let rtm_msg = data[1][cnt++]; //'f:bu-test-file-lines' + let line_count = data[1][cnt++]; + let line_data = ''; + if(line_count>0){ + line_data = data[1][cnt++]; + } + + e_ta_testoutput = document.getElementById( "ta_testoutput" ); + if(e_ta_testoutput==null){ + // page gone (should terminate server open file?) + return; + } + + if(line_count>0){ + //setTextAreaFromArray( e_ta_testoutput, line_data ); + e_ta_testoutput.value += "\n" + line_data; + + setTimeout( () => { + monitorBuOutput(true, buid, line_count + from_line_num ); + }, 1000); + return ; + }else{ + console.log("No more lines to fetch" ); + } + + }, map_params); + +} + + function valid_btn_login(){ let login_name = document.getElementById('login_name').value; diff --git a/html/shd/backup-funcs.php b/html/shd/backup-funcs.php new file mode 100644 index 0000000..d399e29 --- /dev/null +++ b/html/shd/backup-funcs.php @@ -0,0 +1,28 @@ + diff --git a/html/svc/backup.php b/html/svc/backup.php index 9e07bda..875dc83 100644 --- a/html/svc/backup.php +++ b/html/svc/backup.php @@ -2,6 +2,7 @@ $g_data_dir="data"; +include_once "../shd/backup-funcs.php"; /** * Set (unset) the running datbase flag. Return true on success. The flag is @@ -60,6 +61,7 @@ return "Could not set the DB flag ";//.SQLite3::lastErrorMsg(); } + $flags = '-anv'; if($bu_test){ $flags = '-anv'; // n is the dry run indicator }else{ @@ -71,9 +73,13 @@ $fn_o = getFilename_out( $buid, $bu_test, false ); $fn_e = getFilename_out( $buid, $bu_test, true ); + //print("***** output file is $g_data_dir/$fn_o \n"); + // create the output files with the ID in the first line - file_put_contents("$g_data_dir/$fn_o","ID: $buid"); - file_put_contents("$g_data_dir/$fn_e","ID: $buid"); + file_put_contents("$g_data_dir/$fn_o","ID: $buid\n"); + file_put_contents("$g_data_dir/$fn_e","ID: $buid\n"); + + if( !file_exists( "$g_data_dir/$fn_e" )){ return "exec failed to create file $fn_e"; @@ -99,13 +105,22 @@ } set_bu_running( $buid, false, $bu_test ); +//print("***** command is\n$command\n"); +//print("***** test is ". file_get_contents("$g_data_dir/$fn_o") ."\n"); + // can we find any error strings in the error file? $str_errs = file_get_contents( "$g_data_dir/$fn_e" ); $pos = strpos($str_errs, 'error' ); if($pos===FALSE){ $pos = strpos($str_errs, 'failed' ); } - if($pos===FALSE) return; + if($pos===FALSE ){ + if(!$bu_test){ + unlink("$g_data_dir/$fn_e"); + unlink("$g_data_dir/$fn_o"); + } + return; + } //place it in the database rpc( 'f:set_bu_error', $buid, $str_errs ); @@ -169,29 +184,5 @@ } -function getFilename_out( $buid, bool $bu_test, bool $isErr=false ){ - return getFilename_tmp( $buid, $bu_test?'t':'n', $isErr? 'err': 'op' ); -} - -/** - * Create an output file for the backup (forked) process to use. These are essentually - * temporary files which may be deleted after the back process completes. - * - * buid: backup record id - * - * fn_type: - * t: for test files - * n: for non-test files - * x: for file exclusion list - * - * fn_stream: - * op: output stream - * err: error stream - */ -function getFilename_tmp( $buid, $fn_type='t', $fn_stream='op' ){ - return "bu-$fn_type-$fn_stream-$buid.txt"; -} - - ?> diff --git a/html/svc/file-line-reader.php b/html/svc/file-line-reader.php new file mode 100644 index 0000000..877cc99 --- /dev/null +++ b/html/svc/file-line-reader.php @@ -0,0 +1,67 @@ +seek( $lineNum ); + + while (!$objFile->eof()) { + $cnt++; + $lineData .= $objFile->fgets(); + } + return $cnt; +} + +?> diff --git a/html/svc/main.php b/html/svc/main.php index 177ef8b..c28654c 100644 --- a/html/svc/main.php +++ b/html/svc/main.php @@ -33,8 +33,11 @@ include __DIR__."/../inc/paths.php"; $g_database_path = '../' . $g_database_path; -include_once "db-reads.php"; -include_once "db-writes.php"; +$g_data_dir = realpath( __DIR__."/../data" ); + +include "db-reads.php"; +include "db-writes.php"; +include "file-line-reader.php"; // get the latest data and cache it $g_bu_states = null; @@ -287,11 +290,26 @@ // The idea will be that the indicator before the colon will be handled generically in cases // where it is a simple function call, i.e. 'f:' (provided it is a trusted source and/or has been previously checked) + global $g_data_dir; + if( 'f:getbu_list_state' !== $obj[0] ){ print_r( $obj ); } switch( $obj[0] ){ + + case 'f:bu-test-file-lines': + $buid = $obj[1]; + $lineNum = $obj[2]; + $lineData = ''; + $objFile = getObject_outputFile( $g_data_dir, $buid ); + $cnt = getLinesFrom( $objFile, $lineNum, $lineData ); + if($cnt==0){ + closeFile( $buid ); + return array( 'r:bu-test-file-lines', 0 ); + } + return array( 'r:bu-test-file-lines', $cnt, $lineData ); + case 'm:fork-complete': //send the same message back to the caller return $obj; @@ -335,9 +353,7 @@ case 'f:kickoff-backup': $buid = $obj[1]; - $isTest = $obj[2]==1; - //caller expects param1 to be one of error, done or running - //we've changed this slightly e:Error, ok:, done is not working yet + $isTest = $obj[2]; return kickoff_backup( $buid, false, $isTest ); case 'f:set_last_run_date': diff --git a/notes.txt b/notes.txt index 8269837..acf63a7 100644 --- a/notes.txt +++ b/notes.txt @@ -1,12 +1,3 @@ Testing Rqd ----------- - * Run now - - failure report etc - - file type (incremental) - - * Create new bu - file type - - test output - - * Edit existing bu - - test output