#! /local/bin/perl ###################################################################### # $Header: /loc/adm/slink-1.0/src/slink-5.0/Slink/RCS/Mapper.pm,v 5.0 1996/05/13 17:14:27 couch Exp $ # Slink::Mapper.pm: maintain maps of copy sources in Duper. # Revision 5.0 # by # Alva L. Couch, # Associate Prof. of EE/CS # Department of EE/CS # Tufts University, # Medford, Massachusetts, 02155. # couch@cs.tufts.edu # # Copyright (C) 1996 by Alva L. Couch # # This file is part of SLINK # # SLINK is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # SLINK is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with GNU CC; see the file COPYING. If not, write to # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. ###################################################################### package Slink::Mapper; require 5.001; require Exporter; use Cwd; use Slink::Logger; use Slink::MapHash; use Slink::Omniopter; use strict; BEGIN { @Slink::Mapper::ISA = qw(Exporter); @Slink::Mapper::EXPORT = (); @Slink::Mapper::EXPORT_OK = qw(wasmapped mapped source add del reader writer check close); } # a Slink::Mapper works because of an execution context telling it what to do. # This context is a hash to things it needs: # - a filename to read and write # - a Slink::Logger to print to # Given a hash of these, check them and set up as an execution context. sub new { my ($module,$ref) = @_; $ref = {} if ! defined $ref; my $newref = bless [undef,undef,undef]; $newref->config($ref); $newref->default; $newref->[&HASH]=new Slink::MapHash($newref->filename,$newref->logger); return $newref; } # numeric offsets of Slink::Mapper members sub FILENAME { 0 } sub HASH { 1 } sub LOGGER { 2 } sub READMAP { 3 } sub WRITEMAP { 4 } # Formal accessors for Slink::Mapper members sub filename { $_[0]->[&FILENAME]; } sub hash { $_[0]->[&HASH]; } sub logger { $_[0]->[&LOGGER]; } sub readmap { $_[0]->[&READMAP]; } sub writemap { $_[0]->[&WRITEMAP]; } ###################################################################### # set up resources ###################################################################### sub set_filename { my ($this,$pro) = @_; $this->[&FILENAME] = $pro; } sub set_readmap { my ($this,$pro) = @_; $this->[&READMAP] = $pro; } sub set_writemap { my ($this,$pro) = @_; $this->[&WRITEMAP] = $pro; } sub set_logger { my ($this,$log) = @_; if (ref($log) eq 'Slink::Logger') { $this->[&LOGGER] = $log; } else { printf STDERR "passed ref isn't a Slink::Logger"; } } # default unset or incorrect parameters sub default { my ($this) = @_; if (! defined $this->logger) { $this->set_logger(new Slink::Logger()); } elsif (ref($this->logger) ne 'Slink::Logger') { $this->set_logger(new Slink::Logger()); $this->logger->error("replacing invalid Slink::Logger with default"); } if (! defined $this->readmap) { $this->set_readmap(defined $this->filename ? 1 : 0); } if (! defined $this->writemap) { $this->set_writemap(defined $this->filename ? 1 : 0); } } # configure a Slink::Mapper from a hash table sub config { my ($this,$hash) = @_; my @keys = ('logger', 'filename', 'readmap', 'writemap'); my (@illegal) = Slink::Omniopter::illegal($hash, @keys); if (@illegal) { my $illegal = join(' ',@illegal); print STDERR "Slink::Mapper::config: illegal options $illegal\n"; } $this->set_filename($hash->{'filename'}) if (defined $hash->{'filename'}); $this->set_logger($hash->{'logger'}) if (defined $hash->{'logger'}); $this->set_readmap($hash->{'readmap'}) if (defined $hash->{'readmap'}); $this->set_writemap($hash->{'writemap'}) if (defined $hash->{'writemap'}); } # read the Slink::Mapper from a file. sub reader { my ($this,$filename) = @_; $this->[&FILENAME] = $filename if defined $filename; $this->[&HASH] = bless {}; $this->hash->reader($this->filename,$this->logger) if defined $this->filename; } # write out the Slink::Mapper into a file from which it can be re-read sub writer { my ($this,$filename) = @_; $this->[&FILENAME] = $filename if defined $filename; $this->hash->writer($this->filename,$this->logger) if defined $this->filename; } # To copy a Slink::Mapper, copy its hash and descriptor sub copy { bless [ $_[0]->filename, $_[0]->hash->copy, $_[0]->logger] } # check a mapper for internal consistency; # report multiple source errors sub check { my ($this) = @_; $this->hash->check($this->logger); } # close a mapper, writing consistency errors to logfile and # writing out the map file. If $filename is set, then # it overrides whatever mapfile is recorded internally. sub close { my ($this,$filename) = @_; $this->[&FILENAME] = $filename if defined $filename ; $this->hash->close($this->filename,$this->logger); } # fallthroughs for all other Slink::MapHash calls sub wasmapped { $_[0]->hash->wasmapped(@_[1..$#_]) } sub mapped { $_[0]->hash->mapped(@_[1..$#_]) } sub source { $_[0]->hash->source(@_[1..$#_]) } sub add { $_[0]->hash->add(@_[1..$#_]) } sub del { $_[0]->hash->del(@_[1..$#_]) } 1;