-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathfs_helpers.php
More file actions
100 lines (90 loc) · 2.62 KB
/
fs_helpers.php
File metadata and controls
100 lines (90 loc) · 2.62 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
<?php
/* Filesystem helper utilities for dynamix.vm.manager
* - files_identical($a,$b)
* - copy_if_different($src,$dst, $dry_run=false)
* - dir_copy($src,$dst) (recursive, skips identical files)
* - dir_remove($dir) (recursive remove)
*/
function files_identical($a, $b) {
if (!file_exists($a) || !file_exists($b)) return false;
if (filesize($a) !== filesize($b)) return false;
$ha = @md5_file($a);
$hb = @md5_file($b);
if ($ha === false || $hb === false) return false;
return $ha === $hb;
}
function copy_if_different($src, $dst, $dry_run = false) {
$result = [
'src' => $src,
'dst' => $dst,
'would_copy' => false,
'copied' => false,
'error' => null
];
if (!file_exists($src)) {
$result['error'] = 'source not found';
return $result;
}
$dst_dir = dirname($dst);
if (!is_dir($dst_dir)) {
if ($dry_run) {
$result['would_copy'] = true;
return $result;
}
if (!@mkdir($dst_dir, 0755, true)) {
$result['error'] = 'failed to create dest dir';
return $result;
}
}
if (file_exists($dst)) {
if (files_identical($src, $dst)) {
return $result; // identical, nothing to do
}
$result['would_copy'] = true;
} else {
$result['would_copy'] = true;
}
if ($dry_run) return $result;
if (@copy($src, $dst)) {
$result['copied'] = true;
} else {
$result['error'] = 'copy_failed';
}
return $result;
}
function dir_copy($src, $dst) {
if (!is_dir($src)) return false;
if (!is_dir($dst)) {
if (!@mkdir($dst, 0755, true)) return false;
}
$items = scandir($src);
foreach ($items as $item) {
if ($item === '.' || $item === '..') continue;
$s = $src . DIRECTORY_SEPARATOR . $item;
$d = $dst . DIRECTORY_SEPARATOR . $item;
if (is_dir($s)) {
if (!dir_copy($s, $d)) return false;
} else {
if (file_exists($d)) {
if (files_identical($s, $d)) continue;
}
if (!@copy($s, $d)) return false;
}
}
return true;
}
function dir_remove($dir) {
if (!is_dir($dir)) return false;
$items = scandir($dir);
if ($items === false) return false;
foreach ($items as $item) {
if ($item === '.' || $item === '..') continue;
$path = $dir . DIRECTORY_SEPARATOR . $item;
if (is_dir($path)) {
if (!dir_remove($path)) return false;
} else {
if (!@unlink($path)) return false;
}
}
return @rmdir($dir);
}