summaryrefslogtreecommitdiffstats
path: root/cleanoldmeta
blob: 6655c9561942a82948cf54ccc98fa84822830e93 (plain)
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
#!/usr/bin/php
<?php
define('KEEP', 8);

function clean ($path) {
	if (!is_dir($path) || !is_file("$path/repomd.xml")) {
		echo "** $path is not a repodata directory\n";
		return;
	}
	$path = realpath($path);
	echo "+ Clean old metadata in $path\n";
	$dir = new DirectoryIterator($path);
	$tab = [];
	foreach ($dir as $file) {
		if (!$file->isFile()) {
			continue;
		}
		if (preg_match("/^[^-]*-(.*)$/", $file->getFilename(), $reg)) {
			$n = $reg[1];
			$tab[$n][$file->getMtime()] = $file->getFilename();
		}
	}

	$lim = time() - (26*60*60); // Keep younger than 1d + 2h

	foreach ($tab as $n => $files) {
		if (count($tab[$n]) > KEEP) {
			ksort($tab[$n]);
			foreach($tab[$n] as $t => $f) {
				if (count($tab[$n]) <= KEEP) {
					break; // Honours number limit
				}
				if ($t < $lim && unlink("$path/$f")) {
					$x = array_shift($tab[$n]);
					echo "\t$x\n";
				} else {
					break; // Honours time limit
				}
			}
		}
	}
}

if ($_SERVER['argc'] < 2) {
	die("\nusage {$_SERVER['argv'][0]} path/to/repodata\n");
}

for ($i=1 ; $i<$_SERVER['argc'] ; $i++) {
	clean($_SERVER['argv'][$i]);
}