Util::ShortTermRepository (version 0.0)


package Util::ShortTermRepository;
##############################################################################
#
# DESCRIPTION: 
#
# HISTORY:
#
# VERSION: 0.0
#
##############################################################################

use strict;

#########################
# constructor
#########################
sub new { #(directory)
    my $proto = shift;
    my $class = ref($proto) || $proto;

    #####################################
    # initialize some variables
    #####################################
    my $self={};
    $self->{DIR}=shift;
    $self->{FILENAME}=shift;
    $self->{FILES}={};
    $self->{WAIT}=60; # ten minutes
    $self->{MAX_COUNT}=5;

    bless($self,$class);
    return $self;

} # end of constructor

#########################
# accessors
#########################

##############################################################################
#
##############################################################################
sub log {
    my $self = shift;
    return $self->{FILENAME}->log();
}

#########################
# methods
#########################

##############################################################################
#
##############################################################################
sub export {
    my $self = shift;
    my $type = shift;
    my $log = $self->log();

    my $dir = "$self->{DIR}/$type";

    my @files = @_;
    unless(@files) {
        #########################################
        # export all the files of the given type
        #########################################
        @files = $self->{FILENAME}->any($type);
    }

    ########################################
    # copy all the files to the repository
    ########################################
    my $file;
    foreach $file (@files) {
      my $checksum = "$dir/.$file.sha1";
      unlink $checksum;
      system("sha1sum $file > $checksum");
      system("cp $file $dir");
    }

} # end of export method

##############################################################################
#
##############################################################################
sub fetch {
    my $self = shift;
    my $type = shift;
    my @args = @_;
    my $log = $self->log();

    my $filename = $self->{FILENAME};
    my $dir = "$self->{DIR}/$type";

    my $file;
    my $okaysum = 0;
    my $count = 0;
    until($okaysum){
      ##############################################
      # locate the file in the repository directory
      ##############################################
      $filename->glob_dir($dir);
      $file = $filename->get($type, @args);

      ################################################################
      # if we located the file, copy it into the working directory
      ################################################################
      if($file && $self->{FILENAME}->fetch($file, $dir) ) {
      
	my $checksum = "$dir/.$file.sha1";
	if( -f $checksum){
	  open SUM, $checksum;
	  my $sum = (split ' ', <SUM>)[0];
	  close SUM;

	  open SUM, "sha1sum $file |";
	  my $check = (split ' ', <SUM>)[0];
	  close SUM;

	  $okaysum = 1 if $sum eq $check;
	}
      }
      if($okaysum){
        ###########################################################
        # we got a file, so remember its name for cataloging later
        ###########################################################
        $self->remember($type, $file);
      }else{
	unlink $file;
	unless($count){
	  $log->entry("Waiting for repository while fetching $file.");
	}
	last if $count++ > $self->{MAX_COUNT};
	sleep $self->{WAIT};
      }
    }

    unless($okaysum){
      $log->error(1, "Repository timeout while file fetching $file.");
    }
    return $file;
    
} # end of fetch method


##############################################################################
# store a file name and type internally so that it can be included in a catalog
# of all fetched files later.
##############################################################################
sub remember {
    my $self = shift;
    my $type = shift;
    my $file = shift;

    unless($self->{FILES}->{$type}) {
        $self->{FILES}->{$type} = {};
    }

    $self->{FILES}->{$type}->{$file} =1;

} # end of remember method


1;