Difference between
modified post c0da2f by Anonymous on Sun 23rd Sep 2012 14:42
original post b0a8951d by Anonymous on Sat 22nd Sep 2012 15:48
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 | - | use Digest::MD5 qw(md5_hex); | |
| 8 | 7 | use IO::Handle qw(autoflush); | |
| 9 | 8 | use File::Basename qw(basename dirname); | |
| 9 | + | use File::Path qw(make_path remove_tree); | |
| 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 :shared; | |
| 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 | ||
| 57 | - | my $disk_size = 1000000000; | |
| 57 | + | my $tempfs = '/dev/shm/md5sum' . '.' . int(rand(10000)); | |
| 58 | + | my $disk_size = 500000000; | |
| 58 | 59 | ||
| 59 | 60 | # This will be used to control access to the logger subroutine. | |
| 60 | 61 | my $semaphore = Thread::Semaphore->new(); | |
| 61 | 62 | ||
| 62 | 63 | POSIX::sigaction(SIGINT, POSIX::SigAction->new(\&handler)) | |
| 63 | 64 | || die "Error setting SIGINT handler: $!\n"; | |
| 64 | 65 | ||
| 65 | 66 | # Creating a custom POSIX signal handler. | |
| 66 | 67 | # First we create a shared variable that will work as a SIGINT switch. | |
| 67 | 68 | # Then we define the handler subroutine. | |
| 68 | 69 | # If ^C is pressed for the first time, | |
| 69 | 70 | # trip the switch and let the current threads finish. | |
| 70 | 71 | # If ^C is pressed a second time, quit directly. | |
| 71 | 72 | # Each subroutine to be used for starting threads will have to | |
| 72 | 73 | # take notice of the state of the $saw_sigint variable. | |
| 73 | 74 | my $saw_sigint :shared = 0; | |
| 74 | 75 | sub handler { | |
| 75 | - | if ($saw_sigint == 1) { | |
| 76 | + | $saw_sigint = 1; | |
| 80 | 77 | } | |
| 81 | 78 | ||
| 82 | 79 | # Open file handle for the log file | |
| 83 | 80 | open(my $LOG, '>>', $logf) or die "Can't open '$logf': $!"; | |
| 84 | 81 | ||
| 85 | 82 | # Make the $LOG file handle unbuffered for instant logging. | |
| 86 | 83 | $LOG->autoflush(1); | |
| 87 | 84 | ||
| 88 | 85 | # Duplicate STDERR as a regular file handle | |
| 89 | 86 | open(my $SE, ">&STDERR") or die "Can't duplicate STDERR: $!"; | |
| 90 | 87 | ||
| 91 | 88 | ### Subroutine for printing usage instructions | |
| 92 | 89 | sub usage { | |
| 93 | 90 | ||
| 94 | 91 | my $s = basename($0); | |
| 95 | 92 | ||
| 96 | 93 | say <<"HELP" | |
| 97 | 94 | Usage: $s [options] [directory 1] .. [directory N] | |
| 98 | 95 | ||
| 99 | 96 | -help Print this help message. | |
| 100 | 97 | ||
| 101 | 98 | -double Check database for files that have identical | |
| 102 | 99 | hashes. | |
| 103 | 100 | ||
| 104 | 101 | -import Import MD5 sums to the database from already existing | |
| 105 | 102 | \*.MD5 files in each directory. | |
| 106 | 103 | ||
| 107 | 104 | -index Index new files in each directory. | |
| 108 | 105 | ||
| 109 | 106 | -test Test the MD5 sums of the files in the database to see if | |
| 110 | 107 | they've changed. | |
| 111 | 108 | ||
| 112 | 109 | HELP | |
| 113 | 110 | } | |
| 114 | 111 | ||
| 115 | 112 | # This loop goes through the argument list as passed to the script | |
| 116 | 113 | # by the user when ran. | |
| 117 | 114 | foreach my $arg (@ARGV) { | |
| 118 | 115 | ||
| 119 | 116 | # If argument starts with a dash '-', interprete it as an option | |
| 120 | 117 | if ($arg =~ /^-/) { | |
| 121 | 118 | ||
| 122 | 119 | given ($arg) { | |
| 123 | 120 | # When '-double', set script mode to 'double', and call | |
| 124 | 121 | # the md5double subroutine later. | |
| 125 | 122 | when (/^-double$/) { | |
| 126 | 123 | if (!$mode) { push(@cmd, $arg); $mode = 'double'; } | |
| 127 | 124 | } | |
| 128 | 125 | ||
| 129 | 126 | # When '-import', set script mode to 'import', and call | |
| 130 | 127 | # the md5import subroutine later. | |
| 131 | 128 | when (/^-import$/) { | |
| 132 | 129 | if (!$mode) { push(@cmd, $arg); $mode = 'import'; } | |
| 133 | 130 | } | |
| 134 | 131 | ||
| 135 | 132 | # When '-help', set script mode to 'help', and print | |
| 136 | 133 | # usage instructions later. | |
| 137 | 134 | when (/^-help$/) { | |
| 138 | 135 | if (!$mode) { push(@cmd, $arg); $mode = 'help'; } | |
| 139 | 136 | } | |
| 140 | 137 | ||
| 141 | 138 | # When '-index', set script mode to 'index', and call | |
| 142 | 139 | # the md5index subroutine later. | |
| 143 | 140 | when (/^-index$/) { | |
| 144 | 141 | if (!$mode) { push(@cmd, $arg); $mode = 'index'; } | |
| 145 | 142 | } | |
| 146 | 143 | ||
| 147 | 144 | # When '-test', set the script mode to 'test', and call | |
| 148 | 145 | # the md5test subroutine later. | |
| 149 | 146 | when (/^-test$/) { | |
| 150 | 147 | if (!$mode) { push(@cmd, $arg); $mode = 'test'; } | |
| 151 | 148 | } | |
| 152 | 149 | } | |
| 153 | 150 | # If argument is a directory, include it in the @lib array | |
| 154 | 151 | } elsif (-d $arg) { push(@lib, $arg); push(@cmd, $arg); } | |
| 155 | 152 | } | |
| 156 | 153 | ||
| 157 | 154 | # If no switches were used, print usage instructions | |
| 158 | 155 | if (!@lib || !$mode || $mode eq 'help') | |
| 159 | 156 | { usage; exit; } | |
| 160 | 157 | ||
| 161 | 158 | #say "@cmd\n"; | |
| 162 | 159 | ||
| 163 | 160 | # Subroutine for controlling the log file | |
| 164 | 161 | # Applying a semaphore so multiple threads won't try to | |
| 165 | 162 | # access it at once, just in case ;-) | |
| 166 | 163 | sub logger { | |
| 167 | 164 | ||
| 168 | 165 | $semaphore->down(); | |
| 169 | 166 | ||
| 170 | 167 | my($arg, $sw, @fn, $n); | |
| 171 | 168 | ||
| 172 | 169 | # Creating a variable to hold the current time. | |
| 173 | 170 | my $now = localtime(time); | |
| 174 | 171 | ||
| 175 | 172 | # Array of accepted switches to this subroutine | |
| 176 | 173 | my @larg = qw{start int gone corr diff end}; | |
| 177 | 174 | ||
| 178 | 175 | # Loop through all the arguments passed to this subroutine | |
| 179 | 176 | # Perform checks that decide which variable the arguments are to | |
| 180 | 177 | # be assigned to. | |
| 181 | 178 | CHECK: while (@_) { | |
| 182 | 179 | $arg = shift(@_); | |
| 183 | 180 | ||
| 184 | 181 | # If $arg is a switch, set the $sw variable and start | |
| 185 | 182 | # the next iteration of the CHECK loop. | |
| 186 | 183 | foreach (@larg) { | |
| 187 | 184 | if ($_ eq $arg) { $sw = $arg; next CHECK; } | |
| 188 | 185 | } | |
| 189 | 186 | ||
| 190 | 187 | # If $arg is a number assign it to $n, if it's a file | |
| 191 | 188 | # add it to @fn. | |
| 192 | 189 | if ($arg =~ /^[0-9]*$/) { $n = $arg; } | |
| 193 | 190 | else { push(@fn, $arg); } | |
| 194 | 191 | } | |
| 195 | 192 | given ($sw) { | |
| 196 | 193 | # Starts writing the log. | |
| 197 | 194 | when ('start') { | |
| 198 | 195 | say $LOG "\n**** Logging started on $now ****\n"; | |
| 199 | 196 | say $LOG "Running script in '$mode' mode.\n"; | |
| 200 | 197 | } | |
| 201 | 198 | # When the script is interrupted by user pressing ^C, | |
| 202 | 199 | # say so in STDOUT, close the log. | |
| 203 | 200 | when ('int') { | |
| 204 | 201 | say "\nInterrupted by user!\n"; | |
| 205 | 202 | say $LOG $n . " file(s) were tested."; | |
| 206 | 203 | say $LOG "\n**** Logging ended on $now ****\n"; | |
| 207 | 204 | close $LOG or die "Can't close '$LOG': $!"; | |
| 208 | 205 | } | |
| 209 | 206 | # Called when file has been deleted or moved. | |
| 210 | 207 | when ('gone') { | |
| 211 | 208 | say $LOG $fn[0] . "\n\t" . "has been (re)moved.\n"; | |
| 212 | 209 | $err{$fn[0]} = "has been (re)moved.\n"; | |
| 213 | 210 | } | |
| 214 | 211 | # Called when file has been corrupted. | |
| 215 | 212 | when ('corr') { | |
| 216 | 213 | say $LOG $fn[0] . "\n\t" . | |
| 217 | 214 | "has been corrupted.\n"; | |
| 218 | 215 | $err{$fn[0]} = "has been corrupted.\n"; | |
| 219 | 216 | } | |
| 220 | 217 | when ('diff') { | |
| 221 | 218 | say $LOG $fn[0] . "\n\t" . | |
| 222 | 219 | "doesn't match the hash in database.\n"; | |
| 223 | 220 | $err{$fn[0]} = "doesn't match the hash in database.\n"; | |
| 224 | 221 | } | |
| 225 | 222 | # Called when done, and to close the log. | |
| 226 | 223 | # If no errors occurred write "Everything is OK!" to the log. | |
| 227 | 224 | # If errors occurred print the %err hash. | |
| 228 | 225 | # Either way, print number of files processed. | |
| 229 | 226 | when ('end') { | |
| 230 | 227 | if (!%err) { | |
| 231 | 228 | say $LOG "\nEverything is OK!\n"; | |
| 232 | 229 | } else { | |
| 233 | 230 | say "\n**** Errors Occurred ****\n"; | |
| 234 | 231 | foreach my $fn (sort keys %err) { | |
| 235 | 232 | say $SE $fn . "\n\t" . $err{$fn}; | |
| 236 | 233 | } | |
| 237 | 234 | } | |
| 238 | 235 | ||
| 239 | 236 | say $LOG $n . " file(s) were tested.\n" if ($n); | |
| 240 | 237 | say $LOG "\n**** Logging ended on $now ****\n"; | |
| 241 | 238 | close $LOG or die "Can't close '$LOG': $!"; | |
| 242 | 239 | } | |
| 243 | 240 | } | |
| 244 | 241 | $semaphore->up(); | |
| 245 | 242 | } | |
| 246 | 243 | ||
| 247 | 244 | ||
| 248 | 245 | # Subroutine for reading a database file into the database hash. | |
| 249 | 246 | # This is the first subroutine that will be executed and all others | |
| 250 | 247 | # depend upon it, cause without it we don't have a | |
| 251 | 248 | # database hash to work with. | |
| 252 | 249 | sub file2hash { | |
| 253 | 250 | ||
| 254 | 251 | # The format string which is used for parsing the database file | |
| 255 | 252 | my $format = qr/^.*\t\*\t[[:alnum:]]{32}$/; | |
| 256 | 253 | my (@dbfile, @gone); | |
| 257 | 254 | ||
| 258 | 255 | # Open the database file and read it into the @dbfile variable | |
| 259 | 256 | open(MD5DB, '<', $db) or die "Can't open '$db': $!"; | |
| 260 | 257 | chomp (@dbfile = (<MD5DB>)); | |
| 261 | 258 | close(MD5DB) or die "Can't close '$db': $!"; | |
| 262 | 259 | ||
| 263 | 260 | # Loop through all the lines in the database file and split | |
| 264 | 261 | # them before storing in the database hash. | |
| 265 | 262 | # Also, print each line to STDOUT for debug purposes | |
| 266 | 263 | foreach my $line (@dbfile) { | |
| 267 | 264 | ||
| 268 | 265 | # If current line matches the proper database file format, | |
| 269 | 266 | # continue. | |
| 270 | 267 | if ($line =~ /$format/) { | |
| 271 | 268 | ||
| 272 | 269 | my ($fn, $hash) = (split(/\Q$delim/, $line)); | |
| 273 | 270 | ||
| 274 | 271 | # If $fn is a real file and not already in the hash, | |
| 275 | 272 | # continue. | |
| 276 | 273 | if (-f $fn && ! $md5h{$fn}) { | |
| 277 | 274 | $md5h{$fn} = $hash; | |
| 278 | 275 | say "$fn". "$delim" . "$hash"; | |
| 279 | 276 | ||
| 280 | 277 | # Saves the names of deleted or moved files in '@gone' | |
| 281 | 278 | # for printing at the end of this subroutine. | |
| 282 | 279 | } else { push(@gone, $fn); } | |
| 283 | 280 | } | |
| 284 | 281 | } | |
| 285 | 282 | ||
| 286 | 283 | # Clears the screen, thereby scrolling past the database file print | |
| 287 | 284 | print $clear; | |
| 288 | 285 | ||
| 289 | 286 | # Loops through the @gone array and logs every file name | |
| 290 | 287 | # that's been deleted or moved. | |
| 291 | 288 | foreach my $fn (@gone) { logger('gone', $fn); } | |
| 292 | 289 | } | |
| 293 | 290 | ||
| 294 | 291 | # Subroutine for printing the database hash to the database file | |
| 295 | 292 | sub hash2file { | |
| 296 | 293 | ||
| 297 | 294 | open(MD5DB, '>', "$db") or die "Can't open '$db': $!"; | |
| 298 | 295 | # Loops through all the keys in the database hash and prints | |
| 299 | 296 | # the entries (divided by the $delim variable) to the database file. | |
| 300 | 297 | foreach my $k (sort keys %md5h) { | |
| 301 | 298 | say MD5DB $k . $delim . $md5h{$k}; | |
| 302 | 299 | } | |
| 303 | 300 | close(MD5DB) or die "Can't close '$db': $!"; | |
| 304 | 301 | } | |
| 305 | 302 | ||
| 306 | 303 | # Subroutine for finding files | |
| 307 | 304 | # Finds all the files inside the directory name passed to it, | |
| 308 | 305 | # and sorts the output before storing it in the @files array. | |
| 309 | 306 | sub getfiles { | |
| 310 | 307 | ||
| 311 | 308 | my $dn = shift; | |
| 312 | 309 | my @files; | |
| 313 | 310 | ||
| 314 | - | open(FIND, '-|', qq(find -L "$dn" -type f -name "*")) | |
| 311 | + | open(FIND, '-|', qq(find "$dn" -type f -name "*" -nowarn)) | |
| 315 | 312 | or die "Can\'t run 'find': $!"; | |
| 316 | 313 | while (my $fn = (<FIND>)) { | |
| 317 | 314 | chomp($fn); | |
| 318 | 315 | $fn =~ s($dn/)(); | |
| 319 | 316 | push(@files, $fn) if (-f $fn && $fn ne basename($db)); | |
| 320 | 317 | } | |
| 321 | 318 | close(FIND) or die "Can't close 'find': $!"; | |
| 322 | 319 | return @files; | |
| 323 | 320 | } | |
| 324 | 321 | ||
| 325 | 322 | sub md5double { | |
| 326 | 323 | ||
| 327 | 324 | if (!keys(%md5h)) { | |
| 328 | 325 | say "No database file. Run the script in 'index' mode first\n" . | |
| 329 | 326 | "to index the files."; | |
| 330 | 327 | exit; | |
| 331 | 328 | } | |
| 332 | 329 | ||
| 333 | 330 | # Loop through the %md5h hash and save the checksums as keys in a | |
| 334 | 331 | # new hash called %exists. Each of those keys will hold an | |
| 335 | 332 | # anonymous array with the matching file names. | |
| 336 | 333 | my %exists; | |
| 337 | 334 | foreach my $fn (keys(%md5h)) { | |
| 338 | 335 | my $hash = $md5h{$fn}; | |
| 339 | 336 | if (!$exists{"$hash"}) { | |
| 340 | 337 | $exists{"$hash"}->[0] = $fn; | |
| 341 | 338 | } else { | |
| 342 | 339 | push(@{$exists{"$hash"}}, $fn); | |
| 343 | 340 | } | |
| 344 | 341 | } | |
| 345 | 342 | ||
| 346 | 343 | # Loop through the %exists hash and print files that are identical, | |
| 347 | 344 | # if any. | |
| 348 | 345 | foreach my $hash (keys(%exists)) { | |
| 349 | 346 | if (scalar(@{$exists{"$hash"}}) > 1) { | |
| 350 | 347 | say "These files have the same hash (${hash}):"; | |
| 351 | 348 | foreach my $fn (@{$exists{"$hash"}}) { | |
| 352 | 349 | say $fn; | |
| 353 | 350 | } | |
| 354 | 351 | say ""; | |
| 355 | 352 | } | |
| 356 | 353 | } | |
| 357 | 354 | } | |
| 358 | 355 | ||
| 359 | 356 | # Subroutine for finding and parsing *.MD5 files, adding the hashes | |
| 360 | 357 | # to the database hash and thereby also to the file. | |
| 361 | 358 | # It takes 1 argument: | |
| 362 | 359 | # (1) the thread queue | |
| 363 | 360 | sub md5import { | |
| 364 | 361 | ||
| 365 | 362 | my $md5fn = shift; | |
| 366 | 363 | ||
| 367 | 364 | my ($fn, $hash, @fields, @lines); | |
| 368 | 365 | ||
| 369 | 366 | # The format string which is used for parsing the *.MD5 files. | |
| 370 | 367 | my $format = qr/^[[:alnum:]]{32}\s\*.*/; | |
| 371 | 368 | ||
| 372 | 369 | # If the file extension is *.MD5 in either upper- or | |
| 373 | 370 | # lowercase, continue. | |
| 374 | 371 | if ($md5fn =~ /.md5$/i) { | |
| 375 | 372 | ||
| 376 | 373 | # Open the *.MD5 file and read its contents to the | |
| 377 | 374 | # @lines array. | |
| 378 | 375 | open(MD5, '<', $md5fn) or die "Can't open '$md5fn': $!"; | |
| 379 | 376 | chomp(@lines = (<MD5>)); | |
| 380 | 377 | close(MD5) or die "Can't close '$md5fn': $!"; | |
| 381 | 378 | ||
| 382 | 379 | # Loop to check that the format of the *.MD5 file really | |
| 383 | 380 | # is correct before proceeding. | |
| 384 | 381 | foreach my $line (@lines) { | |
| 385 | 382 | ||
| 386 | 383 | # If format string matches the line(s) in the *.MD5 | |
| 387 | 384 | # file, continue. | |
| 388 | 385 | if ($line =~ /$format/) { | |
| 389 | 386 | ||
| 390 | 387 | # Split the line so that the hash and file name go | |
| 391 | 388 | # into @fields array. | |
| 392 | 389 | # After that strip the path (if any) of the file | |
| 393 | 390 | # name, and prepend the path of the *.MD5 file to | |
| 394 | 391 | # it instead. | |
| 395 | 392 | # Store hash and file name in the $hash and $fn | |
| 396 | 393 | # variables for readability. | |
| 397 | 394 | @fields = split(/\s\Q*/, $line, 2); | |
| 398 | 395 | my $path = dirname($md5fn); | |
| 399 | 396 | $hash = $fields[0]; | |
| 400 | 397 | ||
| 401 | 398 | if ($path eq '.') { $fn = basename($fields[1]); } | |
| 402 | 399 | else { $fn = dirname($md5fn) | |
| 403 | 400 | . '/' . basename($fields[1]); } | |
| 404 | 401 | ||
| 405 | 402 | # Convert CR+LF newlines to proper LF to avoid | |
| 406 | 403 | # identical file names from being interpreted as | |
| 407 | 404 | # different. | |
| 408 | 405 | $fn =~ s/\r//; | |
| 409 | 406 | ||
| 410 | 407 | # Unless file name already is in the database hash, | |
| 411 | 408 | # print a message, add it to the hash. | |
| 412 | 409 | if (! $md5h{$fn} && -f $fn) { | |
| 413 | 410 | ||
| 414 | 411 | say "$fn" . "\n\t" . | |
| 415 | 412 | "Imported MD5 sum from '" . | |
| 416 | 413 | basename($md5fn) . | |
| 417 | 414 | "'.\n"; | |
| 418 | 415 | ||
| 419 | 416 | $md5h{$fn} = $hash; | |
| 420 | 417 | ||
| 421 | 418 | # If file name is not a real file, write to | |
| 422 | 419 | # the log. | |
| 423 | 420 | # If file name is in database hash but the | |
| 424 | 421 | # MD5 sum from the MD5 file doesn't match, | |
| 425 | 422 | # print to the log. | |
| 426 | 423 | } elsif (! -f $fn) { logger('gone', $fn); } | |
| 427 | 424 | elsif ($md5h{$fn} ne $hash) | |
| 428 | 425 | { logger('diff', $md5fn); } | |
| 429 | 426 | } | |
| 430 | 427 | } | |
| 431 | 428 | } | |
| 432 | 429 | } | |
| 433 | 430 | ||
| 431 | + | sub iquit { | |
| 434 | 440 | ||
| 435 | 441 | sub md5sum { | |
| 436 | 442 | my $fn = shift; | |
| 437 | 443 | ||
| 438 | 444 | while ($busy) { yield(); } | |
| 439 | 445 | ||
| 440 | 446 | if ($large{$fn}) { | |
| 441 | 447 | lock($busy); | |
| 442 | 448 | $busy = 1; | |
| 443 | - | open(FILE, '<:raw', $fn) or die "Can't open '$fn': $!"; | |
| 449 | + | chomp(my $hash = (split(/\Q */, qx/md5sum -b "$fn"/, 2))[0]); | |
| 446 | 450 | $busy = 0; | |
| 447 | 451 | return $hash; | |
| 448 | 452 | } else { | |
| 453 | - | delete($file_contents{$fn}); | |
| 454 | - | return $hash; | |
| 455 | - | } | |
| 456 | - | } | |
| 457 | 453 | ||
| 458 | - | # Subroutine to index the files | |
| 454 | + | my $tempfn = $tempfs . '/' . quotemeta(basename($fn)) . '.' . | |
| 459 | - | # i.e calculate and store the MD5 sums in the database hash/file. | |
| 455 | + | int(rand(10000)); | |
| 460 | - | # It takes 1 argument: | |
| 456 | + | #say "tempfs: $tempfs"; | |
| 461 | - | # (1) the thread queue | |
| 457 | + | #say "tempfn: $tempfn"; | |
| 462 | - | sub md5index { | |
| 458 | + | if (! -d $tempfs) { | |
| 463 | - | my $q = shift; | |
| 459 | + | make_path($tempfs); | |
| 464 | - | my $tid = threads->tid(); | |
| 460 | + | if (! -d $tempfs) { | |
| 461 | + | die "Couldnt make_path $tempfs: $!"; } | |
| 465 | 463 | ||
| 466 | - | # Loop through the thread que. | |
| 464 | + | my($hash, $md5sum_in); | |
| 476 | 465 | ||
| 477 | - | # If the $saw_sigint variable has been tripped. | |
| 466 | + | my $pid = open($md5sum_in, '|-:raw', | |
| 478 | - | # Quit this 'while' loop, thereby closing the thread. | |
| 467 | + | qq{md5sum -b >${tempfn}}) | |
| 479 | - | if ($saw_sigint == 1) { | |
| 468 | + | or die "Can't open md5sum: $!"; | |
| 480 | - | say "Closing thread: " . $tid; | |
| 469 | + | print $md5sum_in $file_contents{$fn}; | |
| 481 | - | last; | |
| 470 | + | close($md5sum_in); | |
| 482 | - | } | |
| 471 | + | waitpid( $pid, 0 ); | |
| 483 | - | } | |
| 472 | + | if ($? == 2) { iquit(); } | |
| 484 | 473 | ||
| 485 | - | while (!$q->pending() && !$stopping) { | |
| 474 | + | chomp($hash = (split(/\Q */, qx/cat $tempfn/, 2))[0]); | |
| 486 | - | yield(); | |
| 475 | + | say "$fn : $hash"; | |
| 487 | - | goto(LOOP2); | |
| 476 | + | #system(qq{rm -rf $tempfn}); | |
| 477 | + | #if ($? != 0) { die "Couldn't rm '$tempfn': $!"; } | |
| 490 | 478 | ||
| 491 | - | # Subroutine for testing to see if the MD5 sums | |
| 479 | + | lock($file_stack); | |
| 492 | - | # in the database file are correct (i.e. have changed or not). | |
| 480 | + | $file_stack -= length($file_contents{$fn}); | |
| 481 | + | lock(%file_contents); | |
| 493 | 489 | # It takes 1 argument: | |
| 494 | 490 | # (1) the thread queue | |
| 495 | - | sub md5test { | |
| 491 | + | sub md5index { | |
| 496 | 492 | my $q = shift; | |
| 497 | 493 | my $tid = threads->tid(); | |
| 499 | 494 | ||
| 500 | - | # Loop through the thread queue. | |
| 495 | + | # Loop through the thread que. | |
| 501 | - | LOOP: while ((my $fn = $q->dequeue_nb()) || !$stopping) { | |
| 496 | + | LOOP2: while ((my $fn = $q->dequeue_nb()) || !$stopping) { | |
| 502 | 497 | if ($fn) { | |
| 503 | - | $newmd5 = md5sum($fn); | |
| 498 | + | $md5h{$fn} = md5sum($fn); | |
| 504 | - | say "$tid $fn: done testing (${file_stack})"; | |
| 499 | + | #say "$tid $fn: done indexing (${file_stack})"; | |
| 505 | - | $oldmd5 = $md5h{$fn}; | |
| 500 | + | lock($n); | |
| 501 | + | $n++; | |
| 506 | 505 | ||
| 507 | - | # If the new MD5 sum doesn't match the one in the hash, | |
| 506 | + | # If the $saw_sigint variable has been tripped. | |
| 508 | - | # and file doesn't already exist in the %err hash, | |
| 507 | + | # Quit this 'while' loop, thereby closing the thread. | |
| 509 | - | # log it and replace the old MD5 sum in the hash with | |
| 508 | + | if ($saw_sigint == 1) { | |
| 510 | - | # the new one. | |
| 509 | + | iquit(); | |
| 514 | 510 | } | |
| 519 | 511 | } | |
| 520 | - | # If the $saw_sigint variable has been tripped. | |
| 521 | - | # Quit this 'while' loop, thereby closing the thread. | |
| 522 | - | if ($saw_sigint == 1) { | |
| 523 | - | say "Closing thread: " . $tid; | |
| 524 | - | last; | |
| 525 | - | } | |
| 526 | - | } | |
| 512 | + |
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