2019-06-27 09:02:19 -04:00
|
|
|
# This file is part of GNU Stow.
|
|
|
|
#
|
|
|
|
# GNU Stow 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.
|
|
|
|
#
|
|
|
|
# GNU Stow 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 this program. If not, see https://www.gnu.org/licenses/.
|
|
|
|
|
2011-11-24 11:28:09 -05:00
|
|
|
package Stow::Util;
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
Stow::Util - general utilities
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
use Stow::Util qw(debug set_debug_level error ...);
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
Supporting utility routines for L<Stow>.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2024-03-29 18:52:12 -04:00
|
|
|
use File::Spec;
|
2011-11-24 11:28:09 -05:00
|
|
|
use POSIX qw(getcwd);
|
|
|
|
|
|
|
|
use base qw(Exporter);
|
|
|
|
our @EXPORT_OK = qw(
|
|
|
|
error debug set_debug_level set_test_mode
|
2016-07-31 16:55:55 -04:00
|
|
|
join_paths parent canon_path restore_cwd adjust_dotfile
|
2011-11-24 11:28:09 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
our $ProgramName = 'stow';
|
2015-11-09 06:36:11 -05:00
|
|
|
our $VERSION = '@VERSION@';
|
2011-11-24 11:28:09 -05:00
|
|
|
|
|
|
|
#############################################################################
|
|
|
|
#
|
|
|
|
# General Utilities: nothing stow specific here.
|
|
|
|
#
|
|
|
|
#############################################################################
|
|
|
|
|
|
|
|
=head1 IMPORTABLE SUBROUTINES
|
|
|
|
|
|
|
|
=head2 error($format, @args)
|
|
|
|
|
|
|
|
Outputs an error message in a consistent form and then dies.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
sub error {
|
|
|
|
my ($format, @args) = @_;
|
2012-07-08 20:06:13 -04:00
|
|
|
die "$ProgramName: ERROR: " . sprintf($format, @args) . "\n";
|
2011-11-24 11:28:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
=head2 set_debug_level($level)
|
|
|
|
|
|
|
|
Sets verbosity level for C<debug()>.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
our $debug_level = 0;
|
|
|
|
|
|
|
|
sub set_debug_level {
|
|
|
|
my ($level) = @_;
|
|
|
|
$debug_level = $level;
|
|
|
|
}
|
|
|
|
|
|
|
|
=head2 set_test_mode($on_or_off)
|
|
|
|
|
|
|
|
Sets testmode on or off.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
our $test_mode = 0;
|
|
|
|
|
|
|
|
sub set_test_mode {
|
|
|
|
my ($on_or_off) = @_;
|
|
|
|
if ($on_or_off) {
|
|
|
|
$test_mode = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$test_mode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 16:04:22 -05:00
|
|
|
=head2 debug($level[, $indent_level], $msg)
|
2011-11-24 11:28:09 -05:00
|
|
|
|
|
|
|
Logs to STDERR based on C<$debug_level> setting. C<$level> is the
|
|
|
|
minimum verbosity level required to output C<$msg>. All output is to
|
|
|
|
STDERR to preserve backward compatibility, except for in test mode,
|
|
|
|
when STDOUT is used instead. In test mode, the verbosity can be
|
|
|
|
overridden via the C<TEST_VERBOSE> environment variable.
|
|
|
|
|
|
|
|
Verbosity rules:
|
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
|
|
|
=item 0: errors only
|
|
|
|
|
2012-01-09 16:25:35 -05:00
|
|
|
=item >= 1: print operations: LINK/UNLINK/MKDIR/RMDIR/MV
|
2011-11-24 11:28:09 -05:00
|
|
|
|
2011-11-26 13:55:10 -05:00
|
|
|
=item >= 2: print operation exceptions
|
2011-11-24 11:28:09 -05:00
|
|
|
|
2011-11-26 13:55:10 -05:00
|
|
|
e.g. "_this_ already points to _that_", skipping, deferring,
|
|
|
|
overriding, fixing invalid links
|
|
|
|
|
|
|
|
=item >= 3: print trace detail: trace: stow/unstow/package/contents/node
|
2011-11-24 11:28:09 -05:00
|
|
|
|
|
|
|
=item >= 4: debug helper routines
|
|
|
|
|
2011-11-26 13:55:10 -05:00
|
|
|
=item >= 5: debug ignore lists
|
|
|
|
|
2011-11-24 11:28:09 -05:00
|
|
|
=back
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
sub debug {
|
2020-11-01 16:04:22 -05:00
|
|
|
my $level = shift;
|
|
|
|
my $indent_level;
|
|
|
|
# Maintain backwards-compatibility in case anyone's relying on this.
|
|
|
|
$indent_level = $_[0] =~ /^\d+$/ ? shift : 0;
|
|
|
|
my $msg = shift;
|
2011-11-24 11:28:09 -05:00
|
|
|
if ($debug_level >= $level) {
|
2020-11-01 16:04:22 -05:00
|
|
|
my $indent = ' ' x $indent_level;
|
2011-11-24 11:28:09 -05:00
|
|
|
if ($test_mode) {
|
2020-11-01 16:04:22 -05:00
|
|
|
print "# $indent$msg\n";
|
2011-11-24 11:28:09 -05:00
|
|
|
}
|
|
|
|
else {
|
2020-11-01 16:04:22 -05:00
|
|
|
warn "$indent$msg\n";
|
2011-11-24 11:28:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#===== METHOD ===============================================================
|
|
|
|
# Name : join_paths()
|
|
|
|
# Purpose : concatenates given paths
|
|
|
|
# Parameters: path1, path2, ... => paths
|
|
|
|
# Returns : concatenation of given paths
|
|
|
|
# Throws : n/a
|
2024-03-29 18:52:12 -04:00
|
|
|
# Comments : Factors out some redundant path elements:
|
2024-03-31 06:59:52 -04:00
|
|
|
# : '//' => '/', and 'a/b/../c' => 'a/c'. We need this function
|
|
|
|
# : with this behaviour, even though b could be a symlink to
|
|
|
|
# : elsewhere, as noted in the perldoc for File::Spec->canonpath().
|
|
|
|
# : This behaviour is deliberately different to
|
|
|
|
# : Stow::Util::canon_path(), because the way join_paths() is used
|
|
|
|
# : relies on this. Firstly, there is no guarantee that the paths
|
|
|
|
# : exist, so a filesystem check is inappropriate.
|
|
|
|
# :
|
|
|
|
# : For example, it's used to determine the path from the target
|
|
|
|
# : directory to a symlink destination. So if a symlink
|
|
|
|
# : path/to/target/a/b/c points to ../../../stow/pkg/a/b/c,
|
|
|
|
# : then joining path/to/target/a/b with ../../../stow/pkg/a/b/c
|
|
|
|
# : yields path/to/stow/pkg/a/b/c, and it's crucial that the
|
|
|
|
# : path/to/stow prefix matches a recognisable stow directory.
|
2011-11-24 11:28:09 -05:00
|
|
|
#============================================================================
|
|
|
|
sub join_paths {
|
|
|
|
my @paths = @_;
|
|
|
|
|
2024-03-29 18:52:12 -04:00
|
|
|
debug(5, 5, "| Joining: @paths");
|
|
|
|
my $result = '';
|
|
|
|
for my $part (@paths) {
|
|
|
|
next if ! length $part; # probably shouldn't happen?
|
|
|
|
$part = File::Spec->canonpath($part);
|
2011-11-24 11:28:09 -05:00
|
|
|
|
2024-03-29 18:52:12 -04:00
|
|
|
if (substr($part, 0, 1) eq '/') {
|
|
|
|
$result = $part; # absolute path, so ignore all previous parts
|
2011-11-24 11:28:09 -05:00
|
|
|
}
|
|
|
|
else {
|
2024-03-29 18:52:12 -04:00
|
|
|
$result .= '/' if length $result && $result ne '/';
|
|
|
|
$result .= $part;
|
2011-11-24 11:28:09 -05:00
|
|
|
}
|
2024-03-29 18:52:12 -04:00
|
|
|
debug(7, 6, "| Join now: $result");
|
2011-11-24 11:28:09 -05:00
|
|
|
}
|
2024-03-29 18:52:12 -04:00
|
|
|
debug(6, 5, "| Joined: $result");
|
2011-11-24 11:28:09 -05:00
|
|
|
|
2024-03-29 18:52:12 -04:00
|
|
|
# Need this to remove any initial ./
|
|
|
|
$result = File::Spec->canonpath($result);
|
|
|
|
|
|
|
|
# remove foo/..
|
|
|
|
1 while $result =~ s,(^|/)(?!\.\.)[^/]+/\.\.(/|$),$1,;
|
|
|
|
debug(6, 5, "| After .. removal: $result");
|
|
|
|
|
|
|
|
$result = File::Spec->canonpath($result);
|
|
|
|
debug(5, 5, "| Final join: $result");
|
|
|
|
|
|
|
|
return $result;
|
2011-11-24 11:28:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#===== METHOD ===============================================================
|
|
|
|
# Name : parent
|
|
|
|
# Purpose : find the parent of the given path
|
|
|
|
# Parameters: @path => components of the path
|
|
|
|
# Returns : returns a path string
|
|
|
|
# Throws : n/a
|
|
|
|
# Comments : allows you to send multiple chunks of the path
|
|
|
|
# : (this feature is currently not used)
|
|
|
|
#============================================================================
|
|
|
|
sub parent {
|
|
|
|
my @path = @_;
|
|
|
|
my $path = join '/', @_;
|
|
|
|
my @elts = split m{/+}, $path;
|
|
|
|
pop @elts;
|
2021-04-04 18:37:24 -04:00
|
|
|
return join '/', @elts;
|
2011-11-24 11:28:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#===== METHOD ===============================================================
|
|
|
|
# Name : canon_path
|
|
|
|
# Purpose : find absolute canonical path of given path
|
|
|
|
# Parameters: $path
|
|
|
|
# Returns : absolute canonical path
|
|
|
|
# Throws : n/a
|
|
|
|
# Comments : is this significantly different from File::Spec->rel2abs?
|
|
|
|
#============================================================================
|
|
|
|
sub canon_path {
|
|
|
|
my ($path) = @_;
|
|
|
|
|
|
|
|
my $cwd = getcwd();
|
|
|
|
chdir($path) or error("canon_path: cannot chdir to $path from $cwd");
|
|
|
|
my $canon_path = getcwd();
|
|
|
|
restore_cwd($cwd);
|
|
|
|
|
|
|
|
return $canon_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub restore_cwd {
|
|
|
|
my ($prev) = @_;
|
|
|
|
chdir($prev) or error("Your current directory $prev seems to have vanished");
|
|
|
|
}
|
|
|
|
|
2016-07-31 16:55:55 -04:00
|
|
|
sub adjust_dotfile {
|
dotfiles: switch {un,}stow_{contents,node}() recursion parameters
Stow walks the package and target tree hierarchies by using mutually
recursive pairs of functions:
- `stow_contents()` and `stow_node()`
- `unstow_contents()` and `unstow_node()`
As Stow runs its planning from the target directory (`plan_*()` both
call `within_target_do()`), previously the parameters for these
included:
- `$target_subpath` (or `$target_subdir` in the `*_node()` functions):
the relative path from the target top-level directory to the target
subdirectory (initially `.` at the beginning of recursion). For
example, this could be `dir1/subdir1/file1`.
- `$source`: the relative path from the target _subdirectory_ (N.B. _not_
top-level directory) to the package subdirectory. For example, if
the relative path to the Stow directory is `../stow`, this could be
`../../../stow/pkg1/dir1/subdir1/file1`. This is used when stowing
to construct a new link, or when unstowing to detect whether the
link can be unstowed.
Each time it descends into a further subdirectory of the target and
package, it appends the new path segment onto both of these, and also
prefixes `$source` with another `..`. When the `--dotfiles` parameter
is enabled, it adjusts `$target_subdir`, performing the `dot-foo` =>
`.foo` adjustment on all segments of the path in one go. In this
case, `$target_subpath` could be something like `.dir1/subdir1/file1`,
and the corresponding `$source` could be something like
`../../../stow/pkg1/dot-dir1/subdir1/file1`.
However this doesn't leave an easy way to obtain the relative path
from the target _top-level_ directory to the package subdirectory
(i.e. `../stow/pkg1/dot-dir1/subdir1/file1`), which is needed for
checking its existence and if necessary iterating over its contents.
The current implementation solves this by including an extra `$level`
parameter which tracks the recursion depth, and uses that to strip the
right number of leading path segments off the front of `$source`.
(In the above example, it would remove `../..`.)
This implementation isn't the most elegant because:
- It involves adding things to `$source` and then removing them again.
- It performs the `dot-` => `.` adjustment on every path segment
at each level, which is overkill, since when recursing down a level,
only adjustment on the final subdirectory is required since the higher
segments have already had any required adjustment.
This in turn requires `adjust_dotfile` to be more complex than it
needs to be.
It also prevents a potential future where we might want Stow to
optionally start iterating from within a subdirectory of the whole
package install image / target tree, avoiding adjustment at higher
levels and only doing it at the levels below the starting point.
- It requires passing an extra `$level` parameter which can be
automatically calculated simply by counting the number of slashes
in `$target_subpath`.
So change the `$source` recursion parameter to instead track the
relative path from the top-level package directory to the package
subdirectory or file being considered for (un)stowing, and rename it
to avoid the ambiguity caused by the word "source".
Also automatically calculate the depth simply by counting the number
of slashes, and reconstruct `$source` when needed by combining the
relative path to the Stow directory with the package name and
`$target_subpath`.
Closes #33.
2024-04-01 17:50:58 -04:00
|
|
|
my ($pkg_node) = @_;
|
|
|
|
(my $adjusted = $pkg_node) =~ s/^dot-([^.])/.$1/;
|
|
|
|
return $adjusted;
|
2016-07-31 16:55:55 -04:00
|
|
|
}
|
|
|
|
|
2011-11-24 11:28:09 -05:00
|
|
|
=head1 BUGS
|
|
|
|
|
|
|
|
=head1 SEE ALSO
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
# Local variables:
|
|
|
|
# mode: perl
|
|
|
|
# end:
|
|
|
|
# vim: ft=perl
|