<?php
/* (C) 2006 by legolas558
TarBackup class v0.1.1
Licensed under GPL
http://sf.net/projects/phptarbackup
* This program is free software and open source software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or visit
* http://www.gnu.org/licenses/gpl.html
*/
if (!defined('__TBK_DEFAULT_CHUNK')) {
define('__TBK_DEFAULT_CHUNK', 5 * 1024 * 1024);
}
define('__TBK_FNAME', 0);
define('__TBK_FSIZE', 1);
define('__TBK_ROOT', 2);
define('__TBK_TARNAME', 3);
define('__TBK_TOTAL', 4);
define('__TBK_DONE', 5);
class TarBackupSkel {
var $progress;
function TarBackupSkel(&$var) {
$this->progress = &$var;
}
function _addFile($fn, $fs) {
$this->progress[__TBK_FNAME][] = $fn;
$this->progress[__TBK_FSIZE][] = $fs;
$this->progress[__TBK_TOTAL] += $fs;
}
function _init($root, $p_tarname) {
$this->progress = array(__TBK_FNAME => array(), __TBK_FSIZE => array(), __TBK_ROOT => $root, __TBK_TARNAME => $p_tarname, __TBK_DONE => 0, __TBK_TOTAL => 0);
}
function _pclexec($funcname, $args) {
$er = error_reporting(-1 ^ E_NOTICE);
$r = call_user_func_array($funcname, $args);
error_reporting($er);
if (!$r)
trigger_error('PclTar: '.PclErrorString($r));
return $this->Done();
}
function Done() {
$this->finished = ($this->progress[__TBK_TOTAL] == $this->progress[__TBK_DONE]);
if ($this->finished)
return true;
$this->progress[__TBK_FNAME] = array_slice($this->progress[__TBK_FNAME], $this->i+1);
$this->progress[__TBK_FSIZE] = array_slice($this->progress[__TBK_FSIZE], $this->i+1);
return true;
}
function _slice($max_bytes, $rp = false) {
$done = 0;
$c = count($this->progress[__TBK_FNAME]);
$lista = array();
for ($i=0;$i<$c;$i++) {
$done += $this->progress[__TBK_FSIZE][$i];
$lista[] = ($rp ? $this->progress[__TBK_ROOT] : '').$this->progress[__TBK_FNAME][$i];
if ($done>$max_bytes)
break;
}
$this->progress[__TBK_DONE] += $done;
$this->i = $i;
return $lista;
}
function _loop($next_step_cb, $max_time, $method) {
if ($max_time === null)
$max_time = ini_get('max_execution_time');
$start = mt_float();
if ($this->$method()) {
$this->_finalize($next_step_cb);
return;
}
$chunks = ( ($max_time-1) / (mt_float() - $start));
if ($this->$method( $chunks * __TBK_DEFAULT_CHUNK)) {
$this->_finalize($next_step_cb);
return;
}
while (!$this->$method()) {
if (time()-$start>$max_time)
break;
}
$this->_finalize($next_step_cb);
}
function _finalize($next_step_cb) {
$next_step_cb(!$this->finished, $this->progress[__TBK_TOTAL], $this->progress[__TBK_DONE]);
if ($this->finished) { // will eventually free the associated session variable
unset($this->progress[__TBK_FNAME]);
unset($this->progress[__TBK_FSIZE]);
}
}
}
?>