-
Notifications
You must be signed in to change notification settings - Fork 7
/
FileTreeBackuper.php
552 lines (504 loc) · 17.7 KB
/
FileTreeBackuper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<?php
/*
TODO:
make root state be stored in base and arrays
*/
/*!
an interface representing a node of file tree with some basic folder functionality such as hashing
*/
interface IFileTreeItem{
function hash();
function process();
//public $inode,$mtime,$ctime,$size,$uid;
};
/*!
an interface representing a folder with some basic folder functionality such as expanding children
*/
interface IFileTreeDir extends IFileTreeItem{
function processChild(IFileTreeItem &$fChild);
function expandChildren();
};
/*!
converts data from index database to object, because sqlite returns strings instead of integers
*/
function convertNodeFromBase(&$row){
$row->inode=(integer)$row->inode;
$row->parent=(integer)$row->parent;
$row->hash=(integer)$row->hash;
$row->pathhash=(integer)$row->pathhash;
}
class FileTreeBackuperIndex extends BackuperIndex{
public $base=null;
//index
//patch to current state
const indexPrefix="FileTreeBackuper_";
public $moved=array(),$deleted=array(),$changed=array(),$added=array();
//public $modified=array();
public $ignores=array();
static $queriesTemplates=array(
"getTree"=>'SELECT * FROM `%PR%fileTree`;',
"getChildren"=>"SELECT * FROM `%PR%fileTree` where `parent`=?;",
"getIgnores"=>"SELECT * FROM `%PR%ignores`;",
"upsertFileToTree"=>"INSERT INTO `%PR%fileTree` VALUES (:inode, :parent, :hash, :pathhash, :name);",
"deleteFileFromTree"=>"DELETE FROM `%PR%fileTree` WHERE `inode`=:inode;",
"addCommit"=>"INSERT INTO `%PR%commits` (`timestamp`) VALUES (:time);",
"addFileToMoved"=>"INSERT INTO `%PR%movedCurrent` VALUES (:inode,:prevParent);",
"addFileToDeleted"=>"INSERT INTO `%PR%deletedCurrent` VALUES (:name,:parent);",
);
/*!
*/
static $baseStructureBuildQuery=array(
'fileTree'=>'
"inode" INTEGER NOT NULL,
"parent" INTEGER NOT NULL,
"hash" INTEGER NOT NULL,
"pathhash" INTEGER NOT NULL,
"name" VARCHAR(256) NOT NULL,
PRIMARY KEY ("inode") ON CONFLICT REPLACE,
FOREIGN KEY ("parent") REFERENCES "%PR%fileTree" ("inode")
UNIQUE ("inode") ON CONFLICT REPLACE,
UNIQUE ("hash") ON CONFLICT REPLACE,
UNIQUE ("pathhash") ON CONFLICT REPLACE',
'ignores'=>'"ignore" varchar(512) NOT NULL',//!<files to ignore, regexes in pcre format
'commits'=>'
"id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
"timestamp" DateTime NOT NULL',
'movedCurrent'=>'
"inode" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
"prevParent" integer NOT NULL,
FOREIGN KEY ("prevParent") REFERENCES "%PR%fileTree" ("inode")',//!<to mantain moves
'deletedCurrent'=>'
"name" VARCHAR(256) NOT NULL,
"parent" integer NOT NULL',//!<to mantain deletes
);
function __construct(&$base){
parent::__construct($base);
//! dropping databases ended with current
//! @todo: should I add a special stage to BackuperIndex using arrays?
$this->base->exec('delete from `'.static::indexPrefix.'movedCurrent` where 1;');
$this->base->exec('delete from `'.static::indexPrefix.'deletedCurrent` where 1;');
}
public $inodes=array();
function load(){
//commented out because it could be mo rationally to load on demand
/*$res=$this->queries->getTree->execute();
new dBug($res);
while($row=$this->queries->getChildren->fetchObject()){
convertNodeFromBase($row);
$this->inodes[$row->inode]=$row;// it looks like here I need & (a reference) here but id doesn't work by unknown reason
}
$this->queries->getTree->closeCursor();*/
$this->queries->getIgnores->execute();
$this->ignores=$this->queries->getIgnores->fetchAll(PDO::FETCH_COLUMN);
$this->queries->getIgnores->closeCursor();
}
function checkIsIgnored($path){
foreach($this->ignores as $ignoreRule){
if(preg_match($ignoreRule,$path)){
return true;
}
}
return false;
}
function loadChildren(&$parent){
$res=$this->queries->getChildren->execute(array($parent));
$arr=array();
while($row=$this->queries->getChildren->fetchObject()){
convertNodeFromBase($row);
$this->inodes[$row->inode]=&$row;//it looks like i need & (a reference) here but id doesn't work by unknown reason
//$arr[$row->inode]=&$this->inodes[$row->inode];
$arr[$row->inode]=$this->inodes[$row->inode];
}
$this->queries->getChildren->closeCursor();
return $arr;
}
function needBackup(){
return !(empty($this->added)&&empty($this->changed)&&empty($this->deleted)&&empty($this->moved));
}
function save($time=0){
if(!$this->needBackup())return;
$this->base->beginTransaction();
try{
$this->makeCommitRecord();
$this->saveAdded();
$this->saveChanged();
$this->saveMoved();
$this->saveDeleted();
$this->base->commit();
}
catch(Exception $exc){
$this->base->rollBack();
//$this->base->commit();
throw $exc;
}
}
function saveAdded(){
echo "saving added....\n<br/>";
foreach($this->added as &$node){
/// very important
//if you get here error about no disk space it can be REALLY FREE SPACE ON SYSTEM DISK (even though php is installed not to system disk and temp moved to another disk...)
//try to run ccleaner to make some empty space in system disk
//new dBug(array('inode'=>$node->inode,'parent'=>$node->parent,'hash'=>$node->hash,'pathhash'=>$node->pathhash,'name'=>$node->name));
$res=$this->queries->upsertFileToTree->execute(array($node->inode,$node->parent,$node->hash,$node->pathhash,$node->name));
}
}
function saveChanged(){
echo "saving changed....\n<br/>";
foreach($this->changed as &$node){
$res=$this->queries->upsertFileToTree->execute(array($node->inode,$node->parent,$node->hash,$node->pathhash,$node->name));
}
}
function saveMoved(){
echo "saving moved....\n<br/>";
foreach($this->moved as &$node){
$res=$this->queries->upsertFileToTree->execute(array($node->inode,$node->parent,$node->hash,$node->pathhash,$node->name));
$res=$this->queries->addFileToMoved->execute(array($node->inode,$node->prevParent));
}
}
function saveDeleted(){
echo "removing deleted....\n<br/>";
foreach($this->deleted as &$node){
$res=$this->queries->deleteFileFromTree->execute(array($node->inode));
$res=$this->queries->addFileToDeleted->execute(array($node->name,$node->parent));
}
}
function makeCommitRecord($time=0){
if(!$time)$time=time();
$this->queries->addCommit->execute(array($time));
//$this->queries->addCommit->execute();
return $time;
}
};
/*!
a class representing a file (or a folder) with some basic functionality
*/
class FileTreeItem implements IFileTreeItem{
public $name,$parent,$hash=0,$pathhash=0;
static $attributesForHashing=array('getInode'=>'inode','getMTime'=>'mtime','getCTime'=>'ctime','getSize'=>'size','getOwner'=>'uid');
function showAtrs(){
$atrs=new stdClass;
foreach(static::$attributesForHashing as $atrName)$atrs->$atrName=$this->$atrName;
new dBug($atrs);
}
function __construct(RecursiveDirectoryIterator &$file,IFileTreeDir &$parent=null){
//echo "the dir is:".$dir."\n";
//echo "the object path is:".$dir+"/"+$name."\n";
echo "constructing ".$file->getPathname()."<br/>\n";
if(!$parent)throw new Exception("file has no parent. Serious bug!!!");
$this->name=$file->getFilename();
foreach(static::$attributesForHashing as $getter=>&$atrName)$this->$atrName=$file->$getter();
//good style and flexibility makes overhead
echo "size is ".$this->size."<br/>\n";
if(!$this->inode){
//!in windows $file->getInode() returns 0
//!so we have to make some bad kind of inode for debug purposes
$this->inode=unpack('L',md5(
$file->isDir().
"|!".$file->isLink().
"|!".$this->name.
"|!".$this->ctime.
($file->isDir()?"|!".$file->getPathname():
//"|!".$this->mtime.///why had i included it to inode?
"|!".$this->size
)
,1));
$this->inode=$this->inode[1];
}
//$this->showAtrs();
$this->parent=$parent->inode;//it is here because the node may have yourself as parent
$this->pathhash=crc32($file->getPathname());
$this->hash=$this->hash();
unset($file);
$parent->inodes[$this->inode]=&$this;
}
/*!
function which make hashing (using md5), look at static::$attributesForHashing
*/
function hash(){
$hh=hash_init( "md5" );
foreach(static::$attributesForHashing as $atrName)hash_update($hh,$this->$atrName);
$h=unpack('L',hash_final( $hh ,1));
$this->hash=$h[1];
return $h[1];
}
/*function __call($name,$args){
call_user_func_array(array($this->file,$name),$args)
}*/
function process(){}
};
/*!
a class representing a folder with some basic folder functionality such as expanding children
*/
class FileTreeDir extends FileTreeItem implements IFileTreeDir{
protected $childrenIter=null;
public $children=null;
public $path="";
function __construct(RecursiveDirectoryIterator &$file,IFileTreeDir &$parent){
parent::__construct($file,$parent);
$this->path=$file->getPathname();
//new dBug($this->path);
$this->childrenIter=$file->getChildren();
}
function process(){
echo "processing folder $this->name ({$this->inode})<br/>\n";
static::expandChildren();
}
function expandChildren(){
foreach($this->childrenIter as $child){
$fChild=null;
if($child->isDir()){
$fChild=new static($child,$this);
}
else{
$fChild=new FileTreeItem($child,$this);
//new dBug($child);
}
static::processChild($fChild);
}
}
function processChild(IFileTreeItem &$fChild){
}
};
/*!
converts an array of nodes into nested array and visualisates it using dBug
*/
function drawHierarchy(&$inodelist){
$arr=array();
foreach($inodelist as $inode=>&$file){
if(empty($arr[$inode])){
$arr[$inode]=array();
$arr[$inode]["name"]=$file->name;
$arr[$inode]["parent"]=$file->parent;
}
}
$transforms=1;
while($transforms>0){
$transforms=0;
foreach($arr as $inode=>&$file){
if(isset($arr[$file["parent"]])){
$arr[$file["parent"]][$inode]=&$file;
unset($file["parent"]);
$transforms++;
}
}
}
foreach($arr as $inode=>&$file){
if(empty($file["parent"])){
unset($arr[$inode]);
}
}
new dBug($arr);
}
/*!
a class representing a directory with backup functionality
not for direct usage
*/
class FileTreeBackupDir extends FileTreeDir{
public $index,$childrenCache;
public $root,$relPath;
function __construct(RecursiveDirectoryIterator $file,&$parent=null){
//index is singleton
echo "<hr/>";
if($parent instanceof FileTreeBackuper){
echo "We are a root of the backup\n<br/>";
echo 'Root adress is '.$file->getPathname()."\n<br/>";
$this->index=&$parent->index;
echo "now index is of type ".get_class($this->index)."\n<br/>";
parent::__construct($file,$this);//the root directory is parent of oneself
$this->root=&$this;
$this->pathLen=strlen($this->path);
echo "root constructed\n<br/>";
}else{
$this->index=&$parent->index;
$this->root=&$parent->root;
//$this->index=&$this->inodes[$this->parent]->index;
//echo "now index is of type ".get_class($this->index)."\n<br/>";
parent::__construct($file,$parent);
$this->relPath=static::getRelPath();
}
}
function getRelPath(){//returns relative path beginning with /
return substr($this->path,$this->root->pathLen);
//return (this->index->inodes[$this->parent]->relPath.'/'.$this->name);
}
function expandChildren(){
$children=$this->index->loadChildren($this->inode);
/*echo "before\n<br/>";
new dBug($children);*/
foreach($this->childrenIter as $child){
if($this->index->checkIsIgnored($child->getFilename())){
//!@bug ignores feature works inefficiently now b'cause getFilename is called twice
echo $child->getFilename()." <font color='purple'>ignored</font><br/>\n";
continue;
}
if($child->isDir()){
$child=new static($child,$this);
}
else{
$child=new FileTreeItem($child,$this);
//new dBug($child);
}
if(isset($children[$child->inode])){
echo "There is element of children with inode {$child->inode}\n<br/>";
echo "hash is ".$child->hash." cached hash is {$children[$child->inode]->hash}\n<br/>";
//new dBug($this->index->inodes[$child->inode]);
if($children[$child->inode]->hash!=$child->hash){
//changed
echo "file $child->name was <font color='orange'>changed</font>: {$children[$child->inode]->hash} != {$child->hash} \n<br/>\n";
//$this->index->changed[$child->inode]=&$child;//TODO:why does the reference cause terrible errors in logic?
$this->index->changed[$child->inode]=$child;
}
else{
echo "file $child->name was <font color='blue'>not changed</font> or ????\n<br/>\n";
//to prevent processing unchanged
unset($children[$child->inode]);
continue;
}
unset($children[$child->inode]);
}
else{
//added
echo "file {$child->name} ({$child->inode}) was <font color='green'>added</font> to folder $this->name ({$this->inode})\n<br/>\n";
//$this->index->added[$child->inode]=&$child;//TODO:why does the reference cause terrible errors in logic?
$this->index->added[$child->inode]=$child;
}
// put into index an actual object after index was used
$this->index->inodes[$child->inode]=$child;
static::processChild($child);
}
/*echo "after\n<br/>";
new dBug($children);
//var_dump($this->index);
//var_dump($this->index->added);*/
foreach($children as $inode=>$cachedChild){
echo "file {$cachedChild->name} ({$cachedChild->inode}) was deleted\n<br/>\n";
//new dBug($cachedChild);
//$this->index->deleted[$inode]=&$cachedChild;//TODO:why does the reference cause terrible errors in logic?
$this->index->deleted[$inode]=$cachedChild;
}
echo "<hr color='green'/>";
}
function processChild(IFileTreeItem &$fChild){
echo "processing child {$fChild->name} ({$fChild->inode}) of {$this->name} ({$this->inode})\n<br/>\n";
/*echo '//var_dump($this->index->inodes)';
//var_dump($this->index->inodes);
echo '//var_dump($this->index)';
//var_dump($this->index);*/
//drawHierarchy($this->index->added);
$fChild->process();
echo "<hr color='red'/>";
}
function checkDeletedChildren(){
}
};
/*!
a class representing a plugin for backuping file tree
takes array of adresses of roots of the backup
*/
class FileTreeBackuper implements IBackuper{
public $index, $roots=array();
const filesDir="files",patchesDir="patches";
public $zip=null;
function __construct($roots){
$this->roots=&$roots;
}
function prepareForBackup(PDO &$base){
$this->index=new FileTreeBackuperIndex($base);
$roots=array();
foreach($this->roots as $root){
echo "Creating iterator for root: address is $root .\n<br/>";
$flags=FilesystemIterator::CURRENT_AS_SELF;
$root=new RecursiveDirectoryIterator($root,$flags);
$root->setFlags($flags|FilesystemIterator::SKIP_DOTS);
echo "Iterator created: address is ".$root->getPathname()." .\n<br/>";
$root=new FileTreeBackupDir($root,$this);
$roots[$root->inode]=&$root;
$this->index->inodes[$root->inode]=&$root;
}
$this->roots=&$roots;
foreach($this->roots as &$root){
$root->process();
}
//drawHierarchy($this->index->added);
echo "<hr color='purple'/>";
static::detectMoved();
echo "<hr color='magenta'/>";
}
function makeBackup(&$arch){
$this->arch=&$arch;
echo "archivation of changed...<br/>";
static::archivateChanged();
echo "archivating added...<br/>";
static::archivateAdded();
//todo: add processing of deleted
$this->index->save();
return [
'comment'=>[
'+'=>count($this->index->added),
'*'=>count($this->index->changed),
'->'=>count($this->index->moved),
'-'=>count($this->index->deleted)
]
];
}
private function detectMoved(){
echo "<hr color='lemonchiffon'/>Detecting moved....<br/>";
foreach($this->index->deleted as $inode=>&$node){
//new dBug($node);
if(isset($this->index->added[$inode])){
//var_dump($this->index->inodes[$inode]);
//var_dump($this->index->added[$inode]);
//var_dump($this->index->deleted[$inode]);
//var_dump($node);
if($this->index->added[$inode]->pathhash!=$node->pathhash){
//moved
//var_dump($this->index->changed[$node->parent]);
echo "file {$node->name} ({$node->inode}) was moved\n<br/>\n";
//$this->index->moved[$node->inode]=&$node;//TODO:why does the reference cause terrible errors in logic?
$this->index->moved[$inode]=$this->index->added[$inode];
$this->index->moved[$inode]->prevParent=$this->index->deleted[$inode]->parent;
//$this->index->moved[$inode]->prevPathhash=$this->index->deleted[$inode]->pathhash;
unset($this->index->added[$inode]);
unset($this->index->deleted[$inode]);
}else{
//error
}
}
}
//var_dump($this->index->moved);
echo "<hr color='lemonchiffon'/>";
}
function needBackup(){
return $this->index->needBackup();
}
private function archivateAdded(){
foreach($this->index->added as $inode=>&$node){
//var_dump($this->index->inodes[$node->parent]);
echo "archivating ".$this->index->inodes[$node->parent]->path.'/'.$node->name.
' as '.static::filesDir.$this->index->inodes[$node->parent]->relPath.'/'.$node->name.'</br>';
if($node instanceof IFileTreeDir){
$this->arch->addEmptyDir(static::filesDir.$node->relPath);
continue;
}
//var_dump($node);
//var_dump($this->index->inodes[$node->parent]);
$this->arch->addFile(
$this->index->inodes[$node->parent]->path.'/'.$node->name,
static::filesDir.$this->index->inodes[$node->parent]->relPath.'/'.$node->name
);
}
}
private function archivateChanged(){
//! temporary it has practically the same implementation as archivateAdded but later we will add diff for text-based files
foreach($this->index->changed as $inode=>&$node){
echo "archivating ".$this->index->inodes[$node->parent]->path.'/'.$node->name.' as '.static::filesDir.$this->index->inodes[$node->parent]->relPath.'/'.$node->name.'</br>';
if($node instanceof IFileTreeDir){
continue;
}
$this->arch->addFile(
$this->index->inodes[$node->parent]->path.'/'.$node->name,
static::filesDir.$this->index->inodes[$node->parent]->relPath.'/'.$node->name);
}
}
}
?>