-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.php
More file actions
1357 lines (1050 loc) · 42.8 KB
/
core.php
File metadata and controls
1357 lines (1050 loc) · 42.8 KB
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
/* File Grim Reaper v1.7 - It will reap your files!
* (c) 2011-2026 John Wellesz
*
* This file is part of File Grim Reaper.
*
* Project home:
* https://github.com/2072/File-Grim-Reaper
*
* Bug reports/Suggestions:
* https://github.com/2072/File-Grim-Reaper/issues
*
* File Grim Reaper is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* File Grim Reaper 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 File Grim Reaper. If not, see <http://www.gnu.org/licenses/>.
*/
const VERSION = "1";
const REVISION = "7"; // Also remember to change the version at the top of both PHP files.
const RESPITE = 12; // hours
const FOUND_ON = 0;
const FILE_M_TIME = 1;
require_once __DIR__ . '/src/classes.php';
$pid_file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'fileGrimReaper.pid';
clearstatcache();
if (! defined("PROPER_USAGE"))
die("Incorrect usage, you cannot execute this file directly!");
define ('UNAME', php_uname('n'));
error_reporting ( E_ALL | E_STRICT );
ini_set('error_log', dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .UNAME."_errors.log");
const DEFAULT_CONFIG_FILE = "FileGrimReaper-paths.txt";
define ( 'NOW', time() );
define ( 'ISWINDOWS', strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
ini_set('memory_limit','26G');
//ini_set('opcache.jit','1'); // cannot be set at runtime
$old_umaks = umask(0);
function seekSplObjectStorage(SplObjectStorage $s, int $position): void
{
if ($position < 0) {
throw new OutOfBoundsException('Position must be non-negative');
}
$s->rewind();
$currentPos = 0;
while ($s->valid() && $currentPos < $position) {
$s->next();
$currentPos++;
}
if ($currentPos !== $position) {
throw new OutOfBoundsException("Position $position is out of bounds");
}
}
function sortSplObjectStorage(
SplObjectStorage $storage,
callable $callback
): SplObjectStorage {
$temp = [];
foreach ($storage as $obj) {
$temp[] = [$obj, $storage[$obj]];
}
usort($temp, $callback);
$sorted = new SplObjectStorage();
foreach ($temp as [$obj, $value]) {
$sorted[$obj] = $value;
}
return $sorted;
}
function sortByname(SplObjectStorage $storage): SplObjectStorage {
return sortSplObjectStorage($storage, function ($a, $b) {
return strnatcmp((string)$a[0], (string)$b[0]);
});
}
function isProcessRunning($pid) {
if (!is_numeric($pid)) {
throw new InvalidArgumentException("PID must be a number");
}
$output = '';
$return = null;
$command = ISWINDOWS ? 'tasklist /FI "PID eq '.$pid.'" 2>NUL | find /I "'.$pid.'">NUL' : 'ps -p '.$pid;
$process = proc_open(
$command,
[
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w'],
],
$pipes
);
if (is_resource($process)) {
fclose($pipes[0]);
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$errorOutput = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$return = proc_close($process);
}
if (!empty($errorOutput)) {
error("Process running check command '$command' failed: $errorOutput");
return false;
}
return ISWINDOWS ? $return === 0 : strpos($output, "$pid") !== false;
}
function removeFile (string | Path $path)
{
if ($path instanceOf Path)
$path = (string)$path;
if (! SHOW && ! @unlink($path)) {
// Note that on window$ one needs to use rmdir on symbolic links pointing to directories... micro$oft never fails to disappoint!
if (!(@chmod($path, 0777) && @unlink($path)) && (!ISWINDOWS || !@rmdir($path)))
error("Couldn't remove '$path'");
else
return true;
} else
return true;
return false;
}
function removeDirectory (string | Path $path)
{
if (! SHOW && ! @rmdir((string)$path))
error();
else
return true;
return false;
}
function cprint ()
{
$args = func_get_args();
$toPrint = str_replace("\n", "\r\n", implode("", $args))."\r\n";
addToLog($toPrint);
return fwrite(STDOUT, $toPrint);
}
function unlogged_cprint()
{
global $LOGFILEPATH;
$tmp = $LOGFILEPATH;
$LOGFILEPATH = "";
$written = call_user_func_array('cprint', func_get_args());
$LOGFILEPATH = $tmp;
return $written;
}
function temp_cprint()
{
//write something and place the cursor back where it was
$args = func_get_args();
$toPrint = str_replace("\n", "\r\n", implode("", $args));
$written = fwrite(STDOUT, $toPrint);
return fwrite(STDOUT, str_pad("", $written, chr(0x8)));
}
function printUsage ()
{
cprint ( "Usage: ", $_SERVER['PHP_SELF'], " --reap | --show [--config=configFilePath] [--logging] [--doNotCreateDirs]\n");
}
function printHeader ()
{
global $argc;
if ($argc > 1)
cprint ("\nFile Grim Reaper version ",VERSION,".",REVISION," Copyright (C) 2011-2025 John Wellesz\n",
<<<SHORTWELCOME
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; see the provided GPL.txt for details.
SHORTWELCOME
);
else
cprint("\nFile Grim Reaper version ",VERSION,".",REVISION," Copyright (C) 2011-2025 John Wellesz\n",
<<<LONGWELCOME
Project home:
https://github.com/2072/File-Grim-Reaper
Bug reports/Suggestions:
https://github.com/2072/File-Grim-Reaper/issues
File Grim Reaper is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
File Grim Reaper 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 File Grim Reaper. If not, see <http://www.gnu.org/licenses/>.
## Options
-r, --reap Removes expired files, directories and updates snapshots.
-s, --show Shows what would happen with the --reap command (doesn't
actually remove anything and doesn't update snapshots).
-c=FilePath, --config=FilePath
Uses the specified paths configuration file (one path per line).
-l, --logging Creates log files for each configured directories. The log
files will be stored in a 'Logs' sub-folder in the
'FileGrimReaper-Datas' directory. The log file will be
named in the following way: COMPUTERNAME_SANITIZED-PATH.log
The log file will be written if and only if something
changed in the monitored folder. The number of new and
modified files is given as well as the full path of every
deleted file.
-y, --daylightsavingbug
On Microsoft Windows platforms, on some filesystems (such
as FAT or network shares), there is a "feature" that makes
filemtime() report a different file modification time
wether Daylight Saving is active or not.
This option enables the detection of this bug to prevent
files from appearing modified (and thus resetting their
expiry) when DLS status changes.
There is one caveat though: if a file is replaced with a
file whose modification time is exactly one hour apart from
the original file (and older than a day), the file expiry
won't be reset and the file will be deleted sooner than
expected.
-d, --doNotCreateDirs
Do not create missing directories in the configuration
file. The default is to create those directories if they are defined as some
users tend to remove them either by accident or ignorance.
LONGWELCOME
);
}
$ERRORCOUNT = 0;
function error ()
{
global $ERRORCOUNT;
$ERRORCOUNT++;
$args = func_get_args();
$last_error = error_get_last();
if (! empty($last_error)) {
if (count($args))
$args[] = "\n\tLast PHP error: ";
else
$args[] = "ERROR: ";
$args[] = $last_error['message'];
}
$toPrint = str_replace("\n", "\r\n", implode("", $args))."\r\n";
fwrite(STDERR, $toPrint);
addToLog($toPrint);
}
$LOGFILEPATH = "";
$STARTHEADERPRINTED = false;
function IsStringInc($str_1, $str_2) {
if (strlen($str_1) != strlen($str_2) || $str_1 == $str_2)
return false;
$diffs = array(); $d = 0; $diffOffset = 0;
for ($i=0; $i < strlen($str_1); ++$i) {
if ($str_1[$i] != $str_2[$i]) {
$diffOffset = $i - $d;
if (!isset ($diffs[$diffOffset]))
$diffs[$diffOffset] = array (array(),array());
$diffs[$diffOffset][0][] = $str_1[$i];
$diffs[$diffOffset][1][] = $str_2[$i];
++$d;
}
}
if (count($diffs) > 1)
return false;
$diffs[$diffOffset][0] = (int)implode($diffs[$diffOffset][0]);
$diffs[$diffOffset][1] = (int)implode($diffs[$diffOffset][1]);
if ($diffs[$diffOffset][0] + 1 == $diffs[$diffOffset][1])
return true;
else
return false;
}
function addToLog ($toWrite)
{
global $LOGFILEPATH, $STARTHEADERPRINTED;
if (! (defined("LOGGING") && LOGGING && !empty($LOGFILEPATH)))
return;
if (! $STARTHEADERPRINTED) {
$header = "---------------------------------\r\n" . "Started on: " . LOG_HEADER . "\r\n\r\n";
$STARTHEADERPRINTED = true;
} else
$header = "";
error_log("$header$toWrite", 3, $LOGFILEPATH);
}
function getDirectoryDepth(Path $path)
{
return $path->getDepth();
}
function errorExit($code)
{
$args = func_get_args();
$args[0] = "FATAL ERROR: ";
call_user_func_array("error", $args);
global $old_umaks;
umask($old_umaks);
exit ($code);
}
function isDirValid ($name)
{
// format should be something like BLABLABLA__TO_KEEP_XX_LENGTH
// handle the following length : YEAR(S), MONTH(S), DAY(S), HOUR(S), MINUTE(S)
$matches = array();
$badDir = false;
$nameFormat = '#[/\\\\](\w+)__TO_KEEP_(\d+)_(YEAR|MONTH|DAY|HOUR|MINUTE|SECOND)S?$#';
$timeMultiplicators = array (
"YEAR" => 3600 * 24 * 365,
"MONTH" => 3600 * 24 * 30,
"DAY" => 3600 * 24,
"HOUR" => 3600,
"MINUTE" => 60,
"SECOND" => 1,
);
if ( preg_match($nameFormat, $name, $matches) ) {
if ( is_dir ($name) || (!DONOTCREATEDIRS && mkdir($name, 0777))) {
return array ('name' => $matches[1], 'duration' => $matches[2] * $timeMultiplicators [ $matches[3] ]);
} else
$badDir = "Directory '$name' cannot be found!";
} else
$badDir = "Wrong name format, should match '$nameFormat'";
if ($badDir) {
error('Config WARNING: ', "'$name'", " is not a valid directory:\n", $badDir);
return false;
}
}
function isMTimeTheSame ($a, $b)
{
if ($a == $b)
return true;
// if the difference is exactly 1 hour and the file is older than 1 day,
// it's a fucking daylight saving issue (thank you Microsoft)
if ( DAYLIGHTSAVINGBUG && (abs($a-$b) == 3600) && (NOW - $a > 86400) )
return true;
return false;
}
function getPathMTime($path) {
$stats = @lstat($path);
if (is_array($stats) && isset($stats['mtime'])) {
return (int)$stats['mtime'];
}
error("Could not get modification time of '$path'");
return false;
}
function getSplInfoSafeMTime(SplFileInfo $splinfo) {
try {
// Attempt to get the modification time normally.
return !$splinfo->isLink() ? $splinfo->getMTime() : getPathMTime($splinfo->getPathname());
} catch (Exception $e) {
// Fallback: use lstat to get the mtime of the link itself.
return getPathMTime($splinfo->getPathname());
}
}
function GetAndSetOptions ()
{
$longOptions = array (
"config::",
"show",
"reap",
"logging",
"daylightsavingbug",
"doNotCreateDirs"
);
$setOptions = getopt("c::srlyd", $longOptions);
if (isset($setOptions['s']) || isset($setOptions['show']))
define ('SHOW', true);
else
define ('SHOW', false);
if (isset($setOptions['l']) || isset($setOptions['logging']))
define ('LOGGING', true);
else
define ('LOGGING', false);
if (isset($setOptions['r']) || isset($setOptions['reap']))
define ('REAP', true);
else
define ('REAP', false);
if (isset($setOptions['y']) || isset($setOptions['daylightsavingbug']))
define ('DAYLIGHTSAVINGBUG', true);
else
define ('DAYLIGHTSAVINGBUG', false);
if (isset($setOptions['d']) || isset($setOptions['doNotCreateDirs']))
define ('DONOTCREATEDIRS', true);
else
define ('DONOTCREATEDIRS', false);
if (SHOW && REAP)
errorExit(1, '--reap and --show options are exclusive!');
elseif (! (SHOW || REAP)) {
global $argc;
if ($argc > 1) {
printUsage ();
errorExit(1, "Action is missing!");
} else {
global $old_umaks;
umask($old_umaks);
exit(0);
}
}
if (! empty($setOptions['c']) || ! empty($setOptions['config'])) {
$config = ( (! empty($setOptions['c'])) ? $setOptions['c'] : $setOptions['config'] );
if ( file_exists( $config ) )
define ('CONFIG', realpath($config));
else
errorExit(1, "config file '$config' couldn't be found!");
} else
define ('CONFIG', false);
}
function checkDataPath ()
{
// find our config path
if (! @realpath(__FILE__) )
errorExit(2, 'Impossible to determine script directory...');
else
define ('DATA_PATH', dirname(realpath(__FILE__)) . "/FileGrimReaper-Datas");
if (!is_dir(DATA_PATH)) {
mkdir(DATA_PATH);
cprint('Created data folder: ', DATA_PATH);
}
if (LOGGING) {
if (!is_dir(DATA_PATH."/Logs")) {
mkdir(DATA_PATH."/Logs");
cprint('Created log folder: ', DATA_PATH."/Logs");
}
define ('LOGS_PATH', DATA_PATH."/Logs");
define ('LOG_HEADER', date("[Y-m-d H:i:s] "));
}
}
function getConfig ()
{
if (CONFIG)
$configPath = CONFIG;
else
$configPath = getcwd() . '/' . DEFAULT_CONFIG_FILE;
if (file_exists($configPath))
$config = array_map('trim', file($configPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
else
errorExit(1, 'No configuration file provided and no file named "', DEFAULT_CONFIG_FILE, '" found in ', getcwd());
if (count($config) == 0 )
errorExit(2, 'Configuration file is empty!', ' Configuration file used : ', ( isset($configPath) ? realpath($configPath) : CONFIG ));
$directories = array ();
foreach ($config as $path)
if (! ($param = isDirValid($path)))
unset ($config[$path]);
else {
$directories [realpath($path)] = $param;
}
return $directories;
}
function mySerialize($data): string {
static $warningPrintedOnce = false;
if (function_exists('igbinary_serialize'))
return igbinary_serialize($data);
else {
if (!$warningPrintedOnce) {
unlogged_cprint("⚠️ Warning: using standard php serialize because igbinary extension not available (igbinary is recommended)");
$warningPrintedOnce = true;
}
return serialize($data);
}
}
const CHUNK_MARKER = "\n---CHUNK---\n";
function serializeToFile(string $filename, SplObjectStorage $data): int {
if ($data instanceOf SplObjectStorage && count($data))
$data->rewind();
$handle = fopen($filename, 'wb');
if (!$handle) {
throw new RuntimeException("Cannot open file: $filename");
}
$written = 0;
foreach ($data as $dirName) {
$dirItems = $data[$dirName];
$singlePathStorage = new SplObjectStorage();
$singlePathStorage[$dirName] = $dirItems;
// unlogged_cprint("serialized d: ", $dirName, " with ", count($data[$dirName]), " files");
$writtenNow = fwrite($handle, mySerialize($singlePathStorage) . CHUNK_MARKER);
if (!$writtenNow)
return false;
$written += $writtenNow;
}
fclose($handle);
return $written;
}
function myUnSerialize(string $str) {
global $LOGFILEPATH;
static $warningPrintedOnce = false;
if (function_exists('igbinary_unserialize')) {
$data = igbinary_unserialize($str);
if ($data !== false && $data !== null)
return $data;
else {
$debug_strLen = strlen($str);
unlogged_cprint("⚠️ Warning: using standard php unserialize because igbinary failed (str length: $debug_strLen - filename: $filename) (assuming default serialization)");
file_put_contents("$LOGFILEPATH.failedChunk", $str);
return unserialize($str);
}
} else {
if (!$warningPrintedOnce) {
unlogged_cprint("⚠️ Warning: using standard php unserialize because igbinary extension not available (igbinary is recommended)");
$warningPrintedOnce = true;
}
return unserialize($str);
}
}
function unserializeFromFile_gen(string $filename): Generator {
$handle = fopen($filename, 'rb');
if (!$handle) {
throw new RuntimeException("Cannot open file: $filename");
}
$parts = [];
$marker_len = strlen(CHUNK_MARKER);
$overlap = ''; // Buffer to hold partial markers
try {
while (!feof($handle)) {
$data = $overlap . fread($handle, 4 * 1024 * 1024);
// Hold back bytes that could be part of a split marker
if (!feof($handle) && strlen($data) >= $marker_len) {
$overlap = substr($data, -($marker_len - 1));
$data = substr($data, 0, -($marker_len - 1));
} else {
$overlap = '';
}
if ($data === '') {
break;
}
$offset = 0;
while (($pos = strpos($data, CHUNK_MARKER, $offset)) !== false) {
$part_len = $pos - $offset;
if ($part_len > 0) {
$parts[] = substr($data, $offset, $part_len); // Copy only the pre-marker part (< read size)
}
// Chunk complete; implode to minimize temp allocs
$chunk = implode('', $parts);
$parts = [];
if ($chunk !== '') {
$data_unser = myUnSerialize($chunk, $filename);
if ($data_unser !== false && $data_unser !== null) {
yield $data_unser;
} else {
yield false;
}
}
$offset = $pos + $marker_len;
}
// Add remaining (partial chunk or full read if no marker)
if ($offset < strlen($data)) {
if ($offset === 0) {
$parts[] = $data; // Direct ref, no copy
} else {
$parts[] = substr($data, $offset); // Copy only the remainder (< read size)
}
}
}
// Handle any remaining chunk
if (!empty($parts)) {
$chunk = implode('', $parts);
if ($chunk !== '') {
$data_unser = myUnSerialize($chunk, $filename);
if ($data_unser !== false && $data_unser !== null) {
yield $data_unser;
} else {
yield false;
}
}
}
} finally {
// Guarantee file is closed even if generator breaks early
fclose($handle);
}
}
function unserializeFromFile(string $filename) : SplObjectStorage | array | false {
$data = new SplObjectStorage();
$failed = false;
foreach (unserializeFromFile_gen($filename) as $singleOrMultipPathStorage) {
if (is_array($singleOrMultipPathStorage)) { // old format
$data = $singleOrMultipPathStorage;
break;
}
if ($singleOrMultipPathStorage !== false) {
if (count($singleOrMultipPathStorage) > 1)
cprint("⚠️ $filename: old non-chunked serialization found: ", count($singleOrMultipPathStorage), " paths");
foreach ($singleOrMultipPathStorage as $dirName) {
//$temp = $singleOrMultipPathStorage[$dirName]->current();
//unlogged_cprint("unserialized d: ", $dirName, " - did: ", spl_object_id($dirName), " with ", count($singleOrMultipPathStorage[$dirName]), " files - first: ", $temp, " fid: ", spl_object_id($temp));
$data[$dirName] = $singleOrMultipPathStorage[$dirName];
$data[$dirName]->rewind();
}
} else {
$failed = true;
break;
}
}
if (!$failed) {
if ($data instanceOf SplObjectStorage && count($data))
$data->rewind();
return $data;
} else
return false;
}
function getDirectoryScannedDatas ($path, &$lastScanned=false): SplObjectStorage | false
{
$dataFileName = preg_replace("#[\\\\/]|:\\\\#", "-", $path);
if (LOGGING) {
global $LOGFILEPATH, $STARTHEADERPRINTED;
$LOGFILEPATH = LOGS_PATH . "/".UNAME."_$dataFileName.log";
$STARTHEADERPRINTED = false;
}
if (! $dataFileName)
errorExit(2, 'Impossible error #1: preg_replace() failed on: ', $path);
$dataFileName = DATA_PATH . '/'. UNAME . '_' . $dataFileName . '.data.serialized';
if (file_exists($dataFileName) && @filemtime($dataFileName)) { // sometimes file_exists() returns true whereas the file doesn't exist... Clearstatcache is not enough apparently (observed on OSX 10.5 on 2012-12-17 with php 5.3.8)
$lastScanned = filemtime($dataFileName);
$data = false;
$data = unserializeFromFile($dataFileName);
if ($data === false || $data === null || !($data instanceOf SplObjectStorage || is_array($data))) {
if (function_exists('igbinary_unserialize'))
error("Could not unserialize data.");
else
error("Could not unserialize data. installing igbinary might solve the problem");
return false;
}
if (is_array($data)) {
// convert very old format
$firstElement = current($data);
if ($firstElement !== FALSE && isset($firstElement["foundOn"]) ) {
cprint("ℹ️ Converting snapshot from VERY old format...");
$cData = [];
foreach($data as $fullFilePath=>$times) {
$cData[dirname($fullFilePath)][basename($fullFilePath)] = [
FOUND_ON => $times["foundOn"],
FILE_M_TIME => $times["fileMTime"]
];
}
$data = $cData;
}
$new = new SplObjectStorage();
cprint("ℹ️ Converting snapshot $dataFileName from old format...");
foreach ($data as $dir => $files) {
$path = Path::fromString($dir);
$new[$path] ??= new SplObjectStorage();
foreach ($files as $file => $info) {
$new[$path][Name::get($file)] = $info;
}
}
cprint("Done: " . count($new) );
return $new;
} elseif ($data instanceOf SplObjectStorage) {
printStatus();
unlogged_cprint("\tCreating name name cache ", number_format(Name::getPoolSize()));
foreach ($data as $dir) {
$files = $data[$dir];
foreach (clone $files as $file) {
$canonical = Name::get((string)$file);
if (spl_object_id($file) != spl_object_id($canonical)) {
$fTimes = $files[$file];
unset($files[$file]);
$files[$canonical] = $fTimes;
//cprint("\t\tdebug: ", $file, " was migrated to canonical version: ", spl_object_id($canonical));
}
}
}
return $data;
} else {
error('Error loading data for directory: ', $path);
return false;
}
} else
return new SplObjectStorage();
}
function saveDirectoryScannedDatas ($path, SplObjectStorage $datas)
{
global $LOGFILEPATH;
if (SHOW) {
cprint("Changes not saved, showing only.");
$LOGFILEPATH = false;
return;
}
$dataFileName = preg_replace("#[\\\\/]|:\\\\#", "-", $path);
if (! $dataFileName)
errorExit(2, 'Impossible error #2: preg_replace() failed on: ', $path);
$dataFileName = DATA_PATH . '/'. UNAME . '_' . $dataFileName . '.data.serialized';
$tempFileName = $dataFileName . sprintf("_%X", crc32(microtime()));
// Write the data in a temporary file and really check it's complete
if ( ($written = serializeToFile($tempFileName, $datas)) === false
|| filesize($tempFileName) !== $written )
error("Couldn't write scanned datas in: ", $tempFileName);
else {
// If all was written then replace the original file
// This prevents to damage the original file if the disk happened to be full
if (! rename($tempFileName, $dataFileName))
error("Couldn't save scanned datas in: ", $dataFileName);
}
$LOGFILEPATH = false;
}
function printStatus(): void {
unlogged_cprint("\t\tName cache: ", number_format(Name::getPoolSize()), " - Path cache: p-: ", number_format(Path::getCacheSizes()[0]), " - fc-: ", Path::getCacheSizes()[1],
" (max of ",number_format(memory_get_peak_usage() / 1024 / 1024) ," Mb of RAM used), ", "current usage: ", number_format(memory_get_usage() / 1024 / 1024), " Mb" );
}
$pathPidFile = "";
function fileGrimReaper ($dirToScan)
{
global $pathPidFile, $pid_file;
$opCacheState = opcache_get_status();
if (!is_array($opCacheState) || !isset($opCacheState['jit']['on']) || !$opCacheState['jit']['on'])
cprint("⚠️ oopcache.jit is not enabled in config");
// sort directories so the shortest durations are scanned first
uasort($dirToScan, function($a, $b) {$a = $a['duration']; $b=$b['duration']; return ($a < $b) * -1 + ($a > $b) * 1; });
foreach ($dirToScan as $dirPath=>$dirParam) {
Name::_resetPool();
Path::_reset();
if (!empty($pathPidFile))
unlink ($pathPidFile);
$start = microtime(true);
$lastScanned = 0;
// The log file is named after the directory being checked so...
unlogged_cprint("\nℹ️ Now considering files in: ", $dirPath, '...');
// Check if another instance is already scanning this directory
$pathPidFile = $pid_file.".".crc32($dirPath);
if (file_exists($pathPidFile)) {
$pid = trim(file_get_contents($pathPidFile));
if (isProcessRunning($pid)) {
cprint( "Another instance ($pid) is already running. Skipping.");
$pathPidFile = "";
continue;
} else {
unlink($pathPidFile);
}
}
file_put_contents($pathPidFile, getmypid());
if ( (NOW - $lastScanned) < RESPITE * 60 * 60 && $dirParam['duration'] > 2 * RESPITE * 60 * 60) {
unlogged_cprint("ℹ️ Skipping (snapshot is just ", sprintf("%0.1f", (NOW - $lastScanned) / 3600)," hours and life is ", sprintf("%0.1f", $dirParam['duration'] / 86400), " days)");
continue;
}
// get previous scan datas
if (false === ($knownDatas = getDirectoryScannedDatas($dirPath, $lastScanned))) {
cprint("🔴 Error: Unserialize failed, skipping ", $dirPath);
continue;
}
gc_collect_cycles();
printStatus();
/* ########################################
* # Take a new snapshot of the directory #
* ########################################
*/
unlogged_cprint("\tTaking a new snapshot...");
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD);
$NewSnapShot = new SplObjectStorage();
$DirHasChildren = new SplObjectStorage();
$FoundFilesCounter = 0;
$limitTripped = false;
if ( $iterator ) {
foreach ($iterator as $file=>$fileinfo) {
// initialize the per directory child counter
if ($fileinfo->isDir() && ! $fileinfo->isLink() && ! isset($DirHasChildren[Path::fromString($fileinfo->getPathName())]) ) {
// Mark the directory as empty the first time we see it,
// because if there are no file in it, it's the only time
// we'll see it.
$DirHasChildren[Path::fromString($fileinfo->getPathname())] = 0;
}
$p = Path::fromString($fileinfo->getPath());
//unlogged_cprint("found path ", $p, "with id: ", spl_object_id($p));
// Count elements, increment the child number of the parent directory
if (! isset($DirHasChildren[$p]))
$DirHasChildren[$p] = 1;
else
$DirHasChildren[$p] = $DirHasChildren[$p] + 1;
//If it's not a file
if (! $fileinfo->isFile() && ! $fileinfo->isLink()) {
// If neither file or directory, then we have a problem
if (! $fileinfo->isDir())
error("'$file' is immortal... It needs renaming so it can be reaped when its time comes.");
continue;
}
if (false === ($fileSafeMTime = getSplInfoSafeMTime($fileinfo))) {
error("'$file' is impervious to our time scanner! Its modification time could not be determined...");
continue;
}
$NewSnapShot[$p] ??= new SplObjectStorage();
$NewSnapShot[$p][Name::get(basename($fileinfo->getPathname()))] = [
FOUND_ON => time(),
FILE_M_TIME => getSplInfoSafeMTime($fileinfo),
];
$FoundFilesCounter++;
if (! ($FoundFilesCounter % 100) )