1 | # ------------------------------------------------------------------------------ |
---|
2 | # Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
3 | # |
---|
4 | # This file is part of FCM, tools for managing and building source code. |
---|
5 | # |
---|
6 | # FCM is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, either version 3 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # FCM is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | # ------------------------------------------------------------------------------ |
---|
19 | use strict; |
---|
20 | use warnings; |
---|
21 | |
---|
22 | # ------------------------------------------------------------------------------ |
---|
23 | package FCM::Util::Locator::FS; |
---|
24 | use base qw{FCM::Class::CODE}; |
---|
25 | |
---|
26 | use File::Basename qw{dirname}; |
---|
27 | use File::Find qw{}; |
---|
28 | use File::Spec; |
---|
29 | |
---|
30 | our %ACTION_OF = ( |
---|
31 | can_work_with => sub {1}, |
---|
32 | can_work_with_rev => sub {}, |
---|
33 | cat => \&_cat, |
---|
34 | dir => \&_dir, |
---|
35 | find => \&_find, |
---|
36 | origin => \&_parse, |
---|
37 | parse => \&_parse, |
---|
38 | reader => \&_reader, |
---|
39 | read_property => sub {}, |
---|
40 | test_exists => \&_test_exists, |
---|
41 | trunk_at_head => sub {}, |
---|
42 | ); |
---|
43 | |
---|
44 | # Creates the class. |
---|
45 | __PACKAGE__->class({util => '&'}, {action_of => \%ACTION_OF}); |
---|
46 | |
---|
47 | # Joins @paths to the end of $value. |
---|
48 | sub _cat { |
---|
49 | my ($attrib_ref, $value, @paths) = @_; |
---|
50 | _parse( |
---|
51 | $attrib_ref, |
---|
52 | File::Spec->catfile(scalar(_parse($attrib_ref, $value)), @paths), |
---|
53 | ); |
---|
54 | } |
---|
55 | |
---|
56 | # Returns the directory name of $value. |
---|
57 | sub _dir { |
---|
58 | my ($attrib_ref, $value) = @_; |
---|
59 | dirname(scalar(_parse($attrib_ref, $value))); |
---|
60 | } |
---|
61 | |
---|
62 | # Searches directory tree. |
---|
63 | sub _find { |
---|
64 | my ($attrib_ref, $value, $callback) = @_; |
---|
65 | my $found; |
---|
66 | File::Find::find( |
---|
67 | sub { |
---|
68 | $found ||= 1; |
---|
69 | my $path = $File::Find::name; |
---|
70 | my $ns = File::Spec->abs2rel($path, $value); |
---|
71 | if ($ns eq q{.}) { |
---|
72 | $ns = q{}; |
---|
73 | } |
---|
74 | else { |
---|
75 | for my $name (split(q{/}, $ns)) { |
---|
76 | if (index($name, q{.}) == 0) { |
---|
77 | return; # ignore Unix hidden/system files |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | my $last_mod_time = (-l $path ? lstat($path) : stat($path))[9]; |
---|
82 | $callback->( |
---|
83 | $path, |
---|
84 | { is_dir => scalar(-d $path), |
---|
85 | last_mod_rev => undef, |
---|
86 | last_mod_time => $last_mod_time, |
---|
87 | ns => $ns, |
---|
88 | }, |
---|
89 | ); |
---|
90 | }, |
---|
91 | $value, |
---|
92 | ); |
---|
93 | return $found; |
---|
94 | } |
---|
95 | |
---|
96 | # Returns $value in scalar context, or ($value,undef) in list context. |
---|
97 | sub _parse { |
---|
98 | my ($attrib_ref, $value) = @_; |
---|
99 | $value = $attrib_ref->{util}->file_tilde_expand($value); |
---|
100 | $value = File::Spec->rel2abs($value); |
---|
101 | my ($vol, $dir_name, $base) = File::Spec->splitpath($value); |
---|
102 | my @dir_names; |
---|
103 | my %HANDLER_OF = ( |
---|
104 | q{} => sub {push(@dir_names, $_[0])}, |
---|
105 | q{.} => sub {}, |
---|
106 | q{..} => sub {if (@dir_names > 1) {pop(@dir_names)}}, |
---|
107 | ); |
---|
108 | for my $name (File::Spec->splitdir($dir_name)) { |
---|
109 | my $handler |
---|
110 | = exists($HANDLER_OF{$name}) ? $HANDLER_OF{$name} : $HANDLER_OF{q{}}; |
---|
111 | $handler->($name); |
---|
112 | } |
---|
113 | $value = File::Spec->catpath($vol, File::Spec->catdir(@dir_names), $base); |
---|
114 | return (wantarray() ? ($value, undef) : $value); |
---|
115 | } |
---|
116 | |
---|
117 | # Returns a reader (file handle) for a given file system value. |
---|
118 | sub _reader { |
---|
119 | my ($attrib_ref, $value) = @_; |
---|
120 | $value = _parse($attrib_ref, $value); |
---|
121 | open(my $handle, '<', $value) || die("$!\n"); |
---|
122 | return $handle; |
---|
123 | } |
---|
124 | |
---|
125 | # Return a true value if the location $value exists. |
---|
126 | sub _test_exists { |
---|
127 | my ($attrib_ref, $value) = @_; |
---|
128 | -e $value; |
---|
129 | } |
---|
130 | |
---|
131 | # ------------------------------------------------------------------------------ |
---|
132 | 1; |
---|
133 | __END__ |
---|
134 | |
---|
135 | =head1 NAME |
---|
136 | |
---|
137 | FCM::Util::Locator::FS |
---|
138 | |
---|
139 | =head1 SYNOPSIS |
---|
140 | |
---|
141 | use FCM::Util::Locator::FS; |
---|
142 | $util = FCM::Util::Locator::FS->new(\%option); |
---|
143 | $handle = $util->reader($value); |
---|
144 | |
---|
145 | =head1 DESCRIPTION |
---|
146 | |
---|
147 | Provides utilities to manipulate the values of file system locators. |
---|
148 | |
---|
149 | =head1 METHODS |
---|
150 | |
---|
151 | =over 4 |
---|
152 | |
---|
153 | =item $util->can_work_with($value) |
---|
154 | |
---|
155 | Dummy. Always returns true. |
---|
156 | |
---|
157 | =item $util->can_work_with_rev($revision) |
---|
158 | |
---|
159 | Dummy. Always returns false. |
---|
160 | |
---|
161 | =item $util->cat($value,@paths) |
---|
162 | |
---|
163 | Joins @paths to the end of $value. |
---|
164 | |
---|
165 | =item $util->dir($value) |
---|
166 | |
---|
167 | Returns the parent directory of $value. |
---|
168 | |
---|
169 | =item $util->find($value,$callback) |
---|
170 | |
---|
171 | Searches directory tree of $value. |
---|
172 | |
---|
173 | =item $util->origin($value) |
---|
174 | |
---|
175 | Alias of $util->parse($value). |
---|
176 | |
---|
177 | =item $util->parse($value) |
---|
178 | |
---|
179 | In scalar context, returns $value. In list context, returns ($value,undef). |
---|
180 | |
---|
181 | =item $util->reader($value) |
---|
182 | |
---|
183 | Returns a file handle for $value, if it is a readable regular file. |
---|
184 | |
---|
185 | =item $util->read_property($value,$property_name) |
---|
186 | |
---|
187 | Dummy. Always returns undef. |
---|
188 | |
---|
189 | =item $util->test_exists($value) |
---|
190 | |
---|
191 | Return a true value if the location $value exists. |
---|
192 | |
---|
193 | =item $util->trunk_at_head($value) |
---|
194 | |
---|
195 | Dummy. Always returns undef. |
---|
196 | |
---|
197 | =back |
---|
198 | |
---|
199 | =head1 COPYRIGHT |
---|
200 | |
---|
201 | Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
202 | |
---|
203 | =cut |
---|