xref: /openbsd/gnu/usr.bin/perl/install_lib.pl (revision 3bef86f7)
1#!perl
2
3# Initialisation code and subroutines shared between installperl and installman
4# Probably installhtml needs to join the club.
5
6use strict;
7use vars qw($Is_VMS $Is_W32 $Is_OS2 $Is_Cygwin $Is_Darwin $Is_AmigaOS
8	    %opts $packlist);
9use subs qw(unlink link chmod chown);
10require File::Path;
11require File::Copy;
12
13BEGIN {
14    require Config;
15    if ($Config::Config{userelocatableinc}) {
16	# This might be a considered a hack. Need to get information about the
17	# configuration from Config.pm *before* Config.pm expands any .../
18	# prefixes.
19	#
20	# So we set $^X to pretend that we're the already installed perl, so
21	# Config.pm does its ... expansion off that location.
22
23        my $location = $Config::Config{initialinstalllocation};
24	die <<'OS' unless defined $location;
25$Config{initialinstalllocation} is not defined - can't install a relocatable
26perl without this.
27OS
28	$^X = "$location/perl";
29	# And then remove all trace of ever having loaded Config.pm, so that
30	# it will reload with the revised $^X
31	undef %Config::;
32	delete $INC{"Config.pm"};
33	delete $INC{"Config_heavy.pl"};
34	delete $INC{"Config_git.pl"};
35	# You never saw us. We weren't here.
36
37	require Config;
38    }
39    Config->import;
40}
41
42if ($Config{d_umask}) {
43    umask(022); # umasks like 077 aren't that useful for installations
44}
45
46$Is_VMS = $^O eq 'VMS';
47$Is_W32 = $^O eq 'MSWin32';
48$Is_OS2 = $^O eq 'os2';
49$Is_Cygwin = $^O eq 'cygwin';
50$Is_Darwin = $^O eq 'darwin';
51$Is_AmigaOS = $^O eq 'amigaos';
52
53sub unlink {
54    my(@names) = @_;
55    my($cnt) = 0;
56
57    return scalar(@names) if $Is_VMS;
58
59    foreach my $name (@names) {
60	next unless -e $name;
61	chmod 0777, $name if ($Is_OS2 || $Is_W32 || $Is_Cygwin || $Is_AmigaOS);
62	print "  unlink $name\n" if $opts{verbose};
63	( CORE::unlink($name) and ++$cnt
64	  or warn "Couldn't unlink $name: $!\n" ) unless $opts{notify};
65    }
66    return $cnt;
67}
68
69sub link {
70    my($from,$to) = @_;
71    my($success) = 0;
72
73    my $xfrom = $from;
74    $xfrom =~ s/^\Q$opts{destdir}\E// if $opts{destdir};
75    my $xto = $to;
76    $xto =~ s/^\Q$opts{destdir}\E// if $opts{destdir};
77    print $opts{verbose} ? "  ln $xfrom $xto\n" : "  $xto\n"
78	unless $opts{silent};
79    my $link = $Is_AmigaOS ? \&CORE::symlink : \&CORE::link;
80    eval {
81      $link->($from, $to)
82        ? $success++
83          : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
84            ? die "AFS"  # okay inside eval {}
85              : die "Couldn't link $from to $to: $!\n"
86                unless $opts{notify};
87      $packlist->{$xto} = { from => $xfrom, type => 'link' };
88     };
89    if ($@) {
90	warn "Replacing link() with File::Copy::copy(): $@";
91	print $opts{verbose} ? "  cp $from $xto\n" : "  $xto\n"
92	    unless $opts{silent};
93	print "  creating new version of $xto\n"
94		 if $Is_VMS and -e $to and !$opts{silent};
95	unless ($opts{notify} or File::Copy::copy($from, $to) and ++$success) {
96	    # Might have been that F::C::c can't overwrite the target
97	    warn "Couldn't copy $from to $to: $!\n"
98		unless -f $to and (chmod(0666, $to), unlink $to)
99			and File::Copy::copy($from, $to) and ++$success;
100	}
101	if (defined($opts{uid}) || defined($opts{gid})) {
102	    chown($opts{uid}, $opts{gid}, $to) if $success;
103	}
104	$packlist->{$xto} = { type => 'file' };
105    }
106    $success;
107}
108
109sub chmod {
110    my($mode,$name) = @_;
111
112    printf "  chmod %o %s\n", $mode, $name if $opts{verbose};
113    CORE::chmod($mode,$name)
114	|| warn sprintf("Couldn't chmod %o %s: $!\n", $mode, $name)
115      unless $opts{notify};
116}
117
118sub chown {
119    my($uid,$gid,$name) = @_;
120
121    return if ($^O eq 'dos');
122    printf "  chown %s:%s %s\n", $uid, $gid, $name if $opts{verbose};
123    CORE::chown($uid,$gid,$name)
124	|| warn sprintf("Couldn't chown %s:%s %s: $!\n", $uid, $gid, $name)
125      unless $opts{notify};
126}
127
128sub samepath {
129    my($p1, $p2) = @_;
130
131    return (lc($p1) eq lc($p2)) if ($Is_W32);
132
133    return 1
134        if $p1 eq $p2;
135
136    my ($dev1, $ino1) = stat $p1;
137    return 0
138        unless defined $dev1;
139    my ($dev2, $ino2) = stat $p2;
140
141    return $dev1 == $dev2 && $ino1 == $ino2;
142}
143
144sub safe_rename {
145    my($from,$to) = @_;
146    if (-f $to and not unlink($to)) {
147        my($i);
148        for ($i = 1; $i < 50; $i++) {
149            last if rename($to, "$to.$i");
150        }
151        warn("Cannot rename to '$to.$i': $!"), return 0
152           if $i >= 50; # Give up!
153    }
154    link($from,$to) || return 0;
155    unlink($from);
156}
157
158sub mkpath {
159    File::Path::make_path(shift, {owner=>$opts{uid}, group=>$opts{gid},
160        mode=>0777, verbose=>$opts{verbose}}) unless $opts{notify};
161}
162
163sub unixtoamiga
164{
165	my $unixpath = shift;
166
167	my @parts = split("/",$unixpath);
168	my $isdir = 0;
169	$isdir = 1 if substr($unixpath,-1) eq "/";
170
171	my $first = 1;
172	my $amigapath = "";
173
174	my $i = 0;
175
176	for($i = 0; $i <= $#parts;$i++)
177	{
178		next if $parts[$i] eq ".";
179		if($parts[$i] eq "..")
180		{
181			$parts[$i] = "/";
182		}
183		if($i == 0)
184		{
185			if($parts[$i] eq "")
186			{
187				$amigapath .= $parts[$i + 1] . ":";
188				$i++;
189				next;
190			}
191		}
192		$amigapath .= $parts[$i];
193		if($i != $#parts)
194		{
195			$amigapath .= "/" unless $parts[$i] eq "/" ;
196		}
197		else
198		{
199			if($isdir)
200			{
201				$amigapath .= "/" unless $parts[$i] eq "/" ;
202			}
203		}
204	}
205
206	return $amigapath;
207}
208
209sub amigaprotect
210{
211	my ($file,$bits) = @_;
212	print "PROTECT: File $file\n";
213	system("PROTECT $file $bits")
214	      unless $opts{notify};
215}
216
2171;
218