package Util::ShortTermRepository; ############################################################################## # # DESCRIPTION: # # HISTORY: # # VERSION: 0.0 # ############################################################################## use strict; use LockFile::Simple; ######################### # 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->{LOCK_TIMEOUT}=60*10; # ten minutes bless($self,$class); return $self; } # end of constructor ######################### # accessors ######################### ############################################################################## # ############################################################################## sub log { my $self = shift; return $self->{FILENAME}->log(); } ######################### # methods ######################### ############################################################################## # ############################################################################## sub lock { my $self = shift; my $dir = shift; my $log = $self->log(); my $locker = LockFile::Simple->make(-max => 30, -delay => 10, -nfs => 1, -stale => 1, -warn => 0); ############################################## # tell the log if we have to wait for a lock ############################################## my $lock_handle = $locker->trylock($dir); unless( $lock_handle ){ $log->entry("Waiting for lock on repository dir $dir"); $lock_handle = $locker->lock($dir); } ################################# # make sure the directory exists ################################# if ( ! -d $dir ) { system("mkdir $dir"); } return $lock_handle; } # end of lock method ############################################################################## # ############################################################################## sub export { my $self = shift; my $type = shift; my $log = $self->log(); my $dir = "$self->{DIR}/$type"; my $lock_handle = $self->lock($dir); unless($lock_handle){ $log->error(2, "Unable to export $type files to the repository."); return; } 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) { system("cp $file $dir"); } $lock_handle->release(); } # 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"; ######################## # lock the repository ######################## my $file; my $lock_handle = $self->lock($dir); unless($lock_handle){ $log->error(2, "Unable to fetch files from the repository: ". join(' ', ($type, @args)) ); return $file; } ############################################## # 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) ) { ########################################################### # we got a file, so remember its name for cataloging later ########################################################### $self->remember($type, $file); } $lock_handle->release(); 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;