/usr/share/perl5/FCM/System/Make/Share/Dest.pm is in fcm 2016.12.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | # ------------------------------------------------------------------------------
# (C) British Crown Copyright 2006-16 Met Office.
#
# This file is part of FCM, tools for managing and building source code.
#
# FCM 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 3 of the License, or
# (at your option) any later version.
#
# FCM 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 FCM. If not, see <http://www.gnu.org/licenses/>.
# ------------------------------------------------------------------------------
use strict;
use warnings;
# ------------------------------------------------------------------------------
package FCM::System::Make::Share::Dest;
use base qw{FCM::Class::CODE};
use Cwd qw{cwd};
use FCM::Context::Event;
use FCM::System::Exception;
use File::Basename qw{dirname};
use File::Path qw{mkpath rmtree};
use File::Spec::Functions qw{catfile rel2abs};
use File::Temp;
use IO::Uncompress::Gunzip qw{gunzip};
use IO::Compress::Gzip qw{gzip};
use Scalar::Util qw{blessed reftype};
use Storable qw{fd_retrieve nstore_fd};
use Sys::Hostname qw{hostname};
# The relative paths for locating files in a destination
our %PATH_OF = (
'config' => 'fcm-make%s.cfg',
'config-orig' => 'fcm-make%s.cfg.orig',
'sys' => '.fcm-make%s',
'sys-cache' => '.fcm-make%s/cache',
'sys-config-as-parsed' => '.fcm-make%s/config-as-parsed.cfg',
'sys-config-as-parsed-symlink' => 'fcm-make%s-as-parsed.cfg',
'sys-config-on-success' => '.fcm-make%s/config-on-success.cfg',
'sys-config-on-success-symlink' => 'fcm-make%s-on-success.cfg',
'sys-ctx-uncompressed' => '.fcm-make%s/ctx',
'sys-ctx' => '.fcm-make%s/ctx.gz',
'sys-log' => '.fcm-make%s/log',
'sys-log-symlink' => 'fcm-make%s.log',
'sys-lock' => 'fcm-make%s.lock',
'sys-lock-info' => 'fcm-make%s.lock/info.txt',
'target' => '',
);
# Aliases to exception classes
my $E = 'FCM::System::Exception';
# List of actions
my %ACTION_OF = (
ctx_load => \&_ctx_load,
dest_done => \&_dest_done,
dest_init => \&_dest_init,
path => \&_path,
paths => \&_paths,
path_of => sub {$_[0]->{'path_of'}{$_[1]}},
save => \&_save,
tidy => \&_tidy,
);
# Creates the class.
__PACKAGE__->class(
{ path_of => {isa => '%', default => {%PATH_OF}},
shared_util_of => '%',
subsystem_of => '%',
util => '&',
},
{action_of => \%ACTION_OF},
);
# Loads a storable context from a path.
sub _ctx_load {
my ($attrib_ref, $m_ctx, $from) = @_;
my $path;
my $dest;
if ($from) {
NAME:
for my $name ($m_ctx->get_name(), undef) {
$path = _path(
$attrib_ref, {'dest' => $from, 'name' => $name}, 'sys-ctx');
if (-f $path) {
$dest = $from;
last NAME;
}
}
}
else {
$path = _path($attrib_ref, $m_ctx, 'sys-ctx');
$dest = $m_ctx->get_dest();
}
my $old_m_ctx = eval {
my $handle = File::Temp->new('TMPDIR' => 1);
# Open the file here to work around permission problems with file ACLs
open(my $path_handle, '<', $path) || die($!);
gunzip($path_handle, $handle) || die($!);
$handle->seek(0, 0);
fd_retrieve($handle);
};
if (my $e = $@) {
return $E->throw($E->CACHE_LOAD, $path, $e);
}
if ( !$old_m_ctx
|| !$old_m_ctx->isa(blessed($m_ctx))
|| ( defined($old_m_ctx->get_name())
&& $old_m_ctx->get_name() ne $m_ctx->get_name()
)
) {
return $E->throw($E->CACHE_TYPE, $path);
}
my $new_m_dest = rel2abs($dest);
if ($new_m_dest ne $old_m_ctx->get_dest()) {
my $old_m_dest = $old_m_ctx->get_dest();
$old_m_ctx->set_dest($new_m_dest);
$old_m_ctx->set_dest_lock(undef);
SUBSYSTEM:
while (my ($id, $old_ctx) = each(%{$old_m_ctx->get_ctx_of()})) {
my $id_of_class = $old_ctx->get_id_of_class();
if (exists($attrib_ref->{'subsystem_of'}{$id_of_class})) {
my $subsystem = $attrib_ref->{'subsystem_of'}{$id_of_class};
if (!$old_ctx->can('set_dest')) {
next SUBSYSTEM;
}
my $old_dest = $old_ctx->get_dest();
$old_ctx->set_dest(_path(
$attrib_ref,
{'dest' => $new_m_dest, 'name' => $m_ctx->get_name()},
'target',
$old_ctx->get_id(),
));
if ($subsystem->can('ctx_load_hook')) {
$subsystem->ctx_load_hook(
$old_m_ctx, $old_ctx, $old_m_dest, $old_dest);
}
}
}
}
return $old_m_ctx;
}
# Finalises the destination of a make context.
sub _dest_done {
my ($attrib_ref, $m_ctx) = @_;
if (!$m_ctx->get_dest()) {
return;
}
my $dest = _path($attrib_ref, $m_ctx, 'sys-ctx-uncompressed');
my $dest_parent = dirname($dest);
my $dest_lock = $m_ctx->get_dest_lock();
$m_ctx->set_dest_lock(undef);
if (-d $dest_parent) {
eval {
my $handle = File::Temp->new('TMPDIR' => 1);
nstore_fd($m_ctx, $handle) || die($!);
$handle->seek(0, 0) || die($!);
gzip($handle, _path($attrib_ref, $m_ctx, 'sys-ctx')) || die($!);
};
if (my $e = $@) {
return $E->throw($E->DEST_CREATE, $dest, $e);
}
}
my %ctx_of = %{$m_ctx->get_ctx_of()};
for my $path (
_path($attrib_ref, $m_ctx, 'sys'),
(map {_path($attrib_ref, $m_ctx, 'target', $_)} keys(%ctx_of)),
) {
_tidy($attrib_ref, $path);
}
if ($dest_lock) {
rmtree($dest_lock);
}
}
# Initialises the destination of a make context.
sub _dest_init {
my ($attrib_ref, $m_ctx) = @_;
my %OPTION_OF = %{$m_ctx->get_option_of()};
# Select destination
my $dest
= $OPTION_OF{directory} ? $OPTION_OF{directory}
: $m_ctx->get_dest() ? $m_ctx->get_dest()
: cwd()
;
$m_ctx->set_dest(rel2abs($dest));
# Check lock
my $lock = _path($attrib_ref, $m_ctx, 'sys-lock');
if (!$OPTION_OF{'ignore-lock'} && -e $lock) {
return $E->throw($E->DEST_LOCKED, $lock);
}
# Creates the lock (and the destination), if necessary
if (!-e $lock) {
eval {mkpath($lock)};
if (my $e = $@) {
return $E->throw($E->DEST_CREATE, $lock, $e);
}
my $lock_info = scalar(getpwuid($<)) . '@' . hostname() . ':' . $$;
_save($attrib_ref, $lock_info, $m_ctx, 'sys-lock-info');
}
$m_ctx->set_dest_lock($lock);
# Cleans items created by previous make, if necessary
for my $path (
_path($attrib_ref, $m_ctx, 'sys-config-as-parsed-symlink'),
_path($attrib_ref, $m_ctx, 'sys-config-on-success-symlink'),
_path($attrib_ref, $m_ctx, 'sys-config-on-success'),
_path($attrib_ref, $m_ctx, 'sys-log-symlink'),
) {
eval {rmtree($path)};
if (my $e = $@) {
return $E->throw($E->DEST_CLEAN, $path, $e);
}
}
if ($OPTION_OF{new}) {
my @steps = @{$m_ctx->get_steps()};
for my $path (
_path($attrib_ref, $m_ctx, 'sys'),
(map {_path($attrib_ref, $m_ctx, 'target', $_)} @steps),
) {
eval {rmtree($path)};
if (my $e = $@) {
return $E->throw($E->DEST_CLEAN, $path, $e);
}
}
}
# Loads context of previous make, if possible
my $prev_m_ctx = eval {_ctx_load($attrib_ref, $m_ctx)};
if (my $e = $@) {
if ( !$E->caught($e)
|| !grep {$_ eq $e->get_code()} ($E->CACHE_LOAD, $E->CACHE_TYPE)
) {
die($e);
}
$@ = undef;
}
if (defined($prev_m_ctx)) {
$m_ctx->set_prev_ctx($prev_m_ctx);
}
else {
# Creates the system directory
my $sys_dir_path = _path($attrib_ref, $m_ctx, 'sys');
eval {mkpath($sys_dir_path)};
if (my $e = $@) {
return $E->throw($E->DEST_CREATE, $sys_dir_path, $e);
}
}
# Diagnostic
$attrib_ref->{util}->event(
FCM::Context::Event->MAKE_DEST,
$m_ctx, join('@', scalar(getpwuid($<)), hostname()),
);
1;
}
# Returns the path of a named item relative to the context destination.
sub _path {
my ($attrib_ref, $m_ctx, $key, @paths) = @_;
my %ctx = reftype($m_ctx) && reftype($m_ctx) eq 'HASH'
? %{$m_ctx} : ('dest' => $m_ctx, 'name' => q{});
$ctx{'dest'} ||= q{};
$ctx{'name'} ||= q{};
my $path_of_key = $attrib_ref->{path_of}{$key};
catfile(
($ctx{'dest'} ? $ctx{'dest'} : ()),
split(
q{/},
($path_of_key ? sprintf($path_of_key, $ctx{'name'}) : $path_of_key),
),
@paths,
);
}
# Returns an ARRAY reference containing the search paths of a named item
# relative to the destinations of the context and its inherited contexts.
sub _paths {
my ($attrib_ref, $m_ctx, $key, @paths) = @_;
my @dests;
my @ctx_list = ($m_ctx);
# Adds destinations from inherited contexts recursively
# Note: if A inherits from B and C, B from B1 and B2, and C from C1 and C2,
# the search path will be A, C, C2, C1, B, B2, B1.
while (my $current_ctx = pop(@ctx_list)) {
push(@ctx_list, @{$current_ctx->get_inherit_ctx_list()});
push(@dests, _path($attrib_ref, $current_ctx, $key, @paths));
}
return \@dests;
}
# Saves $item in a path given by _path($attrib_ref, $m_ctx, $key, @paths).
sub _save {
my ($attrib_ref, $item, $m_ctx, $key, @paths) = @_;
my $path = _path($attrib_ref, $m_ctx, $key, @paths);
my @contents
= (ref($item) && ref($item) eq 'ARRAY') ? (map {$_ . "\n"} @{$item})
: ($item . "\n")
;
$attrib_ref->{util}->file_save($path, \@contents);
}
# Removes empty directories in a tree.
sub _tidy {
my ($attrib_ref, @paths) = @_;
# Selects only directories which are not symbolic links
my @items = map {[$_, undef, undef]} grep {-d && !-l} @paths;
while (my $item = pop(@items)) {
my ($path, $n_children_ref, $n_siblings_ref) = @{$item};
if (!defined($n_children_ref)) {
opendir(my $handle, $path)
|| return $E->throw($E->DEST_CLEAN, $path, $!);
my @children = grep {$_ ne q{.} && $_ ne q{..}} (readdir($handle));
closedir($handle);
$n_children_ref = \scalar(@children);
if (@children) {
# Descends into directories
my @sub_dirs
= grep {-d && !-l} map {catfile($path, $_)} @children;
if (@sub_dirs == @children) {
# If all children are directories, it may be possible to
# remove this directory later if all children are empty
push(@items, [$path, $n_children_ref, $n_siblings_ref]);
}
push(@items, (map {[$_, undef, $n_children_ref]} @sub_dirs));
}
}
if (!${$n_children_ref}) { # i.e. directory is empty
rmdir($path) || return $E->throw($E->DEST_CLEAN, $path, $!);
if (defined($n_siblings_ref)) {
--${$n_siblings_ref};
}
}
}
}
# ------------------------------------------------------------------------------
1;
__END__
=head1 NAME
FCM::System::Make::Share::Dest
=head1 SYNOPSIS
use FCM::System::Make::Share::Dest;
my $helper = FCM::System::Make::Share::Dest->new(\%attrib);
my $ctx = $helper->ctx_load($path, $expected_class);
my $path = $helper->path($m_ctx, $key);
# ...
=head1 DESCRIPTION
A helper class for manipulating the destination of a context in a FCM make
sub-system, e.g. extract.
=head1 METHODS
=over 4
=item $class->new(\%attrib)
Returns a new instance. The %attrib should contain the following:
=over 4
=item dest_items
An ARRAY containing the names of the items that can be created at the context
destination.
=item path_of
A HASH to map the (keys) names of the items and (values) their relative paths
(as ARRAY) in a context destination.
=back
=item $instance->ctx_load($path,$expected_class)
Loads a storable context from $path and returns the context. The $expected_class
is the expected class of the loaded context. The method die() if it fails to
load the context or if the loaded context does not belong to the expected class.
=item $instance->dest_done($ctx)
Finalises the destination of $ctx by freezing the $ctx in the system directory,
removing the lock file, and tidying up any empty directories created by the
system.
=item $instance->dest_init($ctx)
Initialises the destination of $ctx by checking for a lock directory in the
destination, creating a lock if possible, cleaning up items created by the
previous make of the system if necessary, and setting up the system directory.
=item $instance->path($ctx,$key,@paths)
Returns the path of a named item ($key) relative to $ctx, which can either be a
HASH reference with {'dest' => $dest, 'name' => $name}, or a scalar path
pointing to $dest, where $dest is the root of the path and $name is the name of
the context. If @paths are specified, they are concatenated at the end
of the path.
=item $instance->paths($ctx,$key,@paths)
Returns an ARRAY reference containing the search paths of a named item ($key)
relative to the destinations of $ctx and its inherited contexts. If @paths are
specified, they are concatenated at the end of each returned path.
=item $instance->path_of($key)
Returns the template value of the named item in a make destination.
=item $instance->save($item,$ctx,$key,@paths)
Saves $item in a path given by $instance->path($ctx,$key,@paths). $item can be a
string or a reference to an ARRAY of strings. A "\n" is added to the end of each
string.
=item $instance->tidy(@paths)
Recursively removes empty directories in @paths.
=back
=head1 CONSTANTS
=over 4
=item %FCM::System::Make::PATH_OF
A HASH containing the default values of named paths in a make destination. The
following keys are used by the system:
=over 4
=item config
The standard path to the configuration file.
=item sys
The path to the system directory.
=item sys-cache
The path to the system cache directory.
=item sys-config-as-parsed
The path to the as-parsed configuration file.
=item sys-config-on-success
The path to the on-success configuration file.
=item sys-ctx
The path to the frozen make context (for retrieval by incremental makes).
=item sys-ctx-uncompressed
The path to the uncompressed form of sys-ctx.
=item sys-lock
The path to the lock directory.
=item sys-lock-info
The path to the lock info file.
=item target
The target destination of a make.
=back
=back
=head1 DIAGNOSTICS
=head2 FCM::System::Exception
The methods of this class throws this exception on errors.
=head1 TODO
Time-stamp the as-parsed and the on-success configuration files.
=head1 COPYRIGHT
(C) Crown copyright Met Office. All rights reserved.
=cut
|