install-sh revision a0cd246f
10ed7c580Smrg#!/bin/sh 20ed7c580Smrg# install - install a program, script, or datafile 30ed7c580Smrg 4a0cd246fSmrgscriptversion=2024-06-19.01; # UTC 50ed7c580Smrg 60ed7c580Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 70ed7c580Smrg# later released in X11R6 (xc/config/util/install.sh) with the 80ed7c580Smrg# following copyright and license. 90ed7c580Smrg# 100ed7c580Smrg# Copyright (C) 1994 X Consortium 110ed7c580Smrg# 120ed7c580Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 130ed7c580Smrg# of this software and associated documentation files (the "Software"), to 140ed7c580Smrg# deal in the Software without restriction, including without limitation the 150ed7c580Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 160ed7c580Smrg# sell copies of the Software, and to permit persons to whom the Software is 170ed7c580Smrg# furnished to do so, subject to the following conditions: 180ed7c580Smrg# 190ed7c580Smrg# The above copyright notice and this permission notice shall be included in 200ed7c580Smrg# all copies or substantial portions of the Software. 210ed7c580Smrg# 220ed7c580Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 230ed7c580Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 240ed7c580Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 250ed7c580Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 260ed7c580Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 270ed7c580Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 280ed7c580Smrg# 290ed7c580Smrg# Except as contained in this notice, the name of the X Consortium shall not 300ed7c580Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 310ed7c580Smrg# ings in this Software without prior written authorization from the X Consor- 320ed7c580Smrg# tium. 330ed7c580Smrg# 340ed7c580Smrg# 350ed7c580Smrg# FSF changes to this file are in the public domain. 360ed7c580Smrg# 370ed7c580Smrg# Calling this script install-sh is preferred over install.sh, to prevent 380ed7c580Smrg# 'make' implicit rules from creating a file called install from it 390ed7c580Smrg# when there is no Makefile. 400ed7c580Smrg# 410ed7c580Smrg# This script is compatible with the BSD install script, but was written 420ed7c580Smrg# from scratch. 430ed7c580Smrg 441a28b98bSmrgtab=' ' 450ed7c580Smrgnl=' 460ed7c580Smrg' 471a28b98bSmrgIFS=" $tab$nl" 480ed7c580Smrg 491a28b98bSmrg# Set DOITPROG to "echo" to test this script. 500ed7c580Smrg 510ed7c580Smrgdoit=${DOITPROG-} 521a28b98bSmrgdoit_exec=${doit:-exec} 530ed7c580Smrg 540ed7c580Smrg# Put in absolute file names if you don't have them in your path; 550ed7c580Smrg# or use environment vars. 560ed7c580Smrg 570ed7c580Smrgchgrpprog=${CHGRPPROG-chgrp} 580ed7c580Smrgchmodprog=${CHMODPROG-chmod} 590ed7c580Smrgchownprog=${CHOWNPROG-chown} 600ed7c580Smrgcmpprog=${CMPPROG-cmp} 610ed7c580Smrgcpprog=${CPPROG-cp} 620ed7c580Smrgmkdirprog=${MKDIRPROG-mkdir} 630ed7c580Smrgmvprog=${MVPROG-mv} 640ed7c580Smrgrmprog=${RMPROG-rm} 650ed7c580Smrgstripprog=${STRIPPROG-strip} 660ed7c580Smrg 670ed7c580Smrgposix_mkdir= 680ed7c580Smrg 690ed7c580Smrg# Desired mode of installed file. 700ed7c580Smrgmode=0755 710ed7c580Smrg 721a28b98bSmrg# Create dirs (including intermediate dirs) using mode 755. 731a28b98bSmrg# This is like GNU 'install' as of coreutils 8.32 (2020). 741a28b98bSmrgmkdir_umask=22 751a28b98bSmrg 761a28b98bSmrgbackupsuffix= 770ed7c580Smrgchgrpcmd= 780ed7c580Smrgchmodcmd=$chmodprog 790ed7c580Smrgchowncmd= 800ed7c580Smrgmvcmd=$mvprog 810ed7c580Smrgrmcmd="$rmprog -f" 820ed7c580Smrgstripcmd= 830ed7c580Smrg 840ed7c580Smrgsrc= 850ed7c580Smrgdst= 860ed7c580Smrgdir_arg= 870ed7c580Smrgdst_arg= 880ed7c580Smrg 890ed7c580Smrgcopy_on_change=false 901a28b98bSmrgis_target_a_directory=possibly 910ed7c580Smrg 920ed7c580Smrgusage="\ 930ed7c580SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE 940ed7c580Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 950ed7c580Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 960ed7c580Smrg or: $0 [OPTION]... -d DIRECTORIES... 970ed7c580Smrg 980ed7c580SmrgIn the 1st form, copy SRCFILE to DSTFILE. 990ed7c580SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 1000ed7c580SmrgIn the 4th, create DIRECTORIES. 1010ed7c580Smrg 1020ed7c580SmrgOptions: 1030ed7c580Smrg --help display this help and exit. 1040ed7c580Smrg --version display version info and exit. 1050ed7c580Smrg 1060ed7c580Smrg -c (ignored) 1071a28b98bSmrg -C install only if different (preserve data modification time) 1080ed7c580Smrg -d create directories instead of installing files. 1090ed7c580Smrg -g GROUP $chgrpprog installed files to GROUP. 1100ed7c580Smrg -m MODE $chmodprog installed files to MODE. 1110ed7c580Smrg -o USER $chownprog installed files to USER. 1121a28b98bSmrg -p pass -p to $cpprog. 1130ed7c580Smrg -s $stripprog installed files. 1141a28b98bSmrg -S SUFFIX attempt to back up existing files, with suffix SUFFIX. 1150ed7c580Smrg -t DIRECTORY install into DIRECTORY. 1160ed7c580Smrg -T report an error if DSTFILE is a directory. 1170ed7c580Smrg 1180ed7c580SmrgEnvironment variables override the default commands: 1190ed7c580Smrg CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG 1200ed7c580Smrg RMPROG STRIPPROG 1211a28b98bSmrg 1221a28b98bSmrgBy default, rm is invoked with -f; when overridden with RMPROG, 1231a28b98bSmrgit's up to you to specify -f if you want it. 1241a28b98bSmrg 1251a28b98bSmrgIf -S is not specified, no backups are attempted. 1261a28b98bSmrg 127a0cd246fSmrgReport bugs to <bug-automake@gnu.org>. 128a0cd246fSmrgGNU Automake home page: <https://www.gnu.org/software/automake/>. 129a0cd246fSmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>." 1300ed7c580Smrg 1310ed7c580Smrgwhile test $# -ne 0; do 1320ed7c580Smrg case $1 in 1330ed7c580Smrg -c) ;; 1340ed7c580Smrg 1350ed7c580Smrg -C) copy_on_change=true;; 1360ed7c580Smrg 1370ed7c580Smrg -d) dir_arg=true;; 1380ed7c580Smrg 1390ed7c580Smrg -g) chgrpcmd="$chgrpprog $2" 1401a28b98bSmrg shift;; 1410ed7c580Smrg 1420ed7c580Smrg --help) echo "$usage"; exit $?;; 1430ed7c580Smrg 1440ed7c580Smrg -m) mode=$2 1451a28b98bSmrg case $mode in 1461a28b98bSmrg *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*) 1471a28b98bSmrg echo "$0: invalid mode: $mode" >&2 1481a28b98bSmrg exit 1;; 1491a28b98bSmrg esac 1501a28b98bSmrg shift;; 1510ed7c580Smrg 1520ed7c580Smrg -o) chowncmd="$chownprog $2" 1531a28b98bSmrg shift;; 1541a28b98bSmrg 1551a28b98bSmrg -p) cpprog="$cpprog -p";; 1560ed7c580Smrg 1570ed7c580Smrg -s) stripcmd=$stripprog;; 1580ed7c580Smrg 1591a28b98bSmrg -S) backupsuffix="$2" 1601a28b98bSmrg shift;; 1610ed7c580Smrg 1621a28b98bSmrg -t) 1631a28b98bSmrg is_target_a_directory=always 1641a28b98bSmrg dst_arg=$2 1651a28b98bSmrg # Protect names problematic for 'test' and other utilities. 1661a28b98bSmrg case $dst_arg in 1671a28b98bSmrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 1681a28b98bSmrg esac 1691a28b98bSmrg shift;; 1701a28b98bSmrg 1711a28b98bSmrg -T) is_target_a_directory=never;; 1720ed7c580Smrg 173a0cd246fSmrg --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;; 1740ed7c580Smrg 1751a28b98bSmrg --) shift 1761a28b98bSmrg break;; 1770ed7c580Smrg 1781a28b98bSmrg -*) echo "$0: invalid option: $1" >&2 1791a28b98bSmrg exit 1;; 1800ed7c580Smrg 1810ed7c580Smrg *) break;; 1820ed7c580Smrg esac 1830ed7c580Smrg shift 1840ed7c580Smrgdone 1850ed7c580Smrg 1861a28b98bSmrg# We allow the use of options -d and -T together, by making -d 1871a28b98bSmrg# take the precedence; this is for compatibility with GNU install. 1881a28b98bSmrg 1891a28b98bSmrgif test -n "$dir_arg"; then 1901a28b98bSmrg if test -n "$dst_arg"; then 1911a28b98bSmrg echo "$0: target directory not allowed when installing a directory." >&2 1921a28b98bSmrg exit 1 1931a28b98bSmrg fi 1941a28b98bSmrgfi 1951a28b98bSmrg 1960ed7c580Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then 1970ed7c580Smrg # When -d is used, all remaining arguments are directories to create. 1980ed7c580Smrg # When -t is used, the destination is already specified. 1990ed7c580Smrg # Otherwise, the last argument is the destination. Remove it from $@. 2000ed7c580Smrg for arg 2010ed7c580Smrg do 2020ed7c580Smrg if test -n "$dst_arg"; then 2030ed7c580Smrg # $@ is not empty: it contains at least $arg. 2040ed7c580Smrg set fnord "$@" "$dst_arg" 2050ed7c580Smrg shift # fnord 2060ed7c580Smrg fi 2070ed7c580Smrg shift # arg 2080ed7c580Smrg dst_arg=$arg 2090ed7c580Smrg # Protect names problematic for 'test' and other utilities. 2100ed7c580Smrg case $dst_arg in 2110ed7c580Smrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 2120ed7c580Smrg esac 2130ed7c580Smrg done 2140ed7c580Smrgfi 2150ed7c580Smrg 2160ed7c580Smrgif test $# -eq 0; then 2170ed7c580Smrg if test -z "$dir_arg"; then 2180ed7c580Smrg echo "$0: no input file specified." >&2 2190ed7c580Smrg exit 1 2200ed7c580Smrg fi 2210ed7c580Smrg # It's OK to call 'install-sh -d' without argument. 2220ed7c580Smrg # This can happen when creating conditional directories. 2230ed7c580Smrg exit 0 2240ed7c580Smrgfi 2250ed7c580Smrg 2261a28b98bSmrgif test -z "$dir_arg"; then 2271a28b98bSmrg if test $# -gt 1 || test "$is_target_a_directory" = always; then 2281a28b98bSmrg if test ! -d "$dst_arg"; then 2291a28b98bSmrg echo "$0: $dst_arg: Is not a directory." >&2 2301a28b98bSmrg exit 1 2311a28b98bSmrg fi 2321a28b98bSmrg fi 2331a28b98bSmrgfi 2341a28b98bSmrg 2350ed7c580Smrgif test -z "$dir_arg"; then 2360ed7c580Smrg do_exit='(exit $ret); exit $ret' 2370ed7c580Smrg trap "ret=129; $do_exit" 1 2380ed7c580Smrg trap "ret=130; $do_exit" 2 2390ed7c580Smrg trap "ret=141; $do_exit" 13 2400ed7c580Smrg trap "ret=143; $do_exit" 15 2410ed7c580Smrg 2420ed7c580Smrg # Set umask so as not to create temps with too-generous modes. 2430ed7c580Smrg # However, 'strip' requires both read and write access to temps. 2440ed7c580Smrg case $mode in 2450ed7c580Smrg # Optimize common cases. 2460ed7c580Smrg *644) cp_umask=133;; 2470ed7c580Smrg *755) cp_umask=22;; 2480ed7c580Smrg 2490ed7c580Smrg *[0-7]) 2500ed7c580Smrg if test -z "$stripcmd"; then 2511a28b98bSmrg u_plus_rw= 2520ed7c580Smrg else 2531a28b98bSmrg u_plus_rw='% 200' 2540ed7c580Smrg fi 2550ed7c580Smrg cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 2560ed7c580Smrg *) 2570ed7c580Smrg if test -z "$stripcmd"; then 2581a28b98bSmrg u_plus_rw= 2590ed7c580Smrg else 2601a28b98bSmrg u_plus_rw=,u+rw 2610ed7c580Smrg fi 2620ed7c580Smrg cp_umask=$mode$u_plus_rw;; 2630ed7c580Smrg esac 2640ed7c580Smrgfi 2650ed7c580Smrg 2660ed7c580Smrgfor src 2670ed7c580Smrgdo 2680ed7c580Smrg # Protect names problematic for 'test' and other utilities. 2690ed7c580Smrg case $src in 2700ed7c580Smrg -* | [=\(\)!]) src=./$src;; 2710ed7c580Smrg esac 2720ed7c580Smrg 2730ed7c580Smrg if test -n "$dir_arg"; then 2740ed7c580Smrg dst=$src 2750ed7c580Smrg dstdir=$dst 2760ed7c580Smrg test -d "$dstdir" 2770ed7c580Smrg dstdir_status=$? 2781a28b98bSmrg # Don't chown directories that already exist. 2791a28b98bSmrg if test $dstdir_status = 0; then 2801a28b98bSmrg chowncmd="" 2811a28b98bSmrg fi 2820ed7c580Smrg else 2830ed7c580Smrg 2840ed7c580Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 2850ed7c580Smrg # might cause directories to be created, which would be especially bad 2860ed7c580Smrg # if $src (and thus $dsttmp) contains '*'. 2870ed7c580Smrg if test ! -f "$src" && test ! -d "$src"; then 2880ed7c580Smrg echo "$0: $src does not exist." >&2 2890ed7c580Smrg exit 1 2900ed7c580Smrg fi 2910ed7c580Smrg 2920ed7c580Smrg if test -z "$dst_arg"; then 2930ed7c580Smrg echo "$0: no destination specified." >&2 2940ed7c580Smrg exit 1 2950ed7c580Smrg fi 2960ed7c580Smrg dst=$dst_arg 2970ed7c580Smrg 2981a28b98bSmrg # If destination is a directory, append the input filename. 2990ed7c580Smrg if test -d "$dst"; then 3001a28b98bSmrg if test "$is_target_a_directory" = never; then 3011a28b98bSmrg echo "$0: $dst_arg: Is a directory" >&2 3021a28b98bSmrg exit 1 3030ed7c580Smrg fi 3040ed7c580Smrg dstdir=$dst 3051a28b98bSmrg dstbase=`basename "$src"` 3061a28b98bSmrg case $dst in 3071a28b98bSmrg */) dst=$dst$dstbase;; 3081a28b98bSmrg *) dst=$dst/$dstbase;; 3091a28b98bSmrg esac 3100ed7c580Smrg dstdir_status=0 3110ed7c580Smrg else 3121a28b98bSmrg dstdir=`dirname "$dst"` 3130ed7c580Smrg test -d "$dstdir" 3140ed7c580Smrg dstdir_status=$? 3150ed7c580Smrg fi 3160ed7c580Smrg fi 3170ed7c580Smrg 3181a28b98bSmrg case $dstdir in 3191a28b98bSmrg */) dstdirslash=$dstdir;; 3201a28b98bSmrg *) dstdirslash=$dstdir/;; 3211a28b98bSmrg esac 3221a28b98bSmrg 3230ed7c580Smrg obsolete_mkdir_used=false 3240ed7c580Smrg 3250ed7c580Smrg if test $dstdir_status != 0; then 3260ed7c580Smrg case $posix_mkdir in 3270ed7c580Smrg '') 3281a28b98bSmrg # With -d, create the new directory with the user-specified mode. 3291a28b98bSmrg # Otherwise, rely on $mkdir_umask. 3301a28b98bSmrg if test -n "$dir_arg"; then 3311a28b98bSmrg mkdir_mode=-m$mode 3321a28b98bSmrg else 3331a28b98bSmrg mkdir_mode= 3341a28b98bSmrg fi 3351a28b98bSmrg 3361a28b98bSmrg posix_mkdir=false 3371a28b98bSmrg # The $RANDOM variable is not portable (e.g., dash). Use it 3381a28b98bSmrg # here however when possible just to lower collision chance. 3391a28b98bSmrg tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 3401a28b98bSmrg 3411a28b98bSmrg trap ' 3421a28b98bSmrg ret=$? 3431a28b98bSmrg rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null 3441a28b98bSmrg exit $ret 3451a28b98bSmrg ' 0 3461a28b98bSmrg 3471a28b98bSmrg # Because "mkdir -p" follows existing symlinks and we likely work 348a0cd246fSmrg # directly in world-writable /tmp, make sure that the '$tmpdir' 3491a28b98bSmrg # directory is successfully created first before we actually test 3501a28b98bSmrg # 'mkdir -p'. 3511a28b98bSmrg if (umask $mkdir_umask && 3521a28b98bSmrg $mkdirprog $mkdir_mode "$tmpdir" && 3531a28b98bSmrg exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1 3541a28b98bSmrg then 3551a28b98bSmrg if test -z "$dir_arg" || { 356a0cd246fSmrg # Check for POSIX incompatibility with -m. 3571a28b98bSmrg # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 3581a28b98bSmrg # other-writable bit of parent directory when it shouldn't. 3591a28b98bSmrg # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 3601a28b98bSmrg test_tmpdir="$tmpdir/a" 3611a28b98bSmrg ls_ld_tmpdir=`ls -ld "$test_tmpdir"` 3621a28b98bSmrg case $ls_ld_tmpdir in 3631a28b98bSmrg d????-?r-*) different_mode=700;; 3641a28b98bSmrg d????-?--*) different_mode=755;; 3651a28b98bSmrg *) false;; 3661a28b98bSmrg esac && 3671a28b98bSmrg $mkdirprog -m$different_mode -p -- "$test_tmpdir" && { 3681a28b98bSmrg ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"` 3691a28b98bSmrg test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 3701a28b98bSmrg } 3711a28b98bSmrg } 3721a28b98bSmrg then posix_mkdir=: 3731a28b98bSmrg fi 3741a28b98bSmrg rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 3750ed7c580Smrg else 3761a28b98bSmrg # Remove any dirs left behind by ancient mkdir implementations. 3771a28b98bSmrg rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null 3780ed7c580Smrg fi 3791a28b98bSmrg trap '' 0;; 3800ed7c580Smrg esac 3810ed7c580Smrg 3820ed7c580Smrg if 3830ed7c580Smrg $posix_mkdir && ( 3841a28b98bSmrg umask $mkdir_umask && 3851a28b98bSmrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 3860ed7c580Smrg ) 3870ed7c580Smrg then : 3880ed7c580Smrg else 3890ed7c580Smrg 3901a28b98bSmrg # mkdir does not conform to POSIX, 3910ed7c580Smrg # or it failed possibly due to a race condition. Create the 3920ed7c580Smrg # directory the slow way, step by step, checking for races as we go. 3930ed7c580Smrg 3940ed7c580Smrg case $dstdir in 3951a28b98bSmrg /*) prefix='/';; 3961a28b98bSmrg [-=\(\)!]*) prefix='./';; 3971a28b98bSmrg *) prefix='';; 3980ed7c580Smrg esac 3990ed7c580Smrg 4000ed7c580Smrg oIFS=$IFS 4010ed7c580Smrg IFS=/ 4021a28b98bSmrg set -f 4030ed7c580Smrg set fnord $dstdir 4040ed7c580Smrg shift 4051a28b98bSmrg set +f 4060ed7c580Smrg IFS=$oIFS 4070ed7c580Smrg 4080ed7c580Smrg prefixes= 4090ed7c580Smrg 4100ed7c580Smrg for d 4110ed7c580Smrg do 4121a28b98bSmrg test X"$d" = X && continue 4131a28b98bSmrg 4141a28b98bSmrg prefix=$prefix$d 4151a28b98bSmrg if test -d "$prefix"; then 4161a28b98bSmrg prefixes= 4171a28b98bSmrg else 4181a28b98bSmrg if $posix_mkdir; then 4191a28b98bSmrg (umask $mkdir_umask && 4201a28b98bSmrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 4211a28b98bSmrg # Don't fail if two instances are running concurrently. 4221a28b98bSmrg test -d "$prefix" || exit 1 4231a28b98bSmrg else 4241a28b98bSmrg case $prefix in 4251a28b98bSmrg *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 4261a28b98bSmrg *) qprefix=$prefix;; 4271a28b98bSmrg esac 4281a28b98bSmrg prefixes="$prefixes '$qprefix'" 4291a28b98bSmrg fi 4301a28b98bSmrg fi 4311a28b98bSmrg prefix=$prefix/ 4320ed7c580Smrg done 4330ed7c580Smrg 4340ed7c580Smrg if test -n "$prefixes"; then 4351a28b98bSmrg # Don't fail if two instances are running concurrently. 4361a28b98bSmrg (umask $mkdir_umask && 4371a28b98bSmrg eval "\$doit_exec \$mkdirprog $prefixes") || 4381a28b98bSmrg test -d "$dstdir" || exit 1 4391a28b98bSmrg obsolete_mkdir_used=true 4400ed7c580Smrg fi 4410ed7c580Smrg fi 4420ed7c580Smrg fi 4430ed7c580Smrg 4440ed7c580Smrg if test -n "$dir_arg"; then 4450ed7c580Smrg { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 4460ed7c580Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 4470ed7c580Smrg { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 4480ed7c580Smrg test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 4490ed7c580Smrg else 4500ed7c580Smrg 4510ed7c580Smrg # Make a couple of temp file names in the proper directory. 4521a28b98bSmrg dsttmp=${dstdirslash}_inst.$$_ 4531a28b98bSmrg rmtmp=${dstdirslash}_rm.$$_ 4540ed7c580Smrg 4550ed7c580Smrg # Trap to clean up those temp files at exit. 4560ed7c580Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 4570ed7c580Smrg 4580ed7c580Smrg # Copy the file name to the temp name. 4591a28b98bSmrg (umask $cp_umask && 4601a28b98bSmrg { test -z "$stripcmd" || { 4611a28b98bSmrg # Create $dsttmp read-write so that cp doesn't create it read-only, 4621a28b98bSmrg # which would cause strip to fail. 4631a28b98bSmrg if test -z "$doit"; then 4641a28b98bSmrg : >"$dsttmp" # No need to fork-exec 'touch'. 4651a28b98bSmrg else 4661a28b98bSmrg $doit touch "$dsttmp" 4671a28b98bSmrg fi 4681a28b98bSmrg } 4691a28b98bSmrg } && 4701a28b98bSmrg $doit_exec $cpprog "$src" "$dsttmp") && 4710ed7c580Smrg 4720ed7c580Smrg # and set any options; do chmod last to preserve setuid bits. 4730ed7c580Smrg # 4740ed7c580Smrg # If any of these fail, we abort the whole thing. If we want to 4750ed7c580Smrg # ignore errors from any of these, just make sure not to ignore 4760ed7c580Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 4770ed7c580Smrg # 4780ed7c580Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && 4790ed7c580Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && 4800ed7c580Smrg { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && 4810ed7c580Smrg { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 4820ed7c580Smrg 4830ed7c580Smrg # If -C, don't bother to copy if it wouldn't change the file. 4840ed7c580Smrg if $copy_on_change && 4851a28b98bSmrg old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && 4861a28b98bSmrg new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && 4871a28b98bSmrg set -f && 4880ed7c580Smrg set X $old && old=:$2:$4:$5:$6 && 4890ed7c580Smrg set X $new && new=:$2:$4:$5:$6 && 4901a28b98bSmrg set +f && 4910ed7c580Smrg test "$old" = "$new" && 4920ed7c580Smrg $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 4930ed7c580Smrg then 4940ed7c580Smrg rm -f "$dsttmp" 4950ed7c580Smrg else 4961a28b98bSmrg # If $backupsuffix is set, and the file being installed 4971a28b98bSmrg # already exists, attempt a backup. Don't worry if it fails, 4981a28b98bSmrg # e.g., if mv doesn't support -f. 4991a28b98bSmrg if test -n "$backupsuffix" && test -f "$dst"; then 5001a28b98bSmrg $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null 5011a28b98bSmrg fi 5021a28b98bSmrg 5030ed7c580Smrg # Rename the file to the real destination. 5040ed7c580Smrg $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || 5050ed7c580Smrg 5060ed7c580Smrg # The rename failed, perhaps because mv can't rename something else 5070ed7c580Smrg # to itself, or perhaps because mv is so ancient that it does not 5080ed7c580Smrg # support -f. 5090ed7c580Smrg { 5101a28b98bSmrg # Now remove or move aside any old file at destination location. 5111a28b98bSmrg # We try this two ways since rm can't unlink itself on some 5121a28b98bSmrg # systems and the destination file might be busy for other 5131a28b98bSmrg # reasons. In this case, the final cleanup might fail but the new 5141a28b98bSmrg # file should still install successfully. 5151a28b98bSmrg { 5161a28b98bSmrg test ! -f "$dst" || 5171a28b98bSmrg $doit $rmcmd "$dst" 2>/dev/null || 5181a28b98bSmrg { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && 5191a28b98bSmrg { $doit $rmcmd "$rmtmp" 2>/dev/null; :; } 5201a28b98bSmrg } || 5211a28b98bSmrg { echo "$0: cannot unlink or rename $dst" >&2 5221a28b98bSmrg (exit 1); exit 1 5231a28b98bSmrg } 5241a28b98bSmrg } && 5251a28b98bSmrg 5261a28b98bSmrg # Now rename the file to the real destination. 5271a28b98bSmrg $doit $mvcmd "$dsttmp" "$dst" 5280ed7c580Smrg } 5290ed7c580Smrg fi || exit 1 5300ed7c580Smrg 5310ed7c580Smrg trap '' 0 5320ed7c580Smrg fi 5330ed7c580Smrgdone 5340ed7c580Smrg 5350ed7c580Smrg# Local variables: 5361a28b98bSmrg# eval: (add-hook 'before-save-hook 'time-stamp) 5370ed7c580Smrg# time-stamp-start: "scriptversion=" 5380ed7c580Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 5391a28b98bSmrg# time-stamp-time-zone: "UTC0" 5400ed7c580Smrg# time-stamp-end: "; # UTC" 5410ed7c580Smrg# End: 542