package DBM::Deep::File;
-use 5.6.0;
+use 5.006_000;
use strict;
-use warnings;
+use warnings FATAL => 'all';
use Fcntl qw( :DEFAULT :flock :seek );
-our $VERSION = '0.01';
+use constant DEBUG => 0;
sub new {
my $class = shift;
my ($args) = @_;
my $self = bless {
- audit_fh => undef,
- audit_file => undef,
autobless => 1,
- autoflush => undef,
+ autoflush => 1,
end => 0,
fh => undef,
file => undef,
file_offset => 0,
- locking => undef,
+ locking => 1,
locked => 0,
+#XXX Migrate this to the engine, where it really belongs.
filter_store_key => undef,
filter_store_value => undef,
filter_fetch_key => undef,
filter_fetch_value => undef,
-
- # These are values that are not expected to be passed in through
- # $args. They are here for documentation purposes.
- transaction_id => 0,
- transaction_offset => 0,
- base_db_obj => undef,
}, $class;
# Grab the parameters we want to use
$self->open unless $self->{fh};
- if ( $self->{audit_file} && !$self->{audit_fh} ) {
- my $flags = O_WRONLY | O_APPEND | O_CREAT;
-
- my $fh;
- sysopen( $fh, $self->{audit_file}, $flags )
- or die "Cannot open audit file '$self->{audit_file}' for read/write: $!";
-
- # Set the audit_fh to autoflush
- my $old = select $fh;
- $|=1;
- select $old;
-
- $self->{audit_fh} = $fh;
- }
-
-
return $self;
}
-sub set_db {
- unless ( $_[0]{base_db_obj} ) {
- $_[0]{base_db_obj} = $_[1];
- Scalar::Util::weaken( $_[0]{base_db_obj} );
- }
-}
-
sub open {
my $self = shift;
- # Adding O_BINARY does remove the need for the binmode below. However,
+ # Adding O_BINARY should remove the need for the binmode below. However,
# I'm not going to remove it because I don't have the Win32 chops to be
# absolutely certain everything will be ok.
- my $flags = O_RDWR | O_CREAT | O_BINARY;
+ my $flags = O_CREAT | O_BINARY;
+
+ if ( !-e $self->{file} || -w _ ) {
+ $flags |= O_RDWR;
+ }
+ else {
+ $flags |= O_RDONLY;
+ }
my $fh;
sysopen( $fh, $self->{file}, $flags )
return 1;
}
+sub size {
+ my $self = shift;
+
+ return 0 unless $self->{fh};
+ return -s $self->{fh};
+}
+
sub set_inode {
my $self = shift;
- unless ( $self->{inode} ) {
+ unless ( defined $self->{inode} ) {
my @stats = stat($self->{fh});
$self->{inode} = $stats[1];
$self->{end} = $stats[7];
seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
}
- print( $fh @_ );
+ if ( DEBUG ) {
+ my $caller = join ':', (caller)[0,2];
+ my $len = length( join '', @_ );
+ warn "($caller) print_at( " . (defined $loc ? $loc : '<undef>') . ", $len )\n";
+ }
+
+ print( $fh @_ ) or die "Internal Error (print_at($loc)): $!\n";
return 1;
}
seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
}
+ if ( DEBUG ) {
+ my $caller = join ':', (caller)[0,2];
+ warn "($caller) read_at( " . (defined $loc ? $loc : '<undef>') . ", $size )\n";
+ }
+
my $buffer;
read( $fh, $buffer, $size);
return $buffer;
}
-sub increment_pointer {
- my $self = shift;
- my ($size) = @_;
-
- if ( defined $size ) {
- seek( $self->{fh}, $size, SEEK_CUR );
- }
-
- return 1;
-}
-
sub DESTROY {
my $self = shift;
return unless $self;
return $loc;
}
-#sub release_space {
-# my $self = shift;
-# my ($size, $loc) = @_;
-#
-# local($/,$\);
-#
-# my $next_loc = 0;
-#
-# my $fh = $self->{fh};
-# seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
-# print( $fh SIG_FREE
-# . pack($self->{long_pack}, $size )
-# . pack($self->{long_pack}, $next_loc )
-# );
-#
-# return;
-#}
-
##
# If db locking is set, flock() the db file. If called multiple
# times before unlock(), then the same number of unlocks() must
# be called before the lock is released.
##
+sub lock_exclusive {
+ my $self = shift;
+ my ($obj) = @_;
+ return $self->lock( $obj, LOCK_EX );
+}
+
+sub lock_shared {
+ my $self = shift;
+ my ($obj) = @_;
+ return $self->lock( $obj, LOCK_SH );
+}
+
sub lock {
my $self = shift;
my ($obj, $type) = @_;
+
$type = LOCK_EX unless defined $type;
+ #XXX This is a temporary fix for Win32 and autovivification. It
+ # needs to improve somehow. -RobK, 2008-03-09
+ if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
+ $type = LOCK_EX;
+ }
+
if (!defined($self->{fh})) { return; }
+ #XXX This either needs to allow for upgrading a shared lock to an
+ # exclusive lock or something else with autovivification.
+ # -RobK, 2008-03-09
if ($self->{locking}) {
if (!$self->{locked}) {
flock($self->{fh}, $type);
# double-check file inode, in case another process
# has optimize()d our file while we were waiting.
- if ($stats[1] != $self->{inode}) {
+ if (defined($self->{inode}) && $stats[1] != $self->{inode}) {
$self->close;
$self->open;
if ($self->{locking} && $self->{locked} > 0) {
$self->{locked}--;
- if (!$self->{locked}) { flock($self->{fh}, LOCK_UN); }
- return 1;
- }
-
- return;
-}
-
-sub set_transaction_offset {
- my $self = shift;
- $self->{transaction_offset} = shift;
-}
-
-sub audit {
- my $self = shift;
-
- if ( my $afh = $self->{audit_fh} ) {
- my ($string) = @_;
-
- flock( $afh, LOCK_EX );
-
- if ( $string =~ /^#/ ) {
- print( $afh "$string " . localtime(time) . "\n" );
- }
- else {
- print( $afh "$string # " . localtime(time) . "\n" );
+ if (!$self->{locked}) {
+ flock($self->{fh}, LOCK_UN);
+ return 1;
}
- flock( $afh, LOCK_UN );
+ return;
}
- return 1;
+ return;
}
-sub begin_transaction {
+sub flush {
my $self = shift;
- my $fh = $self->{fh};
-
- $self->lock;
-
- seek( $fh, $self->{transaction_offset} + $self->{file_offset}, SEEK_SET );
- my $buffer;
- read( $fh, $buffer, 4 );
- $buffer = unpack( 'N', $buffer );
-
- for ( 1 .. 32 ) {
- next if $buffer & (1 << ($_ - 1));
- $self->{transaction_id} = $_;
- $buffer |= (1 << $_-1 );
- last;
- }
-
- seek( $fh, $self->{transaction_offset} + $self->{file_offset}, SEEK_SET );
- print( $fh pack( 'N', $buffer ) );
-
- $self->unlock;
+ # Flush the filehandle
+ my $old_fh = select $self->{fh};
+ my $old_af = $|; $| = 1; $| = $old_af;
+ select $old_fh;
- return $self->{transaction_id};
+ return 1;
}
-sub end_transaction {
+# Taken from http://www.perlmonks.org/?node_id=691054
+sub is_writable {
my $self = shift;
my $fh = $self->{fh};
-
- $self->lock;
-
- seek( $fh, $self->{transaction_offset} + $self->{file_offset}, SEEK_SET );
- my $buffer;
- read( $fh, $buffer, 4 );
- $buffer = unpack( 'N', $buffer );
-
- # Unset $self->{transaction_id} bit
-
- seek( $fh, $self->{transaction_offset} + $self->{file_offset}, SEEK_SET );
- print( $fh pack( 'N', $buffer ) );
-
- $self->unlock;
-
- $self->{transaction_id} = 0;
+ return unless defined $fh;
+ return unless defined fileno $fh;
+ local $\ = ''; # just in case
+ no warnings; # temporarily disable warnings
+ local $^W; # temporarily disable warnings
+ return print $fh '';
}
-sub current_transactions {
+sub copy_stats {
my $self = shift;
-
- my $fh = $self->{fh};
-
- $self->lock;
-
- seek( $fh, $self->{transaction_offset} + $self->{file_offset}, SEEK_SET );
- my $buffer;
- read( $fh, $buffer, 4 );
- $buffer = unpack( 'N', $buffer );
-
- $self->unlock;
-
- my @transactions;
- for ( 1 .. 32 ) {
- if ( $buffer & (1 << ($_ - 1)) ) {
- push @transactions, $_;
- }
- }
-
- return grep { $_ != $self->{transaction_id} } @transactions;
+ my ($temp_filename) = @_;
+
+ my @stats = stat( $self->{fh} );
+ my $perms = $stats[2] & 07777;
+ my $uid = $stats[4];
+ my $gid = $stats[5];
+ chown( $uid, $gid, $temp_filename );
+ chmod( $perms, $temp_filename );
}
-sub transaction_id { return $_[0]->{transaction_id} }
-
-#sub commit {
-#}
-
1;
__END__
-