# Example: $self->_sysseek(147); $self->_syswrite("cl", 4); sub _sysseek { my ($self, $pos) = @_; my $fh = $self->{fh}; my $cur_pos = sysseek($fh, 0, SEEK_CUR); if (!defined $cur_pos) { $cur_pos = ""; } warn "+ $$ $pos $cur_pos" if $$ != PROCESS_ID && $WRITE; while ( 1 ) { $cur_pos = sysseek($fh, $pos, SEEK_SET); if (!defined $cur_pos) { die "system seek error: $!"; } if ($cur_pos == $pos) { last; } $DEBUG = 1; warn "unable to seek to pos $pos (curpos $cur_pos), try again"; } warn "- $$ $pos $cur_pos\n" if $$ != PROCESS_ID && $WRITE; } sub _syswrite { my ($self, $data, $size) = @_; my $fh = $self->{fh}; my $offset = 0; my $length = length($data); if ($length < $size) { $data .= $SEPARATOR x ($size - $length); } flock($fh, LOCK_EX) or die "unable to lock file"; while ($size) { my $pos = sysseek($fh, 0, SEEK_CUR); warn "start to write at pos $pos" if $WRITE; my $written = syswrite $fh, $data, $size, $offset; if (!defined $written) { die "system write error: $!"; } elsif ($written) { $size -= $written; $offset += $written; $pos = sysseek($fh, 0, SEEK_CUR); warn "written $written newpos $pos" if $WRITE; } } flock($fh, LOCK_UN) or die "unable to unlock file"; }