install-sh revision 054b3d00
1a1d141d5Smrg#!/bin/sh 2a1d141d5Smrg# install - install a program, script, or datafile 32ddb6cf1Smrg 4054b3d00Smrgscriptversion=2013-12-25.23; # UTC 52ddb6cf1Smrg 6a1d141d5Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 7a1d141d5Smrg# later released in X11R6 (xc/config/util/install.sh) with the 8a1d141d5Smrg# following copyright and license. 9a1d141d5Smrg# 10a1d141d5Smrg# Copyright (C) 1994 X Consortium 11a1d141d5Smrg# 12a1d141d5Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 13a1d141d5Smrg# of this software and associated documentation files (the "Software"), to 14a1d141d5Smrg# deal in the Software without restriction, including without limitation the 15a1d141d5Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 16a1d141d5Smrg# sell copies of the Software, and to permit persons to whom the Software is 17a1d141d5Smrg# furnished to do so, subject to the following conditions: 18a1d141d5Smrg# 19a1d141d5Smrg# The above copyright notice and this permission notice shall be included in 20a1d141d5Smrg# all copies or substantial portions of the Software. 21a1d141d5Smrg# 22a1d141d5Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23a1d141d5Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24a1d141d5Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25a1d141d5Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26a1d141d5Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 27a1d141d5Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28a1d141d5Smrg# 29a1d141d5Smrg# Except as contained in this notice, the name of the X Consortium shall not 30a1d141d5Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 31a1d141d5Smrg# ings in this Software without prior written authorization from the X Consor- 32a1d141d5Smrg# tium. 33a1d141d5Smrg# 34a1d141d5Smrg# 35a1d141d5Smrg# FSF changes to this file are in the public domain. 36a1d141d5Smrg# 37a1d141d5Smrg# Calling this script install-sh is preferred over install.sh, to prevent 38054b3d00Smrg# 'make' implicit rules from creating a file called install from it 39a1d141d5Smrg# when there is no Makefile. 40a1d141d5Smrg# 41a1d141d5Smrg# This script is compatible with the BSD install script, but was written 422ddb6cf1Smrg# from scratch. 43a1d141d5Smrg 44054b3d00Smrgtab=' ' 452ddb6cf1Smrgnl=' 462ddb6cf1Smrg' 47054b3d00SmrgIFS=" $tab$nl" 48a1d141d5Smrg 49054b3d00Smrg# Set DOITPROG to "echo" to test this script. 50a1d141d5Smrg 512ddb6cf1Smrgdoit=${DOITPROG-} 52054b3d00Smrgdoit_exec=${doit:-exec} 53a1d141d5Smrg 542ddb6cf1Smrg# Put in absolute file names if you don't have them in your path; 552ddb6cf1Smrg# or use environment vars. 562ddb6cf1Smrg 572ddb6cf1Smrgchgrpprog=${CHGRPPROG-chgrp} 582ddb6cf1Smrgchmodprog=${CHMODPROG-chmod} 592ddb6cf1Smrgchownprog=${CHOWNPROG-chown} 602ddb6cf1Smrgcmpprog=${CMPPROG-cmp} 612ddb6cf1Smrgcpprog=${CPPROG-cp} 622ddb6cf1Smrgmkdirprog=${MKDIRPROG-mkdir} 632ddb6cf1Smrgmvprog=${MVPROG-mv} 642ddb6cf1Smrgrmprog=${RMPROG-rm} 652ddb6cf1Smrgstripprog=${STRIPPROG-strip} 662ddb6cf1Smrg 672ddb6cf1Smrgposix_mkdir= 682ddb6cf1Smrg 692ddb6cf1Smrg# Desired mode of installed file. 702ddb6cf1Smrgmode=0755 712ddb6cf1Smrg 722ddb6cf1Smrgchgrpcmd= 732ddb6cf1Smrgchmodcmd=$chmodprog 742ddb6cf1Smrgchowncmd= 752ddb6cf1Smrgmvcmd=$mvprog 762ddb6cf1Smrgrmcmd="$rmprog -f" 772ddb6cf1Smrgstripcmd= 78a1d141d5Smrg 792ddb6cf1Smrgsrc= 802ddb6cf1Smrgdst= 812ddb6cf1Smrgdir_arg= 822ddb6cf1Smrgdst_arg= 83a1d141d5Smrg 842ddb6cf1Smrgcopy_on_change=false 85054b3d00Smrgis_target_a_directory=possibly 86a1d141d5Smrg 872ddb6cf1Smrgusage="\ 882ddb6cf1SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE 892ddb6cf1Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 902ddb6cf1Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 912ddb6cf1Smrg or: $0 [OPTION]... -d DIRECTORIES... 92a1d141d5Smrg 932ddb6cf1SmrgIn the 1st form, copy SRCFILE to DSTFILE. 942ddb6cf1SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 952ddb6cf1SmrgIn the 4th, create DIRECTORIES. 96a1d141d5Smrg 972ddb6cf1SmrgOptions: 982ddb6cf1Smrg --help display this help and exit. 992ddb6cf1Smrg --version display version info and exit. 100a1d141d5Smrg 1012ddb6cf1Smrg -c (ignored) 1022ddb6cf1Smrg -C install only if different (preserve the last data modification time) 1032ddb6cf1Smrg -d create directories instead of installing files. 1042ddb6cf1Smrg -g GROUP $chgrpprog installed files to GROUP. 1052ddb6cf1Smrg -m MODE $chmodprog installed files to MODE. 1062ddb6cf1Smrg -o USER $chownprog installed files to USER. 1072ddb6cf1Smrg -s $stripprog installed files. 1082ddb6cf1Smrg -t DIRECTORY install into DIRECTORY. 1092ddb6cf1Smrg -T report an error if DSTFILE is a directory. 110a1d141d5Smrg 1112ddb6cf1SmrgEnvironment variables override the default commands: 1122ddb6cf1Smrg CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG 1132ddb6cf1Smrg RMPROG STRIPPROG 1142ddb6cf1Smrg" 115a1d141d5Smrg 1162ddb6cf1Smrgwhile test $# -ne 0; do 1172ddb6cf1Smrg case $1 in 1182ddb6cf1Smrg -c) ;; 119a1d141d5Smrg 1202ddb6cf1Smrg -C) copy_on_change=true;; 121a1d141d5Smrg 1222ddb6cf1Smrg -d) dir_arg=true;; 123a1d141d5Smrg 1242ddb6cf1Smrg -g) chgrpcmd="$chgrpprog $2" 125054b3d00Smrg shift;; 126a1d141d5Smrg 1272ddb6cf1Smrg --help) echo "$usage"; exit $?;; 128a1d141d5Smrg 1292ddb6cf1Smrg -m) mode=$2 130054b3d00Smrg case $mode in 131054b3d00Smrg *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*) 132054b3d00Smrg echo "$0: invalid mode: $mode" >&2 133054b3d00Smrg exit 1;; 134054b3d00Smrg esac 135054b3d00Smrg shift;; 136a1d141d5Smrg 1372ddb6cf1Smrg -o) chowncmd="$chownprog $2" 138054b3d00Smrg shift;; 139a1d141d5Smrg 1402ddb6cf1Smrg -s) stripcmd=$stripprog;; 141a1d141d5Smrg 142054b3d00Smrg -t) 143054b3d00Smrg is_target_a_directory=always 144054b3d00Smrg dst_arg=$2 145054b3d00Smrg # Protect names problematic for 'test' and other utilities. 146054b3d00Smrg case $dst_arg in 147054b3d00Smrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 148054b3d00Smrg esac 149054b3d00Smrg shift;; 150a1d141d5Smrg 151054b3d00Smrg -T) is_target_a_directory=never;; 152a1d141d5Smrg 1532ddb6cf1Smrg --version) echo "$0 $scriptversion"; exit $?;; 154a1d141d5Smrg 155054b3d00Smrg --) shift 156054b3d00Smrg break;; 157a1d141d5Smrg 158054b3d00Smrg -*) echo "$0: invalid option: $1" >&2 159054b3d00Smrg exit 1;; 160a1d141d5Smrg 1612ddb6cf1Smrg *) break;; 1622ddb6cf1Smrg esac 1632ddb6cf1Smrg shift 1642ddb6cf1Smrgdone 165a1d141d5Smrg 166054b3d00Smrg# We allow the use of options -d and -T together, by making -d 167054b3d00Smrg# take the precedence; this is for compatibility with GNU install. 168054b3d00Smrg 169054b3d00Smrgif test -n "$dir_arg"; then 170054b3d00Smrg if test -n "$dst_arg"; then 171054b3d00Smrg echo "$0: target directory not allowed when installing a directory." >&2 172054b3d00Smrg exit 1 173054b3d00Smrg fi 174054b3d00Smrgfi 175054b3d00Smrg 1762ddb6cf1Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then 1772ddb6cf1Smrg # When -d is used, all remaining arguments are directories to create. 1782ddb6cf1Smrg # When -t is used, the destination is already specified. 1792ddb6cf1Smrg # Otherwise, the last argument is the destination. Remove it from $@. 1802ddb6cf1Smrg for arg 1812ddb6cf1Smrg do 1822ddb6cf1Smrg if test -n "$dst_arg"; then 1832ddb6cf1Smrg # $@ is not empty: it contains at least $arg. 1842ddb6cf1Smrg set fnord "$@" "$dst_arg" 1852ddb6cf1Smrg shift # fnord 1862ddb6cf1Smrg fi 1872ddb6cf1Smrg shift # arg 1882ddb6cf1Smrg dst_arg=$arg 189054b3d00Smrg # Protect names problematic for 'test' and other utilities. 190054b3d00Smrg case $dst_arg in 191054b3d00Smrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 192054b3d00Smrg esac 1932ddb6cf1Smrg done 1942ddb6cf1Smrgfi 195a1d141d5Smrg 1962ddb6cf1Smrgif test $# -eq 0; then 1972ddb6cf1Smrg if test -z "$dir_arg"; then 1982ddb6cf1Smrg echo "$0: no input file specified." >&2 1992ddb6cf1Smrg exit 1 2002ddb6cf1Smrg fi 201054b3d00Smrg # It's OK to call 'install-sh -d' without argument. 2022ddb6cf1Smrg # This can happen when creating conditional directories. 2032ddb6cf1Smrg exit 0 2042ddb6cf1Smrgfi 205a1d141d5Smrg 2062ddb6cf1Smrgif test -z "$dir_arg"; then 207054b3d00Smrg if test $# -gt 1 || test "$is_target_a_directory" = always; then 208054b3d00Smrg if test ! -d "$dst_arg"; then 209054b3d00Smrg echo "$0: $dst_arg: Is not a directory." >&2 210054b3d00Smrg exit 1 211054b3d00Smrg fi 212054b3d00Smrg fi 213054b3d00Smrgfi 214054b3d00Smrg 215054b3d00Smrgif test -z "$dir_arg"; then 216054b3d00Smrg do_exit='(exit $ret); exit $ret' 217054b3d00Smrg trap "ret=129; $do_exit" 1 218054b3d00Smrg trap "ret=130; $do_exit" 2 219054b3d00Smrg trap "ret=141; $do_exit" 13 220054b3d00Smrg trap "ret=143; $do_exit" 15 2212ddb6cf1Smrg 2222ddb6cf1Smrg # Set umask so as not to create temps with too-generous modes. 2232ddb6cf1Smrg # However, 'strip' requires both read and write access to temps. 2242ddb6cf1Smrg case $mode in 2252ddb6cf1Smrg # Optimize common cases. 2262ddb6cf1Smrg *644) cp_umask=133;; 2272ddb6cf1Smrg *755) cp_umask=22;; 2282ddb6cf1Smrg 2292ddb6cf1Smrg *[0-7]) 2302ddb6cf1Smrg if test -z "$stripcmd"; then 231054b3d00Smrg u_plus_rw= 2322ddb6cf1Smrg else 233054b3d00Smrg u_plus_rw='% 200' 2342ddb6cf1Smrg fi 2352ddb6cf1Smrg cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 2362ddb6cf1Smrg *) 2372ddb6cf1Smrg if test -z "$stripcmd"; then 238054b3d00Smrg u_plus_rw= 2392ddb6cf1Smrg else 240054b3d00Smrg u_plus_rw=,u+rw 2412ddb6cf1Smrg fi 2422ddb6cf1Smrg cp_umask=$mode$u_plus_rw;; 2432ddb6cf1Smrg esac 2442ddb6cf1Smrgfi 245a1d141d5Smrg 2462ddb6cf1Smrgfor src 2472ddb6cf1Smrgdo 248054b3d00Smrg # Protect names problematic for 'test' and other utilities. 2492ddb6cf1Smrg case $src in 250054b3d00Smrg -* | [=\(\)!]) src=./$src;; 2512ddb6cf1Smrg esac 2522ddb6cf1Smrg 2532ddb6cf1Smrg if test -n "$dir_arg"; then 2542ddb6cf1Smrg dst=$src 2552ddb6cf1Smrg dstdir=$dst 2562ddb6cf1Smrg test -d "$dstdir" 2572ddb6cf1Smrg dstdir_status=$? 2582ddb6cf1Smrg else 2592ddb6cf1Smrg 2602ddb6cf1Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 2612ddb6cf1Smrg # might cause directories to be created, which would be especially bad 2622ddb6cf1Smrg # if $src (and thus $dsttmp) contains '*'. 2632ddb6cf1Smrg if test ! -f "$src" && test ! -d "$src"; then 2642ddb6cf1Smrg echo "$0: $src does not exist." >&2 2652ddb6cf1Smrg exit 1 2662ddb6cf1Smrg fi 2672ddb6cf1Smrg 2682ddb6cf1Smrg if test -z "$dst_arg"; then 2692ddb6cf1Smrg echo "$0: no destination specified." >&2 2702ddb6cf1Smrg exit 1 2712ddb6cf1Smrg fi 2722ddb6cf1Smrg dst=$dst_arg 273a1d141d5Smrg 2742ddb6cf1Smrg # If destination is a directory, append the input filename; won't work 2752ddb6cf1Smrg # if double slashes aren't ignored. 2762ddb6cf1Smrg if test -d "$dst"; then 277054b3d00Smrg if test "$is_target_a_directory" = never; then 278054b3d00Smrg echo "$0: $dst_arg: Is a directory" >&2 279054b3d00Smrg exit 1 2802ddb6cf1Smrg fi 2812ddb6cf1Smrg dstdir=$dst 2822ddb6cf1Smrg dst=$dstdir/`basename "$src"` 2832ddb6cf1Smrg dstdir_status=0 2842ddb6cf1Smrg else 285054b3d00Smrg dstdir=`dirname "$dst"` 2862ddb6cf1Smrg test -d "$dstdir" 2872ddb6cf1Smrg dstdir_status=$? 2882ddb6cf1Smrg fi 2892ddb6cf1Smrg fi 2902ddb6cf1Smrg 2912ddb6cf1Smrg obsolete_mkdir_used=false 2922ddb6cf1Smrg 2932ddb6cf1Smrg if test $dstdir_status != 0; then 2942ddb6cf1Smrg case $posix_mkdir in 2952ddb6cf1Smrg '') 296054b3d00Smrg # Create intermediate dirs using mode 755 as modified by the umask. 297054b3d00Smrg # This is like FreeBSD 'install' as of 1997-10-28. 298054b3d00Smrg umask=`umask` 299054b3d00Smrg case $stripcmd.$umask in 300054b3d00Smrg # Optimize common cases. 301054b3d00Smrg *[2367][2367]) mkdir_umask=$umask;; 302054b3d00Smrg .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;; 303054b3d00Smrg 304054b3d00Smrg *[0-7]) 305054b3d00Smrg mkdir_umask=`expr $umask + 22 \ 306054b3d00Smrg - $umask % 100 % 40 + $umask % 20 \ 307054b3d00Smrg - $umask % 10 % 4 + $umask % 2 308054b3d00Smrg `;; 309054b3d00Smrg *) mkdir_umask=$umask,go-w;; 310054b3d00Smrg esac 311054b3d00Smrg 312054b3d00Smrg # With -d, create the new directory with the user-specified mode. 313054b3d00Smrg # Otherwise, rely on $mkdir_umask. 314054b3d00Smrg if test -n "$dir_arg"; then 315054b3d00Smrg mkdir_mode=-m$mode 316054b3d00Smrg else 317054b3d00Smrg mkdir_mode= 318054b3d00Smrg fi 319054b3d00Smrg 320054b3d00Smrg posix_mkdir=false 321054b3d00Smrg case $umask in 322054b3d00Smrg *[123567][0-7][0-7]) 323054b3d00Smrg # POSIX mkdir -p sets u+wx bits regardless of umask, which 324054b3d00Smrg # is incompatible with FreeBSD 'install' when (umask & 300) != 0. 325054b3d00Smrg ;; 326054b3d00Smrg *) 327054b3d00Smrg tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 328054b3d00Smrg trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0 329054b3d00Smrg 330054b3d00Smrg if (umask $mkdir_umask && 331054b3d00Smrg exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1 332054b3d00Smrg then 333054b3d00Smrg if test -z "$dir_arg" || { 334054b3d00Smrg # Check for POSIX incompatibilities with -m. 335054b3d00Smrg # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 336054b3d00Smrg # other-writable bit of parent directory when it shouldn't. 337054b3d00Smrg # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 338054b3d00Smrg ls_ld_tmpdir=`ls -ld "$tmpdir"` 339054b3d00Smrg case $ls_ld_tmpdir in 340054b3d00Smrg d????-?r-*) different_mode=700;; 341054b3d00Smrg d????-?--*) different_mode=755;; 342054b3d00Smrg *) false;; 343054b3d00Smrg esac && 344054b3d00Smrg $mkdirprog -m$different_mode -p -- "$tmpdir" && { 345054b3d00Smrg ls_ld_tmpdir_1=`ls -ld "$tmpdir"` 346054b3d00Smrg test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 347054b3d00Smrg } 348054b3d00Smrg } 349054b3d00Smrg then posix_mkdir=: 350054b3d00Smrg fi 351054b3d00Smrg rmdir "$tmpdir/d" "$tmpdir" 352054b3d00Smrg else 353054b3d00Smrg # Remove any dirs left behind by ancient mkdir implementations. 354054b3d00Smrg rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null 355054b3d00Smrg fi 356054b3d00Smrg trap '' 0;; 357054b3d00Smrg esac;; 3582ddb6cf1Smrg esac 359a1d141d5Smrg 3602ddb6cf1Smrg if 3612ddb6cf1Smrg $posix_mkdir && ( 362054b3d00Smrg umask $mkdir_umask && 363054b3d00Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 3642ddb6cf1Smrg ) 3652ddb6cf1Smrg then : 3662ddb6cf1Smrg else 3672ddb6cf1Smrg 3682ddb6cf1Smrg # The umask is ridiculous, or mkdir does not conform to POSIX, 3692ddb6cf1Smrg # or it failed possibly due to a race condition. Create the 3702ddb6cf1Smrg # directory the slow way, step by step, checking for races as we go. 3712ddb6cf1Smrg 3722ddb6cf1Smrg case $dstdir in 373054b3d00Smrg /*) prefix='/';; 374054b3d00Smrg [-=\(\)!]*) prefix='./';; 375054b3d00Smrg *) prefix='';; 3762ddb6cf1Smrg esac 3772ddb6cf1Smrg 3782ddb6cf1Smrg oIFS=$IFS 3792ddb6cf1Smrg IFS=/ 380054b3d00Smrg set -f 3812ddb6cf1Smrg set fnord $dstdir 3822ddb6cf1Smrg shift 383054b3d00Smrg set +f 3842ddb6cf1Smrg IFS=$oIFS 3852ddb6cf1Smrg 3862ddb6cf1Smrg prefixes= 3872ddb6cf1Smrg 3882ddb6cf1Smrg for d 3892ddb6cf1Smrg do 390054b3d00Smrg test X"$d" = X && continue 391054b3d00Smrg 392054b3d00Smrg prefix=$prefix$d 393054b3d00Smrg if test -d "$prefix"; then 394054b3d00Smrg prefixes= 395054b3d00Smrg else 396054b3d00Smrg if $posix_mkdir; then 397054b3d00Smrg (umask=$mkdir_umask && 398054b3d00Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 399054b3d00Smrg # Don't fail if two instances are running concurrently. 400054b3d00Smrg test -d "$prefix" || exit 1 401054b3d00Smrg else 402054b3d00Smrg case $prefix in 403054b3d00Smrg *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 404054b3d00Smrg *) qprefix=$prefix;; 405054b3d00Smrg esac 406054b3d00Smrg prefixes="$prefixes '$qprefix'" 407054b3d00Smrg fi 408054b3d00Smrg fi 409054b3d00Smrg prefix=$prefix/ 4102ddb6cf1Smrg done 4112ddb6cf1Smrg 4122ddb6cf1Smrg if test -n "$prefixes"; then 413054b3d00Smrg # Don't fail if two instances are running concurrently. 414054b3d00Smrg (umask $mkdir_umask && 415054b3d00Smrg eval "\$doit_exec \$mkdirprog $prefixes") || 416054b3d00Smrg test -d "$dstdir" || exit 1 417054b3d00Smrg obsolete_mkdir_used=true 4182ddb6cf1Smrg fi 4192ddb6cf1Smrg fi 4202ddb6cf1Smrg fi 4212ddb6cf1Smrg 4222ddb6cf1Smrg if test -n "$dir_arg"; then 4232ddb6cf1Smrg { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 4242ddb6cf1Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 4252ddb6cf1Smrg { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 4262ddb6cf1Smrg test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 4272ddb6cf1Smrg else 4282ddb6cf1Smrg 4292ddb6cf1Smrg # Make a couple of temp file names in the proper directory. 4302ddb6cf1Smrg dsttmp=$dstdir/_inst.$$_ 4312ddb6cf1Smrg rmtmp=$dstdir/_rm.$$_ 4322ddb6cf1Smrg 4332ddb6cf1Smrg # Trap to clean up those temp files at exit. 4342ddb6cf1Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 4352ddb6cf1Smrg 4362ddb6cf1Smrg # Copy the file name to the temp name. 4372ddb6cf1Smrg (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") && 4382ddb6cf1Smrg 4392ddb6cf1Smrg # and set any options; do chmod last to preserve setuid bits. 4402ddb6cf1Smrg # 4412ddb6cf1Smrg # If any of these fail, we abort the whole thing. If we want to 4422ddb6cf1Smrg # ignore errors from any of these, just make sure not to ignore 4432ddb6cf1Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 4442ddb6cf1Smrg # 4452ddb6cf1Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && 4462ddb6cf1Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && 4472ddb6cf1Smrg { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && 4482ddb6cf1Smrg { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 4492ddb6cf1Smrg 4502ddb6cf1Smrg # If -C, don't bother to copy if it wouldn't change the file. 4512ddb6cf1Smrg if $copy_on_change && 452054b3d00Smrg old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && 453054b3d00Smrg new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && 454054b3d00Smrg set -f && 4552ddb6cf1Smrg set X $old && old=:$2:$4:$5:$6 && 4562ddb6cf1Smrg set X $new && new=:$2:$4:$5:$6 && 457054b3d00Smrg set +f && 4582ddb6cf1Smrg test "$old" = "$new" && 4592ddb6cf1Smrg $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 4602ddb6cf1Smrg then 4612ddb6cf1Smrg rm -f "$dsttmp" 4622ddb6cf1Smrg else 4632ddb6cf1Smrg # Rename the file to the real destination. 4642ddb6cf1Smrg $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || 4652ddb6cf1Smrg 4662ddb6cf1Smrg # The rename failed, perhaps because mv can't rename something else 4672ddb6cf1Smrg # to itself, or perhaps because mv is so ancient that it does not 4682ddb6cf1Smrg # support -f. 4692ddb6cf1Smrg { 470054b3d00Smrg # Now remove or move aside any old file at destination location. 471054b3d00Smrg # We try this two ways since rm can't unlink itself on some 472054b3d00Smrg # systems and the destination file might be busy for other 473054b3d00Smrg # reasons. In this case, the final cleanup might fail but the new 474054b3d00Smrg # file should still install successfully. 475054b3d00Smrg { 476054b3d00Smrg test ! -f "$dst" || 477054b3d00Smrg $doit $rmcmd -f "$dst" 2>/dev/null || 478054b3d00Smrg { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && 479054b3d00Smrg { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; } 480054b3d00Smrg } || 481054b3d00Smrg { echo "$0: cannot unlink or rename $dst" >&2 482054b3d00Smrg (exit 1); exit 1 483054b3d00Smrg } 484054b3d00Smrg } && 485054b3d00Smrg 486054b3d00Smrg # Now rename the file to the real destination. 487054b3d00Smrg $doit $mvcmd "$dsttmp" "$dst" 4882ddb6cf1Smrg } 4892ddb6cf1Smrg fi || exit 1 4902ddb6cf1Smrg 4912ddb6cf1Smrg trap '' 0 4922ddb6cf1Smrg fi 4932ddb6cf1Smrgdone 494a1d141d5Smrg 4952ddb6cf1Smrg# Local variables: 4962ddb6cf1Smrg# eval: (add-hook 'write-file-hooks 'time-stamp) 4972ddb6cf1Smrg# time-stamp-start: "scriptversion=" 4982ddb6cf1Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 4992ddb6cf1Smrg# time-stamp-time-zone: "UTC" 5002ddb6cf1Smrg# time-stamp-end: "; # UTC" 5012ddb6cf1Smrg# End: 502