Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re^5: maddening system call gzip/gunzip problems by Crackers2
in thread maddening system call gzip/gunzip problems by neilwatson

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (6)
    As of 2018-03-18 08:35 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (229 votes). Check out past polls.