ac397a166a511ce06d039cb40733b1b7b6c8de25
[scpubgit/DKit.git] / lib / DX / Lib / FS / Observation / PathStatus.pm
1 package DX::Lib::FS::Observation::PathStatus;
2
3 use aliased 'DX::Lib::FS::Fact::PathStatus';
4 use aliased 'DX::Lib::FS::Fact::PathStatusInfo';
5 use POSIX qw(ENOENT);
6 use File::stat;
7 use Moo;
8
9 has path => (is => 'ro', required => 1);
10
11 sub run {
12   my ($self) = @_;
13   if (my $stat = stat(my $path = $self->path)) {
14     (path_status => PathStatus->new(
15       path => $path,
16       info => PathStatusInfo->new(
17         is_directory => -d _,
18         is_file => -f _,
19         mode => sprintf("%04o", ($stat->mode & 07777)),
20       )
21     ));
22   } elsif ($! == ENOENT) {
23     (path_status => PathStatus->new(path => $path));
24   } else {
25     die "Couldn't stat ${path}: $!";
26   }
27 }
28
29 1;