Files
resatennis_tyroltenn/IDrive/.scripts/getCleanupList.php

302 lines
8.0 KiB
PHP
Executable File

<?php
include 'fetchPath.php';
define('RESULTFILE', "result.txt");
define('COUNTFILE', "count.txt");
$lastLine = "";
$filename = "";
$hidden = "Enabled";
$totalFileCount = 0;
$notExistCount = 0;
$count = 0;
$excludePartialPath = $userDir.PATHSEPARATOR.EXCLUDE_PARTIALPATH;
$countArr = array();
$isFolderEmpty = true;
$errNo = 0;
$RESULT1= "";
if(file_exists($excludePartialPath)){
$excludeParArray = file($excludePartialPath); #read the whole file into an array in a single stretch
if(count($excludeParArray)){
$tmp = trim($excludeParArray[0],LINEFEED);
if($tmp === '\/.')
$hidden = "Disabled";
}
}
#parsing output for search and finding list of missing files in local
$OUTPUTFILE = fopen(OUTPUTFILE,"r");
if(!$OUTPUTFILE){
$errNo = 2;
errorCleanup($errNo);
}
# create a file with item count of missing files locally
if(!file_exists(COUNTFILE)){
if($COUNTFD = fopen(COUNTFILE,"w"))
fclose($COUNTFD);
else {
traceLog("Unable to open file ".COUNTFILE);
$errNo = 2;
errorCleanup($errNo);
}
}
chmod(COUNTFILE, PERMISSIONVAL);
if($COUNT_FILE = fopen(COUNTFILE,"r")){
$countArr = file(COUNTFILE);
fclose($COUNT_FILE);
if(!empty($countArr)){
$totalFileCount += trim($countArr[2]);
$notExistCount += trim($countArr[3]);
}
}
else {
traceLog("Unable to open file ".COUNTFILE);
return;
}
$count =0;
createResultSet();
$isDedup = file_exists($dedupDetailFile);
while(1){
$buffer = fread($OUTPUTFILE, LIMIT);
if(empty($buffer)){
if(customFileSize(ERRORFILE)){
$error = file_get_contents(ERRORFILE);
checkDedupDeviceError($error);
break;
}
if(!file_exists(PIDFILE)){
traceLog(PIDFILE." does not exist.");
$errNo = 4;
errorCleanup($errNo);
}
if($count > 300) {
# As search command is taking too much time to respond for some users where there is huge set of data in TB ,we are adding sleep.
sleep(2);
break;
}
$count++;
sleep(1);
fseek($OUTPUTFILE, 0, 1); #to clear eof flag
continue;
}
//need to check appending partial record to packet or to first line of packet
if(!empty($lastLine))
$buffer = $lastLine.$buffer;
if($isDedup){
if(substr($buffer,-1) !== "\n"){
$lastNewlinePos = strrpos($buffer, "\n");
$resultList = substr($buffer,0,$lastNewlinePos+1);
$lastLine = substr($buffer,$lastNewlinePos+1);
} else {
$resultList = $buffer;
$lastLine = '';
if(strpos($resultList,'<item items_found=') !== FALSE)
$flag = 1;
}
$resultListXML = "<root>$resultList</root>";
$xml = simplexml_load_string($resultListXML);
if(!$xml){
$resultListXML = checkAndConstructXML($resultList);
$resultListXML = "<root>$resultListXML</root>";
$xml = simplexml_load_string($resultListXML);
}
if(!$xml){
if($flag == 1)
break;
continue;
}
$i=0;
foreach ($xml->item as $value)
{
if(isset($value['items_found']))
break;
$filename = $value['fname'];
$itemType = trim($value['restype']);
# check for file existance
if(!empty($filename) && $itemType === "F"){
if(($hidden === 'Disabled') && (strpos($filename,"/.") !== FALSE))
continue;
++$totalFileCount;
if(!file_exists($filename)){
++$notExistCount;
fwrite($RESULT1,$filename.LINEFEED);
if($notExistCount % 10000 == 0)
createResultSet();
}
else {
$isFolderEmpty = false;
}
unset($countArr);
$countArr[0] = $errNo;
$countArr[1] = $filename;
$countArr[2] = $totalFileCount;
$countArr[3] = $notExistCount;
if(++$i%50 === 0){
usleep(1000); // 1000 == 1 millisecond
file_put_contents(COUNTFILE, $errNo."\n".$filename."\n".$totalFileCount."\n".$notExistCount);
}
}
}
}
else {
$resultList = explode(LINEFEED, $buffer);
$bufIndex = count($resultList);
if(substr($buffer,-1) !== "\n"){
#keep last line of buffer only when it not ends with newline.
$lastLine = array_pop($resultList);
# In case of partial or line ending with new line both case we need to reduce the index by one
--$bufIndex;
}
else
$lastLine = "";
$flag = 0;
if(0 < ($bufIndex and strpos($resultList[$bufIndex-1],"files_found") !== FALSE) || (strpos($resultList[$bufIndex-1],"Total folders/files found") !== FALSE)){
$flag = 1;
}
$backupLocation = "/".ltrim(readBackupLocation(),"/");
$len = strlen($backupLocation);
foreach($resultList as $key => $item){
++$count;
if(!($count%10) && !file_exists(PIDFILE)){
traceLog(PIDFILE." file does not exist.");
$errNo = 4;
$count = 0;
errorCleanup($errNo);
}
$filename = "";
$fields = explode("] [",$item,6);
if(count($fields) < 6){
if((strpos($item,"files_found") !== FALSE) || (strpos($item,"Total folders/files found") !== FALSE))
$flag = 1;
continue;
}
$fields[2] = ltrim($fields[2]);
if($fields[2] !== "0"){
# remove backup location from beginning in string
$fields[5] = substr($fields[5], $len);
# remove last character ] from last field
$fields[5] = preg_replace("/.$/","",$fields[5]);
# remove spaces from beginning from required fields
$fields[5] = ltrim($fields[5]);
$filename = $fields[5];
# check for file existance
if(!empty($filename)){
if(strpos($filename,"/.") !== FALSE && ($hidden === 'Disabled'))
continue;
++$totalFileCount;
if(!file_exists($filename)){
# check if filename is starts with "/" and check for non admin permission
if(strpos($filename,"/") === 0){
++$notExistCount;
fwrite($RESULT1,$backupLocation.$filename.LINEFEED);
if($notExistCount % 10000 == 0)
createResultSet();
}
else
traceLog("Unable to consider for archive. item name: $filename and backupLocation: $backupLocation");
}
else
$isFolderEmpty = false;
$countArr[0] = $errNo;
$countArr[1] = $filename;
$countArr[2] = $totalFileCount;
$countArr[3] = $notExistCount;
file_put_contents(COUNTFILE, implode("\n",$countArr));
}
}
}
}
if(1 === $flag)
break;
}
print $isFolderEmpty;
errorCleanup($errNo);
#****************************************************************************************************
# Subroutine Name : errorCleanup
# Objective : To complete the error cleanup
# Added By : Pooja Havaldar
#*****************************************************************************************************/
function errorCleanup($errNo)
{
GLOBAL $countArr, $RESULT1, $OUTPUTFILE;
if($errNo == 0)
$errNo = 1;
$countArr[0] = $errNo;
$countArr[1] = trim($countArr[1], "\n");
$countArr[2] = trim($countArr[2], "\n");
$countArr[3] = trim($countArr[3], "\n");
file_put_contents(COUNTFILE, $countArr[0]."\n".$countArr[1]."\n".$countArr[2]."\n".$countArr[3]);
fclose($RESULT1);
fclose($OUTPUTFILE);
exit;
}
#****************************************************************************************************
# Subroutine Name : updateCountFile
# Objective : Update the count file with latest content
# Added By : Kamalraj
#*****************************************************************************************************/
function updateCountFile($countArr=array())
{
file_put_contents(COUNTFILE, implode("\n",$countArr));
}
#****************************************************************************************************
# Subroutine Name : createResultSet
# Objective : This is used to update the result file handler when the counter reaches 1000 for each file.
# Added By : Anil Kumar
#*****************************************************************************************************/
function createResultSet()
{
GLOBAL $notExistCount, $RESULT1;
fclose($RESULT1);
$val = (int)(($notExistCount/10000)+1);
$resultFile = "temp/result".$val.".txt";
$temp = "temp/";
if(!is_dir($temp)){
`mkdir $temp`;
chmod($temp,PERMISSIONVAL);
}
if(!$RESULT1 = fopen($resultFile,"a")){
traceLog("Unable to open file ".$resultFile);
$errNo = 3;
errorCleanup($errNo);
}
chmod($resultFile,PERMISSIONVAL);
}
?>