Files
fdn2/private/lib/system/fsobjects/LocallyMountedFS.php
T

223 lines
5.9 KiB
PHP

<?php
class LocallyMountedFS extends FSObject {
public static $ACCESSMODE_RO = 1;
public static $ACCESSMODE_RW = 2;
private $sys;
private $catalog;
public function __construct($deviceType, $writelaw,$accessmode,$mountPoint,$name){
$this->type=$deviceType;
$this->writeLaw=$writelaw;
$this->accessMode=$accessmode;
$this->mountPoint=$mountPoint;
$this->name=$name;
}
private function useSysFunction($funcName){
$funcArgs = array();
for ($i = 1; $i < func_num_args(); $i++){
$funcArgs[] = func_get_arg($i);
}
if ($this->accessMode==self::$ACCESSMODE_RW){
if ($this->writeLaw==parent::$WRITELAW_NOT_WRITEABLE){
throw new SFSException('fsobj.not_writeable',$this->name);
}
else if ($this->writeLaw==parent::$WRITELAW_NORMALLY_RO){
SystemController::mountRw($this->mountPoint);
call_user_func_array(array("SystemController",$funcName),$funcArgs);
SystemController::mountRw($this->mountPoint);
}
else if ($this->writeLaw==parent::$WRITELAW_NORMALLY_W){
call_user_func_array(array("SystemController",$funcName),$funcArgs);
}
}
else {
throw new SFSException('fsobj.not_writeable',$this->name);
}
}
private function write($content,$file,$mode='w'){
$fh = fopen($file, $mode);
if ($fh === false){
throw new SFSException('fsobj.file_not_writeable',$file);
}
$count = 0;
$done = false;
while (!$done && $count<3){
if (flock($fh, LOCK_EX)){
fwrite($fh,$content);
// flock($fh, LOCK_UN); // not necessary
$done = true;
}
else {
sleep(5);
}
$count++;
}
fclose($fh);
if (!$done){
throw new SFSException("Error writing file '$file'... the file is being used from another resource.");
}
}
public function readFile($path){
// var_dump($path);
$fh = fopen($path, 'r');
if ($fh === false){
throw new SFSException('fsobj.file_not_readable',$path);
}
$done = false;
$count = 0;
while (!$done && $count<3){
if (flock($fh, LOCK_EX)){
$rows=file($path);
$done = true;
}
else {
sleep(5);
}
$count++;
}
fclose($fh);
if (!$done){
throw new SFSException("Error reading file '$path'... the file is being used from another resource.");
}
return $rows;
}
public function appendToFile($content,$file){
if ($this->accessMode==self::$ACCESSMODE_RO || $this->writeLaw==parent::$WRITELAW_NOT_WRITEABLE){
throw new SFSException('fsobj.not_writeable',$this->name);
}
else if ($this->writeLaw==parent::$WRITELAW_NORMALLY_W) {
$this->write($content,$file,'a');
}
else if ($this->writeLaw==parent::$WRITELAW_NORMALLY_RO) {
SystemController::mountRw($this->mountPoint);
$this->write($content,$file,'a');
SystemController::mountRw($this->mountPoint);
}
}
public function writeFile($content,$file){
if ($this->accessMode==self::$ACCESSMODE_RO || $this->writeLaw==parent::$WRITELAW_NOT_WRITEABLE){
throw new SFSException('fsobj.not_writeable',$this->name);
}
else if ($this->writeLaw==parent::$WRITELAW_NORMALLY_W) {
$this->write($content,$file);
}
else if ($this->writeLaw==parent::$WRITELAW_NORMALLY_RO) {
SystemController::mountRw($this->mountPoint);
$this->write($content,$file);
SystemController::mountRw($this->mountPoint);
}
}
public function moveFile($source,$destination,$useSudo=false){
$this->useSysFunction("mv",$source,$destination,$useSudo);
}
public function copyFile($source,$destination,$useSudo=false){
$this->useSysFunction("cp",$source,$destination,$useSudo);
}
public function touchFile($path,$useSudo=false){
$this->useSysFunction("touch",$path,$useSudo);
}
public function deleteFile($path,$useSudo=false){
$this->useSysFunction("rm",$path,$useSudo);
}
public function createDirectory($path,$useSudo=false){
$this->useSysFunction("mkdir",$path,$useSudo);
}
public function copyDirectory($source,$destination,$useSudo=false){
$this->useSysFunction("cpRec",$source,$destination,$useSudo);
}
public function fileExists($path){
return (file_exists($path));
}
public function directoryExists($path){
return (file_exists($path));
}
public function isDirectory($path){
return ($this->directoryExists($path) && is_dir($path));
}
public function lsDir($path){
$rval = array();
if ($this->isDirectory($path)){
if ($dh = opendir($path)) {
while (($file = readdir($dh)) !== false) {
if (strcmp($file,".")!=0 && strcmp($file,"..")!=0){
$rval[] = $file;
}
}
closedir($dh);
}
}
return $rval;
}
/**
* (non-PHPdoc)
* @see FSObject::createArchive()
*/
public function createArchive($path,$origin){
if ($this->accessMode==self::$ACCESSMODE_RO || $this->writeLaw==parent::$WRITELAW_NOT_WRITEABLE){
throw new SFSException('fsobj.not_writeable',$this->name);
}
else if ($this->writeLaw==parent::$WRITELAW_NORMALLY_W) {
$matches= array();
if (preg_match("/^(.*)\.gz$/",$path,$matches)){
$path = $matches[1];
}
$a = new PharData($path);
if (is_array($origin) && sizeof($origin)>0){
foreach ($origin as $file){
$a->addFile($file);
}
}
else {
$a->buildFromDirectory($origin);
}
$a->compress(Phar::GZ); // Crea il file .tar.gz
unset($a);
$this->deleteFile($path); // Cancella il tar (non il tar.gz)
}
else if ($this->writeLaw==parent::$WRITELAW_NORMALLY_RO) {
SystemController::mountRw($this->mountPoint);
$matches= array();
if (preg_match("/^(.*)\.gz$/",$path,$matches)){
$path = $matches[1];
}
$a = new PharData($path);
if (is_array($origin) && sizeof($origin)>0){
foreach ($origin as $file){
$a->addFile($file);
}
}
else {
$a->buildFromDirectory($origin);
}
$a->compress(Phar::GZ); // Crea il file .tar.gz
unset($a);
$this->deleteFile($path); // Cancella il tar (non il tar.gz)
SystemController::mountRw($this->mountPoint);
}
}
public function deleteDirectory($source,$destination,$useSudo=false){
$this->useSysFunction("rmDir",$source,$destination,$useSudo);
}
}