Subversion Repositories checksum-tools

Rev

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