Difference between
modified post a63 by Anonymous on Mon 15th Oct 2012 06:13
original post da393 by Anonymous on Thu 11th Oct 2012 04:48
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | #!/usr/bin/perl | |
| 2 | 2 | ||
| 3 | 3 | use 5.14.2; | |
| 4 | 4 | use strict; | |
| 5 | 5 | use warnings; | |
| 6 | 6 | use Cwd qw(abs_path cwd); | |
| 7 | 7 | use Digest::MD5 qw(md5_hex); | |
| 8 | 8 | use IO::Handle qw(autoflush); | |
| 9 | 9 | use File::Basename qw(basename dirname); | |
| 10 | 10 | #use File::Slurp qw(read_file); | |
| 11 | 11 | use diagnostics; | |
| 12 | 12 | ||
| 13 | 13 | use threads qw(yield); | |
| 14 | 14 | use threads::shared; | |
| 15 | 15 | use Thread::Queue; | |
| 16 | 16 | use Thread::Semaphore; | |
| 17 | 17 | #use Fcntl qw(:flock); | |
| 18 | 18 | use POSIX qw(SIGINT); | |
| 19 | 19 | use POSIX qw(ceil); | |
| 20 | 20 | ||
| 21 | 21 | # Create the thread queue. | |
| 22 | 22 | my $q = Thread::Queue->new(); | |
| 23 | 23 | ||
| 24 | 24 | chomp(my $cores = `grep -c ^processor /proc/cpuinfo`); | |
| 25 | 25 | ||
| 26 | 26 | my (@lib, $mode); | |
| 27 | 27 | ||
| 28 | 28 | # Path to and name of log file to be used for logging. | |
| 29 | 29 | my $logf = "$ENV{HOME}/md5db.log"; | |
| 30 | 30 | ||
| 31 | 31 | # Delimiter used for database | |
| 32 | 32 | my $delim = "\t\*\t"; | |
| 33 | 33 | ||
| 34 | 34 | # Array for storing the actual arguments used by the script internally. | |
| 35 | 35 | # Might be useful for debugging. | |
| 36 | 36 | my @cmd = (basename($0)); | |
| 37 | 37 | ||
| 38 | 38 | # Name of database file. | |
| 39 | 39 | my $db = 'md5.db'; | |
| 40 | 40 | ||
| 41 | 41 | # Clear screen command. | |
| 42 | 42 | my $clear = `clear && echo`; | |
| 43 | 43 | ||
| 44 | 44 | # Creating a hash that will store the names of files that are | |
| 45 | 45 | # too big to fit into RAM. We'll process them last. | |
| 46 | 46 | my %large :shared; | |
| 47 | 47 | ||
| 48 | 48 | # Creating a few shared variables. | |
| 49 | 49 | # %err will be used for errors | |
| 50 | 50 | # $n will be used to count the number of files processed | |
| 51 | 51 | # %md5h is the database hash | |
| 52 | 52 | my %err :shared; | |
| 53 | 53 | my $n :shared = 0; | |
| 54 | 54 | my %md5h :shared; | |
| 55 | 55 | my %file_contents :shared; | |
| 56 | 56 | my $stopping :shared = 0; | |
| 57 | 57 | my $file_stack :shared = 0; | |
| 58 | 58 | my $busy :shared = 0; | |
| 59 | 59 | ||
| 60 | 60 | my $disk_size = 1000000000; | |
| 61 | 61 | ||
| 62 | 62 | # This will be used to control access to the logger subroutine. | |
| 63 | 63 | my $semaphore = Thread::Semaphore->new(); | |
| 64 | 64 | ||
| 65 | 65 | POSIX::sigaction(SIGINT, POSIX::SigAction->new(\&handler)) | |
| 66 | 66 | || die "Error setting SIGINT handler: $!\n"; | |
| 67 | 67 | ||
| 68 | 68 | # Creating a custom POSIX signal handler. | |
| 69 | 69 | # First we create a shared variable that will work as a SIGINT switch. | |
| 70 | 70 | # Then we define the handler subroutine. | |
| 71 | 71 | # Each subroutine to be used for starting threads will have to | |
| 72 | 72 | # take notice of the state of the $saw_sigint variable. | |
| 73 | 73 | my $saw_sigint :shared = 0; | |
| 74 | 74 | sub handler { $saw_sigint = 1; } | |
| 75 | 75 | ||
| 76 | 76 | # Open file handle for the log file | |
| 77 | 77 | open(my $LOG, '>>', $logf) or die "Can't open '$logf': $!"; | |
| 78 | 78 | ||
| 79 | 79 | # Make the $LOG file handle unbuffered for instant logging. | |
| 80 | 80 | $LOG->autoflush(1); | |
| 81 | 81 | ||
| 82 | 82 | # Duplicate STDERR as a regular file handle | |
| 83 | 83 | open(my $SE, ">&STDERR") or die "Can't duplicate STDERR: $!"; | |
| 84 | 84 | ||
| 85 | 85 | ### Subroutine for printing usage instructions | |
| 86 | 86 | sub usage { | |
| 87 | 87 | ||
| 88 | 88 | my $s = basename($0); | |
| 89 | 89 | ||
| 90 | 90 | say <<"HELP" | |
| 91 | 91 | Usage: $s [options] [directory 1] .. [directory N] | |
| 92 | 92 | ||
| 93 | 93 | -help Print this help message. | |
| 94 | 94 | ||
| 95 | 95 | -double Check database for files that have identical | |
| 96 | 96 | hashes. | |
| 97 | 97 | ||
| 98 | 98 | -import Import MD5 sums to the database from already existing | |
| 99 | 99 | \*.MD5 files in each directory. | |
| 100 | 100 | ||
| 101 | 101 | -index Index new files in each directory. | |
| 102 | 102 | ||
| 103 | 103 | -test Test the MD5 sums of the files in the database to see if | |
| 104 | 104 | they've changed. | |
| 105 | 105 | ||
| 106 | 106 | HELP | |
| 107 | 107 | } | |
| 108 | 108 | ||
| 109 | 109 | # This loop goes through the argument list as passed to the script | |
| 110 | 110 | # by the user when ran. | |
| 111 | 111 | foreach my $arg (@ARGV) { | |
| 112 | 112 | ||
| 113 | 113 | # If argument starts with a dash '-', interprete it as an option | |
| 114 | 114 | if ($arg =~ /^-/) { | |
| 115 | 115 | ||
| 116 | 116 | given ($arg) { | |
| 117 | 117 | # When '-double', set script mode to 'double', and call | |
| 118 | 118 | # the md5double subroutine later. | |
| 119 | 119 | when (/^-double$/) { | |
| 120 | 120 | if (!$mode) { push(@cmd, $arg); $mode = 'double'; } | |
| 121 | 121 | } | |
| 122 | 122 | ||
| 123 | 123 | # When '-import', set script mode to 'import', and call | |
| 124 | 124 | # the md5import subroutine later. | |
| 125 | 125 | when (/^-import$/) { | |
| 126 | 126 | if (!$mode) { push(@cmd, $arg); $mode = 'import'; } | |
| 127 | 127 | } | |
| 128 | 128 | ||
| 129 | 129 | # When '-help', set script mode to 'help', and print | |
| 130 | 130 | # usage instructions later. | |
| 131 | 131 | when (/^-help$/) { | |
| 132 | 132 | if (!$mode) { push(@cmd, $arg); $mode = 'help'; } | |
| 133 | 133 | } | |
| 134 | 134 | ||
| 135 | 135 | # When '-index', set script mode to 'index', and call | |
| 136 | 136 | # the md5index subroutine later. | |
| 137 | 137 | when (/^-index$/) { | |
| 138 | 138 | if (!$mode) { push(@cmd, $arg); $mode = 'index'; } | |
| 139 | 139 | } | |
| 140 | 140 | ||
| 141 | 141 | # When '-test', set the script mode to 'test', and call | |
| 142 | 142 | # the md5test subroutine later. | |
| 143 | 143 | when (/^-test$/) { | |
| 144 | 144 | if (!$mode) { push(@cmd, $arg); $mode = 'test'; } | |
| 145 | 145 | } | |
| 146 | 146 | } | |
| 147 | 147 | # If argument is a directory, include it in the @lib array | |
| 148 | 148 | } elsif (-d $arg) { | |
| 149 | 149 | my $dn = abs_path($arg); | |
| 150 | 150 | push(@lib, $dn); push(@cmd, $dn); } | |
| 151 | 151 | } | |
| 152 | 152 | ||
| 153 | 153 | # If no switches were used, print usage instructions | |
| 154 | 154 | if (!@lib || !$mode || $mode eq 'help') | |
| 155 | - | { usage; exit; } | |
| 155 | + | { usage(); exit; } | |
| 156 | 156 | ||
| 157 | 157 | #say "@cmd\n"; | |
| 158 | 158 | ||
| 159 | 159 | # This routine is called if something goes wrong and the script needs | |
| 160 | 160 | # to quit prematurely. | |
| 161 | 161 | sub iquit { | |
| 162 | 162 | my $tid = threads->tid(); | |
| 163 | 163 | if ($tid == 1) { | |
| 164 | 164 | ||
| 165 | 165 | # Set the $stopping variable to let the threads know it's time | |
| 166 | 166 | # to stop, and sleep for 1 second so they'll have time to quit. | |
| 167 | 167 | { lock($stopping); | |
| 168 | 168 | $stopping = 1; } | |
| 169 | 169 | ||
| 170 | 170 | sleep(1); | |
| 171 | 171 | ||
| 172 | 172 | # Write the hash to the database file and write to the log. | |
| 173 | 173 | hash2file(); | |
| 174 | 174 | logger('int', $n); | |
| 175 | 175 | ||
| 176 | 176 | # Detaching the threads so Perl will clean up after us. | |
| 177 | 177 | foreach my $t (threads->list()) { $t->detach(); } | |
| 178 | 178 | exit; | |
| 179 | 179 | ||
| 180 | 180 | # If the thread calling this function isn't thread 0/1, yield until | |
| 181 | 181 | # $stopping is set. | |
| 182 | 182 | } elsif ($tid > 1) { while (!$stopping) { yield(); } } | |
| 183 | 183 | } | |
| 184 | 184 | ||
| 185 | 185 | # Subroutine for controlling the log file | |
| 186 | 186 | # Applying a semaphore so multiple threads won't try to | |
| 187 | 187 | # access it at once, just in case ;-) | |
| 188 | 188 | sub logger { | |
| 189 | 189 | ||
| 190 | 190 | $semaphore->down(); | |
| 191 | 191 | ||
| 192 | 192 | my($arg, $sw, @fn, $n); | |
| 193 | 193 | ||
| 194 | 194 | # Creating a variable to hold the current time. | |
| 195 | 195 | my $now = localtime(time); | |
| 196 | 196 | ||
| 197 | 197 | # Array of accepted switches to this subroutine | |
| 198 | 198 | my @larg = qw{start int gone corr diff end}; | |
| 199 | 199 | ||
| 200 | 200 | # Loop through all the arguments passed to this subroutine | |
| 201 | 201 | # Perform checks that decide which variable the arguments are to | |
| 202 | 202 | # be assigned to. | |
| 203 | 203 | CHECK: while (@_) { | |
| 204 | 204 | $arg = shift(@_); | |
| 205 | 205 | ||
| 206 | 206 | # If $arg is a switch, set the $sw variable and start | |
| 207 | 207 | # the next iteration of the CHECK loop. | |
| 208 | 208 | foreach (@larg) { | |
| 209 | 209 | if ($_ eq $arg) { $sw = $arg; next CHECK; } | |
| 210 | 210 | } | |
| 211 | 211 | ||
| 212 | 212 | # If $arg is a number assign it to $n, if it's a file | |
| 213 | 213 | # add it to @fn. | |
| 214 | - | if ($arg =~ /^[0-9]*$/) { $n = $arg; } | |
| 214 | + | if ($arg =~ /^[0-9]+$/) { $n = $arg; } | |
| 215 | 215 | else { push(@fn, $arg); } | |
| 216 | 216 | } | |
| 217 | 217 | given ($sw) { | |
| 218 | 218 | # Starts writing the log. | |
| 219 | 219 | when ('start') { | |
| 220 | 220 | say $LOG "\n**** Logging started on $now ****\n"; | |
| 221 | - | say $LOG "Running script in '$mode' mode.\n"; | |
| 221 | + | say $LOG "Running script in '$mode' mode on:\n"; | |
| 222 | + | foreach my $dn (@lib) { say $LOG $dn; } | |
| 222 | 224 | } | |
| 223 | 225 | # When the script is interrupted by user pressing ^C, | |
| 224 | 226 | # say so in STDOUT, close the log. | |
| 225 | 227 | when ('int') { | |
| 226 | 228 | say "\nInterrupted by user!\n"; | |
| 227 | 229 | say $LOG $n . " file(s) were tested."; | |
| 228 | 230 | say $LOG "\n**** Logging ended on $now ****\n"; | |
| 229 | 231 | close $LOG or die "Can't close '$LOG': $!"; | |
| 230 | 232 | } | |
| 231 | 233 | # Called when file has been deleted or moved. | |
| 232 | 234 | when ('gone') { | |
| 233 | 235 | say $LOG $fn[0] . "\n\t" . "has been (re)moved.\n"; | |
| 234 | 236 | $err{$fn[0]} = "has been (re)moved.\n"; | |
| 235 | 237 | } | |
| 236 | 238 | # Called when file has been corrupted. | |
| 237 | 239 | when ('corr') { | |
| 238 | 240 | say $LOG $fn[0] . "\n\t" . | |
| 239 | 241 | "has been corrupted.\n"; | |
| 240 | 242 | $err{$fn[0]} = "has been corrupted.\n"; | |
| 241 | 243 | } | |
| 242 | 244 | when ('diff') { | |
| 243 | 245 | say $LOG $fn[0] . "\n\t" . | |
| 244 | 246 | "doesn't match the hash in database.\n"; | |
| 245 | 247 | $err{$fn[0]} = "doesn't match the hash in database.\n"; | |
| 246 | 248 | } | |
| 247 | 249 | # Called when done, and to close the log. | |
| 248 | 250 | # If no errors occurred write "Everything is OK!" to the log. | |
| 249 | 251 | # If errors occurred print the %err hash. | |
| 250 | 252 | # Either way, print number of files processed. | |
| 251 | 253 | when ('end') { | |
| 252 | 254 | if (!%err) { | |
| 253 | 255 | say $LOG "\nEverything is OK!\n"; | |
| 254 | 256 | } else { | |
| 255 | 257 | say "\n**** Errors Occurred ****\n"; | |
| 256 | 258 | foreach my $fn (sort keys %err) { | |
| 257 | 259 | say $SE $fn . "\n\t" . $err{$fn}; | |
| 258 | 260 | } | |
| 259 | 261 | } | |
| 260 | 262 | ||
| 261 | 263 | say $LOG $n . " file(s) were tested.\n" if ($n); | |
| 262 | 264 | say $LOG "\n**** Logging ended on $now ****\n"; | |
| 263 | 265 | close $LOG or die "Can't close '$LOG': $!"; | |
| 264 | 266 | } | |
| 265 | 267 | } | |
| 266 | 268 | $semaphore->up(); | |
| 267 | 269 | } | |
| 268 | 270 | ||
| 269 | 271 | ||
| 270 | 272 | # Subroutine for reading a database file into the database hash. | |
| 271 | 273 | # This is the first subroutine that will be executed and all others | |
| 272 | 274 | # depend upon it, cause without it we don't have a | |
| 273 | 275 | # database hash to work with. | |
| 274 | 276 | sub file2hash { | |
| 275 | 277 | ||
| 276 | 278 | my $db = shift; | |
| 277 | 279 | my $dn = shift; | |
| 278 | 280 | ||
| 279 | 281 | # The format string which is used for parsing the database file | |
| 280 | 282 | my $format = qr/^.*\t\*\t[[:alnum:]]{32}$/; | |
| 281 | - | my (@dbfile, @gone); | |
| 283 | + | my (@dbfile, @gone, $md5db_in); | |
| 282 | 284 | ||
| 283 | 285 | # Open the database file and read it into the @dbfile variable | |
| 284 | - | open(MD5DB, '<', $db) or die "Can't open '$db': $!"; | |
| 286 | + | open($md5db_in, '<', $db) or die "Can't open '$db': $!"; | |
| 285 | - | chomp (@dbfile = (<MD5DB>)); | |
| 287 | + | chomp (@dbfile = (<$md5db_in>)); | |
| 286 | - | close(MD5DB) or die "Can't close '$db': $!"; | |
| 288 | + | close($md5db_in) or die "Can't close '$db': $!"; | |
| 287 | 289 | ||
| 288 | 290 | # Loop through all the lines in the database file and split | |
| 289 | 291 | # them before storing in the database hash. | |
| 290 | 292 | # Also, print each line to STDOUT for debug purposes | |
| 291 | 293 | foreach my $line (@dbfile) { | |
| 292 | 294 | ||
| 293 | 295 | # If current line matches the proper database file format, | |
| 294 | 296 | # continue. | |
| 295 | 297 | if ($line =~ /$format/) { | |
| 296 | 298 | ||
| 297 | 299 | # Split the line into relative file name, and MD5 sum. | |
| 298 | 300 | # Also create another variable that contains the absolute | |
| 299 | 301 | # file name. | |
| 300 | 302 | my ($rel_fn, $hash) = (split(/\Q$delim/, $line)); | |
| 301 | 303 | my $abs_fn; | |
| 302 | 304 | if ($dn ne '.') { $abs_fn = "$dn/$rel_fn"; } | |
| 303 | 305 | else { $abs_fn = $rel_fn; } | |
| 304 | 306 | ||
| 305 | - | # If $rel_fn is a real file and not already in the hash, | |
| 307 | + | # If $abs_fn is a real file and not already in the hash, | |
| 306 | 308 | # continue. | |
| 307 | - | if (-f $rel_fn && ! $md5h{$rel_fn}) { | |
| 309 | + | if (-f $abs_fn && ! $md5h{$abs_fn}) { | |
| 312 | 310 | $md5h{$abs_fn} = $hash; | |
| 313 | 311 | say $abs_fn . $delim . $hash; | |
| 314 | 312 | ||
| 315 | 313 | # If the file is in the database hash but the MD5 sum | |
| 316 | 314 | # found in the database doesn't match the one in the hash, | |
| 317 | 315 | # print to the log. | |
| 318 | 316 | # | |
| 319 | 317 | # This will most likely only be the case for any extra | |
| 320 | 318 | # databases that are found in the search path given to | |
| 321 | 319 | # the script. | |
| 322 | 320 | } elsif (-f $abs_fn && $md5h{$abs_fn} ne $hash) { | |
| 323 | 321 | logger('diff', $abs_fn); | |
| 324 | 322 | # Saves the names of deleted or moved files in '@gone' | |
| 325 | 323 | # for printing at the end of this subroutine. | |
| 326 | 324 | } elsif (! -f $abs_fn) { push(@gone, $abs_fn); } | |
| 327 | 325 | } | |
| 328 | 326 | } | |
| 329 | 327 | ||
| 330 | 328 | # Clears the screen, thereby scrolling past the database file print | |
| 331 | 329 | print $clear; | |
| 332 | 330 | ||
| 333 | 331 | # Loops through the @gone array and logs every file name | |
| 334 | 332 | # that's been deleted or moved. | |
| 335 | 333 | foreach my $fn (@gone) { logger('gone', $fn); } | |
| 336 | 334 | } | |
| 337 | 335 | ||
| 338 | 336 | # Subroutine for printing the database hash to the database file | |
| 339 | 337 | sub hash2file { | |
| 340 | 338 | ||
| 341 | - | open(MD5DB, '>', $db) or die "Can't open '$db': $!"; | |
| 339 | + | my $md5db_out; | |
| 340 | + | ||
| 342 | 342 | # Loops through all the keys in the database hash and prints | |
| 343 | 343 | # the entries (divided by the $delim variable) to the database file. | |
| 344 | 344 | foreach my $k (sort(keys(%md5h))) { | |
| 345 | - | say MD5DB $k . $delim . $md5h{$k}; | |
| 345 | + | say $md5db_out $k . $delim . $md5h{$k}; | |
| 346 | 346 | } | |
| 347 | - | close(MD5DB) or die "Can't close '$db': $!"; | |
| 347 | + | close($md5db_out) or die "Can't close '$db': $!"; | |
| 348 | 348 | } | |
| 349 | 349 | ||
| 350 | 350 | sub init_hash { | |
| 351 | 351 | my $dn = shift; | |
| 352 | 352 | ||
| 353 | 353 | # Get all the file names in the path. | |
| 354 | 354 | my($files, $md5dbs) = getfiles($dn); | |
| 355 | 355 | ||
| 356 | 356 | # But first import hashes from any databases found | |
| 357 | 357 | # in the search path to avoid re-hashing them. | |
| 358 | 358 | if (@{$md5dbs}) { | |
| 359 | 359 | foreach my $db (@{$md5dbs}) { | |
| 360 | 360 | my $dn = dirname($db); | |
| 361 | 361 | file2hash($db, $dn); | |
| 362 | 362 | } | |
| 363 | 363 | } | |
| 364 | 364 | return($files, $md5dbs); | |
| 365 | 365 | } | |
| 366 | 366 | ||
| 367 | 367 | # Subroutine for finding files | |
| 368 | 368 | # Finds all the files inside the directory name passed to it, | |
| 369 | 369 | # and sorts the output before storing it in the @files array. | |
| 370 | 370 | sub getfiles { | |
| 371 | 371 | ||
| 372 | 372 | my $dn = shift; | |
| 373 | 373 | my(@files, @md5dbs); | |
| 374 | 374 | ||
| 375 | 375 | open(FIND, '-|', 'find', $dn, '-type', 'f', '-name', '*', '-nowarn') | |
| 376 | 376 | or die "Can't run 'find': $!"; | |
| 377 | 377 | while (my $fn = (<FIND>)) { | |
| 378 | 378 | chomp($fn); | |
| 379 | 379 | $fn =~ s($dn/)(); | |
| 380 | 380 | if (-f $fn && basename($fn) ne $db) { | |
| 381 | 381 | push(@files, $fn); | |
| 382 | 382 | } elsif (-f $fn && basename($fn) eq $db) { | |
| 383 | 383 | push(@md5dbs, $fn); | |
| 384 | 384 | } | |
| 385 | 385 | } | |
| 386 | 386 | close(FIND) or die "Can't close 'find': $!"; | |
| 387 | 387 | return(\@files, \@md5dbs); | |
| 388 | 388 | } | |
| 389 | 389 | ||
| 390 | 390 | sub md5double { | |
| 391 | 391 | ||
| 392 | 392 | if (!keys(%md5h)) { | |
| 393 | 393 | say "No database file. Run the script in 'index' mode first\n" . | |
| 394 | 394 | "to index the files."; | |
| 395 | 395 | exit; | |
| 396 | 396 | } | |
| 397 | 397 | ||
| 398 | 398 | # Loop through the %md5h hash and save the checksums as keys in a | |
| 399 | 399 | # new hash called %exists. Each of those keys will hold an | |
| 400 | 400 | # anonymous array with the matching file names. | |
| 401 | 401 | my %exists; | |
| 402 | 402 | foreach my $fn (keys(%md5h)) { | |
| 403 | 403 | my $hash = $md5h{$fn}; | |
| 404 | 404 | if (!$exists{${hash}}) { | |
| 405 | 405 | $exists{${hash}}->[0] = $fn; | |
| 406 | 406 | } else { | |
| 407 | 407 | push(@{$exists{${hash}}}, $fn); | |
| 408 | 408 | } | |
| 409 | 409 | } | |
| 410 | 410 | ||
| 411 | 411 | # Loop through the %exists hash and print files that are identical, | |
| 412 | 412 | # if any. | |
| 413 | 413 | foreach my $hash (keys(%exists)) { | |
| 414 | 414 | if (@{$exists{${hash}}} > 1) { | |
| 415 | 415 | say "These files have the same hash (${hash}):"; | |
| 416 | 416 | foreach my $fn (@{$exists{${hash}}}) { | |
| 417 | 417 | say $fn; | |
| 418 | 418 | } | |
| 419 | 419 | say ""; | |
| 420 | 420 | } | |
| 421 | 421 | } | |
| 422 | 422 | } | |
| 423 | 423 | ||
| 424 | 424 | # Subroutine for finding and parsing *.MD5 files, adding the hashes | |
| 425 | 425 | # to the database hash and thereby also to the file. | |
| 426 | 426 | # It takes 1 argument: | |
| 427 | 427 | # (1) file name | |
| 428 | 428 | sub md5import { | |
| 429 | 429 | ||
| 430 | 430 | my $md5fn = shift; | |
| 431 | 431 | ||
| 432 | 432 | my ($fn, $hash, @fields, @lines); | |
| 433 | 433 | ||
| 434 | 434 | # The format string which is used for parsing the *.MD5 files. | |
| 435 | 435 | my $format = qr/^[[:alnum:]]{32}\s\*.*/; | |
| 436 | 436 | ||
| 437 | 437 | # If the file extension is *.MD5 in either upper- or | |
| 438 | 438 | # lowercase, continue. | |
| 439 | 439 | if ($md5fn =~ /.md5$/i) { | |
| 440 | 440 | ||
| 441 | 441 | # Open the *.MD5 file and read its contents to the | |
| 442 | 442 | # @lines array. | |
| 443 | 443 | open(MD5, '<', $md5fn) or die "Can't open '$md5fn': $!"; | |
| 444 | 444 | chomp(@lines = (<MD5>)); | |
| 445 | 445 | close(MD5) or die "Can't close '$md5fn': $!"; | |
| 446 | 446 | ||
| 447 | 447 | # Loop to check that the format of the *.MD5 file really | |
| 448 | 448 | # is correct before proceeding. | |
| 449 | 449 | foreach my $line (@lines) { | |
| 450 | 450 | ||
| 451 | 451 | # If format string matches the line(s) in the *.MD5 | |
| 452 | 452 | # file, continue. | |
| 453 | 453 | if ($line =~ /$format/) { | |
| 454 | 454 | ||
| 455 | 455 | # Split the line so that the hash and file name go | |
| 456 | 456 | # into @fields array. | |
| 457 | 457 | # After that strip the path (if any) of the file | |
| 458 | 458 | # name, and prepend the path of the *.MD5 file to | |
| 459 | 459 | # it instead. | |
| 460 | 460 | # Store hash and file name in the $hash and $fn | |
| 461 | 461 | # variables for readability. | |
| 462 | 462 | @fields = split(/\s\Q*/, $line, 2); | |
| 463 | 463 | my $path = dirname($md5fn); | |
| 464 | 464 | $hash = $fields[0]; | |
| 465 | 465 | ||
| 466 | 466 | if ($path eq '.') { $fn = basename($fields[1]); } | |
| 467 | 467 | else { $fn = dirname($md5fn) | |
| 468 | 468 | . '/' . basename($fields[1]); } | |
| 469 | 469 | ||
| 470 | 470 | # Convert CR+LF newlines to proper LF to avoid | |
| 471 | 471 | # identical file names from being interpreted as | |
| 472 | 472 | # different. | |
| 473 | 473 | $fn =~ s/\r//; | |
| 474 | 474 | ||
| 475 | 475 | # Unless file name already is in the database hash, | |
| 476 | 476 | # print a message, add it to the hash. | |
| 477 | 477 | if (! $md5h{$fn} && -f $fn) { | |
| 478 | 478 | ||
| 479 | 479 | say "$fn" . "\n\t" . | |
| 480 | 480 | "Imported MD5 sum from '" . | |
| 481 | 481 | basename($md5fn) . | |
| 482 | 482 | "'.\n"; | |
| 483 | 483 | ||
| 484 | 484 | $md5h{$fn} = $hash; | |
| 485 | 485 | ||
| 486 | 486 | # If file name is not a real file, write to | |
| 487 | 487 | # the log. | |
| 488 | 488 | # If file name is in database hash but the | |
| 489 | 489 | # MD5 sum from the MD5 file doesn't match, | |
| 490 | 490 | # print to the log. | |
| 491 | 491 | } elsif (! -f $fn) { logger('gone', $fn); } | |
| 492 | 492 | elsif ($md5h{$fn} ne $hash) | |
| 493 | 493 | { logger('diff', $md5fn); } | |
| 494 | 494 | } | |
| 495 | 495 | } | |
| 496 | 496 | } | |
| 497 | 497 | } | |
| 498 | 498 | ||
| 499 | 499 | ||
| 500 | 500 | sub md5sum { | |
| 501 | 501 | my $fn = shift; | |
| 502 | 502 | my $hash; | |
| 503 | 503 | ||
| 504 | 504 | while ($busy) { yield(); } | |
| 505 | 505 | ||
| 506 | 506 | if ($large{$fn}) { | |
| 507 | 507 | lock($busy); | |
| 508 | 508 | $busy = 1; | |
| 509 | - | open(FILE, '<:raw', $fn) or die "Can't open '$fn': $!"; | |
| 509 | + | my $read_fn; | |
| 510 | - | $hash = Digest::MD5->new->addfile("FILE")->hexdigest; | |
| 510 | + | open($read_fn, '<:raw', $fn) or die "Can't open '$fn': $!"; | |
| 511 | - | close(FILE) or die "Can't close '$fn': $!"; | |
| 511 | + | $hash = Digest::MD5->new->addfile($read_fn)->hexdigest; | |
| 512 | + | close($read_fn) or die "Can't close '$fn': $!"; | |
| 512 | 513 | $busy = 0; | |
| 513 | 514 | } else { | |
| 514 | 515 | $hash = md5_hex($file_contents{$fn}); | |
| 515 | 516 | ||
| 516 | 517 | { lock($file_stack); | |
| 517 | 518 | $file_stack -= length($file_contents{$fn}); } | |
| 518 | 519 | ||
| 519 | 520 | { lock(%file_contents); | |
| 520 | 521 | delete($file_contents{$fn}); } | |
| 521 | 522 | } | |
| 522 | 523 | ||
| 523 | 524 | return $hash; | |
| 524 | 525 | } | |
| 525 | 526 | ||
| 526 | 527 | # Subroutine to index the files | |
| 527 | 528 | # i.e calculate and store the MD5 sums in the database hash/file. | |
| 528 | 529 | sub md5index { | |
| 529 | 530 | my $tid = threads->tid(); | |
| 530 | 531 | ||
| 531 | 532 | # Loop through the thread que. | |
| 532 | 533 | LOOP2: while ((my $fn = $q->dequeue_nb()) || !$stopping) { | |
| 533 | 534 | if (!$fn) { yield(); next; } | |
| 534 | 535 | ||
| 535 | 536 | $md5h{$fn} = md5sum($fn); | |
| 536 | 537 | say "$tid $fn: done indexing (${file_stack})"; | |
| 537 | 538 | ||
| 538 | 539 | { lock($n); | |
| 539 | 540 | $n++; } | |
| 540 | 541 | ||
| 541 | 542 | # If the $saw_sigint variable has been tripped. | |
| 542 | 543 | # Quit this 'while' loop, thereby closing the thread. | |
| 543 | 544 | if ($saw_sigint == 1) { | |
| 544 | 545 | say "Closing thread: " . $tid; | |
| 545 | 546 | iquit(); | |
| 546 | 547 | } | |
| 547 | 548 | } | |
| 548 | 549 | ||
| 549 | 550 | while (!$q->pending() && !$stopping) { | |
| 550 | 551 | yield(); | |
| 551 | 552 | goto(LOOP2); | |
| 552 | 553 | } | |
| 553 | 554 | } | |
| 554 | 555 | ||
| 555 | 556 | # Subroutine for testing to see if the MD5 sums | |
| 556 | 557 | # in the database file are correct (i.e. have changed or not). | |
| 557 | 558 | sub md5test { | |
| 558 | 559 | my $tid = threads->tid(); | |
| 559 | 560 | my ($oldmd5, $newmd5); | |
| 560 | 561 | ||
| 561 | 562 | # Loop through the thread queue. | |
| 562 | 563 | LOOP: while ((my $fn = $q->dequeue_nb()) || !$stopping) { | |
| 563 | 564 | if (!$fn) { yield(); next; } | |
| 564 | 565 | ||
| 565 | 566 | $newmd5 = md5sum($fn); | |
| 566 | 567 | say "$tid $fn: done testing (${file_stack})"; | |
| 567 | 568 | $oldmd5 = $md5h{$fn}; | |
| 568 | 569 | ||
| 569 | 570 | # If the new MD5 sum doesn't match the one in the hash, | |
| 570 | 571 | # and file doesn't already exist in the %err hash, | |
| 571 | 572 | # log it and replace the old MD5 sum in the hash with | |
| 572 | 573 | # the new one. | |
| 573 | 574 | if ($newmd5 ne $oldmd5 && ! $err{$fn}) { | |
| 574 | 575 | logger('diff', $fn); | |
| 575 | 576 | $md5h{$fn} = $newmd5; | |
| 576 | 577 | } | |
| 577 | 578 | ||
| 578 | 579 | { lock($n); | |
| 579 | 580 | $n++; } | |
| 580 | 581 | ||
| 581 | 582 | # If the $saw_sigint variable has been tripped. | |
| 582 | 583 | # Quit this 'while' loop, thereby closing the thread. | |
| 583 | 584 | if ($saw_sigint == 1) { | |
| 584 | 585 | say "Closing thread: " . $tid; | |
| 585 | 586 | iquit(); | |
| 586 | 587 | } | |
| 587 | 588 | } | |
| 588 | 589 | ||
| 589 | 590 | while (!$q->pending() && !$stopping) { | |
| 590 | 591 | yield(); | |
| 591 | 592 | goto(LOOP); | |
| 592 | 593 | } | |
| 593 | 594 | } | |
| 594 | 595 | ||
| 595 | 596 | sub md5flac { | |
| 596 | 597 | my $fn = shift; | |
| 597 | 598 | my (@req, $hash); | |
| 598 | 599 | ||
| 599 | 600 | if ($fn =~ /.flac$/i) { | |
| 600 | 601 | ||
| 601 | 602 | if (! @req) { | |
| 602 | 603 | chomp(@req = ( `which flac metaflac 2>&-` )); | |
| 603 | 604 | ||
| 604 | 605 | if (! $req[0] || ! $req[1]) { | |
| 605 | 606 | say "You need both 'flac' and 'metaflac' to test FLAC files!\n" . | |
| 606 | 607 | "Using normal test method...\n"; | |
| 607 | 608 | @req = '0'; | |
| 608 | 609 | return; | |
| 609 | 610 | } | |
| 610 | 611 | } | |
| 611 | 612 | ||
| 612 | 613 | unless ($req[0] = '0') { | |
| 613 | 614 | chomp($hash = `metaflac --show-md5sum "$fn" 2>&-`); | |
| 614 | 615 | if ($? != 0 && $? != 2) { logger('corr', $fn); return; } | |
| 615 | 616 | ||
| 616 | 617 | system('flac', '--totally-silent', '--test', "$fn"); | |
| 617 | 618 | if ($? != 0 && $? != 2) { logger('corr', $fn); return; } | |
| 618 | 619 | ||
| 619 | 620 | return $hash; | |
| 620 | 621 | } | |
| 621 | 622 | } | |
| 622 | 623 | } | |
| 623 | 624 | ||
| 624 | 625 | # Depending on which script mode is active, | |
| 625 | 626 | # set the @run array to the correct arguments. | |
| 626 | 627 | # This will be used to start the threads later. | |
| 627 | 628 | my @run; | |
| 628 | 629 | given ($mode) { | |
| 629 | 630 | when ('index') { | |
| 630 | 631 | @run = (\&md5index); | |
| 631 | 632 | } | |
| 632 | 633 | when ('test') { | |
| 633 | 634 | @run = (\&md5test); | |
| 634 | 635 | } | |
| 635 | 636 | } | |
| 636 | 637 | ||
| 637 | 638 | # If script mode is either 'import' or 'double' we'll start only | |
| 638 | 639 | # one thread, else we'll start as many as the available number of CPUs. | |
| 639 | 640 | my @threads; | |
| 640 | 641 | if ($mode ne 'import' && $mode ne 'double') { | |
| 641 | 642 | foreach (1 .. $cores) { | |
| 642 | 643 | START_THREADS: push(@threads, threads->create(@run)); | |
| 643 | 644 | } | |
| 644 | 645 | } | |
| 645 | 646 | ||
| 646 | 647 | # This loop is where the actual action takes place | |
| 647 | 648 | # (i.e. where all the subroutines get called from) | |
| 648 | 649 | foreach my $dn (@lib) { | |
| 649 | 650 | if (-d $dn) { | |
| 650 | 651 | ||
| 651 | 652 | # Change directory to $dn. | |
| 652 | 653 | chdir($dn) | |
| 653 | 654 | or die "Can't change directory to '$dn': $!"; | |
| 654 | 655 | ||
| 655 | 656 | # Start logging. | |
| 656 | 657 | logger('start'); | |
| 657 | 658 | ||
| 658 | 659 | # Initialize the database hash, and the files array. | |
| 659 | 660 | # The init_hash function returns references. | |
| 660 | 661 | my($files, $md5dbs) = init_hash($dn); | |
| 661 | 662 | ||
| 662 | 663 | given ($mode) { | |
| 663 | 664 | when ('double') { | |
| 664 | 665 | # Find identical files in database. | |
| 665 | 666 | md5double(); | |
| 666 | 667 | } | |
| 667 | 668 | when ('import') { | |
| 668 | 669 | ||
| 669 | 670 | # For all the files in $dn, run md5import. | |
| 670 | 671 | foreach my $fn (@{$files}) { md5import($fn); } | |
| 671 | 672 | } | |
| 672 | 673 | when ('index') { | |
| 673 | 674 | ||
| 674 | 675 | foreach my $fn (@{$files}) { | |
| 675 | 676 | if ($saw_sigint) { iquit(); } | |
| 676 | 677 | while ($file_stack >= $disk_size) { | |
| 677 | 678 | my $active = threads->running(); | |
| 678 | 679 | say("${active}: $file_stack > $disk_size"); | |
| 679 | 680 | yield(); | |
| 680 | 681 | } | |
| 681 | 682 | # Unless file name exists in the database hash, | |
| 682 | 683 | # continue. | |
| 683 | 684 | unless ($md5h{$fn}) { | |
| 684 | 685 | my $size = (stat($fn))[7]; | |
| 685 | 686 | if ($size && $size < $disk_size) { | |
| 686 | 687 | ||
| 687 | 688 | open(FILE, '<:raw', $fn) or die "Can't open '$fn': $!"; | |
| 688 | 689 | sysread(FILE, $file_contents{$fn}, $size); | |
| 689 | 690 | close(FILE) or die "Can't close '$fn': $!"; | |
| 690 | 691 | ||
| 691 | 692 | { lock($file_stack); | |
| 692 | 693 | $file_stack += length($file_contents{$fn}); } | |
| 693 | 694 | ||
| 694 | 695 | $q->enqueue($fn); | |
| 695 | 696 | } elsif ($size) { | |
| 696 | 697 | $large{$fn} = 1; | |
| 697 | 698 | } | |
| 698 | 699 | } | |
| 699 | 700 | } | |
| 700 | 701 | } | |
| 701 | 702 | when ('test') { | |
| 702 | 703 | ||
| 703 | 704 | # Fetch all the keys for the database hash and put | |
| 704 | 705 | # them in the queue. | |
| 705 | 706 | foreach my $fn (sort(keys(%md5h))) { | |
| 706 | 707 | if ($saw_sigint) { iquit(); } | |
| 707 | 708 | while ($file_stack >= $disk_size) { | |
| 708 | 709 | say("$file_stack > $disk_size"); | |
| 709 | 710 | yield(); | |
| 710 | 711 | } | |
| 711 | 712 | ||
| 712 | 713 | my $size = (stat($fn))[7]; | |
| 713 | 714 | if ($size && $size < $disk_size) { | |
| 714 | 715 | ||
| 715 | 716 | open(FILE, '<:raw', $fn) or die "Can't open '$fn': $!"; | |
| 716 | 717 | sysread(FILE, $file_contents{$fn}, $size); | |
| 717 | 718 | close(FILE) or die "Can't close '$fn': $!"; | |
| 718 | 719 | ||
| 719 | 720 | { lock($file_stack); | |
| 720 | 721 | $file_stack += length($file_contents{$fn}); } | |
| 721 | 722 | ||
| 722 | 723 | $q->enqueue($fn); | |
| 723 | 724 | } elsif ($size) { | |
| 724 | 725 | $large{$fn} = 1; | |
| 725 | 726 | } | |
| 726 | 727 | } | |
| 727 | 728 | } | |
| 728 | 729 | } | |
| 729 | 730 | ||
| 730 | 731 | if (%large) { | |
| 731 | 732 | while ($file_stack > 0) { | |
| 732 | 733 | say("$file_stack > 0"); | |
| 733 | 734 | yield(); | |
| 734 | 735 | } | |
| 735 | 736 | foreach my $fn (sort(keys(%large))) { | |
| 736 | 737 | $q->enqueue($fn); | |
| 737 | 738 | } | |
| 738 | 739 | } | |
| 739 | 740 | ||
| 740 | 741 | # use Digest::MD5; | |
| 741 | 742 | # $md5 = Digest::MD5->new; | |
| 742 | 743 | # $md5->add('foo', 'bar'); | |
| 743 | 744 | # $md5->add('baz'); | |
| 744 | 745 | # $digest = $md5->hexdigest; | |
| 745 | 746 | # print "Digest is $digest\n"; | |
| 746 | 747 | ||
| 747 | 748 | { lock($stopping); | |
| 748 | 749 | $stopping = 1; } | |
| 749 | 750 | ||
| 750 | 751 | foreach my $t (threads->list()) { $t->join(); } | |
| 751 | 752 | #say("All threads joined"); | |
| 752 | 753 | ||
| 753 | 754 | # Print the hash to the database file and close the log | |
| 754 | 755 | hash2file(); | |
| 755 | 756 | logger('end', $n); | |
| 756 | 757 | } | |
| 757 | 758 | } | |
| 758 | 759 |
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
kpaste.net RSS