All pastes #830688 Raw Edit

Stuff

public text v1 · immutable
#830688 ·published 2007-12-24 04:33 UTC
rendered paste body
<?php

echo "\nsfvcheck, (c) 2007 lily.\n";

error_reporting(0);

function err($err) {
	echo "usage: $argv[0] operation directory sfvfile\n";
	echo "\toperation: (verify|generate)\n";
	echo "\tdirectory: target directory\n";
	echo "\tsfvfile: name of sfvfile inside directory\n";
	echo "\n$err\n";
	die();
	}

$op = $argv[1];
$dir = $argv[2];
$sfv = $argv[3];

if ($sfv == "") {
	if ($dir == "") {
		if ($op == "") {
			err("Error: no op");
			}
		err("Err: no directory");
	}
	err("Err: no sfv file");
}

// Get list of files in the target directory.
$files = array_values(array_diff(glob($dir . "/*"), glob($dir . "/*", GLOB_ONLYDIR)));



// Bunch of looping over arrays. Figures.

// remove directory prefix
foreach ($files as $key => $value) {
	$files[$key] = substr($value, strlen($dir . "/*") - 1);
	}
	
// Remove the sfv file from the list of files
$files = array_diff($files, array($sfv));

if ($op == "verify") {

$list = file($dir . "/" . $sfv);

// Process the file into arrays
foreach ($list as $key => $value) {
	if (substr($value, 0, 1) != ";") {
		$list[$key] = array_diff(explode(" ", $value), array(""));
		}
	else { $list[$key] = ""; }
	}

// Why the hell is this here? No idea. Maybe we'll take it out later
$list = array_diff($list, array(""));

// Look for file from $list in $file. If its there, make that value an array with the filename and the stored crc...
foreach ($list as $key => $value) {
	if (array_search($value[0], $files)) { 
		$files[array_search($value[0], $files)] = 
			array(
				$files[array_search($value[0], $files)], 
				substr($value[1], 0, strlen($value[1]) - 1)); }
			}

// and if it isn't, store the crc as -1
foreach ($files as $key => $value) {
	if (!is_array($value)) {
		$files[$key] = array($value, -1);
		}
	}
	
// calculate the actual crc of the file
foreach ($files as $key => $value) {
	$files[$key] = array_merge($value, array(hash_file("crc32", $dir . "/" . $value[0])));
}
	
foreach ($files as $key => $value) {
	if ($value[1] != $value[2]) {
		if ($value[1] == -1) {
			echo "No CRC stored for $value[0] (calculated $value[2])\n";
			$no_stored++;
			}
		else {
			echo "Bad CRC in $value[0]: stored $value[1], calculated $value[2]\n";
			$bad_crc++;
			}
		}
	else {
		echo "OK CRC in $value[0] ($value[1])\n";
		$ok_crc++;
		}
	}
	
	$bad_crc = isset($bad_crc) ? $bad_crc : 0;

	echo "Summary: $bad_crc bad, $ok_crc ok, $no_stored not stored\n";
	
	
}

else if ($op == "generate") {

foreach ($files as $key => $value) {
	$out .= $value .  " " . hash_file("crc32", $dir . "/" . $value) . "\n";
	$num++;
	}
	
file_put_contents($dir . "/" . $sfv, $out);

echo "Wrote SFV for $num files to " . $dir . "/" . $sfv . "\n";

}
	
?>