-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.php
More file actions
83 lines (64 loc) · 2.11 KB
/
main.php
File metadata and controls
83 lines (64 loc) · 2.11 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
<?php
/**
* Rebalance the directory by writing the file across all block devices.
* RecursiveIteratorIterator does not follow symlinks by default, and we do not
* wish too since that could lead to all kinds of hell.
* @param $directory - the path to the directory we wish to rebalance
*/
function rebalanceDir($directory)
{
# Get file count for progress
$countingIterator = new RecursiveDirectoryIterator($directory);
$numFiles = 0;
foreach (new RecursiveIteratorIterator($countingIterator) as $filename => $file)
{
if (!is_dir($filename))
{
$numFiles++;
}
}
# Now "rebalance"
$iterator = new RecursiveDirectoryIterator($directory);
$progressCounter = 0;
$lastPercentage = 0;
foreach (new RecursiveIteratorIterator($iterator) as $filename => $file)
{
if (!is_dir($filename))
{
$progressCounter++;
$newPercentage = intval(($progressCounter / $numFiles * 100));
if ($newPercentage !== $lastPercentage)
{
print $newPercentage . "%" . PHP_EOL;
$lastPercentage = $newPercentage;
}
$counter = 1;
while (is_file($filename . $counter))
{
$counter++;
}
$newname = $filename . $counter;
# We have to use copy first instead of rename because we need
# new data blocks to be rewritten across all drives. Performing
# a rename would just change the metadata.
copy($filename, $newname);
unlink($filename);
# Rename the file back to what it was originally called.
rename($newname, $filename);
}
}
}
$directory = $argv[1];
if (is_dir($directory))
{
$directory = realpath($directory);
if (strtolower(readline("Balance the files within [$directory] (y/n)? ")) == "y")
{
rebalanceDir($directory);
print "complete!" . PHP_EOL;
}
}
else
{
print 'Invalid directory given!' . PHP_EOL;
}