On Fri, Nov 17, 2000 at 03:51:35PM +1100, Steve Smith wrote:
> "Steve" == Steve Smith <[EMAIL PROTECTED]> writes:
> > Hi, Could somebody tell me why the following testcase doesn't work?
> 
> <snip>
> 
> Nevermind, I got it from the archives eventually :
> 
>     seek $f, 0, 0;
      ^^^^

Had a look in Apache::File (below), and it sysopens, so you might want
to sysseek(...) instead.

That seek is flushing the filehandle for you though, here's without the
seek (on Linux, YMMV):

   strace perl -we 'use Fcntl ; sysopen F, "foo", O_RDWR | O_CREAT or die $! ; print F 
"hi1\n" ; sleep 5 ; print F "hi2\n" ; close F'
   [snip]
   open("foo", O_RDWR|O_CREAT|0x8000, 0666) = 3
   fcntl(3, F_GETFL)                       = 0x8002 (flags O_RDWR|0x8000)
   fstat(3, {st_mode=S_IFCHR|S_ISUID|S_ISGID|0463, st_rdev=makedev(79, 18), ...}) =
    0
   mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x400180
   00
   _llseek(0x3, 0, 0, 0xbffff67c, 0x1)     = 0
   fstat(3, {st_mode=S_IFCHR|S_ISUID|S_ISGID|0463, st_rdev=makedev(79, 18), ...}) =
    0
   fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
   time([974438839])                       = 974438839
   SYS_175(0, 0xbffff8d0, 0xbffff850, 0x8, 0) = 0
   SYS_174(0x11, 0, 0xbffff654, 0x8, 0x11) = 0
   SYS_175(0x2, 0xbffff850, 0, 0x8, 0x2)   = 0
   nanosleep(0xbffff7bc, 0xbffff7bc, 0x401791b4, 0xbffff7bc, 0xbffff8d0) = 0
   time([974438844])                       = 974438844
   write(3, "hi1\nhi2\n", 8)               = 8
   close(3)                                = 0

and here's with the seek():

   strace perl -we 'use Fcntl ; sysopen F, "foo", O_RDWR | O_CREAT or die $! ; print F 
"hi1\n" ; seek F, 0, 0 ; sleep 5 ; print F "hi2\n" ; close F'

   open("foo", O_RDWR|O_CREAT|0x8000, 0666) = 3
   fcntl(3, F_GETFL)                       = 0x8002 (flags O_RDWR|0x8000)
   fstat(3, {st_mode=S_IFCHR|S_ISUID|S_ISGID|0463, st_rdev=makedev(79, 18), ...}) = 0
   mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40018000
   _llseek(0x3, 0, 0, 0xbffff66c, 0x1)     = 0
   fstat(3, {st_mode=S_IFCHR|S_ISUID|S_ISGID|0463, st_rdev=makedev(79, 18), ...}) = 0
   fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
   write(3, "hi1\n", 4)                    = 4
   _llseek(0x3, 0, 0, 0xbffff788, 0)       = 0
   time([974438924])                       = 974438924
   SYS_175(0, 0xbffff8c0, 0xbffff840, 0x8, 0) = 0
   SYS_174(0x11, 0, 0xbffff644, 0x8, 0x11) = 0
   SYS_175(0x2, 0xbffff840, 0, 0x8, 0x2)   = 0
   nanosleep(0xbffff7ac, 0xbffff7ac, 0x401791b4, 0xbffff7ac, 0xbffff8c0) = 0
   time([974438929])                       = 974438929
   write(3, "hi2\n", 4)                    = 4
   close(3)                                = 0

Calling syswrite bypasses Perl's buffering, but doesn't address the seek
issue:

   open("foo", O_RDWR|O_CREAT|0x8000, 0666) = 3
   fcntl(3, F_GETFL)                       = 0x8002 (flags O_RDWR|0x8000)
   fstat(3, {st_mode=S_IFCHR|S_ISUID|S_ISGID|0463, st_rdev=makedev(79, 18), ...}) = 0
   mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40018000
   _llseek(0x3, 0, 0, 0xbffff66c, 0x1)     = 0
   fstat(3, {st_mode=S_IFCHR|S_ISUID|S_ISGID|0463, st_rdev=makedev(79, 18), ...}) = 0
   fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
   write(3, "hi1\n", 4)                    = 4
   time([974439066])                       = 974439066
   SYS_175(0, 0xbffff8c0, 0xbffff840, 0x8, 0) = 0
   SYS_174(0x11, 0, 0xbffff644, 0x8, 0x11) = 0
   SYS_175(0x2, 0xbffff840, 0, 0x8, 0x2)   = 0
   nanosleep(0xbffff7ac, 0xbffff7ac, 0x401791b4, 0xbffff7ac, 0xbffff8c0) = 0
   time([974439071])                       = 974439071
   write(3, "hi2\n", 4)                    = 4
   close(3)                                = 0


- Barrie

sub tmpfile {
    my $class = shift;
    my $limit = 100;
    my $r = Apache->request;
    while($limit--) {
        my $tmpfile = "$TMPDIR/${$}" . $TMPNAM++;
        my $fh = $class->new;
        sysopen($fh, $tmpfile, $Mode, $Perms);
        $r->register_cleanup(sub { unlink $tmpfile }) if $r;
        if($fh) {
            return wantarray ? ($tmpfile,$fh) : $fh;
        }
    }
}

Reply via email to