SFV Checker

You can share your Lua Scripts with everybody here.

SFV Checker

Postby benny » Wed Dec 15, 2010 8:49 am

This isn't a Lua script, its a perl script -- but I didn't see another place to put this at. I noticed the Wing Server sfv checker wasn't recursive so I wrote this for my self and hope its helpful to everyone else and hope yall enjoy!

Code: Select all
#!/usr/bin/perl
# Make sure this points to your perl executable

# String::CRC32 is required you can install using cpan
use String::CRC32;

# Wing Server SFV Checker v1.00b - benny / clarjl3@students.fscj.edu
#
# I put this together because the sfv checker that comes with Wing Server isn't recursive
# and wont check sub directories inside the specified sfv checking directory
#
# Apologies for the sloppy code I wanted a working sfv checker quick, ill try and clean it
# up and add more features when I have more time.
#
# Known bugs / Irrelevant issues?
#  - As of now this will only check files in a sfv against the files uploaded
#    to the directory that your uploading to, so if any file not in the sfv is uploaded
#    it will just be ignored and possible to just have misc unwanted files laying around..
#
#  - The $home variable could cause issues with multiple domains and users depending on your setup..
#    on my setup i have all my files going to /usr/files and my one user having a home directory of /usr/files
#    so everything sent to this script would be /Dircreated/Files.. which would be
#    ($home/Dircreated/Files) -> /usr/files/Dircreated/Files
#      - I'll add a more flexable $home in the future!
#
#  - If there is more than one sfv this script will just exit..
#
# Using / Installing this
#  - You will need to run cpan and install String::CRC32
#  - Put this perl script somewhere you can find it later
#    - chmod +x /path/to/where/you/put/checker
#
#  - Goto the Wing Server http admin panel and click on a domain
#    - Click on Ftp Events
#    - Double Click OnFileUploaded
#      - Check mark "Enable Execute Program"
#      - Program Path is /path/to/where/you/put/checker you did earlier
#      - For Paramaters put: %Dir
#      - Press OK!
#
# Conclusion
#  - I would recommend having your ftp client set a priority on sending sfv first but on a side
#    note even if you sent a few files first and then uploaded the sfv how this scans files apon upload
#    it will check the previous files you uploaded and add them to the cached files
#
#  - Enjoy and let me know if you have troubles or like this!
#

my $home  = "/usr/files";
my $sfiles = 0; my $tfiles = 0;

# For creating empty missing/bad files
sub touch {
        if ($#_ != 0)   { return; }
        if ( -f $_[0] ) { return; }
        open tch, ">$_[0]"; close tch;
}

opendir(SFVCHK, ${home}.$ARGV[0]); my @SFVFIN = grep(/\.sfv/, readdir(SFVCHK)); closedir(SFVCHK);
opendir(SFVCHK, ${home}.$ARGV[0]); my @CMPCHK = grep(/^\[.*COMPLETE\s\]$/, readdir(SFVCHK)); closedir(SFVCHK);

# If there isn't a sfv or more than 1 just exit
if (@SFVFIN != 1) { exit; }

# Keep a hidden verified file to prevent double checking and system load
my @CHECKED = ();
if ( -e "${home}$ARGV[0]/.chk" ) {
        open CKSFV, "<${home}$ARGV[0]/.chk";
        while (<CKSFV>) { chomp($_); push(@CHECKED, $_); }
        close CKSFV;
}

# Open file for appending anymore valid files we might come across
open CKSFV, ">>${home}$ARGV[0]/.chk";

# Open the sfv and log the files that are suppose to be here
# before we run the sfv checker to compare
open  SFVFD, "<${home}$ARGV[0]/$SFVFIN[0]"; while (<SFVFD>) { chomp($_); push(@LINES, $_); }
close SFVFD;

foreach my $deldir (@CMPCHK) { rmdir "${home}$ARGV[0]/$deldir"; }

foreach my $line (@LINES) {
        chomp($line); if ($line =~ /^;/) { next; }
        my @SP = split(/ /, $line); my $rel = uc($SP[1]); $tfiles++;
        $rel =~ s/(\W)//g; # Make sure the crc string doesnt contain any control characters

        if ( -e "${home}$ARGV[0]/$SP[0]" ) {
          my $LFILE = $SP[0];
          if (grep(/$LFILE/, @CHECKED)) { $sfiles++; next; }
          open(CRCK, "${home}$ARGV[0]/$SP[0]"); my $crc = crc32(*CRCK); close(CRCK);

          my $hex = sprintf "%x", $crc; $hex = uc($hex);
          if (length($hex) < 8) { $hex = "0".$hex; }
          if (length($rel) < 8) { $rel = "0".$rel; }

          if ($hex eq $rel) {
                if ( -e "${home}$ARGV[0]/$SP[0]-missing" ) { unlink("${home}$ARGV[0]/$SP[0]-missing"); }
                if ( -e "${home}$ARGV[0]/$SP[0]-bad" ) { unlink("${home}$ARGV[0]/$SP[0]-bad"); }
                print CKSFV "$LFILE\n";
                $sfiles++;
          } else {
                if ( ! -e "${home}$ARGV[0]/$SP[0]-bad" ) { touch("${home}$ARGV[0]/$SP[0]-bad"); }
          }
        } else {
          if ( -e "${home}$ARGV[0]/$SP[0]-missing" ) { next; }
          touch("${home}$ARGV[0]/$SP[0]-missing");
        }
}

close CKSFV;

my $newdir = sprintf "[ %iF %iF FILES - 0.00%% COMPLETE ]", $sfiles, $tfiles;
if ($sfiles != 0) { $newdir = sprintf "[ %iF of %iF - %.1f%% COMPLETE ]", $sfiles, $tfiles, (($sfiles / $tfiles) * 100); }
if ($sfiles == $tfiles) { $newdir = "[ ${tfiles}F COMPLETE ]"; }
mkdir "${home}$ARGV[0]/$newdir";
benny
 
posts 1
 
joined Wed Dec 15, 2010 8:29 am

Re: SFV Checker

Postby FTP » Wed Dec 15, 2010 9:56 am

Hi,Benny

Thanks for your contribution! Hope your Perl scripts will be helpful for somebody.
FTP
Site Admin
 
posts 1212
 
joined Tue Sep 29, 2009 6:09 am


Return to Lua scripts

Who is online

Users browsing this forum: No registered users and 3 guests