| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
#********************************************************************* |
| 4 |
# |
| 5 |
# fcopy -- copy files using FAI classes and preserve directory structure |
| 6 |
# |
| 7 |
# This script is part of FAI (Fully Automatic Installation) |
| 8 |
# Copyright (C) 2000-2022 Thomas Lange, [email protected] |
| 9 |
# Universitaet zu Koeln |
| 10 |
# Copyright (C) 2001-2005 Henning Glawe, [email protected] |
| 11 |
# Freie Univeritaet Berlin |
| 12 |
# |
| 13 |
#********************************************************************* |
| 14 |
# This program is free software; you can redistribute it and/or modify |
| 15 |
# it under the terms of the GNU General Public License as published by |
| 16 |
# the Free Software Foundation; either version 2 of the License, or |
| 17 |
# (at your option) any later version. |
| 18 |
# |
| 19 |
# This program is distributed in the hope that it will be useful, but |
| 20 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 |
# General Public License for more details. |
| 23 |
# |
| 24 |
# A copy of the GNU General Public License is available as |
| 25 |
# '/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution |
| 26 |
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You |
| 27 |
# can also obtain it by writing to the Free Software Foundation, Inc., |
| 28 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 29 |
#********************************************************************* |
| 30 |
|
| 31 |
use strict; |
| 32 |
use File::Copy; |
| 33 |
use File::Compare; |
| 34 |
use File::Find; |
| 35 |
use File::Path; |
| 36 |
use File::Basename; |
| 37 |
use File::Spec; |
| 38 |
use File::Temp qw/tempfile/; |
| 39 |
use Getopt::Std; |
| 40 |
|
| 41 |
# load module only if available |
| 42 |
our $haslchown = eval { |
| 43 |
require File::lchown; |
| 44 |
File::lchown->import (qw/lchown lutimes/); |
| 45 |
1; |
| 46 |
}; |
| 47 |
|
| 48 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 49 |
# Global variables |
| 50 |
my $debug; |
| 51 |
my $error = 0; |
| 52 |
my $verbose; |
| 53 |
my $target; |
| 54 |
my $source; |
| 55 |
my $substitute; |
| 56 |
my $logfile; |
| 57 |
my @classes; |
| 58 |
my $dryrun; |
| 59 |
|
| 60 |
my @opt_modes; |
| 61 |
my @rlist; |
| 62 |
my $modeset; |
| 63 |
my $nobackup; |
| 64 |
my $backupdir; |
| 65 |
my @ignoredirs = qw'CVS .svn .arch-ids {arch} .git'; |
| 66 |
my $preinst; |
| 67 |
|
| 68 |
# getopts: |
| 69 |
our ($opt_s, $opt_t, $opt_r, $opt_m, $opt_M, $opt_v, $opt_d, $opt_D, $opt_i); |
| 70 |
our ($opt_B, $opt_c, $opt_C, $opt_h, $opt_F, $opt_l, $opt_L, $opt_P, $opt_b); |
| 71 |
our ($opt_I, $opt_U, $opt_n, $opt_H, $opt_S); |
| 72 |
|
| 73 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 74 |
sub copy_one { |
| 75 |
|
| 76 |
# copy file $prefix/$source/$class to $target/$source |
| 77 |
my ($prefix,$source,$target) = @_; |
| 78 |
|
| 79 |
my ($class,$sourcefile,$destfile,$sourcelink); |
| 80 |
my ($tmpfh,$tmpfile); |
| 81 |
my ($ps,$tpath,$bpath,$backupfile); |
| 82 |
my $preserve = 0; # if 1, the destination remains unchanged |
| 83 |
my $logcomment = ""; |
| 84 |
$preinst = 0; # remember if a tmpfile was used, needed later for unlink |
| 85 |
|
| 86 |
# 'normalize' source filenames: very important for updating ! |
| 87 |
$source =~ s/^(\.\/|\/)*//; |
| 88 |
|
| 89 |
$ps = "$prefix/$source"; |
| 90 |
$ps =~ s#//#/#; |
| 91 |
$tpath = "$target/" . dirname $source; |
| 92 |
$destfile = "$target/$source"; |
| 93 |
$backupfile = $backupdir ? "$backupdir/$source" : "$destfile.pre_fcopy"; |
| 94 |
$bpath = dirname $backupfile; |
| 95 |
|
| 96 |
warn "copy_one: source: $source: ps: $ps tpath: $tpath\n" if $debug; |
| 97 |
|
| 98 |
# $prefix/$source must be a directory |
| 99 |
if (-f $ps) { ewarn("$ps is a file, but must be a directory containing templates.");return }; |
| 100 |
unless (-d $ps) { |
| 101 |
# we could not copy anything |
| 102 |
warn "fcopy: Nonexisting directory $ps. No files copied.\n" if $debug; |
| 103 |
$error = 1 unless $opt_i; # do not set $error if -i was set |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
# use the last class for which a file exists |
| 108 |
foreach (@classes) { $class = $_,last if -f "$ps/$_" || ($opt_H && -l "$ps/$_"); } |
| 109 |
|
| 110 |
# if no class matches, remove destination or move it to the backup file |
| 111 |
unless (defined $class) { |
| 112 |
warn "fcopy: no matching file for any class for $source defined.\n"; |
| 113 |
$error = 1 unless $opt_i; # do not set $error if -i was set |
| 114 |
# do not copy |
| 115 |
if ($opt_d and -f $destfile) { |
| 116 |
print LOGFILE "$source\tNONE\t# removed (no matching class)\n" if $logfile; |
| 117 |
if ($nobackup) { |
| 118 |
_unlink($destfile) || ewarn("Could not remove file $destfile"); |
| 119 |
} else { |
| 120 |
_mkpath($bpath,$debug,0755) unless -d $bpath; |
| 121 |
_move($destfile,$backupfile) if -d $bpath;; |
| 122 |
} |
| 123 |
} |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
# a class matches |
| 128 |
warn "using class: $class\n" if $debug; |
| 129 |
$tmpfile = $sourcefile = "$ps/$class"; |
| 130 |
|
| 131 |
# read symlink |
| 132 |
if ($opt_H and -l $sourcefile) { |
| 133 |
$sourcelink = readlink($sourcefile); |
| 134 |
if (!defined($sourcelink)) { |
| 135 |
ewarn("reading symlink $sourcefile failed. $!") ; |
| 136 |
return; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
# handle broken symlink |
| 141 |
unless (stat($sourcefile)) { |
| 142 |
ewarn("$sourcefile is a broken symlink\n"); |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
# do we need a tmpfile |
| 147 |
if ($substitute or -f "$ps/_substitute" or -x "$ps/preinst") { |
| 148 |
($tmpfh,$tmpfile)=tempfile("fcopy.XXXXXX",DIR=>File::Spec->tmpdir()); |
| 149 |
$preinst = 1; # a tmpfile is used because of preinst script |
| 150 |
} |
| 151 |
|
| 152 |
if ($substitute or -f "$ps/_substitute") { |
| 153 |
warn "Enable substitution. Copying $sourcefile to $tmpfile" if $debug; |
| 154 |
|
| 155 |
do_substitution($sourcefile,$tmpfh); |
| 156 |
} |
| 157 |
|
| 158 |
if ( -x "$ps/preinst" ) { |
| 159 |
# execute preinst script, but not if we also do substitution |
| 160 |
unless ($substitute or -f "$ps/_substitute") { |
| 161 |
warn "preinst script found, copying $sourcefile to $tmpfile" if $debug; |
| 162 |
ewarn("copying $sourcefile for preinst processing failed !") unless |
| 163 |
_copy($sourcefile,$tmpfile); |
| 164 |
} |
| 165 |
if (runscript($ps,"preinst",$tmpfile,$class)) { |
| 166 |
ewarn("preinst for $sourcefile failed; not proceeding"); |
| 167 |
_unlink($tmpfile); |
| 168 |
return 0; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
# compare symlinks or real files |
| 173 |
if (defined($sourcelink)) { |
| 174 |
# it doesn't harm if the readlink failes |
| 175 |
if ((-l $destfile) and (readlink($destfile) eq $sourcelink)) { |
| 176 |
$logcomment="preserved symlink"; |
| 177 |
$preserve = 1; |
| 178 |
} else { |
| 179 |
$logcomment="new symlink"; |
| 180 |
} |
| 181 |
} else { |
| 182 |
if ( compare($tmpfile,$destfile)) { |
| 183 |
$logcomment="new file"; |
| 184 |
} else { |
| 185 |
$logcomment="preserved file"; |
| 186 |
$preserve = 1; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
#if a package is being purged, our information about its config files is |
| 191 |
#wrong, so first check if they exist. if not, don't preserve, but copy |
| 192 |
if ($preserve && ! -e $destfile) { |
| 193 |
$logcomment="magically disappeared (maybe purged or dangling symlink)"; |
| 194 |
$preserve=0; |
| 195 |
} |
| 196 |
|
| 197 |
print LOGFILE "$source\t$class\t# $logcomment\n" if $logfile; |
| 198 |
# the destination remains unchanged |
| 199 |
if ($preserve) { |
| 200 |
warn "fcopy: destination $source remains unchanged\n" if $verbose; |
| 201 |
_unlink($tmpfile) if $preinst; |
| 202 |
set_mode($ps,$destfile,$class); # set mode even no file was copied |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
# if destination is a symlink and -l is given, complain about it |
| 207 |
if ($opt_l && -l $destfile) { |
| 208 |
ewarn("Destination $destfile is a symlink"); |
| 209 |
_unlink($tmpfile) if $preinst; |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
# create subdirectories if they do not exist |
| 214 |
_mkpath($tpath,$debug,0755) unless -d $tpath; |
| 215 |
|
| 216 |
# save existing file, add suffix .pre_fcopy |
| 217 |
# what should I do if $destfile is a symlink? |
| 218 |
$nobackup or (-f $destfile and |
| 219 |
(-d $bpath or _mkpath($bpath,$debug,0755)) and _move($destfile,$backupfile)); |
| 220 |
|
| 221 |
# handle symlink |
| 222 |
if (defined($sourcelink)) { |
| 223 |
# remove destination if either a regular file or a symlink |
| 224 |
# complain if existst but neither of these |
| 225 |
if (-f $destfile or -l $destfile) { |
| 226 |
if (!_unlink($destfile)) { |
| 227 |
ewarn("removing destfile $destfile for symlink $sourcefile failed. $!") ; |
| 228 |
return; |
| 229 |
} |
| 230 |
} elsif (-e $destfile) { |
| 231 |
ewarn("destination $destfile for symlink $sourcefile is neither a regular file nor a symlink $!") ; |
| 232 |
return; |
| 233 |
} |
| 234 |
if (_symlink($sourcelink, $destfile)) { |
| 235 |
print "fcopy: symlinked $sourcefile -> $sourcelink to $destfile\n" ; |
| 236 |
runscript($ps,"postinst",$destfile,$class); |
| 237 |
} else { |
| 238 |
ewarn("copy symlink $sourcefile -> $sourcelink to $destfile failed. $!") ; |
| 239 |
} |
| 240 |
} else { |
| 241 |
# handle ordinary file |
| 242 |
if (_copy($tmpfile,$destfile)) { |
| 243 |
print "fcopy: copied $sourcefile to $destfile\n" ; |
| 244 |
set_mode($ps,$destfile,$class); |
| 245 |
runscript($ps,"postinst",$destfile,$class); |
| 246 |
} else { |
| 247 |
ewarn("copy $sourcefile to $destfile failed. $!") ; |
| 248 |
} |
| 249 |
} |
| 250 |
_unlink($tmpfile) if $preinst; |
| 251 |
} |
| 252 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 253 |
sub do_substitution { |
| 254 |
|
| 255 |
my ($src,$tmp) = @_; |
| 256 |
my $line; |
| 257 |
|
| 258 |
open (SRC,"< $src") || die "fcopy: can't open $src\n"; |
| 259 |
while ($line = <SRC>) { |
| 260 |
$line =~ s/\{%(\w+)%\}/ |
| 261 |
if (exists $ENV{$1}) { $ENV{$1}} else {$error=1; warn "Error in substitution: Variable \$$1 undefined. File $src,line $.\n"; "\${$1}"} |
| 262 |
/eg; |
| 263 |
print $tmp $line; |
| 264 |
} |
| 265 |
close SRC; |
| 266 |
close $tmp; |
| 267 |
} |
| 268 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 269 |
sub _mkpath { |
| 270 |
|
| 271 |
return 1 if $dryrun; # do not execute if -n or FCOPY_DRYRUN was given |
| 272 |
mkpath(@_); |
| 273 |
} |
| 274 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 275 |
sub _unlink { |
| 276 |
|
| 277 |
return 1 if $dryrun; # do not execute if -n or FCOPY_DRYRUN was given |
| 278 |
unlink(@_); |
| 279 |
} |
| 280 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 281 |
sub _move { |
| 282 |
|
| 283 |
return 1 if $dryrun; # do not execute if -n or FCOPY_DRYRUN was given |
| 284 |
move(@_); |
| 285 |
} |
| 286 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 287 |
sub _copy { |
| 288 |
|
| 289 |
return 1 if $dryrun; # do not execute if -n or FCOPY_DRYRUN was given |
| 290 |
copy(@_); |
| 291 |
} |
| 292 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 293 |
sub _symlink { |
| 294 |
|
| 295 |
return 1 if $dryrun; # do not execute if -n or FCOPY_DRYRUN was given |
| 296 |
my($from,$to) = @_; |
| 297 |
symlink($from,$to); # make perl -wc happy |
| 298 |
} |
| 299 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 300 |
sub runscript { |
| 301 |
|
| 302 |
my ($path,$script,$destfile,$class) = @_; |
| 303 |
# run script $path/$script on $destfile, additional option is $class |
| 304 |
# returns 0 on success, 1 if the script failed |
| 305 |
|
| 306 |
return 1 unless -f "$path/$script"; |
| 307 |
unless ( -x "$path/$script") { |
| 308 |
warn "ERROR: $path/$script is not executable\n" if $verbose; |
| 309 |
return 1; |
| 310 |
} |
| 311 |
warn "executing $path/$script $class $destfile\n" if $debug; |
| 312 |
return 0 if $dryrun; # do not execute if -n or FCOPY_DRYRUN was given |
| 313 |
system "$path/$script $class $destfile"; |
| 314 |
my $rc = $?>>8; |
| 315 |
if ($rc) { |
| 316 |
warn "ERROR: $script returned code $rc\n"; |
| 317 |
$error = 1; |
| 318 |
return 1; |
| 319 |
} |
| 320 |
return 0; |
| 321 |
} |
| 322 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 323 |
sub name2num { |
| 324 |
|
| 325 |
# convert names to numeric uid, gid |
| 326 |
my ($user, $group) = @_; |
| 327 |
my ($uid, $gid); |
| 328 |
|
| 329 |
if ( !defined( $ENV{ROOTCMD} ) || $ENV{ROOTCMD} =~ /^\s*$/ ) { |
| 330 |
$uid = ($user =~ /^\d+$/) ? $user : getpwnam $user; |
| 331 |
$gid = ($group =~ /^\d+$/) ? $group : getgrnam $group; |
| 332 |
} else { |
| 333 |
$uid = ($user =~ /^\d+$/) ? $user : `$ENV{ROOTCMD} perl -e '\$uid = getpwnam "$user"; print \$uid'`; |
| 334 |
$gid = ($group =~ /^\d+$/) ? $group : `$ENV{ROOTCMD} perl -e '\$gid = getgrnam "$group"; print \$gid'`; |
| 335 |
} |
| 336 |
warn "name2num $user = $uid ; $group = $gid\n" if $debug; |
| 337 |
return ($uid,$gid); |
| 338 |
} |
| 339 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 340 |
sub set_mode { |
| 341 |
|
| 342 |
# set target file's owner, group, mode and time |
| 343 |
# use owner,group,mode from -m or from the file file-modes or |
| 344 |
# use the values from the source file |
| 345 |
my ($sourcefile,$destfile,$class) = @_; |
| 346 |
my ($uid,$gid,$owner,$group,$mode,$omode); |
| 347 |
my ($stime,@smodes); |
| 348 |
|
| 349 |
# get mtime,uid,gid,mode from source file or from -m |
| 350 |
if ($modeset) { # use -m values |
| 351 |
@smodes = @opt_modes; |
| 352 |
$stime = (stat("$sourcefile/$class"))[9]; |
| 353 |
} else { |
| 354 |
# get mtime,uid,gid,mode from source file |
| 355 |
if ($opt_H) { |
| 356 |
($stime,@smodes) = (lstat("$sourcefile/$class"))[9,4,5,2]; |
| 357 |
} else { |
| 358 |
($stime,@smodes) = (stat("$sourcefile/$class"))[9,4,5,2]; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
# get mtime,uid,gid,mode from destination file |
| 363 |
my ($dtime,@dmodes); |
| 364 |
if ($opt_H) { |
| 365 |
($dtime,@dmodes) = (lstat("$destfile"))[9,4,5,2]; |
| 366 |
} else { |
| 367 |
($dtime,@dmodes) = (stat("$destfile"))[9,4,5,2]; |
| 368 |
} |
| 369 |
# compare time,uid,gid and mode of source file and target file |
| 370 |
|
| 371 |
if (-f "$sourcefile/file-modes") { |
| 372 |
($owner,$group,$mode) = read_file_mode("$sourcefile/file-modes",$class); |
| 373 |
} elsif ($modeset) { # use -m values |
| 374 |
($owner,$group,$mode) = @opt_modes; |
| 375 |
} else { # use values from source file |
| 376 |
($owner,$group,$mode) = @smodes; |
| 377 |
} |
| 378 |
|
| 379 |
# if different: change the values |
| 380 |
# setting modes on a symlink is not portable, so ignore it |
| 381 |
my $issymlink = $opt_H && -l $destfile; |
| 382 |
return if ($stime == $dtime && (($dmodes[0] == $owner) && |
| 383 |
($dmodes[1] == $group) && ($issymlink || ($dmodes[2] == $mode)))); |
| 384 |
|
| 385 |
($uid,$gid) = name2num($owner,$group); |
| 386 |
$omode = sprintf "%04o",$mode; |
| 387 |
warn "chown/chmod u:$uid g:$gid m:$omode $destfile\n" if $debug; |
| 388 |
return if $dryrun; # do not execute if -n or FCOPY_DRYRUN was given |
| 389 |
if ($issymlink && $haslchown) { |
| 390 |
lchown ($uid,$gid, $destfile) || ewarn("lchown $owner $group $destfile failed. $!"); |
| 391 |
unless ($preinst) { |
| 392 |
lutimes ($stime,$stime, $destfile) || ewarn("lutimes for $destfile failed. $!"); |
| 393 |
} |
| 394 |
} else { |
| 395 |
chown ($uid,$gid, $destfile) || ewarn("chown $owner $group $destfile failed. $!"); |
| 396 |
chmod ($mode, $destfile) || ewarn("chmod $omode $destfile failed. $!"); |
| 397 |
unless ($preinst) { |
| 398 |
utime ($stime,$stime, $destfile) || ewarn("utime for $destfile failed. $!"); |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 403 |
sub check_mopt { |
| 404 |
|
| 405 |
# save and check -m options |
| 406 |
$modeset = 1; |
| 407 |
my $n = @opt_modes = split(/,/,$opt_m); |
| 408 |
($n != 3) && |
| 409 |
die "fcopy: wrong number of options for -m. Exact 3 comma separated items needed.\n"; |
| 410 |
unless ($opt_modes[2] =~/^[0-7]+$/) { |
| 411 |
die "fcopy: file mode should be an octal number. Value is: $opt_modes[2]\n"; |
| 412 |
} |
| 413 |
$opt_modes[2] = oct($opt_modes[2]); |
| 414 |
} |
| 415 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 416 |
sub read_file_mode { |
| 417 |
|
| 418 |
my ($modefile,$class) = @_; |
| 419 |
my ($owner,$group,$mode,$fclass,@defaults); |
| 420 |
|
| 421 |
warn "reading $modefile\n" if $verbose; |
| 422 |
open (MODEFILE,"<$modefile") || die "fcopy: can't open $modefile\n"; |
| 423 |
while (<MODEFILE>) { |
| 424 |
next if /^\s*$/; # skip empty lines |
| 425 |
next if /^#/; # skip comment lines |
| 426 |
($owner,$group,$mode,$fclass) = split; |
| 427 |
$mode = oct($mode); |
| 428 |
# class found |
| 429 |
return ($owner,$group,$mode) if ($fclass eq $class); |
| 430 |
# when no class is specified use data for all classes |
| 431 |
$fclass or @defaults = ($owner,$group,$mode); |
| 432 |
} |
| 433 |
close MODEFILE; |
| 434 |
return @defaults if @defaults; |
| 435 |
ewarn("no modes found for $class in $modefile"); |
| 436 |
} |
| 437 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 438 |
sub read_classes { |
| 439 |
|
| 440 |
# read class names from a file |
| 441 |
my $file = shift; |
| 442 |
my @classes; |
| 443 |
|
| 444 |
open(CLASS,$file) || die "fcopy: can't open class file $file. $!\n"; |
| 445 |
while (<CLASS>) { |
| 446 |
next if /^#/; |
| 447 |
push @classes, split; |
| 448 |
} |
| 449 |
close CLASS; |
| 450 |
return @classes; |
| 451 |
} |
| 452 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 453 |
sub ewarn { |
| 454 |
|
| 455 |
# print warnings and set error to 1 |
| 456 |
$error = 1; |
| 457 |
warn "fcopy: @_\n"; |
| 458 |
} |
| 459 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 460 |
sub read_files { |
| 461 |
|
| 462 |
# read list of files |
| 463 |
# lines starting with # are comments |
| 464 |
my $file = shift; |
| 465 |
my @list; |
| 466 |
|
| 467 |
open(LIST,"<$file") || die "fcopy: Can't open file $file\n"; |
| 468 |
while (<LIST>) { |
| 469 |
next if /^#/; |
| 470 |
chomp; |
| 471 |
push @list, $_; |
| 472 |
} |
| 473 |
return @list; |
| 474 |
} |
| 475 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 476 |
sub usage { |
| 477 |
|
| 478 |
print << "EOF"; |
| 479 |
fcopy, copy files using classes. |
| 480 |
|
| 481 |
Copyright (C) 2001-2022 by Thomas Lange |
| 482 |
|
| 483 |
Usage: fcopy [OPTION] ... SOURCE ... |
| 484 |
|
| 485 |
-B Remove backup file. |
| 486 |
-c class[,class] Define classes. |
| 487 |
-C file Read classes from file. |
| 488 |
-d Remove target file if no class applies. |
| 489 |
-D Create debug output. |
| 490 |
-F file Read list of sources from file. |
| 491 |
-h Show summary of options. |
| 492 |
-H If source is a symlink, make dest a symlink too. |
| 493 |
-i Exit with 0 when no class applies. |
| 494 |
-I dir[,dir] Override default list of ignored subdirectories |
| 495 |
-l Do not copy if destination is a symbolic link. |
| 496 |
-L file Log destination and used class to file |
| 497 |
-m user,group,mode Set user, group and mode for copied files. |
| 498 |
-M Same as -m root,root,0644 |
| 499 |
-n Print the commands, but do not execute them. |
| 500 |
-r Copy recursivly but skip ignored directories. |
| 501 |
-s source_dir Look for source files relative to source_dir. |
| 502 |
-S Enable substition of variables in files. |
| 503 |
-t target_dir Copy files relativ to target_dir. |
| 504 |
-b backup_dir Where to save backups of overwritten files |
| 505 |
-v Create verbose output. |
| 506 |
|
| 507 |
EOF |
| 508 |
exit 0; |
| 509 |
} |
| 510 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 511 |
# main program |
| 512 |
|
| 513 |
$|=1; |
| 514 |
getopts('Ms:t:rm:vidDc:C:hF:lL:P:Bb:I:UnHS') || usage; |
| 515 |
$opt_h && usage; |
| 516 |
$opt_P and die "Option -P is not supported any more. You may want to use -F\n"; |
| 517 |
$dryrun = $ENV{FCOPY_DRYRUN} || $opt_n || 0; # is true if in dry-run mode |
| 518 |
$dryrun and warn "Dry-run only! Nothing is really executed.\n"; |
| 519 |
$opt_M and $opt_m = "root,root,0644"; # set default modes |
| 520 |
$opt_m && check_mopt(); |
| 521 |
$nobackup = $opt_B || $ENV{FCOPY_NOBACKUP} || 0; |
| 522 |
$substitute = $opt_S || $ENV{FCOPY_SUBSTITUTE} || 0; |
| 523 |
$verbose = $opt_v || $ENV{verbose} || 0; |
| 524 |
$debug = $opt_D || $ENV{debug} || 0; |
| 525 |
$source = $opt_s || $ENV{FAI} && "$ENV{FAI}/files" || `pwd`; |
| 526 |
chomp $source; # since pwd contains a newline |
| 527 |
$target = $opt_t || $ENV{FAI_ROOT} || $ENV{target}; |
| 528 |
$target eq "/" or $ENV{'ROOTCMD'}="chroot $target"; |
| 529 |
$logfile = $opt_L || $ENV{LOGDIR} && "$ENV{LOGDIR}/fcopy.log" || 0; |
| 530 |
$logfile and (open(LOGFILE,">> $logfile") || die "can't open logfile: $!"); |
| 531 |
$backupdir = $opt_b || $ENV{FAI_BACKUPDIR}; |
| 532 |
|
| 533 |
if ($opt_U && -f "/var/run/fai/fai_softupdate_is_running" ) { |
| 534 |
print "Skipping this fcopy command during softupdate." if $verbose; |
| 535 |
exit 0; |
| 536 |
} |
| 537 |
|
| 538 |
#for postinst scripts |
| 539 |
$ENV{'FAI_ROOT'} = $ENV{'target'} = $target; |
| 540 |
|
| 541 |
# last class has highest priority |
| 542 |
$ENV{classes} and @classes = split /\s+/,$ENV{classes}; |
| 543 |
$opt_c and @classes = split /,/,$opt_c; |
| 544 |
$opt_C and @classes = read_classes($opt_C); |
| 545 |
warn join ' ','Classes:',@classes,"\n" if $debug; |
| 546 |
@classes = reverse @classes; |
| 547 |
$opt_F and @ARGV = read_files($opt_F); |
| 548 |
$ENV{'FCOPY_IGNOREDIRS'} and @ignoredirs = split /\s+/,$ENV{'FCOPY_IGNOREDIRS'}; |
| 549 |
$opt_I and @ignoredirs = split /,/,$opt_I; |
| 550 |
|
| 551 |
die "fcopy: source undefined\n" unless $source; |
| 552 |
die "fcopy: target undefined\n" unless $target; |
| 553 |
|
| 554 |
if ($opt_r) { |
| 555 |
foreach (@ARGV) { $_="$source/$_"; } # add prefix to list of directories |
| 556 |
my %has_subdirs; |
| 557 |
my %ignoredirs; |
| 558 |
map $ignoredirs{$_}=1,@ignoredirs; |
| 559 |
File::Find::find({ |
| 560 |
wanted => sub { $has_subdirs{$File::Find::dir} |= !($opt_H && -l) && -d}, |
| 561 |
preprocess => sub { grep ! (-d and exists($ignoredirs{$_})),@_}}, @ARGV); |
| 562 |
|
| 563 |
foreach (keys %has_subdirs) { |
| 564 |
unless ($has_subdirs{$_}) { |
| 565 |
# remove prefix from all files found |
| 566 |
s#^\Q$source/##; |
| 567 |
push @rlist,$_; |
| 568 |
} |
| 569 |
} |
| 570 |
warn "List of all files found by File::Find::find: @rlist" if $debug; |
| 571 |
@ARGV = @rlist; |
| 572 |
} |
| 573 |
|
| 574 |
foreach (@ARGV) { copy_one($source,$_,$target); } |
| 575 |
exit $error; |