Subversion Repositories checksum-tools

Rev

Rev 4 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #!/usr/bin/php
  2. <?php
  3.  
  4. /*
  5.    Copyright 2020 Daniel Marschall, ViaThinkSoft
  6.  
  7.    Licensed under the Apache License, Version 2.0 (the "License");
  8.    you may not use this file except in compliance with the License.
  9.    You may obtain a copy of the License at
  10.  
  11.        http://www.apache.org/licenses/LICENSE-2.0
  12.  
  13.    Unless required by applicable law or agreed to in writing, software
  14.    distributed under the License is distributed on an "AS IS" BASIS,
  15.    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.    See the License for the specific language governing permissions and
  17.    limitations under the License.
  18. */
  19.  
  20. function testmd5($file) {
  21.         // TODO: warn if an entry is multiple times (with different checksums) in a single file
  22.         if (!file_exists($file)) {
  23.                 echo "ERROR: File $file does not exist.\n";
  24.                 return;
  25.         }
  26.  
  27.         $lines = file($file);
  28.         foreach ($lines as $line) {
  29.                 $line = str_replace("\xEF\xBB\xBF",'',$line);
  30.                 $line = trim($line);
  31.                 if ($line == '') continue;
  32.                 $line = str_replace('*', ' ', $line);
  33.                 $line = str_replace("\t", ' ', $line);
  34.                 list($checksum, $origname) = explode(' ', $line, 2);
  35.                 $origname = dirname($file) . '/' . trim($origname);
  36.                 $checksum = trim($checksum);
  37.                 if (!file_exists($origname)) {
  38.                         echo "WARNING: File vanished : $origname\n";
  39.                 } else {
  40.                         $checksum2 = md5_file($origname);
  41.                         if (strtolower($checksum) != strtolower($checksum2)) {
  42.                                 echo "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n";
  43.                         } else {
  44.                                 //echo "OK: $origname\n";
  45.                         }
  46.                 }
  47.                 // TODO: Also warn about extra files which are not indexed
  48.         }
  49. }
  50.  
  51. function _rec($directory) {
  52.         if (!is_dir($directory)) {
  53.                 exit("Invalid directory path $directory\n");
  54.         }
  55.  
  56.         if ($dont_add_files = count(glob("$directory/*.md5")) == 0) {
  57.                 // echo "Directory $directory has no MD5 file. Skipping.\n";
  58.         } else {
  59.                 $out = array();
  60.  
  61.                 // echo "Check $directory\n";
  62.                 $md5files = glob('*.md5');
  63.                 foreach ($md5files as $md5file) {
  64.                         testmd5($md5file);
  65.                 }
  66.         }
  67.  
  68.         foreach (scandir($directory) as $file) {
  69.                 if ($file !== '.' && $file !== '..') {
  70.                         $file = $directory . '/' . $file;
  71.                         if (is_dir($file)) {
  72.                                 _rec($file);
  73.                         }
  74.                 }
  75.         }
  76. }
  77.  
  78.  
  79. # ---
  80.  
  81. if ($argc != 2) {
  82.         echo "Syntax: $argv[0] <directory>\n";
  83.         exit(2);
  84. }
  85.  
  86. if (!is_dir($argv[1])) {
  87.         echo "Directory not found\n";
  88.         exit(1);
  89. }
  90.  
  91. _rec($argv[1]);
  92.  
  93. echo "Done.\n";