#!/usr/bin/php
<?php
/*
Copyright 2020-2022 Daniel Marschall, ViaThinkSoft
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This script generates MD5 files
// If there is already an MD5 file existing, only new files get appended to the existing MD5 files.
function _rec($directory) {
$res = true;
fwrite(STDERR
, "Invalid directory path '$directory'\n");
return false;
}
$md5_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.
} else {
$md5_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.
}
$existing_files = md5_get_files($md5_file);
} else {
$existing_files = array();
}
if ($sd === false) {
fwrite(STDERR
, "Error: Cannot scan directory $directory\n");
return false;
}
foreach ($sd as $file) {
if ($file === '.') continue;
if ($file === '..') continue;
if (substr($file,0,1) === '.') continue;
$fullpath = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file;
global $do_recursive;
if ($do_recursive) _rec($fullpath);
global $show_verbose;
if ($show_verbose) echo "$fullpath\n";
$dir = pathinfo($fullpath, PATHINFO_DIRNAME
);
}
if (!in_arrayi($file, $existing_files)) {
if ($hash === false) {
fwrite(STDERR
, "Cannot get hash of '$fullpath'\n");
$res = false;
} else {
}
}
} else {
// For some reason, some files on a NTFS volume are "FIFO" pipe files?!
fwrite(STDERR
, "Warning: $fullpath is not a regular file!\n");
$res = false;
}
}
return $res;
}
function md5_get_files($filename) {
$lines = file($filename);
foreach ($lines as $line) {
if ($line == '') continue;
$origname = dirname($filename) . '/' . trim($origname);
$checksum = trim($checksum);
$out[] = $origname;
}
return $out;
}
function in_arrayi($needle, $haystack) {
// Source: https://www.php.net/manual/en/function.in-array.php#89256
}
# ---
$show_verbose = false;
$do_recursive = false;
for ($i=1; $i<$argc; $i++) {
if ($argv[$i] == '-v') {
$show_verbose = true;
} else if ($argv[$i] == '-r') {
$do_recursive = true;
} else {
$dirs[] = $argv[$i];
}
}
echo "Syntax: $argv[0] [-v] [-r] <directory> [<directory> [...]]\n";
}
$res = 0;
foreach ($dirs as $dir) {
if (!_rec($dir)) $res = 1;
}
if ($show_verbose) echo "Done.\n";