평소 초보가 많은 도움받고 있습니다
다름이 아니라 아래파일의 각 행별로 명령어 설명 좀 부탁드립니다
고수님들 대충이라도 부탁드립니다
여름 건강하시길 바랍니다^^
use constant PROG_DESCRIPTION => "XPlanet Cloudmap Downloader";
use constant PROG_COPYRIGHT => "Copyright (c) 2003, cueSim Ltd.";
use constant PROG_VERSION => "0.01";
use constant PROG_AUTHOR => "cueSim Ltd ; modified by Matthew Gates";
use strict;
use LWP::Simple;
use Digest::MD5;
use Getopt::Long;
use File::Basename;
Presets & defaults
my @ga_mirrors = (
"http://home.megapass.co.kr/~gitto88/cloud_data/clouds_2048.jpg"
, "http://home.megapass.co.kr/~holywatr/cloud_data/clouds_2048.jpg"
);
my $gs_debug = 0;
my $gs_overwrite = 0;
my $gs_xp_dir = "$ENV{HOME}/.xplanet";
my $gs_cloud_file = "clouds.jpg";
my $gs_hours = 0.1;
my $gs_retries = 6;
my $gs_thisscript = basename($0);
Process command line options
GetOptions(
‘debug=i’ => $gs_debug,
‘filename=s’ => $gs_cloud_file,
‘force’ => $gs_overwrite,
‘help’ => &usage,
‘hours=i’ => $gs_hours,
‘list-mirrors’ => &list_mirrors,
‘output-directory=s’ => $gs_xp_dir,
‘retries=i’ => $gs_retries,
‘version’ => sub {
print &PROG_DESCRIPTION
. " by " . &PROG_AUTHOR . "\n"
. "version " . &PROG_VERSION . "\n"
. &PROG_COPYRIGHT . "\n";
},
) || usage(1);
Change to directory where we’ll put the output file
chdir "$gs_xp_dir" || die "cannot change dir to $gs_xp_dir : $!\n";
Get file details
if( -f $gs_cloud_file )
{
my @stat_info = stat($gs_cloud_file);
my $file_age = (time() - $stat_info[9]);
my $file_size = $stat_info[7];
# Check if file is already up to date
if ( ( $file_age < 60 * 60 * $gs_hours && $file_size > 2048)
&& ! $gs_overwrite )
{
db_out(3,"$gs_cloud_file is already up to date");
exit(1);
}
}
Try several times to download the file if necessary
for(1…$gs_retries)
{
# Get a random website to hit for the file
my $mirror_url = get_random_mirror();
# Download the file
db_out(2,"downloading from $mirror_url");
my $response = getstore($mirror_url, $gs_cloud_file);
# If successfully downloaded, that's it, nothing more to do
if( indicate_success($response) )
{
my $size = (stat($gs_cloud_file))[7];
db_out(2,"successfully downloaded $gs_cloud_file (size=$size)");
exit(0);
}
# Warning that we're retrying another random server
db_out(1,"download FAILED from $mirror_url");
}
Warning that no servers could be contacted
db_out(-1, "ERROR: tried to download the file $gs_retries times, but no servers could provide the file");
exit(2);
Return codes of 200 to 299 are "success" in HTTP-speak
sub indicate_success
{
# If $gs_cloud_file file is not existing or zero bytes, it didn’t work
if ( ! -e $gs_cloud_file || -z $gs_cloud_file )
{
db_out(2, "FAILED to download (non-existing or 0-size file)");
return(0);
}
# Sometime the file is downloaded but contains an HTML message, despite
# getting a success message. In this case the file is very small (115
# bytes is typical). We'll reject any file smaller than 1024 bytes.
my $size = (stat($gs_cloud_file))[7];
if ( $size < 1024 )
{
db_out(2, "FAILED to download (too small - $size bytes)");
return(0);
}
if ( check_error_image($gs_cloud_file) )
{
db_out(2, "FAILED - image is known error message image");
return(0);
}
my $response = shift();
if($response =~ /2\d\d/)
{
db_out(4, "downloaded file looks good");
return(1);
}
else
{
db_out(2,"FAILED to download - HTTP error: $response");
return(0);
}
}
Returns the name of an internet resource which can provide the clouds image
sub get_random_mirror
{
# Return one at random
return $ga_mirrors[rand scalar(@ga_mirrors)];
}
sub list_mirrors
{
foreach my $m (@ga_mirrors) {
print "$m\n";
}
exit(1);
}
sub check_error_image
{
# Sometimes the image downloaded is a picture with an error message in it
# saying something like the image is being received from the satellite
# In this case we want to "fail" the download. It’s easy to see, but it’s
# very difficult to check this programatically, so we keep a list of
# checksums of these files and check that way.
my $file = shift;
my %bad_image_md5s = ( "e0efd0c168fcdeb3d3029cf35ae230a9" => 1, );
open(IMG, "<$file") || return(1);
my $md5 = Digest::MD5->new;
$md5->add(*IMG);
close(IMG);
if ( defined($bad_image_md5s{$md5->hexdigest}) )
{
return(1);
}
else {
return(0);
}
}
sub db_out {
my $lev = shift;
return if ( $gs_debug < $lev );
my $message = shift;
print STDERR "$gs_thisscript: $message\n";
}
sub usage {
my $lev = shift || 0;
system("pod2usage -verbose 1 $0");
exit($lev);
}
END