package Project::Net::FTP::FileHandle; use strict; use vars qw($VERSION); $VERSION = '0.1'; use Carp qw(croak confess); use Net::FTPServer::FileHandle; use Project::Net::FTP::DirHandle; use Project::IO::Blob; use vars qw(@ISA); @ISA = qw(Net::FTPServer::FileHandle); use vars qw($sth1 $sth2 $sth3 $sth4 $sth13); # Return a new file handle. sub new { my $class = shift; my $ftps = shift; my $pathname = shift; my $dir_id = shift; my $file_id = shift; my $content = shift; # Create object. my $self = Net::FTPServer::FileHandle->new ($ftps, $pathname); $self->{fs_dir_id} = $dir_id; $self->{fs_file_id} = $file_id; $self->{fs_content} = $content; return bless $self, $class; } # Return the directory handle for this file. sub dir { my $self = shift; return Project::Net::FTP::DirHandle->new ( $self->{ftps}, $self->dirname, $self->{fs_dir_id} ); } # Open the file handle. sub open { my $self = shift; my $mode = shift; if ($mode eq "r") # Open file for reading. { return Project::IO::Blob->new({ mode => 'r', dbh => $self->{ftps}{fs_dbh}, data => \$self->{fs_content}, ID => undef }); } elsif ($mode eq "w") # Create/overwrite the file. { # Remove the existing large object and create a new one. my $dbh = $self->{ftps}{fs_dbh}; my $sql = "update FILES set data = null where ID = ?"; $sth4 ||= $dbh->prepare ($sql); $sth4->execute (int ($self->{fs_file_id})); return Project::IO::Blob->new({ mode => 'w', dbh => $self->{ftps}{fs_dbh}, data => \$self->{fs_content}, ID => $self->{fs_file_id} }); } elsif ($mode eq "a") # Append to the file. { return Project::IO::Blob->new({ mode => 'w', dbh => $self->{ftps}{fs_dbh}, data => \$self->{fs_content}, ID => $self->{fs_file_id} }); } else { croak "unknown file mode: $mode; use 'r', 'w' or 'a' instead"; } } sub status { my $self = shift; my $dbh = $self->{ftps}{fs_dbh}; my $username = substr $self->{ftps}{user}, 0, 8; my $blob_fd; my $sql = "select length(data) from FILES where ID = ?"; $sth13 ||= $self->{ftps}{fs_dbh}->prepare ($sql); $sth13->execute (int ($self->{fs_file_id})); my $row = $sth13->fetch or return undef; return ( 'f', 0644, 1, $username, "users", $row->[0], 0 ); } # Move a file to elsewhere. sub move { my $self = shift; my $dirh = shift; my $filename = shift; my $sql = "update FILES set directory_ID = ?, name = ? where ID = ?"; $sth2 ||= $self->{ftps}{fs_dbh}->prepare ($sql); $sth2->execute (int ($dirh->{fs_dir_id}), $filename, int ($self->{fs_file_id})); return 0; } # Delete a file. sub delete { my $self = shift; my $sql = "delete from FILES where ID = ?"; $sth1 ||= $self->{ftps}{fs_dbh}->prepare ($sql); $sth1->execute (int ($self->{fs_file_id})); return 0; } 1; __END__