Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi,

according to the Linux man page of fsync(2):

Calling fsync() does not necessarily ensure that the entry in the directory containing the file has also reached disk. For that an explicit fsync() on a file descriptor for the directory is also needed.

So, how do we fsync directories in Perl? perldoc POSIX says that fsync() should be done with the sync method in IO::Handle. So I tried this with a directory:

use IO::Handle; open my $f, '/tmp' or die $!; $f->sync or die "fsync: $!\n";

This bails out with "fsync: Invalid argument". According to strace, no fsync syscall is even tried.

I tried opendir too:

use IO::Handle; my $f = IO::Handle->new; opendir $f, '/tmp' or die $!; $f->sync or die "fsync: $!\n";

with the same result.

On the other hand, an equivalent C program succeeds and properly calls fsync according to strace:

#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> int main (void) { int fd = open("/tmp", 0); if (fd < 0) { perror("open: /tmp"); exit(1); } if (fsync(fd) < 0) { perror("fsync: /tmp"); exit(1); } return 0; }

My question: What is the proper way to do fsync with directories in Perl? Or is this just a bug in IO::Handle?

(All tests have been done on a current Debian Squeeze (testing) with perl 5.10.1-12.)


In reply to fsyncing directories by betterworld

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-29 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found