Filesystem.pm 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 get_revision {
  30. my $self = shift;
  31. my $source = shift // $self->{source};
  32. my $file = $self->_get_filename($source);
  33. return (stat($file))[9]; # 9 = mtime
  34. }
  35. sub load_text {
  36. my $self = shift;
  37. my $source = shift;
  38. # load
  39. my $fn = $self->_get_filename($source);
  40. my $fh;
  41. unless (open $fh, "<", $fn) {
  42. $self->{last_error} = "cannot open master $fn for reading: $!";
  43. carp $self->{last_error};
  44. return;
  45. }
  46. my $text = join "", <$fh>;
  47. close $fh;
  48. return $text;
  49. }
  50. 1;