Subversion Repositories checksum-tools

Rev

Rev 14 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #!/usr/bin/php
  2. <?php
  3.  
  4. /*
  5.    Copyright 2020-2022 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. // TODO: On Windows file systems, accept file names case insensitively
  21.  
  22. function utf8_normalize($str) {
  23.         // This helps to handle decomposite Unicode endpoints (E.g. German Umlauts have different representations)
  24.         // Requires php-intl
  25.         if (!class_exists('Normalizer')) return $str;
  26.         return Normalizer::normalize($str);
  27. }
  28.  
  29. function convertToUTF8($str) {
  30.         $enc = mb_detect_encoding($str);
  31.         if ($enc && $enc != 'UTF-8') {
  32.                 return iconv($enc, 'UTF-8', $str);
  33.         } else {
  34.                 return $str;
  35.         }
  36. }
  37.  
  38. function testmd5($file) {
  39.         // TODO: warn if an entry is multiple times (with different checksums) in a single file
  40.         if (!file_exists($file)) {
  41.                 fwrite(STDERR, "ERROR: File $file does not exist.\n");
  42.                 return;
  43.         }
  44.  
  45.         $files_checked = array();
  46.  
  47.         $lines = file($file);
  48.         $is_first_line = true;
  49.         $force_utf8 = false;
  50.         foreach ($lines as $line) {
  51.                 if ($is_first_line) {
  52.                         $tmp = 0;
  53.                         $line = str_replace("\xEF\xBB\xBF",'',$line,$tmp);
  54.                         if ($tmp > 0) $force_utf8 = true;
  55.                         $is_first_line = false;
  56.                 }
  57.                 if (!$force_utf8) $line = convertToUTF8($line);
  58.  
  59.                 if (substr(trim($line),0,1) == ';') continue;
  60.  
  61.                 $line = trim($line);
  62.                 if ($line == '') continue;
  63.                 $line = str_replace('*', ' ', $line);
  64.                 $line = str_replace("\t", ' ', $line);
  65.                 list($checksum, $origname) = explode(' ', $line, 2);
  66.                 $origname = dirname($file) . '/' . trim($origname);
  67.                 $checksum = trim($checksum);
  68.                 if (!file_exists($origname)) {
  69.                         fwrite(STDERR, "WARNING: File vanished : $origname\n");
  70.                 } else {
  71.                         if (is_file($origname)) {
  72.                                 $checksum2 = md5_file($origname);
  73.                                 if (strtolower($checksum) != strtolower($checksum2)) {
  74.                                         fwrite(STDERR, "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n");
  75.                                 } else {
  76.                                         global $show_verbose;
  77.                                         if ($show_verbose) echo "OK: $origname\n";
  78.                                 }
  79.                         } else {
  80.                                 // For some reason, some files on a NTFS volume are "FIFO" pipe files?!
  81.                                 fwrite(STDERR, "Warning: $origname is not a regular file!\n");
  82.                         }
  83.                 }
  84.  
  85.                 $origname = utf8_normalize(basename($origname));
  86.                 $files_checked[] = dirname($file) . '/' . $origname;
  87.         }
  88.  
  89.         // Now check if files have vanished!
  90.         $directory = dirname($file);
  91.         $sd = @scandir($directory);
  92.         if ($sd === false) {
  93.                 fwrite(STDERR, "Error: Cannot scan directory $directory\n");
  94.         } else {
  95.                 foreach ($sd as $file) {
  96.                         if ($file === '.') continue;
  97.                         if ($file === '..') continue;
  98.                         if (substr($file,0,1) === '.') continue;
  99.                         if (strtolower($file) === 'thumbs.db') continue;
  100.                         if (strtolower(substr($file, -4)) === '.md5') continue;
  101.                         if (strtolower(substr($file, -4)) === '.sfv') continue;
  102.                         $fullpath = $directory . '/' . $file;
  103.                         if (!is_dir($fullpath)) {
  104.                                 $fullpath = utf8_normalize($fullpath);
  105.                                 if (!in_array($fullpath,$files_checked)) {
  106.                                         fwrite(STDERR, "Warning: File not in SFV checksum file: $fullpath\n");
  107.                                 }
  108.                         }
  109.                 }
  110.         }
  111. }
  112.  
  113. function _rec($directory) {
  114.         $directory = rtrim($directory, '/\\');
  115.  
  116.         if (!is_dir($directory)) {
  117.                 fwrite(STDERR, "Invalid directory path $directory\n");
  118.                 return false;
  119.         }
  120.  
  121.         if ($dont_add_files = count(glob("$directory/*.md5")) == 0) {
  122.                 global $show_verbose;
  123.                 if ($show_verbose) echo "Directory $directory has no MD5 file. Skipping.\n";
  124.         } else {
  125.                 $out = array();
  126.  
  127.                 global $show_verbose;
  128.                 if ($show_verbose) echo "Check directory $directory\n";
  129.                 $md5files = glob($directory.'/*.md5');
  130.                 foreach ($md5files as $md5file) {
  131.                         testmd5($md5file);
  132.                 }
  133.         }
  134.  
  135.         $sd = @scandir($directory);
  136.         if ($sd === false) {
  137.                 fwrite(STDERR, "Error: Cannot scan directory $directory\n");
  138.                 return false;
  139.         }
  140.  
  141.         foreach ($sd as $file) {
  142.                 if ($file !== '.' && $file !== '..') {
  143.                         $file = $directory . '/' . $file;
  144.                         if (is_dir($file)) {
  145.                                 _rec($file);
  146.                         }
  147.                 }
  148.         }
  149.  
  150.         return true;
  151. }
  152.  
  153.  
  154. # ---
  155.  
  156. $show_verbose = false;
  157. $dirs = array();
  158.  
  159. for ($i=1; $i<$argc; $i++) {
  160.         if ($argv[$i] == '-v') {
  161.                 $show_verbose = true;
  162.         } else {
  163.                 $dirs[] = $argv[$i];
  164.         }
  165. }
  166.  
  167. if (count($dirs) == 0) {
  168.         echo "Syntax: $argv[0] [-v] <directory> [<directory> [...]]\n";
  169.         exit(2);
  170. }
  171.  
  172. $res = 0;
  173. foreach ($dirs as $dir) {
  174.         if (!_rec($dir)) $res = 1;
  175. }
  176. if ($show_verbose) echo "Done.\n";
  177. exit($res);
  178.  
  179.