Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: maddening system call gzip/gunzip problems

by idsfa (Vicar)
on Dec 07, 2006 at 17:31 UTC ( #588405=note: print w/ replies, xml ) Need Help??


in reply to Re^2: maddening system call gzip/gunzip problems
in thread maddening system call gzip/gunzip problems

You are using readdir() to iterate over all of the files in the directory, but adding files to the directory inside of your loop. Remember that g(un)zip creates a new file to receive the output of the (de)compression. This is why things appear to be getting processed twice.


The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon


Comment on Re^3: maddening system call gzip/gunzip problems
Select or Download Code
Re^4: maddening system call gzip/gunzip problems
by neilwatson (Curate) on Dec 07, 2006 at 18:43 UTC
    I know that system does fork but the parent waits. How can the script continue before the fork of gzip finishes clean up?

    Neil Watson
    watson-wilson.ca

      How can the script continue before the fork of gzip finishes clean up?

      Every successful system call in the loop modifies the contents of the directory, potentially changing what readdir will return on the next loop iteration.

      The problem is that opendir does not just take a snapshot of the current directory. Any files added to the directory after the call to opendir may or may not show up when you iterate over the entries with readdir

      So say you have a.gz,b.gz,c.gz in your directory. You do an opendir and then start iterating.

      The first entry returned will be a.gz, so you call gunzip. This will first create a file a, then remove a.gz.

      Depending on the filesystem, this new file a may now be returned by one of your following readdir calls, even though it was not in the directory when you did the opendir

      Here's a _possible_ way how this could be implemented internally.

      directory MYDIR block: 0: [a.gz] => inode x 1: [b.gz] => inode y 2: [c.gz] => inode z opendir pointer: MYDIR:0

      So a directory has a number of records consisting of a filename and an inode number. When you do an opendir the opendir code will remember what directory it's iterating over, as well as the position it's at. Then after you do a readdir, which returns a.gz, the pointer will point to item 1 instead of item 0.

      Now the gunzip happens, and creates the new file:

      directory MYDIR block: 0: [a.gz] => inode x 1: [b.gz] => inode y 2: [c.gz] => inode z 3: [a] => inode v opendir pointer: MYDIR:1

      Then the gunzip finishes and removes a.gz

      directory MYDIR block: 0: empty 1: [b.gz] => inode y 2: [c.gz] => inode z 3: [a] => inode v opendir pointer: MYDIR:1

      The next readdir will return b.gz and move the pointer to item 2. Then gzip creates a new file for that item..

      directory MYDIR block: 0: [b] => inode h 1: [b.gz] => inode y 2: [c.gz] => inode z 3: [a] => inode v opendir pointer: MYDIR:2

      And after removing the original...

      directory MYDIR block: 0: [b] => inode h 1: empty 2: [c.gz] => inode z 3: [a] => inode v opendir pointer: MYDIR:2

      The next readdir returns c.gz in the same fashion.

      After that however, readdir finds the new item a and returns it, which is what causes your problem. b and c are never returned cause they occupy positions that readdir already processed.

      All this is just a long way of explaining why readdir may or may not return files that get created or deleted while you are iterating over the directory.

        Great answer. Thank you (and thank you to all who added their two cents). I solved this by using two loops. One to create the file list and the second to go through the list and issue the system commands for each item.
        sub gzip { my $cmd = shift; my $dir = shift; my ($file, $fullfile); my @files = (); print "$cmd files in $dir...\n"; # Make sure command is secure and predictable. if ( $cmd eq 'zip' ){ $cmd = 'gzip'; } elsif ( $cmd eq 'unzip' ){ $cmd = 'gunzip'; } else { warn "zip or unzip command not given $!"; } # Generate list of files opendir DIR, $dir or warn "Cannot open $dir $!"; while ( defined ( $file = readdir(DIR) ) ){ $fullfile = "$dir/$file"; next if ( !-f $fullfile || $file =~ m/\.txt$/ || $file !~ m/^[\w-]+(\.[\w-]+)*$/ ); push @files, $fullfile; } closedir DIR; # Using file list from above, issue zip or unzip command. You can +not do # both in the same loop as the readdir may pickup the newly create +d files # and try to process them. Thus you may endup to zipping a zipped + file or # unzipping an upzipped file. while (<@files>) { system ( "$cmd $_") == 0 or warn "Cannot $cmd $fullfile : $!"; } }

        Neil Watson
        watson-wilson.ca

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://588405]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (16)
As of 2014-09-30 14:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (373 votes), past polls