-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathStow.pm.in
More file actions
executable file
·2148 lines (1823 loc) · 68.5 KB
/
Copy pathStow.pm.in
File metadata and controls
executable file
·2148 lines (1823 loc) · 68.5 KB
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#
# 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/.
package Stow;
=head1 NAME
Stow - manage farms of symbolic links
=head1 SYNOPSIS
my $stow = new Stow(%$options);
$stow->plan_unstow(@pkgs_to_unstow);
$stow->plan_stow (@pkgs_to_stow);
my %conflicts = $stow->get_conflicts;
$stow->process_tasks() unless %conflicts;
=head1 DESCRIPTION
This is the backend Perl module for GNU Stow, a program for managing
the installation of software packages, keeping them separate
(C</usr/local/stow/emacs> vs. C</usr/local/stow/perl>, for example)
while making them appear to be installed in the same place
(C</usr/local>).
Stow doesn't store an extra state between runs, so there's no danger
of mangling directories when file hierarchies don't match the
database. Also, stow will never delete any files, directories, or
links that appear in a stow directory, so it is always possible to
rebuild the target tree.
=cut
use strict;
use warnings;
use Carp qw(carp cluck croak confess longmess);
use File::Copy qw(move);
use File::Spec;
use POSIX qw(getcwd);
use Stow::Util qw(set_debug_level debug error set_test_mode
join_paths restore_cwd canon_path parent adjust_dotfile);
our $ProgramName = 'stow';
our $VERSION = '@VERSION@';
our $LOCAL_IGNORE_FILE = '.stow-local-ignore';
our $GLOBAL_IGNORE_FILE = '.stow-global-ignore';
our @default_global_ignore_regexps =
__PACKAGE__->get_default_global_ignore_regexps();
# These are the default options for each Stow instance.
our %DEFAULT_OPTIONS = (
conflicts => 0,
simulate => 0,
verbose => 0,
paranoid => 0,
compat => 0,
test_mode => 0,
dotfiles => 0,
adopt => 0,
'no-folding' => 0,
absolute => 0,
ignore => [],
override => [],
defer => [],
);
=head1 CONSTRUCTORS
=head2 new(%options)
=head3 Required options
=over 4
=item * dir - the stow directory
=item * target - the target directory
=back
=head3 Non-mandatory options
See the documentation for the F<stow> CLI front-end for information on these.
=over 4
=item * conflicts
=item * simulate
=item * verbose
=item * paranoid
=item * compat
=item * test_mode
=item * adopt
=item * no-folding
=item * absolute
=item * ignore
=item * override
=item * defer
=back
N.B. This sets the current working directory to the target directory.
=cut
sub new {
my $self = shift;
my $class = ref($self) || $self;
my %opts = @_;
my $new = bless { }, $class;
$new->{action_count} = 0;
for my $required_arg (qw(dir target)) {
croak "$class->new() called without '$required_arg' parameter\n"
unless exists $opts{$required_arg};
$new->{$required_arg} = delete $opts{$required_arg};
}
for my $opt (keys %DEFAULT_OPTIONS) {
$new->{$opt} = exists $opts{$opt} ? delete $opts{$opt}
: $DEFAULT_OPTIONS{$opt};
}
if (%opts) {
croak "$class->new() called with unrecognised parameter(s): ",
join(", ", keys %opts), "\n";
}
set_debug_level($new->get_verbosity());
set_test_mode($new->{test_mode});
$new->set_stow_dir();
$new->init_state();
return $new;
}
sub get_verbosity {
my $self = shift;
return $self->{verbose} unless $self->{test_mode};
return 0 unless exists $ENV{TEST_VERBOSE};
return 0 unless length $ENV{TEST_VERBOSE};
# Convert TEST_VERBOSE=y into numeric value
$ENV{TEST_VERBOSE} = 3 if $ENV{TEST_VERBOSE} !~ /^\d+$/;
return $ENV{TEST_VERBOSE};
}
=head2 set_stow_dir([$dir])
Sets a new stow directory. This allows the use of multiple stow
directories within one Stow instance, e.g.
$stow->plan_stow('foo');
$stow->set_stow_dir('/different/stow/dir');
$stow->plan_stow('bar');
$stow->process_tasks;
If C<$dir> is omitted, uses the value of the C<dir> parameter passed
to the L<new()> constructor.
=cut
sub set_stow_dir {
my $self = shift;
my ($dir) = @_;
if (defined $dir) {
$self->{dir} = $dir;
}
my $stow_dir = canon_path($self->{dir});
my $target = canon_path($self->{target});
$self->{stow_path} = $self->{absolute} ? $stow_dir :
File::Spec->abs2rel($stow_dir, $target);
debug(2, "stow dir is $stow_dir");
if (!$self->{absolute}) {
debug(2, "stow dir path relative to target $target is $self->{stow_path}");
} else {
debug(2, "stow dir path for target $target is $self->{stow_path}");
}
}
sub init_state {
my $self = shift;
# Store conflicts during pre-processing
$self->{conflicts} = {};
$self->{conflict_count} = 0;
# Store command line packages to stow (-S and -R)
$self->{pkgs_to_stow} = [];
# Store command line packages to unstow (-D and -R)
$self->{pkgs_to_delete} = [];
# The following structures are used by the abstractions that allow us to
# defer operating on the filesystem until after all potential conflicts have
# been assessed.
# $self->{tasks}: list of operations to be performed (in order)
# each element is a hash ref of the form
# {
# action => ... ('create' or 'remove' or 'move')
# type => ... ('link' or 'dir' or 'file')
# path => ... (unique)
# source => ... (only for links)
# dest => ... (only for moving files)
# }
$self->{tasks} = [];
# $self->{dir_task_for}: map a path to the corresponding directory task reference
# This structure allows us to quickly determine if a path has an existing
# directory task associated with it.
$self->{dir_task_for} = {};
# $self->{link_task_for}: map a path to the corresponding directory task reference
# This structure allows us to quickly determine if a path has an existing
# directory task associated with it.
$self->{link_task_for} = {};
# N.B.: directory tasks and link tasks are NOT mutually exclusive due
# to tree splitting (which involves a remove link task followed by
# a create directory task).
}
=head1 METHODS
=head2 plan_unstow(@packages)
Plan which symlink/directory creation/removal tasks need to be executed
in order to unstow the given packages. Any potential conflicts are then
accessible via L<get_conflicts()>.
=cut
sub plan_unstow {
my $self = shift;
my @packages = @_;
$self->within_target_do(sub {
for my $package (@packages) {
my $path = join_paths($self->{stow_path}, $package);
if (not -d $path) {
error("The stow directory $self->{stow_path} does not contain package $package");
}
debug(2, "Planning unstow of package $package...");
if ($self->{compat}) {
$self->unstow_contents_orig(
$self->{stow_path},
$package,
'.',
);
}
else {
$self->unstow_contents(
$self->{stow_path},
$package,
'.',
);
}
debug(2, "Planning unstow of package $package... done");
$self->{action_count}++;
}
});
}
=head2 plan_stow(@packages)
Plan which symlink/directory creation/removal tasks need to be executed
in order to stow the given packages. Any potential conflicts are then
accessible via L<get_conflicts()>.
=cut
sub plan_stow {
my $self = shift;
my @packages = @_;
$self->within_target_do(sub {
for my $package (@packages) {
my $path = join_paths($self->{stow_path}, $package);
if (not -d $path) {
error("The stow directory $self->{stow_path} does not contain package $package");
}
debug(2, "Planning stow of package $package...");
$self->stow_contents(
$self->{stow_path},
$package,
'.',
$path, # source from target
);
debug(2, "Planning stow of package $package... done");
$self->{action_count}++;
}
});
}
#===== METHOD ===============================================================
# Name : within_target_do()
# Purpose : execute code within target directory, preserving cwd
# Parameters: $code => anonymous subroutine to execute within target dir
# Returns : n/a
# Throws : n/a
# Comments : This is done to ensure that the consumer of the Stow interface
# : doesn't have to worry about (a) what their cwd is, and
# : (b) that their cwd might change.
#============================================================================
sub within_target_do {
my $self = shift;
my ($code) = @_;
my $cwd = getcwd();
chdir($self->{target})
or error("Cannot chdir to target tree: $self->{target} ($!)");
debug(3, "cwd now $self->{target}");
$self->$code();
restore_cwd($cwd);
debug(3, "cwd restored to $cwd");
}
#===== METHOD ===============================================================
# Name : stow_contents()
# Purpose : stow the contents of the given directory
# Parameters: $stow_path => relative path from current (i.e. target) directory
# : to the stow dir containing the package to be stowed
# : $package => the package whose contents are being stowed
# : $target => subpath relative to package directory which needs
# : stowing as a symlink at subpath relative to target
# : directory.
# : $source => relative path from the (sub)dir of target
# : to symlink source
# Returns : n/a
# Throws : a fatal error if directory cannot be read
# Comments : stow_node() and stow_contents() are mutually recursive.
# : $source and $target are used for creating the symlink
# : $path is used for folding/unfolding trees as necessary
#============================================================================
sub stow_contents {
my $self = shift;
my ($stow_path, $package, $target, $source) = @_;
my $path = join_paths($stow_path, $package, $target);
return if $self->should_skip_target_which_is_stow_dir($target);
my $cwd = getcwd();
my $msg = "Stowing contents of $path (cwd=$cwd)";
$msg =~ s!$ENV{HOME}(/|$)!~$1!g;
debug(3, $msg);
debug(4, " => $source");
error("stow_contents() called with non-directory path: $path")
unless -d $path;
error("stow_contents() called with non-directory target: $target")
unless $self->is_a_node($target);
opendir my $DIR, $path
or error("cannot read directory: $path ($!)");
my @listing = readdir $DIR;
closedir $DIR;
NODE:
for my $node (@listing) {
next NODE if $node eq '.';
next NODE if $node eq '..';
my $node_target = join_paths($target, $node);
next NODE if $self->ignore($stow_path, $package, $node_target);
if ($self->{dotfiles}) {
my $adj_node_target = adjust_dotfile($node_target);
debug(4, " Adjusting: $node_target => $adj_node_target");
$node_target = $adj_node_target;
}
$self->stow_node(
$stow_path,
$package,
$node_target, # target
join_paths($source, $node), # source
);
}
}
#===== METHOD ===============================================================
# Name : stow_node()
# Purpose : stow the given node
# Parameters: $stow_path => relative path from current (i.e. target) directory
# : to the stow dir containing the node to be stowed
# : $package => the package containing the node being stowed
# : $target => subpath relative to package directory of node which
# : needs stowing as a symlink at subpath relative to
# : target directory.
# : $source => relative path to symlink source from the dir of target
# Returns : n/a
# Throws : fatal exception if a conflict arises
# Comments : stow_node() and stow_contents() are mutually recursive
# : $source and $target are used for creating the symlink
# : $path is used for folding/unfolding trees as necessary
#============================================================================
sub stow_node {
my $self = shift;
my ($stow_path, $package, $target, $source) = @_;
my $path = join_paths($stow_path, $package, $target);
debug(3, "Stowing $stow_path / $package / $target");
debug(4, " => $source");
# Don't try to stow absolute symlinks (they can't be unstowed)
if ((not $self->{absolute}) && -l $source) {
my $second_source = $self->read_a_link($source);
if ($second_source =~ m{\A/}) {
$self->conflict(
'stow',
$package,
"source is an absolute symlink $source => $second_source"
);
debug(3, "Absolute symlinks cannot be unstowed");
return;
}
}
if ($self->{absolute}) {
debug(3, "Absolute symlinks cannot be unstowed, proceeding due to command-line option");
}
# Does the target already exist?
if ($self->is_a_link($target)) {
# Where is the link pointing?
my $existing_source = $self->read_a_link($target);
if (not $existing_source) {
error("Could not read link: $target");
}
debug(4, " Evaluate existing link: $target => $existing_source");
# Does it point to a node under any stow directory?
my ($existing_path, $existing_stow_path, $existing_package) =
$self->find_stowed_path($target, $existing_source);
if (not $existing_path) {
$self->conflict(
'stow',
$package,
"existing target is not owned by stow: $target"
);
return; # XXX #
}
# Does the existing $target actually point to anything?
if ($self->is_a_node($existing_path)) {
if ($existing_source eq $source) {
debug(2, "--- Skipping $target as it already points to $source");
}
elsif ($self->defer($target)) {
debug(2, "--- Deferring installation of: $target");
}
elsif ($self->override($target)) {
debug(2, "--- Overriding installation of: $target");
$self->do_unlink($target);
$self->do_link($source, $target);
}
elsif ($self->is_a_dir(join_paths(parent($target), $existing_source)) &&
$self->is_a_dir(join_paths(parent($target), $source)) ) {
# If the existing link points to a directory,
# and the proposed new link points to a directory,
# then we can unfold (split open) the tree at that point
debug(2, "--- Unfolding $target which was already owned by $existing_package");
$self->do_unlink($target);
$self->do_mkdir($target);
$self->stow_contents(
$existing_stow_path,
$existing_package,
$target,
join_paths('..', $existing_source),
);
$self->stow_contents(
$self->{stow_path},
$package,
$target,
join_paths('..', $source),
);
}
else {
$self->conflict(
'stow',
$package,
"existing target is stowed to a different package: "
. "$target => $existing_source"
);
}
}
else {
# The existing link is invalid, so replace it with a good link
debug(2, "--- replacing invalid link: $path");
$self->do_unlink($target);
$self->do_link($source, $target);
}
}
elsif ($self->is_a_node($target)) {
debug(4, " Evaluate existing node: $target");
if ($self->is_a_dir($target)) {
$self->stow_contents(
$self->{stow_path},
$package,
$target,
join_paths('..', $source),
);
}
else {
if ($self->{adopt}) {
$self->do_mv($target, $path);
$self->do_link($source, $target);
}
else {
$self->conflict(
'stow',
$package,
"existing target is neither a link nor a directory: $target"
);
}
}
}
elsif ($self->{'no-folding'} && -d $path && ! -l $path) {
$self->do_mkdir($target);
$self->stow_contents(
$self->{stow_path},
$package,
$target,
join_paths('..', $source),
);
}
else {
$self->do_link($source, $target);
}
return;
}
#===== METHOD ===============================================================
# Name : should_skip_target_which_is_stow_dir()
# Purpose : determine whether target is a stow directory which should
# : not be stowed to or unstowed from
# Parameters: $target => relative path to symlink target from the current directory
# Returns : true iff target is a stow directory
# Throws : n/a
# Comments : none
#============================================================================
sub should_skip_target_which_is_stow_dir {
my $self = shift;
my ($target) = @_;
# Don't try to remove anything under a stow directory
if ($target eq $self->{stow_path}) {
warn "WARNING: skipping target which was current stow directory $target\n";
return 1;
}
if ($self->marked_stow_dir($target)) {
warn "WARNING: skipping protected directory $target\n";
return 1;
}
debug(4, "$target not protected");
return 0;
}
sub marked_stow_dir {
my $self = shift;
my ($target) = @_;
for my $f (".stow", ".nonstow") {
if (-e join_paths($target, $f)) {
debug(4, "$target contained $f");
return 1;
}
}
return 0;
}
#===== METHOD ===============================================================
# Name : unstow_contents_orig()
# Purpose : unstow the contents of the given directory
# Parameters: $stow_path => relative path from current (i.e. target) directory
# : to the stow dir containing the package to be unstowed
# : $package => the package whose contents are being unstowed
# : $target => relative path to symlink target from the current directory
# Returns : n/a
# Throws : a fatal error if directory cannot be read
# Comments : unstow_node_orig() and unstow_contents_orig() are mutually recursive
# : Here we traverse the target tree, rather than the source tree.
#============================================================================
sub unstow_contents_orig {
my $self = shift;
my ($stow_path, $package, $target) = @_;
my $path = join_paths($stow_path, $package, $target);
return if $self->should_skip_target_which_is_stow_dir($target);
my $cwd = getcwd();
my $msg = "Unstowing from $target (compat mode, cwd=$cwd, stow dir=$self->{stow_path})";
$msg =~ s!$ENV{HOME}(/|$)!~$1!g;
debug(3, $msg);
debug(4, " source path is $path");
# In compat mode we traverse the target tree not the source tree,
# so we're unstowing the contents of /target/foo, there's no
# guarantee that the corresponding /stow/mypkg/foo exists.
error("unstow_contents_orig() called with non-directory target: $target")
unless -d $target;
opendir my $DIR, $target
or error("cannot read directory: $target ($!)");
my @listing = readdir $DIR;
closedir $DIR;
NODE:
for my $node (@listing) {
next NODE if $node eq '.';
next NODE if $node eq '..';
my $node_target = join_paths($target, $node);
next NODE if $self->ignore($stow_path, $package, $node_target);
$self->unstow_node_orig($stow_path, $package, $node_target);
}
}
#===== METHOD ===============================================================
# Name : unstow_node_orig()
# Purpose : unstow the given node
# Parameters: $stow_path => relative path from current (i.e. target) directory
# : to the stow dir containing the node to be stowed
# : $package => the package containing the node being stowed
# : $target => relative path to symlink target from the current directory
# Returns : n/a
# Throws : fatal error if a conflict arises
# Comments : unstow_node() and unstow_contents() are mutually recursive
#============================================================================
sub unstow_node_orig {
my $self = shift;
my ($stow_path, $package, $target) = @_;
my $path = join_paths($stow_path, $package, $target);
debug(3, "Unstowing $target (compat mode)");
debug(4, " source path is $path");
# Does the target exist?
if ($self->is_a_link($target)) {
debug(4, " Evaluate existing link: $target");
# Where is the link pointing?
my $existing_source = $self->read_a_link($target);
if (not $existing_source) {
error("Could not read link: $target");
}
# Does it point to a node under any stow directory?
my ($existing_path, $existing_stow_path, $existing_package) =
$self->find_stowed_path($target, $existing_source);
if (not $existing_path) {
# We're traversing the target tree not the package tree,
# so we definitely expect to find stuff not owned by stow.
# Therefore we can't flag a conflict.
return; # XXX #
}
# Does the existing $target actually point to anything?
if (-e $existing_path) {
# Does link point to the right place?
if ($existing_path eq $path) {
$self->do_unlink($target);
}
elsif ($self->override($target)) {
debug(2, "--- overriding installation of: $target");
$self->do_unlink($target);
}
# else leave it alone
}
else {
debug(2, "--- removing invalid link into a stow directory: $path");
$self->do_unlink($target);
}
}
elsif (-d $target) {
$self->unstow_contents_orig($stow_path, $package, $target);
# This action may have made the parent directory foldable
if (my $parent = $self->foldable($target)) {
$self->fold_tree($target, $parent);
}
}
elsif (-e $target) {
$self->conflict(
'unstow',
$package,
"existing target is neither a link nor a directory: $target",
);
}
else {
debug(2, "$target did not exist to be unstowed");
}
return;
}
#===== METHOD ===============================================================
# Name : unstow_contents()
# Purpose : unstow the contents of the given directory
# Parameters: $stow_path => relative path from current (i.e. target) directory
# : to the stow dir containing the package to be unstowed
# : $package => the package whose contents are being unstowed
# : $target => relative path to symlink target from the current directory
# Returns : n/a
# Throws : a fatal error if directory cannot be read
# Comments : unstow_node() and unstow_contents() are mutually recursive
# : Here we traverse the source tree, rather than the target tree.
#============================================================================
sub unstow_contents {
my $self = shift;
my ($stow_path, $package, $target) = @_;
my $path = join_paths($stow_path, $package, $target);
return if $self->should_skip_target_which_is_stow_dir($target);
my $cwd = getcwd();
my $msg = "Unstowing from $target (cwd=$cwd, stow dir=$self->{stow_path})";
$msg =~ s!$ENV{HOME}/!~/!g;
debug(3, $msg);
debug(4, " source path is $path");
# We traverse the source tree not the target tree, so $path must exist.
error("unstow_contents() called with non-directory path: $path")
unless -d $path;
# When called at the top level, $target should exist. And
# unstow_node() should only call this via mutual recursion if
# $target exists.
error("unstow_contents() called with invalid target: $target")
unless $self->is_a_node($target);
opendir my $DIR, $path
or error("cannot read directory: $path ($!)");
my @listing = readdir $DIR;
closedir $DIR;
NODE:
for my $node (@listing) {
next NODE if $node eq '.';
next NODE if $node eq '..';
my $node_target = join_paths($target, $node);
next NODE if $self->ignore($stow_path, $package, $node_target);
if ($self->{dotfiles}) {
my $adj_node_target = adjust_dotfile($node_target);
debug(4, " Adjusting: $node_target => $adj_node_target");
$node_target = $adj_node_target;
}
$self->unstow_node($stow_path, $package, $node_target);
}
if (-d $target) {
$self->cleanup_invalid_links($target);
}
}
#===== METHOD ===============================================================
# Name : unstow_node()
# Purpose : unstow the given node
# Parameters: $stow_path => relative path from current (i.e. target) directory
# : to the stow dir containing the node to be stowed
# : $package => the package containing the node being unstowed
# : $target => relative path to symlink target from the current directory
# Returns : n/a
# Throws : fatal error if a conflict arises
# Comments : unstow_node() and unstow_contents() are mutually recursive
#============================================================================
sub unstow_node {
my $self = shift;
my ($stow_path, $package, $target) = @_;
my $path = join_paths($stow_path, $package, $target);
debug(3, "Unstowing $path");
debug(4, " target is $target");
# Does the target exist?
if ($self->is_a_link($target)) {
debug(4, " Evaluate existing link: $target");
# Where is the link pointing?
my $existing_source = $self->read_a_link($target);
if (not $existing_source) {
error("Could not read link: $target");
}
if (not $self->{absolute} and $existing_source =~ m{\A/}) {
warn "Ignoring an absolute symlink: $target => $existing_source\n";
return; # XXX #
}
# Does it point to a node under any stow directory?
my ($existing_path, $existing_stow_path, $existing_package) =
$self->find_stowed_path($target, $existing_source);
if (not $existing_path) {
$self->conflict(
'unstow',
$package,
"existing target is not owned by stow: $target => $existing_source"
);
return; # XXX #
}
# Does the existing $target actually point to anything?
if (-e $existing_path) {
# Does link points to the right place?
# Adjust for dotfile if necessary.
if ($self->{dotfiles}) {
$existing_path = adjust_dotfile($existing_path);
}
if ($existing_path eq $path) {
$self->do_unlink($target);
}
# XXX we quietly ignore links that are stowed to a different
# package.
#elsif (defer($target)) {
# debug(2, "--- deferring to installation of: $target");
#}
#elsif ($self->override($target)) {
# debug(2, "--- overriding installation of: $target");
# $self->do_unlink($target);
#}
#else {
# $self->conflict(
# 'unstow',
# $package,
# "existing target is stowed to a different package: "
# . "$target => $existing_source"
# );
#}
}
else {
debug(2, "--- removing invalid link into a stow directory: $path");
$self->do_unlink($target);
}
}
elsif (-e $target) {
debug(4, " Evaluate existing node: $target");
if (-d $target) {
$self->unstow_contents($stow_path, $package, $target);
# This action may have made the parent directory foldable
if (my $parent = $self->foldable($target)) {
$self->fold_tree($target, $parent);
}
}
else {
$self->conflict(
'unstow',
$package,
"existing target is neither a link nor a directory: $target",
);
}
}
else {
debug(2, "$target did not exist to be unstowed");
}
return;
}
#===== METHOD ===============================================================
# Name : path_owned_by_package()
# Purpose : determine whether the given link points to a member of a
# : stowed package
# Parameters: $target => path to a symbolic link under current directory
# : $source => where that link points to
# Returns : the package iff link is owned by stow, otherwise ''
# Throws : n/a
# Comments : lossy wrapper around find_stowed_path()
#============================================================================
sub path_owned_by_package {
my $self = shift;
my ($target, $source) = @_;
my ($path, $stow_path, $package) =
$self->find_stowed_path($target, $source);
return $package;
}
#===== METHOD ===============================================================
# Name : find_stowed_path()
# Purpose : determine whether the given link points to a member of a
# : stowed package
# Parameters: $target => path to a symbolic link under current directory.
# : Must share a common prefix with $self->{stow_path}
# : $source => where that link points to (needed because link
# : might not exist yet due to two-phase approach,
# : so we can't just call readlink()). This must be
# : expressed relative to (the directory containing)
# : $target.
# Returns : ($path, $stow_path, $package) where $path and $stow_path are
# : relative from the current (i.e. target) directory. $path
# : is the full relative path, $stow_path is the relative path
# : to the stow directory, and $package is the name of the package.
# : or ('', '', '') if link is not owned by stow
# Throws : n/a
# Comments : Allow for stow dir not being under target dir.
# : We could put more logic under here for multiple stow dirs.
#============================================================================
sub find_stowed_path {
my $self = shift;
my ($target, $source) = @_;
# Evaluate softlink relative to its target
my $path = join_paths(parent($target), $source);
debug(4, " is path $path owned by stow?");
# Search for .stow files - this allows us to detect links
# owned by stow directories other than the current one.
my $dir = '';
my @path = split m{/+}, $path;
for my $i (0 .. $#path) {
my $part = $path[$i];
$dir = join_paths($dir, $part);
if ($self->marked_stow_dir($dir)) {
# FIXME - not sure if this can ever happen
internal_error("find_stowed_path() called directly on stow dir")
if $i == $#path;
debug(4, " yes - $dir was marked as a stow dir");
my $package = $path[$i + 1];
return ($path, $dir, $package);
}
}
# If no .stow file was found, we need to find out whether it's
# owned by the current stow directory, in which case $path will be
# a prefix of $self->{stow_path}.
if (substr($path, 0, 1) eq '/' xor substr($self->{stow_path}, 0, 1) eq '/')
{
warn "BUG in find_stowed_path? Absolute/relative mismatch between " .
"Stow dir $self->{stow_path} and path $path";
}
my @stow_path = split m{/+}, $self->{stow_path};
# Strip off common prefixes until one is empty
while (@path && @stow_path) {
if ((shift @path) ne (shift @stow_path)) {
debug(4, " no - either $path not under $self->{stow_path} or vice-versa");
return ('', '', '');
}
}
if (@stow_path) { # @path must be empty
debug(4, " no - $path is not under $self->{stow_path}");
return ('', '', '');
}
my $package = shift @path;
debug(4, " yes - by $package in " . join_paths(@path));
return ($path, $self->{stow_path}, $package);