Walczyłem z tym bardzo długo lecz
error #10 :: NIE MOZNA ODCZYTAC LUB ZAPISAC KATALOGU TYMCZASOWEGO
Ma ktoś jakiś pomysł ?
#!/usr/bin/perl -w
use lib "./lib";
use DBI;
use Date::Parse;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use Net::FTP;
use File::Copy;
my $COMP_LEVEL = 6; #poziom kompresji; 0-brak, 9-max
my $max_demo = 60*60*24*30; #ile sekund trzymac dema, standardowo 30dni (2592000)
#USTAWIENIA FOLDEROW
my $from = 1; #pliki pobierane z: 0 - FTP, 1 - lokalny
my $to = 1; #pliki wysyłane na: 0 - FTP, 1 - lokalny
my $compress = 1; #kompresja pobieranych plików: 0 - pliki juz sa skompresowane, 1 - kompresuj pliki
my $cext = ".dem"; #rozszerzenie plików;
#USTAWIENIA BAZY DANYCH
my $DB_host = "localhost"; #adres
my $DB_port = "3306"; #port
my $DB_user = "user"; #uzytkownik
my $DB_pass = "*****"; #haslo uzytkownika
my $DB_name = "nowa"; #nazwa bazy
my $DB_table = "hlds_dema"; #nazwa tabeli
my $DB_type = "mysql";
#FTP ZRODLOWY (zdalny HLDS) [[jesli $from==0]
my $ftp_host_from = "77.55.218.167"; #adres serwera
my $ftp_user_from = "****"; #login
my $ftp_pass_from = "****"; #haslo
#FTP DOCELOWY (zdalny WWW) [jesli $to==0]
my $ftp_host_to = "77.55.218.167"; #nazwa serwera
my $ftp_user_to = "*****"; #login
my $ftp_pass_to = "******"; #haslo
#FOLDER ZRODLOWY (zdalny HLDS) [jesli $from==0]
my @FTPdir_from = ("", "/cstrike/");
#FTP DOCELOWY (zdalny WWW) [jesli $to==0]
my @FTPdir_to = ("", "/cstrike/dema/");
#FOLDER ZRODLOWY (lokalny HLDS) [jesli $from==1]
my @loc_dir_from = ("/home/ogpbot/OGP_User_Files/hltvjb/cstrike");
#FOLDER DOCELOWY (lokalny WWW) [jesli $to==1]
my @loc_dir_to = ("/var/www/html/dema");
#KONIEC EDYCJI
my @dir_from = (\@FTPdir_from, \@loc_dir_from);
my @dir_to = (\@FTPdir_to, \@loc_dir_to);
if ($from != 0 && $from != 1)
{
print "error #00 :: Zly folder zrodlowy\n";
exit;
}
if ($from != 0 && $from != 1)
{
print "error #01 :: Zly folder docelowy\n";
exit;
}
my $num_from = $#{$dir_from[$from]};
my $num_to = $#{$dir_to[$to]};
if ($num_from != $num_to)
{
print "error #02 :: Nie zgadzaja sie liczby katalogow zrodlowych i docelowych\n";
exit;
}
my $num = $num_from+1;
my $pl_add = 0;
my $pl_rem = 0;
my $arg = 0;
if ($ARGV[0]) {$arg = $ARGV[0]}
if ($arg==0 || $arg>=$num)
{
print "error #03 :: Zle ID => ".$arg."\n";
exit;
}
my $DBh = DBI->connect("DBI:$DB_type:$DB_name:$DB_host:$DB_port", $DB_user, $DB_pass) or
die "error #04 :: NIE MOZNA PODLACZYC DO BAZY";
my $ftp_from;
my $ftp_to;
if ($from==0)
{
$ftp_from = Net::FTP->new($ftp_host_from, Debug => 0)
or die "error #05 :: NIE MOZNA PODLACZYC DO FTP";
$ftp_from->login($ftp_user_from,$ftp_pass_from)
or die "error #06 :: NIE MOZNA ZALOGOWAC DO FTP ", $ftp_from->message;
$ftp_from->binary();
}
if ($to==0)
{
$ftp_to = Net::FTP->new($ftp_host_to, Debug => 0)
or die "error #07 :: NIE MOZNA PODLACZYC DO FTP";
$ftp_to->login($ftp_user_to,$ftp_pass_to)
or die "error #08 :: NIE MOZNA ZALOGOWAC DO FTP ", $ftp_to->message;
$ftp_to->binary();
}
my $demos_dir = "./tmp/"; #tymczasowy katalog z demami
my $direxist = 0;
if (-d $demos_dir)
{
$direxist = 1;
}
else {
mkdir $demos_dir or die "error #09 :: NIE MOZNA UTOWRZYC KATALOGU";
}
if (!(-r $demos_dir) || !(-w $demos_dir))
{
print "error #10 :: NIE MOZNA ODCZYTAC LUB ZAPISAC KATALOGU TYMCZASOWEGO\n";
exit;
}
if ($arg>0)
{
demos($arg)
}
else
{
for (my $a=1; $a<$num; $a++)
{
demos($a)
}
}
sub demos
{
my($harg) = @_;
my @files = {};
my $count = 0;
my $newest = 0;
my $newnum = -1;
if ($from == 0)
{
my @filelist = grep { !/^\.{1,2}$/ }$ftp_from->ls($dir_from[$from][$harg]);
my $num = scalar @filelist;
$ftp_from->cwd($dir_from[$from][$harg]) or die "error #24 :: NIE MOZNA ZMIENIC FOLDERU ", $ftp_from->message;
for (my $i=0; $i < $num; $i++)
{
my $filename = $filelist[$i];
$filename = (split(/\//,$filename))[-1];
#$filename =~ s/\///g;
if ($filename =~ m/$cext$/)
{
$files[$count]{"file_name"} = $filename;
if ($ftp_from->supported("STAT"))
{
my @msg;
$ftp_from->_STAT($filename);
@msg = $ftp_from->message;
my $line;
foreach $line (@msg)
{
if ($line =~ /^[-rwxSsTt]{10}/)
{
my @tmpdate = (split(/\s+/, $line));
$files[$count]{"file_data"} = str2time($tmpdate[6]." ".$tmpdate[5]." ".$tmpdate[7]);
$files[$count]{"file_size"} = $tmpdate[4];
}
}
} else {
print "#11 :: NIE MOZNA UZYC 'STAT'";
exit;
}
if ($files[$count]{"file_data"} < time-$max_demo)
{
next;
}
if ($files[$count]{"file_data"}>$newest)
{
$newest = $files[$count]{"file_data"};
$newnum = $count;
}
$filename =~ m/-(.*)$cext/ , $1 =~ m/(.*)-(.*)/ ,
$files[$count]{"demo_data"} = $1,
$files[$count]{"map"} = $2;
$files[$count]{"demo_data"} =~ m/(..)(..)(..)(..)(..)/,
$files[$count]{"data_day"} = $3,
$files[$count]{"data_hour"} = "$4:$5",
$files[$count]{"data_month"} = $2,
$files[$count]{"data_year"} = "20$1",
$files[$count]{"demo_data"} = str2time("20$1-$2-$3 $4:$5");
$count++;
}
}
} else {
opendir(DIR, $dir_from[$from][$harg]) or die "error #12 :: NIE MOZNA OTWORZYC FOLDERU\n";
while( (my $filename = readdir(DIR)))
{
if ($filename =~ m/$cext$/)
{
$files[$count]{"file_name"} = $filename;
$files[$count]{"file_data"} = (stat($dir_from[$from][$harg]."/".$filename))[9];
if ($files[$count]{"file_data"}>$newest)
{
$newest = $files[$count]{"file_data"};
$newnum = $count;
}
$filename =~ m/-(.*)$cext/ , $1 =~ m/(.*)-(.*)/ ,
$files[$count]{"demo_data"} = $1,
$files[$count]{"map"} = $2;
$files[$count]{"demo_data"} =~ m/(..)(..)(..)(..)(..)/,
$files[$count]{"data_day"} = $3,
$files[$count]{"data_hour"} = "$4:$5",
$files[$count]{"data_month"} = $2,
$files[$count]{"data_year"} = "20$1",
$files[$count]{"demo_data"} = str2time("20$1-$2-$3 $4:$5");
$count++;
}
}
closedir(DIR);
}
if ($newnum ne -1)
{
splice(@files, $newnum, 1);
$count--;
}
for (my $i=0; $i < $count; $i++)
{
my $file;
if ($compress==0)
{
$file = $files[$i]{"file_name"};
} else {
$file = $files[$i]{"file_name"}.".zip";
}
my $querry1 = $DBh->prepare("SELECT 'id' FROM ".$DB_table." WHERE server='".$harg."' and file_name='".$file."'");
$querry1->execute() or die "error #13 :: NIE MOZNA WYKONAC ZAPYTANIA";
my $rows = $querry1->rows;
if ($rows > 0)
{
next;
}
if ($from==0)
{
$ftp_from->get($files[$i]{"file_name"}, $demos_dir.$files[$i]{"file_name"});
} else {
copy($dir_from[$from][$harg].$file, $demos_dir.$file) or die "error #15 :: NIE MOZNA SKOPIOWAC PLIKU $!";
}
if ($compress==1)
{
my $zip = Archive::Zip->new();
my $member;
if ($from==0)
{
$member = $zip->addFile($demos_dir.$files[$i]{"file_name"}, $files[$i]{"file_name"});
} else {
$member = $zip->addFile($dir_from[$from][$harg].$files[$i]{"file_name"}, $files[$i]{"file_name"});
}
$member->desiredCompressionMethod(COMPRESSION_DEFLATED);
$member->desiredCompressionLevel($COMP_LEVEL);
unless ( $zip->writeToFileNamed($demos_dir.$file) == AZ_OK ) {
die "error #14 :: BLAD ZAPISU\n";
}
}
if ($to==0)
{
$ftp_to->cwd($dir_to[$to][$harg])
or die "error #16 :: NIE MOZNA ZMIENIC FOLDERU ", $ftp_to->message;
$ftp_to->put($demos_dir.$file)
or die "error #17 :: NIE MOZNA WYSLAC PLIKU ", $ftp_to->message;
} else {
copy($demos_dir.$file, $dir_to[$to][$harg].$file) or die "error #18 :: NIE MOZNA SKOPIOWAC PLIKU $!";
}
my $querry2 = $DBh->prepare("INSERT INTO ".$DB_table." VALUES ('', '".$harg."', '".$file."', '".$files[$i]{"map"}."',
'".$files[$i]{"demo_data"}."', '".$files[$i]{"file_data"}."', '".(stat($demos_dir.$file))[7]."', '0',
'".$files[$i]{"data_hour"}."', '".$files[$i]{"data_day"}."', '".$files[$i]{"data_month"}."', '".$files[$i]{"data_year"}."')");
$querry2->execute() or die "error #19 :: NIE MOZNA WYKONAC ZAPYTANIA";
if ($from==0)
{
$ftp_from->delete($files[$i]{"file_name"});
unlink($demos_dir.$files[$i]{"file_name"});
} else {
unlink($dir_from[$from][$harg].$files[$i]{"file_name"});
}
unlink($demos_dir.$file);
$pl_add += 1;
}
###$pl_add += $count;
my $now = time-$max_demo;
my $rcount = 0;
my $querry = $DBh->prepare("SELECT * FROM ".$DB_table." WHERE server='".$harg."' and file_data<'".$now."'");
$querry->execute() or die "error #20 :: NIE MOZNA WYKONAC ZAPYTANIA";
while ( my $row = $querry->fetchrow_arrayref() )
{
my $querry = $DBh->prepare("DELETE FROM ".$DB_table." WHERE id = '".@$row[0]."'");
$querry->execute() or die "error #21 :: NIE MOZNA WYKONAC ZAPYTANIA";
if ($to==0)
{
$ftp_to->cwd($FTPdir_to[$harg])
or die "error #22 :: NIE MOZNA ZMIENIC FOLDERU ", $ftp_to->message;
$ftp_to->delete(@$row[2])
or die "error #23 :: NIE MOZNA USUNAC PLIKU ", $ftp_to->message;
} else {
unlink($dir_to[$to][$harg].@$row[2]);
}
$rcount++;
}
$pl_rem += $rcount;
if ($direxist == 0)
{
rmdir ($demos_dir);
}
}
if ($from==0)
{
$ftp_from->quit;
}
if ($to==0)
{
$ftp_to->quit;
}
$DBh->disconnect();
print "FINISHED\nDodano: ".$pl_add."\nUsunieto: ".$pl_rem."\n";