1 package DBM::Deep::File;
6 use warnings FATAL => 'all';
8 use Fcntl qw( :DEFAULT :flock :seek );
10 use constant DEBUG => 0;
25 #XXX Migrate this to the engine, where it really belongs.
26 filter_store_key => undef,
27 filter_store_value => undef,
28 filter_fetch_key => undef,
29 filter_fetch_value => undef,
32 # Grab the parameters we want to use
33 foreach my $param ( keys %$self ) {
34 next unless exists $args->{$param};
35 $self->{$param} = $args->{$param};
38 if ( $self->{fh} && !$self->{file_offset} ) {
39 $self->{file_offset} = tell( $self->{fh} );
42 $self->open unless $self->{fh};
50 # Adding O_BINARY should remove the need for the binmode below. However,
51 # I'm not going to remove it because I don't have the Win32 chops to be
52 # absolutely certain everything will be ok.
53 my $flags = O_CREAT | O_BINARY;
55 if ( !-e $self->{file} || -w _ ) {
63 sysopen( $fh, $self->{file}, $flags )
64 or die "DBM::Deep: Cannot sysopen file '$self->{file}': $!\n";
67 # Even though we use O_BINARY, better be safe than sorry.
70 if ($self->{autoflush}) {
93 return 0 unless $self->{fh};
94 return( (-s $self->{fh}) - $self->{file_offset} );
100 unless ( defined $self->{inode} ) {
101 my @stats = stat($self->{fh});
102 $self->{inode} = $stats[1];
103 $self->{end} = $stats[7];
115 my $fh = $self->{fh};
116 if ( defined $loc ) {
117 seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
121 my $caller = join ':', (caller)[0,2];
122 my $len = length( join '', @_ );
123 warn "($caller) print_at( " . (defined $loc ? $loc : '<undef>') . ", $len )\n";
126 print( $fh @_ ) or die "Internal Error (print_at($loc)): $!\n";
133 my ($loc, $size) = @_;
137 my $fh = $self->{fh};
138 if ( defined $loc ) {
139 seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
143 my $caller = join ':', (caller)[0,2];
144 warn "($caller) read_at( " . (defined $loc ? $loc : '<undef>') . ", $size )\n";
148 read( $fh, $buffer, $size);
166 #XXX Do I need to reset $self->{end} here? I need a testcase
167 my $loc = $self->{end};
168 $self->{end} += $size;
174 # If db locking is set, flock() the db file. If called multiple
175 # times before unlock(), then the same number of unlocks() must
176 # be called before the lock is released.
181 return $self->lock( $obj, LOCK_EX );
187 return $self->lock( $obj, LOCK_SH );
192 my ($obj, $type) = @_;
194 $type = LOCK_EX unless defined $type;
196 #XXX This is a temporary fix for Win32 and autovivification. It
197 # needs to improve somehow. -RobK, 2008-03-09
198 if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
202 if (!defined($self->{fh})) { return; }
204 #XXX This either needs to allow for upgrading a shared lock to an
205 # exclusive lock or something else with autovivification.
207 if ($self->{locking}) {
208 if (!$self->{locked}) {
209 flock($self->{fh}, $type);
211 # refresh end counter in case file has changed size
212 my @stats = stat($self->{fh});
213 $self->{end} = $stats[7];
215 # double-check file inode, in case another process
216 # has optimize()d our file while we were waiting.
217 if (defined($self->{inode}) && $stats[1] != $self->{inode}) {
222 $obj->{engine}->setup_fh( $obj );
224 flock($self->{fh}, $type); # re-lock
226 # This may not be necessary after re-opening
227 $self->{end} = (stat($self->{fh}))[7]; # re-end
239 # If db locking is set, unlock the db file. See note in lock()
240 # regarding calling lock() multiple times.
245 if (!defined($self->{fh})) { return; }
247 if ($self->{locking} && $self->{locked} > 0) {
250 if (!$self->{locked}) {
251 flock($self->{fh}, LOCK_UN);
264 # Flush the filehandle
265 my $old_fh = select $self->{fh};
266 my $old_af = $|; $| = 1; $| = $old_af;
272 # Taken from http://www.perlmonks.org/?node_id=691054
276 my $fh = $self->{fh};
277 return unless defined $fh;
278 return unless defined fileno $fh;
279 local $\ = ''; # just in case
280 no warnings; # temporarily disable warnings
281 local $^W; # temporarily disable warnings
287 my ($temp_filename) = @_;
289 my @stats = stat( $self->{fh} );
290 my $perms = $stats[2] & 07777;
293 chown( $uid, $gid, $temp_filename );
294 chmod( $perms, $temp_filename );