Filesystem.pm 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package SugarTrail::Repo::Filesystem;
  2. # provide access to STM data
  3. # * init()
  4. # * load_text()
  5. # * load_vcs()
  6. use strict;
  7. use warnings;
  8. use parent 'SugarTrail::Repo';
  9. use Carp;
  10. # initialize repo
  11. sub new {
  12. my $class = shift;
  13. my $args = { @_ };
  14. exists($args->{$_}) or croak "missing mandatory parameter: $_"
  15. foreach (qw/root/);
  16. my $self = { %$args };
  17. unless (-d $args->{root}) {
  18. $self->{last_error} = "invalid repository root directory: "
  19. . $args->{root};
  20. carp $self->{last_error};
  21. }
  22. return bless $self, $class;
  23. }
  24. sub _get_filename {
  25. my $self = shift;
  26. my $source = shift;
  27. return $self->{root} . $source;
  28. }
  29. sub load_text {
  30. my $self = shift;
  31. my $source = shift;
  32. # load
  33. my $fn = $self->_get_filename($source);
  34. my $fh;
  35. unless (open $fh, "<", $fn) {
  36. $self->{last_error} = "cannot open master $fn for reading: $!";
  37. carp $self->{last_error};
  38. return;
  39. }
  40. my $text = join "", <$fh>;
  41. close $fh;
  42. return $text;
  43. }
  44. 1;