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