Subversion Repositories checksum-tools

Rev

Rev 15 | 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. // This script generates SFV files
  21. // If there is already an SFV file existing, only new files get appended to the existing SFV files.
  22.  
  23. function _rec($directory) {
  24.         $res = true;
  25.  
  26.         if (!is_dir($directory)) {
  27.                 fwrite(STDERR, "Invalid directory path '$directory'\n");
  28.                 return false;
  29.         }
  30.  
  31.         if ((basename($directory) == '.') || (basename($directory) == '..')) {
  32.                 $sfv_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.
  33.                             basename(realpath($directory)).'.sfv';
  34.  
  35.         } else {
  36.                 $sfv_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.
  37.                             basename($directory).'.sfv';
  38.         }
  39.  
  40.         if (file_exists($sfv_file)) {
  41.                 $existing_files = sfv_get_files($sfv_file);
  42.         } else {
  43.                 $existing_files = array();
  44.         }
  45.  
  46.         $sd = @scandir($directory);
  47.         if ($sd === false) {
  48.                 fwrite(STDERR, "Error: Cannot scan directory $directory\n");
  49.                 return false;
  50.         }
  51.  
  52.         foreach ($sd as $file) {
  53.                 if ($file === '.') continue;
  54.                 if ($file === '..') continue;
  55.                 if (substr($file,0,1) === '.') continue;
  56.                 if (strtolower($file) === 'thumbs.db') continue;
  57.                 if (strtolower(substr($file, -4)) === '.md5') continue;
  58.                 if (strtolower(substr($file, -4)) === '.sfv') continue;
  59.  
  60.                 $fullpath = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file;
  61.                 if (is_dir($fullpath)) {
  62.                         global $do_recursive;
  63.                         if ($do_recursive) _rec($fullpath);
  64.                 } else if (is_file($fullpath)) {
  65.                         global $show_verbose;
  66.                         if ($show_verbose) echo "$fullpath\n";
  67.                         $dir = pathinfo($fullpath, PATHINFO_DIRNAME);
  68.  
  69.                         if (!file_exists($sfv_file)) {
  70.                                 file_put_contents($sfv_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM
  71.                         }
  72.  
  73.                         if (!in_arrayi($file, $existing_files)) {
  74.                                 $hash = crc32_file($fullpath);
  75.                                 if ($hash === false) {
  76.                                         fwrite(STDERR, "Cannot get hash of '$fullpath'\n");
  77.                                         $res = false;
  78.                                 } else {
  79.                                         $crc32 = strtoupper($hash);
  80.                                         file_put_contents($sfv_file, "$file $crc32\r\n", FILE_APPEND);
  81.                                 }
  82.                         }
  83.                 } else {
  84.                         // For some reason, some files on a NTFS volume are "FIFO" pipe files?!
  85.                         fwrite(STDERR, "Warning: $fullpath is not a regular file!\n");
  86.                         $res = false;
  87.                 }
  88.         }
  89.  
  90.         return $res;
  91. }
  92.  
  93. function sfv_get_files($filename) {
  94.         $out = array();
  95.         $lines = file($filename);
  96.         foreach ($lines as $line) {
  97.                 $line = rtrim($line);
  98.                 if ($line == '') continue;
  99.                 if (substr($line,0,1) == ';') continue;
  100.                 $out[] = substr($line, 0, strrpos($line, ' '));
  101.  
  102.         }
  103.         return $out;
  104. }
  105.  
  106. function swapEndianness($hex) {
  107.         return implode('', array_reverse(str_split($hex, 2)));
  108. }
  109.  
  110. function crc32_file($filename, $rawOutput = false) {
  111.         $hash = hash_file('crc32b', $filename, true);
  112.         if ($hash === false) return false;
  113.         $out = bin2hex($hash);
  114.         if (hash('crc32b', 'TEST') == 'b893eaee') {
  115.                 // hash_file() in PHP 5.2 has the wrong Endianess!
  116.                 // https://bugs.php.net/bug.php?id=47467
  117.                 $out = swapEndianness($out);
  118.         }
  119.         return $out;
  120. }
  121.  
  122. function in_arrayi($needle, $haystack) {
  123.         // Source: https://www.php.net/manual/en/function.in-array.php#89256
  124.         return in_array(strtolower($needle), array_map('strtolower', $haystack));
  125. }
  126.  
  127. # ---
  128.  
  129. $show_verbose = false;
  130. $do_recursive = false;
  131. $dirs = array();
  132.  
  133. for ($i=1; $i<$argc; $i++) {
  134.         if ($argv[$i] == '-v') {
  135.                 $show_verbose = true;
  136.         } else if ($argv[$i] == '-r') {
  137.                 $do_recursive = true;
  138.         } else {
  139.                 $dirs[] = $argv[$i];
  140.         }
  141. }
  142.  
  143. if (count($dirs) == 0) {
  144.         echo "Syntax: $argv[0] [-v] [-r] <directory> [<directory> [...]]\n";
  145.         exit(2);
  146. }
  147.  
  148. $res = 0;
  149. foreach ($dirs as $dir) {
  150.         if (!_rec($dir)) $res = 1;
  151. }
  152. if ($show_verbose) echo "Done.\n";
  153. exit($res);
  154.