Commit | Line | Data |
---|---|---|
9e6fc21f NC |
1 | #!perl |
2 | ||
3 | # Initialisation code and subroutines shared between installperl and installman | |
4 | # Probably installhtml needs to join the club. | |
5 | ||
6 | use strict; | |
2eb109a4 | 7 | use vars qw($Is_VMS $Is_W32 $Is_OS2 $Is_Cygwin $Is_Darwin $Is_AmigaOS |
9e6fc21f NC |
8 | %opts $packlist); |
9 | use subs qw(unlink link chmod); | |
a01f5661 | 10 | require File::Path; |
b3387df1 | 11 | require File::Copy; |
9e6fc21f | 12 | |
9e6fc21f | 13 | BEGIN { |
f4df373d NC |
14 | require Config; |
15 | if ($Config::Config{userelocatableinc}) { | |
9e6fc21f NC |
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 | |
2effe01f | 21 | # Config.pm does its ... expansion off that location. |
9e6fc21f | 22 | |
f4df373d | 23 | my $location = $Config::Config{initialinstalllocation}; |
9e6fc21f NC |
24 | die <<'OS' unless defined $location; |
25 | $Config{initialinstalllocation} is not defined - can't install a relocatable | |
26 | perl without this. | |
27 | OS | |
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"}; | |
46807d8e | 34 | delete $INC{"Config_git.pl"}; |
9e6fc21f NC |
35 | # You never saw us. We weren't here. |
36 | ||
37 | require Config; | |
9e6fc21f | 38 | } |
f4df373d | 39 | Config->import; |
9e6fc21f NC |
40 | } |
41 | ||
42 | if ($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'; | |
de35015f | 51 | $Is_AmigaOS = $^O eq 'amigaos'; |
9e6fc21f NC |
52 | |
53 | sub 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; | |
2eb109a4 | 61 | chmod 0777, $name if ($Is_OS2 || $Is_W32 || $Is_Cygwin || $Is_AmigaOS); |
9e6fc21f NC |
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 | ||
69 | sub 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}; | |
de35015f | 79 | my $link = $Is_AmigaOS ? \&CORE::symlink : \&CORE::link; |
9e6fc21f | 80 | eval { |
de35015f AB |
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 | }; | |
9e6fc21f NC |
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 | $packlist->{$xto} = { type => 'file' }; | |
102 | } | |
103 | $success; | |
104 | } | |
105 | ||
106 | sub chmod { | |
107 | my($mode,$name) = @_; | |
108 | ||
9e6fc21f NC |
109 | printf " chmod %o %s\n", $mode, $name if $opts{verbose}; |
110 | CORE::chmod($mode,$name) | |
111 | || warn sprintf("Couldn't chmod %o %s: $!\n", $mode, $name) | |
112 | unless $opts{notify}; | |
113 | } | |
114 | ||
9e6fc21f NC |
115 | sub samepath { |
116 | my($p1, $p2) = @_; | |
117 | ||
2eb109a4 | 118 | return (lc($p1) eq lc($p2)) if ($Is_W32); |
9e6fc21f | 119 | |
06647572 NC |
120 | return 1 |
121 | if $p1 eq $p2; | |
122 | ||
123 | my ($dev1, $ino1) = stat $p1; | |
124 | return 0 | |
125 | unless defined $dev1; | |
126 | my ($dev2, $ino2) = stat $p2; | |
127 | ||
128 | return $dev1 == $dev2 && $ino1 == $ino2; | |
9e6fc21f NC |
129 | } |
130 | ||
4c432614 NC |
131 | sub safe_rename { |
132 | my($from,$to) = @_; | |
133 | if (-f $to and not unlink($to)) { | |
134 | my($i); | |
135 | for ($i = 1; $i < 50; $i++) { | |
136 | last if rename($to, "$to.$i"); | |
137 | } | |
138 | warn("Cannot rename to '$to.$i': $!"), return 0 | |
139 | if $i >= 50; # Give up! | |
140 | } | |
141 | link($from,$to) || return 0; | |
142 | unlink($from); | |
143 | } | |
144 | ||
a01f5661 NC |
145 | sub mkpath { |
146 | File::Path::mkpath(shift , $opts{verbose}, 0777) unless $opts{notify}; | |
147 | } | |
148 | ||
de35015f AB |
149 | sub unixtoamiga |
150 | { | |
151 | my $unixpath = shift; | |
152 | ||
153 | my @parts = split("/",$unixpath); | |
154 | my $isdir = 0; | |
155 | $isdir = 1 if substr($unixpath,-1) eq "/"; | |
156 | ||
157 | my $first = 1; | |
158 | my $amigapath = ""; | |
159 | ||
160 | my $i = 0; | |
161 | ||
162 | for($i = 0; $i <= $#parts;$i++) | |
163 | { | |
164 | next if $parts[$i] eq "."; | |
165 | if($parts[$i] eq "..") | |
166 | { | |
167 | $parts[$i] = "/"; | |
168 | } | |
169 | if($i == 0) | |
170 | { | |
171 | if($parts[$i] eq "") | |
172 | { | |
173 | $amigapath .= $parts[$i + 1] . ":"; | |
174 | $i++; | |
175 | next; | |
176 | } | |
177 | } | |
178 | $amigapath .= $parts[$i]; | |
179 | if($i != $#parts) | |
180 | { | |
181 | $amigapath .= "/" unless $parts[$i] eq "/" ; | |
182 | } | |
183 | else | |
184 | { | |
185 | if($isdir) | |
186 | { | |
187 | $amigapath .= "/" unless $parts[$i] eq "/" ; | |
188 | } | |
189 | } | |
190 | } | |
191 | ||
192 | return $amigapath; | |
193 | } | |
194 | ||
195 | sub amigaprotect | |
196 | { | |
197 | my ($file,$bits) = @_; | |
198 | print "PROTECT: File $file\n"; | |
199 | system("PROTECT $file $bits") | |
200 | unless $opts{notify}; | |
201 | } | |
202 | ||
9e6fc21f | 203 | 1; |