BGPmon-core-2
view release on metacpan or search on metacpan
lib/BGPmon/Fetch/Archive.pm view on Meta::CPAN
# or the empty string if none is found.
sub get_filename_from_line{
my $line = shift;
my $fname = "get_filename_from_line";
if( !defined($line) ){
$error_code{$fname} = UNDEFINED_ARGUMENT_CODE;
$error_msg{$fname} = UNDEFINED_ARGUMENT_MSG;
return undef;
}
#Filenames look like this: updates.YYYYMMDD.HHMM.*.xml
#Filenames may also have an up-to-4-character extension for compressed
if($line =~
m/(\"updates\.[0-9][0-9][0-9][0-9][0-1][0-9][0-3][0-9]\.[0-2][0-9][0-5][0-9].*\.xml(\.\w{0,4})?\")/){
my $filename = $1;
#Hack off the double-quotes on either side of the filename
$filename =~ s/\"//g;
return $filename;
}
return "";
}
#set_append
#This function tests different variants of the final possible subdirectory
#under an archive page.
#Input: None
#Output: 0
sub set_append{
my $fname = "set_append";
my $url = $upd_url."/$year.$month/";
my $output = "$scratch_dir/index-test.html";
if(download_URL($url."UPDATES/",$output) == 0 && validateIndex($output) ){
$append = "UPDATES/";
}
elsif(download_URL($url."updates/",$output)==0 && validateIndex($output) ){
$append = "updates/";
}
elsif(download_URL($url,$output) == 0 && validateIndex($output) ){
$append = "";
}
else {
$append = undef;
}
eval{
unlink($output);
1;
} or do {
$error_code{$fname} = SYSCALL_FAIL_CODE;
$error_msg{$fname} = SYSCALL_FAIL_MSG.": $@";
};
return 0;
}
# validateIndex
#A helper function to scan an HTML page to determine whether the page is
#a valid index page or something else. It looks for the key phrase
#"Index of" with the current year.month subdirectory.
#Input: The filename to check
#Output: 1 if the file matches the search, 0 otherwise
sub validateIndex{
my $index_fn = shift;
my $fname = "validateIndex";
if( !defined($index_fn) ){
$error_code{$fname} = UNDEFINED_ARGUMENT_CODE;
$error_msg{$fname} = UNDEFINED_ARGUMENT_MSG;
return 0;
}
my $index_fh;
unless( open($index_fh,"<",$index_fn) ){
$error_code{$fname} = FILE_OPERATION_FAIL_CODE;
$error_msg{$fname} = FILE_OPERATION_FAIL_MSG;
return 0;
}
while(<$index_fh>){
my $line = $_;
if( $line =~ m/Index of .*\/$year.$month\// ){
close($index_fh);
return 1;
}
}
close($index_fh);
return 0;
}
# download_URL
#
#Downloads a target file and saves it to a user-specified file
#This function is primarily a wrapper around several functions of
# the LWP::Simple module.
#Input: a target URL, either an HTML index or another file
# an output file name
#Output: 0 on success, 1 on failure
sub download_URL{
my $fname = "download_URL";
#Get argument(s) and check that they exist
my $target_url = shift;
my $output = shift;
#If the target is not defined, obviously that is a problem
if(!defined($target_url) || $target_url eq "" ||
!defined($output) || $output eq "" ){
$error_code{$fname} = UNDEFINED_ARGUMENT_CODE;
$error_msg{$fname} = UNDEFINED_ARGUMENT_MSG;
return 1;
}
if($target_url !~ m/http/){
$target_url = "http://".$target_url;
}
## Download the specified file into the given output file
my $ret = LWP::Simple::getstore($target_url, $output);
( run in 0.719 second using v1.01-cache-2.11-cpan-56fb94df46f )