From 1bd736ddde793d4c89d44c1c53fd8f3fe43da86a Mon Sep 17 00:00:00 2001
From: Elena.Budnik <ebudnik@irap.omp.eu>
Date: Thu, 14 Jun 2018 15:36:12 +0200
Subject: [PATCH] add cleanRemote

---
 CMakeLists.txt                   |  1 +
 scripts/CleanRemote.sh           | 10 ++++++++++
 src/DATA/MANAGER/CleanRemote.php | 21 +++++++++++++++++++++
 src/DATA/MANAGER/DDBaseMgr.php   | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 101 insertions(+), 0 deletions(-)
 create mode 100644 scripts/CleanRemote.sh
 create mode 100644 src/DATA/MANAGER/CleanRemote.php

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3d593d3..46226c7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -77,6 +77,7 @@ install(FILES "scripts/AddLocalVI.sh" DESTINATION . PERMISSIONS OWNER_READ OWNER
 install(FILES "scripts/UpdateInfoVI.sh" DESTINATION . PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
 install(FILES "scripts/RemoveVI.sh" DESTINATION . PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
 install(FILES "scripts/MakeRemoteProxy.sh" DESTINATION . PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
+install(FILES "scripts/CleanRemote.sh" DESTINATION . PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
 
 install(DIRECTORY "src/CALLEXT/" DESTINATION bin/CALLEXT)
 install(DIRECTORY "src/DATA/MANAGER/" DESTINATION bin/DATAMANAGER)
diff --git a/scripts/CleanRemote.sh b/scripts/CleanRemote.sh
new file mode 100644
index 0000000..ab52e43
--- /dev/null
+++ b/scripts/CleanRemote.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+BASEDIR=$(dirname "$0")
+
+. ${BASEDIR}/DDServer.env
+
+php ${DATAMANAGER}/CleanRemote.php
+
+ 
+
diff --git a/src/DATA/MANAGER/CleanRemote.php b/src/DATA/MANAGER/CleanRemote.php
new file mode 100644
index 0000000..69914c8
--- /dev/null
+++ b/src/DATA/MANAGER/CleanRemote.php
@@ -0,0 +1,21 @@
+<?php
+
+/**
+*  @file CleanRemote.php
+*  @brief DD Server Tools: 
+*/
+	if (!function_exists('__autoload')) 
+	{
+		function __autoload($class_name) {
+				require_once $class_name . '.php';
+		}
+	}
+   
+	putenv("LD_LIBRARY_PATH=".getenv("LD_LIBRARY_PATH"));
+	putenv("PATH=./:".getenv("DDBASEBIN").":/bin:/usr/bin");
+	set_include_path("./:".getenv("DATAMANAGER")); 
+
+   $baseMgr = new DDBaseMgr();
+   $baseMgr->cleanRemote();
+ 
+?>
diff --git a/src/DATA/MANAGER/DDBaseMgr.php b/src/DATA/MANAGER/DDBaseMgr.php
index aa83d73..85ab4a3 100644
--- a/src/DATA/MANAGER/DDBaseMgr.php
+++ b/src/DATA/MANAGER/DDBaseMgr.php
@@ -290,6 +290,75 @@ class DDBaseMgr
 		system("infoLocal2nc ".$this->info_xml." ".$this->info);
 		chdir($currDir);
 	}
+	
+	public function cleanRemote($opt = NULL)
+	{
+		$res_before = disk_free_space(getenv("DDBASE"));
+		$Days = 10;  // default
+		$SecDay = 3600*24;
+		$nodata = false;
+
+		if (isset($opt)) {
+			if ($opt == "NODATA") 
+					$nodata = true;
+			else {
+				$Days = is_numeric($opt) ? $opt : 10;
+				settype($Days, "int");
+				echo "!!!!!!!!!!!!!!!! Days   ".$Days.PHP_EOL;
+			}
+		}
+		
+		$GlobalDelta = $Days*$SecDay;
+	
+		$xp = new domxpath($this->DDsysDoc);
+		$dataSets = $xp->query("//NAME[attribute::base!='LOCAL']");
+		
+		for ($i = 0; $i < $dataSets->length; $i++) 
+		{ 
+			$parent = $dataSets->item($i)->parentNode;
+			$location = $parent->getElementsByTagName("LOCATION")->item(0)->nodeValue;
+ 
+			if (!is_dir($location)) {
+				$parent->parentNode->removeChild($parent);
+			}
+			else {
+				$time_info = $parent->getElementsByTagName("TIMES")->item(0)->nodeValue;
+				chdir($location);
+				system("./clean");
+				touch($location."/LOCK");
+
+				fprintf(STDERR,"In the ".$location."\n");
+
+				/*  Delete No Data Intervals */        
+				if ($nodata) {
+					system("CleanNoData ".$time_info); 
+				}
+				/*  Delete Old Files */  
+				else {
+					foreach (glob("*.gz") as $filename) {
+						$info = stat($filename);
+						if ((time() - $info['atime']) > $GlobalDelta) {
+							//fprintf(STDERR, $filename."\n");
+							system("TimesUpdate -d ".$time_info." ".$filename);
+						}
+						//fprintf(STDERR, date("Y-m-d",$info['atime'])." - ".date("Y-m-d",$info['mtime'])."\n");
+					}
+				}
+
+			system("DDClearTimes ".$time_info);
+			unlink($location."/LOCK");  
+        }
+		}
+    /* 
+		$res_after = disk_free_space(getenv("DDBASE"));
+		$difference = ($res_after - $res_before)/1024.0;
+		$res_after /=  1024.0;
+		fprintf(STDERR,"+ Free Space  ".$difference." kB\n");
+		fprintf(STDERR,"  Free Space  ".$res_after." kB\n");*/
+		
+		$this->DDsysDoc->save($this->DDsys);
+		system("makeDDsys");	
+	}
 } 
 ?>   
   
--
libgit2 0.21.2