# $Id: File.pm,v 1.8 2002/05/31 19:22:24 matts Exp $
package Apache::AxKit::Provider::File;
use strict;
use vars qw/@ISA/;
@ISA = ('Apache::AxKit::Provider');
use Apache;
use Apache::Log;
use Apache::Constants qw(HTTP_OK);
use Apache::AxKit::Exception;
use Apache::AxKit::Provider;
use AxKit;
use File::Basename;
use Fcntl qw(O_RDONLY LOCK_SH);
sub init {
my $self = shift;
my (%p) = @_;
my $stats_done;
if ($p{key}) {
AxKit::Debug(8, "File Provider instantiated by key: $p{key}");
$self->{file} = $p{key};
}
else {
if ($p{uri} and $p{uri} =~ s|^file:(//)?||) {
$p{file} = delete $p{uri};
}
if ($p{uri}) {
my $r = $self->apache_request();
AxKit::Debug(8, "[uri] File Provider looking up uri $p{uri}");
$self->{apache} = $r->lookup_uri($p{uri});
my $status = $self->{apache}->status();
if ($status != HTTP_OK) {
throw Apache::AxKit::Exception::Error(-text => "Subrequest failed with status: " . $status);
}
$self->{file} = $self->{apache}->filename();
AxKit::Debug(8, "[uri] File Provider set filename to $self->{file}");
}
elsif ($p{file}) {
AxKit::Debug(8, "[file] File Provider given file: $p{file}");
$self->{file} = $p{file};
}
else {
$self->{file} = $self->{apache}->filename();
AxKit::Debug(8, "[req] File Provider given \$r: $self->{file}");
my @stats = stat($self->{apache}->finfo());
$self->{mtime} = $stats[9];
if (-e _) {
if (-r _ ) {
$self->{file_exists} = 1;
}
if (-d _) {
$self->{is_dir} = 1;
}
else {
$self->{is_dir} = 0;
}
}
$stats_done++;
}
}
if (!$stats_done) {
my @stats = stat($self->{file});
$self->{mtime} = $stats[9];
if (-e _) {
if (-r _ ) {
$self->{file_exists} = 1;
}
if (-d _) {
$self->{is_dir} = 1;
}
else {
$self->{is_dir} = 0;
}
}
}
}
sub _is_dir {
my $self = shift;
return $self->{is_dir} if exists $self->{is_dir};
return -d $self->{file};
}
sub key {
my $self = shift;
return $self->{file};
}
sub exists {
my $self = shift;
return $self->{file_exists} if exists $self->{file_exists};
if (-e $self->{file}) {
if (-r _ ) {
$self->{file_exists} = 1;
return 1;
}
else {
AxKit::Debug(2, "'$self->{file}' not readable");
return;
}
}
return;
}
sub process {
my $self = shift;
my $xmlfile = $self->{file};
unless ($self->exists()) {
AxKit::Debug(5, "file '$xmlfile' does not exist or is not readable");
return 0;
}
if ( $self->_is_dir ) {
if ($AxKit::Cfg->HandleDirs()) {
return 1;
}
# else
AxKit::Debug(5, "'$xmlfile' is a directory");
return 0;
}
# Test for an XML file type only if not using FastHandler
if (!$AxKit::FastHandler) {
local $^W;
if (($xmlfile =~ /\.xml$/i) ||
($self->{apache}->content_type() =~ /^(text|application)\/xml/) ||
$self->{apache}->pnotes('xml_string')
) {
# chdir(dirname($xmlfile));
return 1;
}
}
else {
return 1;
}
AxKit::Debug(5, "'$xmlfile' not recognised as XML");
return 0;
}
sub mtime {
my $self = shift;
return $self->{mtime} if defined $self->{mtime};
return ($self->{mtime} = (stat($self->{file}))[9]);
}
sub get_fh {
my $self = shift;
if (!$self->exists()) {
throw Apache::AxKit::Exception::IO(-text => "File '$self->{file}' does not exist or is not readable");
}
if ($self->_is_dir()) {
throw Apache::AxKit::Exception::IO(-text => "$self->{file} is a directory");
}
my $filename = $self->{file};
# chdir(dirname($filename));
my $fh = Apache->gensym();
if (sysopen($fh, $filename, O_RDONLY)) {
# seek($fh, 0, 0);
return $fh;
}
throw Apache::AxKit::Exception::IO( -text => "Can't open '$self->{file}': $!" );
}
sub get_strref {
my $self = shift;
if ($self->_is_dir()) {
throw Apache::AxKit::Exception::IO(
-text => "$self->{file} is a directory - please overload File provider and use AxContentProvider option");
}
my $fh = $self->get_fh();
local $/;
my $contents = <$fh>;
return \$contents
}
1;