1 package DBM::Deep::File;
8 our $VERSION = q(1.0013);
10 use Fcntl qw( :DEFAULT :flock :seek );
12 use constant DEBUG => 0;
27 #XXX Migrate this to the engine, where it really belongs.
28 filter_store_key => undef,
29 filter_store_value => undef,
30 filter_fetch_key => undef,
31 filter_fetch_value => undef,
34 # Grab the parameters we want to use
35 foreach my $param ( keys %$self ) {
36 next unless exists $args->{$param};
37 $self->{$param} = $args->{$param};
40 if ( $self->{fh} && !$self->{file_offset} ) {
41 $self->{file_offset} = tell( $self->{fh} );
44 $self->open unless $self->{fh};
52 # Adding O_BINARY should remove the need for the binmode below. However,
53 # I'm not going to remove it because I don't have the Win32 chops to be
54 # absolutely certain everything will be ok.
55 my $flags = O_CREAT | O_BINARY;
57 if ( !-e $self->{file} || -w _ ) {
65 sysopen( $fh, $self->{file}, $flags )
66 or die "DBM::Deep: Cannot sysopen file '$self->{file}': $!\n";
69 # Even though we use O_BINARY, better be safe than sorry.
72 if ($self->{autoflush}) {
95 unless ( defined $self->{inode} ) {
96 my @stats = stat($self->{fh});
97 $self->{inode} = $stats[1];
98 $self->{end} = $stats[7];
110 my $fh = $self->{fh};
111 if ( defined $loc ) {
112 seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
116 my $caller = join ':', (caller)[0,2];
117 my $len = length( join '', @_ );
118 warn "($caller) print_at( " . (defined $loc ? $loc : '<undef>') . ", $len )\n";
121 print( $fh @_ ) or die "Internal Error (print_at($loc)): $!\n";
128 my ($loc, $size) = @_;
132 my $fh = $self->{fh};
133 if ( defined $loc ) {
134 seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
138 my $caller = join ':', (caller)[0,2];
139 warn "($caller) read_at( " . (defined $loc ? $loc : '<undef>') . ", $size )\n";
143 read( $fh, $buffer, $size);
161 #XXX Do I need to reset $self->{end} here? I need a testcase
162 my $loc = $self->{end};
163 $self->{end} += $size;
169 # If db locking is set, flock() the db file. If called multiple
170 # times before unlock(), then the same number of unlocks() must
171 # be called before the lock is released.
175 my ($obj, $type) = @_;
177 $type = LOCK_EX unless defined $type;
179 #XXX This is a temporary fix for Win32 and autovivification. It
180 # needs to improve somehow. -RobK, 2008-03-09
181 if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
185 if (!defined($self->{fh})) { return; }
187 #XXX This either needs to allow for upgrading a shared lock to an
188 # exclusive lock or something else with autovivification.
190 if ($self->{locking}) {
191 if (!$self->{locked}) {
192 flock($self->{fh}, $type);
194 # refresh end counter in case file has changed size
195 my @stats = stat($self->{fh});
196 $self->{end} = $stats[7];
198 # double-check file inode, in case another process
199 # has optimize()d our file while we were waiting.
200 if (defined($self->{inode}) && $stats[1] != $self->{inode}) {
205 $obj->{engine}->setup_fh( $obj );
207 flock($self->{fh}, $type); # re-lock
209 # This may not be necessary after re-opening
210 $self->{end} = (stat($self->{fh}))[7]; # re-end
222 # If db locking is set, unlock the db file. See note in lock()
223 # regarding calling lock() multiple times.
228 if (!defined($self->{fh})) { return; }
230 if ($self->{locking} && $self->{locked} > 0) {
232 if (!$self->{locked}) { flock($self->{fh}, LOCK_UN); }
243 # Flush the filehandle
244 my $old_fh = select $self->{fh};
245 my $old_af = $|; $| = 1; $| = $old_af;
251 # Taken from http://www.perlmonks.org/?node_id=691054
255 my $fh = $self->{fh};
256 return unless defined $fh;
257 return unless defined fileno $fh;
258 local $\ = ''; # just in case
259 no warnings; # temporarily disable warnings
260 local $^W; # temporarily disable warnings
266 my ($temp_filename) = @_;
268 my @stats = stat( $self->{fh} );
269 my $perms = $stats[2] & 07777;
272 chown( $uid, $gid, $temp_filename );
273 chmod( $perms, $temp_filename );