install-sh revision fc34a306
10e20ee16Smrg#!/bin/sh 20e20ee16Smrg# install - install a program, script, or datafile 33e6697d4Smrg 4fc34a306Smrgscriptversion=2018-03-11.20; # UTC 53e6697d4Smrg 60e20ee16Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 70e20ee16Smrg# later released in X11R6 (xc/config/util/install.sh) with the 80e20ee16Smrg# following copyright and license. 90e20ee16Smrg# 100e20ee16Smrg# Copyright (C) 1994 X Consortium 110e20ee16Smrg# 120e20ee16Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 130e20ee16Smrg# of this software and associated documentation files (the "Software"), to 140e20ee16Smrg# deal in the Software without restriction, including without limitation the 150e20ee16Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 160e20ee16Smrg# sell copies of the Software, and to permit persons to whom the Software is 170e20ee16Smrg# furnished to do so, subject to the following conditions: 180e20ee16Smrg# 190e20ee16Smrg# The above copyright notice and this permission notice shall be included in 200e20ee16Smrg# all copies or substantial portions of the Software. 210e20ee16Smrg# 220e20ee16Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 230e20ee16Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 240e20ee16Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 250e20ee16Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 260e20ee16Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 270e20ee16Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 280e20ee16Smrg# 290e20ee16Smrg# Except as contained in this notice, the name of the X Consortium shall not 300e20ee16Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 310e20ee16Smrg# ings in this Software without prior written authorization from the X Consor- 320e20ee16Smrg# tium. 330e20ee16Smrg# 340e20ee16Smrg# 350e20ee16Smrg# FSF changes to this file are in the public domain. 360e20ee16Smrg# 370e20ee16Smrg# Calling this script install-sh is preferred over install.sh, to prevent 382f167a42Smrg# 'make' implicit rules from creating a file called install from it 390e20ee16Smrg# when there is no Makefile. 400e20ee16Smrg# 410e20ee16Smrg# This script is compatible with the BSD install script, but was written 423e6697d4Smrg# from scratch. 430e20ee16Smrg 442f167a42Smrgtab=' ' 453e6697d4Smrgnl=' 463e6697d4Smrg' 472f167a42SmrgIFS=" $tab$nl" 480e20ee16Smrg 492f167a42Smrg# Set DOITPROG to "echo" to test this script. 500e20ee16Smrg 513e6697d4Smrgdoit=${DOITPROG-} 522f167a42Smrgdoit_exec=${doit:-exec} 530e20ee16Smrg 543e6697d4Smrg# Put in absolute file names if you don't have them in your path; 553e6697d4Smrg# or use environment vars. 563e6697d4Smrg 573e6697d4Smrgchgrpprog=${CHGRPPROG-chgrp} 583e6697d4Smrgchmodprog=${CHMODPROG-chmod} 593e6697d4Smrgchownprog=${CHOWNPROG-chown} 603e6697d4Smrgcmpprog=${CMPPROG-cmp} 613e6697d4Smrgcpprog=${CPPROG-cp} 623e6697d4Smrgmkdirprog=${MKDIRPROG-mkdir} 633e6697d4Smrgmvprog=${MVPROG-mv} 643e6697d4Smrgrmprog=${RMPROG-rm} 653e6697d4Smrgstripprog=${STRIPPROG-strip} 663e6697d4Smrg 673e6697d4Smrgposix_mkdir= 683e6697d4Smrg 693e6697d4Smrg# Desired mode of installed file. 703e6697d4Smrgmode=0755 713e6697d4Smrg 723e6697d4Smrgchgrpcmd= 733e6697d4Smrgchmodcmd=$chmodprog 743e6697d4Smrgchowncmd= 753e6697d4Smrgmvcmd=$mvprog 763e6697d4Smrgrmcmd="$rmprog -f" 773e6697d4Smrgstripcmd= 780e20ee16Smrg 793e6697d4Smrgsrc= 803e6697d4Smrgdst= 813e6697d4Smrgdir_arg= 823e6697d4Smrgdst_arg= 830e20ee16Smrg 843e6697d4Smrgcopy_on_change=false 852f167a42Smrgis_target_a_directory=possibly 860e20ee16Smrg 873e6697d4Smrgusage="\ 883e6697d4SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE 893e6697d4Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 903e6697d4Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 913e6697d4Smrg or: $0 [OPTION]... -d DIRECTORIES... 920e20ee16Smrg 933e6697d4SmrgIn the 1st form, copy SRCFILE to DSTFILE. 943e6697d4SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 953e6697d4SmrgIn the 4th, create DIRECTORIES. 960e20ee16Smrg 973e6697d4SmrgOptions: 983e6697d4Smrg --help display this help and exit. 993e6697d4Smrg --version display version info and exit. 1000e20ee16Smrg 1013e6697d4Smrg -c (ignored) 1023e6697d4Smrg -C install only if different (preserve the last data modification time) 1033e6697d4Smrg -d create directories instead of installing files. 1043e6697d4Smrg -g GROUP $chgrpprog installed files to GROUP. 1053e6697d4Smrg -m MODE $chmodprog installed files to MODE. 1063e6697d4Smrg -o USER $chownprog installed files to USER. 1073e6697d4Smrg -s $stripprog installed files. 1083e6697d4Smrg -t DIRECTORY install into DIRECTORY. 1093e6697d4Smrg -T report an error if DSTFILE is a directory. 1100e20ee16Smrg 1113e6697d4SmrgEnvironment variables override the default commands: 1123e6697d4Smrg CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG 1133e6697d4Smrg RMPROG STRIPPROG 1143e6697d4Smrg" 1150e20ee16Smrg 1163e6697d4Smrgwhile test $# -ne 0; do 1173e6697d4Smrg case $1 in 1183e6697d4Smrg -c) ;; 1190e20ee16Smrg 1203e6697d4Smrg -C) copy_on_change=true;; 1210e20ee16Smrg 1223e6697d4Smrg -d) dir_arg=true;; 1230e20ee16Smrg 1243e6697d4Smrg -g) chgrpcmd="$chgrpprog $2" 1252f167a42Smrg shift;; 1260e20ee16Smrg 1273e6697d4Smrg --help) echo "$usage"; exit $?;; 1280e20ee16Smrg 1293e6697d4Smrg -m) mode=$2 1302f167a42Smrg case $mode in 1312f167a42Smrg *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*) 1322f167a42Smrg echo "$0: invalid mode: $mode" >&2 1332f167a42Smrg exit 1;; 1342f167a42Smrg esac 1352f167a42Smrg shift;; 1360e20ee16Smrg 1373e6697d4Smrg -o) chowncmd="$chownprog $2" 1382f167a42Smrg shift;; 1390e20ee16Smrg 1403e6697d4Smrg -s) stripcmd=$stripprog;; 1410e20ee16Smrg 1422f167a42Smrg -t) 1432f167a42Smrg is_target_a_directory=always 1442f167a42Smrg dst_arg=$2 1452f167a42Smrg # Protect names problematic for 'test' and other utilities. 1462f167a42Smrg case $dst_arg in 1472f167a42Smrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 1482f167a42Smrg esac 1492f167a42Smrg shift;; 1500e20ee16Smrg 1512f167a42Smrg -T) is_target_a_directory=never;; 1520e20ee16Smrg 1533e6697d4Smrg --version) echo "$0 $scriptversion"; exit $?;; 1540e20ee16Smrg 1552f167a42Smrg --) shift 1562f167a42Smrg break;; 1570e20ee16Smrg 1582f167a42Smrg -*) echo "$0: invalid option: $1" >&2 1592f167a42Smrg exit 1;; 1600e20ee16Smrg 1613e6697d4Smrg *) break;; 1623e6697d4Smrg esac 1633e6697d4Smrg shift 1643e6697d4Smrgdone 1650e20ee16Smrg 1662f167a42Smrg# We allow the use of options -d and -T together, by making -d 1672f167a42Smrg# take the precedence; this is for compatibility with GNU install. 1682f167a42Smrg 1692f167a42Smrgif test -n "$dir_arg"; then 1702f167a42Smrg if test -n "$dst_arg"; then 1712f167a42Smrg echo "$0: target directory not allowed when installing a directory." >&2 1722f167a42Smrg exit 1 1732f167a42Smrg fi 1742f167a42Smrgfi 1752f167a42Smrg 1763e6697d4Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then 1773e6697d4Smrg # When -d is used, all remaining arguments are directories to create. 1783e6697d4Smrg # When -t is used, the destination is already specified. 1793e6697d4Smrg # Otherwise, the last argument is the destination. Remove it from $@. 1803e6697d4Smrg for arg 1813e6697d4Smrg do 1823e6697d4Smrg if test -n "$dst_arg"; then 1833e6697d4Smrg # $@ is not empty: it contains at least $arg. 1843e6697d4Smrg set fnord "$@" "$dst_arg" 1853e6697d4Smrg shift # fnord 1863e6697d4Smrg fi 1873e6697d4Smrg shift # arg 1883e6697d4Smrg dst_arg=$arg 1892f167a42Smrg # Protect names problematic for 'test' and other utilities. 1902f167a42Smrg case $dst_arg in 1912f167a42Smrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 1922f167a42Smrg esac 1933e6697d4Smrg done 1943e6697d4Smrgfi 1950e20ee16Smrg 1963e6697d4Smrgif test $# -eq 0; then 1973e6697d4Smrg if test -z "$dir_arg"; then 1983e6697d4Smrg echo "$0: no input file specified." >&2 1993e6697d4Smrg exit 1 2003e6697d4Smrg fi 2012f167a42Smrg # It's OK to call 'install-sh -d' without argument. 2023e6697d4Smrg # This can happen when creating conditional directories. 2033e6697d4Smrg exit 0 2043e6697d4Smrgfi 2050e20ee16Smrg 2063e6697d4Smrgif test -z "$dir_arg"; then 2072f167a42Smrg if test $# -gt 1 || test "$is_target_a_directory" = always; then 2082f167a42Smrg if test ! -d "$dst_arg"; then 2092f167a42Smrg echo "$0: $dst_arg: Is not a directory." >&2 2102f167a42Smrg exit 1 2112f167a42Smrg fi 2122f167a42Smrg fi 2132f167a42Smrgfi 2142f167a42Smrg 2152f167a42Smrgif test -z "$dir_arg"; then 2162f167a42Smrg do_exit='(exit $ret); exit $ret' 2172f167a42Smrg trap "ret=129; $do_exit" 1 2182f167a42Smrg trap "ret=130; $do_exit" 2 2192f167a42Smrg trap "ret=141; $do_exit" 13 2202f167a42Smrg trap "ret=143; $do_exit" 15 2213e6697d4Smrg 2223e6697d4Smrg # Set umask so as not to create temps with too-generous modes. 2233e6697d4Smrg # However, 'strip' requires both read and write access to temps. 2243e6697d4Smrg case $mode in 2253e6697d4Smrg # Optimize common cases. 2263e6697d4Smrg *644) cp_umask=133;; 2273e6697d4Smrg *755) cp_umask=22;; 2283e6697d4Smrg 2293e6697d4Smrg *[0-7]) 2303e6697d4Smrg if test -z "$stripcmd"; then 2312f167a42Smrg u_plus_rw= 2323e6697d4Smrg else 2332f167a42Smrg u_plus_rw='% 200' 2343e6697d4Smrg fi 2353e6697d4Smrg cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 2363e6697d4Smrg *) 2373e6697d4Smrg if test -z "$stripcmd"; then 2382f167a42Smrg u_plus_rw= 2393e6697d4Smrg else 2402f167a42Smrg u_plus_rw=,u+rw 2413e6697d4Smrg fi 2423e6697d4Smrg cp_umask=$mode$u_plus_rw;; 2433e6697d4Smrg esac 2443e6697d4Smrgfi 2450e20ee16Smrg 2463e6697d4Smrgfor src 2473e6697d4Smrgdo 2482f167a42Smrg # Protect names problematic for 'test' and other utilities. 2493e6697d4Smrg case $src in 2502f167a42Smrg -* | [=\(\)!]) src=./$src;; 2513e6697d4Smrg esac 2523e6697d4Smrg 2533e6697d4Smrg if test -n "$dir_arg"; then 2543e6697d4Smrg dst=$src 2553e6697d4Smrg dstdir=$dst 2563e6697d4Smrg test -d "$dstdir" 2573e6697d4Smrg dstdir_status=$? 2583e6697d4Smrg else 2593e6697d4Smrg 2603e6697d4Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 2613e6697d4Smrg # might cause directories to be created, which would be especially bad 2623e6697d4Smrg # if $src (and thus $dsttmp) contains '*'. 2633e6697d4Smrg if test ! -f "$src" && test ! -d "$src"; then 2643e6697d4Smrg echo "$0: $src does not exist." >&2 2653e6697d4Smrg exit 1 2663e6697d4Smrg fi 2673e6697d4Smrg 2683e6697d4Smrg if test -z "$dst_arg"; then 2693e6697d4Smrg echo "$0: no destination specified." >&2 2703e6697d4Smrg exit 1 2713e6697d4Smrg fi 2723e6697d4Smrg dst=$dst_arg 2730e20ee16Smrg 274fc34a306Smrg # If destination is a directory, append the input filename. 2753e6697d4Smrg if test -d "$dst"; then 2762f167a42Smrg if test "$is_target_a_directory" = never; then 2772f167a42Smrg echo "$0: $dst_arg: Is a directory" >&2 2782f167a42Smrg exit 1 2793e6697d4Smrg fi 2803e6697d4Smrg dstdir=$dst 281fc34a306Smrg dstbase=`basename "$src"` 282fc34a306Smrg case $dst in 283fc34a306Smrg */) dst=$dst$dstbase;; 284fc34a306Smrg *) dst=$dst/$dstbase;; 285fc34a306Smrg esac 2863e6697d4Smrg dstdir_status=0 2873e6697d4Smrg else 2882f167a42Smrg dstdir=`dirname "$dst"` 2893e6697d4Smrg test -d "$dstdir" 2903e6697d4Smrg dstdir_status=$? 2913e6697d4Smrg fi 2923e6697d4Smrg fi 2933e6697d4Smrg 294fc34a306Smrg case $dstdir in 295fc34a306Smrg */) dstdirslash=$dstdir;; 296fc34a306Smrg *) dstdirslash=$dstdir/;; 297fc34a306Smrg esac 298fc34a306Smrg 2993e6697d4Smrg obsolete_mkdir_used=false 3003e6697d4Smrg 3013e6697d4Smrg if test $dstdir_status != 0; then 3023e6697d4Smrg case $posix_mkdir in 3033e6697d4Smrg '') 3042f167a42Smrg # Create intermediate dirs using mode 755 as modified by the umask. 3052f167a42Smrg # This is like FreeBSD 'install' as of 1997-10-28. 3062f167a42Smrg umask=`umask` 3072f167a42Smrg case $stripcmd.$umask in 3082f167a42Smrg # Optimize common cases. 3092f167a42Smrg *[2367][2367]) mkdir_umask=$umask;; 3102f167a42Smrg .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;; 3112f167a42Smrg 3122f167a42Smrg *[0-7]) 3132f167a42Smrg mkdir_umask=`expr $umask + 22 \ 3142f167a42Smrg - $umask % 100 % 40 + $umask % 20 \ 3152f167a42Smrg - $umask % 10 % 4 + $umask % 2 3162f167a42Smrg `;; 3172f167a42Smrg *) mkdir_umask=$umask,go-w;; 3182f167a42Smrg esac 3192f167a42Smrg 3202f167a42Smrg # With -d, create the new directory with the user-specified mode. 3212f167a42Smrg # Otherwise, rely on $mkdir_umask. 3222f167a42Smrg if test -n "$dir_arg"; then 3232f167a42Smrg mkdir_mode=-m$mode 3242f167a42Smrg else 3252f167a42Smrg mkdir_mode= 3262f167a42Smrg fi 3272f167a42Smrg 3282f167a42Smrg posix_mkdir=false 3292f167a42Smrg case $umask in 3302f167a42Smrg *[123567][0-7][0-7]) 3312f167a42Smrg # POSIX mkdir -p sets u+wx bits regardless of umask, which 3322f167a42Smrg # is incompatible with FreeBSD 'install' when (umask & 300) != 0. 3332f167a42Smrg ;; 3342f167a42Smrg *) 335fc34a306Smrg # Note that $RANDOM variable is not portable (e.g. dash); Use it 336fc34a306Smrg # here however when possible just to lower collision chance. 3372f167a42Smrg tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 3382f167a42Smrg 339fc34a306Smrg trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0 340fc34a306Smrg 341fc34a306Smrg # Because "mkdir -p" follows existing symlinks and we likely work 342fc34a306Smrg # directly in world-writeable /tmp, make sure that the '$tmpdir' 343fc34a306Smrg # directory is successfully created first before we actually test 344fc34a306Smrg # 'mkdir -p' feature. 3452f167a42Smrg if (umask $mkdir_umask && 346fc34a306Smrg $mkdirprog $mkdir_mode "$tmpdir" && 347fc34a306Smrg exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1 3482f167a42Smrg then 3492f167a42Smrg if test -z "$dir_arg" || { 3502f167a42Smrg # Check for POSIX incompatibilities with -m. 3512f167a42Smrg # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 3522f167a42Smrg # other-writable bit of parent directory when it shouldn't. 3532f167a42Smrg # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 354fc34a306Smrg test_tmpdir="$tmpdir/a" 355fc34a306Smrg ls_ld_tmpdir=`ls -ld "$test_tmpdir"` 3562f167a42Smrg case $ls_ld_tmpdir in 3572f167a42Smrg d????-?r-*) different_mode=700;; 3582f167a42Smrg d????-?--*) different_mode=755;; 3592f167a42Smrg *) false;; 3602f167a42Smrg esac && 361fc34a306Smrg $mkdirprog -m$different_mode -p -- "$test_tmpdir" && { 362fc34a306Smrg ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"` 3632f167a42Smrg test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 3642f167a42Smrg } 3652f167a42Smrg } 3662f167a42Smrg then posix_mkdir=: 3672f167a42Smrg fi 368fc34a306Smrg rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 3692f167a42Smrg else 3702f167a42Smrg # Remove any dirs left behind by ancient mkdir implementations. 371fc34a306Smrg rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null 3722f167a42Smrg fi 3732f167a42Smrg trap '' 0;; 3742f167a42Smrg esac;; 3753e6697d4Smrg esac 3760e20ee16Smrg 3773e6697d4Smrg if 3783e6697d4Smrg $posix_mkdir && ( 3792f167a42Smrg umask $mkdir_umask && 3802f167a42Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 3813e6697d4Smrg ) 3823e6697d4Smrg then : 3833e6697d4Smrg else 3843e6697d4Smrg 3853e6697d4Smrg # The umask is ridiculous, or mkdir does not conform to POSIX, 3863e6697d4Smrg # or it failed possibly due to a race condition. Create the 3873e6697d4Smrg # directory the slow way, step by step, checking for races as we go. 3883e6697d4Smrg 3893e6697d4Smrg case $dstdir in 3902f167a42Smrg /*) prefix='/';; 3912f167a42Smrg [-=\(\)!]*) prefix='./';; 3922f167a42Smrg *) prefix='';; 3933e6697d4Smrg esac 3943e6697d4Smrg 3953e6697d4Smrg oIFS=$IFS 3963e6697d4Smrg IFS=/ 3972f167a42Smrg set -f 3983e6697d4Smrg set fnord $dstdir 3993e6697d4Smrg shift 4002f167a42Smrg set +f 4013e6697d4Smrg IFS=$oIFS 4023e6697d4Smrg 4033e6697d4Smrg prefixes= 4043e6697d4Smrg 4053e6697d4Smrg for d 4063e6697d4Smrg do 4072f167a42Smrg test X"$d" = X && continue 4082f167a42Smrg 4092f167a42Smrg prefix=$prefix$d 4102f167a42Smrg if test -d "$prefix"; then 4112f167a42Smrg prefixes= 4122f167a42Smrg else 4132f167a42Smrg if $posix_mkdir; then 4142f167a42Smrg (umask=$mkdir_umask && 4152f167a42Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 4162f167a42Smrg # Don't fail if two instances are running concurrently. 4172f167a42Smrg test -d "$prefix" || exit 1 4182f167a42Smrg else 4192f167a42Smrg case $prefix in 4202f167a42Smrg *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 4212f167a42Smrg *) qprefix=$prefix;; 4222f167a42Smrg esac 4232f167a42Smrg prefixes="$prefixes '$qprefix'" 4242f167a42Smrg fi 4252f167a42Smrg fi 4262f167a42Smrg prefix=$prefix/ 4273e6697d4Smrg done 4283e6697d4Smrg 4293e6697d4Smrg if test -n "$prefixes"; then 4302f167a42Smrg # Don't fail if two instances are running concurrently. 4312f167a42Smrg (umask $mkdir_umask && 4322f167a42Smrg eval "\$doit_exec \$mkdirprog $prefixes") || 4332f167a42Smrg test -d "$dstdir" || exit 1 4342f167a42Smrg obsolete_mkdir_used=true 4353e6697d4Smrg fi 4363e6697d4Smrg fi 4373e6697d4Smrg fi 4383e6697d4Smrg 4393e6697d4Smrg if test -n "$dir_arg"; then 4403e6697d4Smrg { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 4413e6697d4Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 4423e6697d4Smrg { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 4433e6697d4Smrg test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 4443e6697d4Smrg else 4453e6697d4Smrg 4463e6697d4Smrg # Make a couple of temp file names in the proper directory. 447fc34a306Smrg dsttmp=${dstdirslash}_inst.$$_ 448fc34a306Smrg rmtmp=${dstdirslash}_rm.$$_ 4493e6697d4Smrg 4503e6697d4Smrg # Trap to clean up those temp files at exit. 4513e6697d4Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 4523e6697d4Smrg 4533e6697d4Smrg # Copy the file name to the temp name. 4543e6697d4Smrg (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") && 4553e6697d4Smrg 4563e6697d4Smrg # and set any options; do chmod last to preserve setuid bits. 4573e6697d4Smrg # 4583e6697d4Smrg # If any of these fail, we abort the whole thing. If we want to 4593e6697d4Smrg # ignore errors from any of these, just make sure not to ignore 4603e6697d4Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 4613e6697d4Smrg # 4623e6697d4Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && 4633e6697d4Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && 4643e6697d4Smrg { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && 4653e6697d4Smrg { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 4663e6697d4Smrg 4673e6697d4Smrg # If -C, don't bother to copy if it wouldn't change the file. 4683e6697d4Smrg if $copy_on_change && 4692f167a42Smrg old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && 4702f167a42Smrg new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && 4712f167a42Smrg set -f && 4723e6697d4Smrg set X $old && old=:$2:$4:$5:$6 && 4733e6697d4Smrg set X $new && new=:$2:$4:$5:$6 && 4742f167a42Smrg set +f && 4753e6697d4Smrg test "$old" = "$new" && 4763e6697d4Smrg $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 4773e6697d4Smrg then 4783e6697d4Smrg rm -f "$dsttmp" 4793e6697d4Smrg else 4803e6697d4Smrg # Rename the file to the real destination. 4813e6697d4Smrg $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || 4823e6697d4Smrg 4833e6697d4Smrg # The rename failed, perhaps because mv can't rename something else 4843e6697d4Smrg # to itself, or perhaps because mv is so ancient that it does not 4853e6697d4Smrg # support -f. 4863e6697d4Smrg { 4872f167a42Smrg # Now remove or move aside any old file at destination location. 4882f167a42Smrg # We try this two ways since rm can't unlink itself on some 4892f167a42Smrg # systems and the destination file might be busy for other 4902f167a42Smrg # reasons. In this case, the final cleanup might fail but the new 4912f167a42Smrg # file should still install successfully. 4922f167a42Smrg { 4932f167a42Smrg test ! -f "$dst" || 4942f167a42Smrg $doit $rmcmd -f "$dst" 2>/dev/null || 4952f167a42Smrg { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && 4962f167a42Smrg { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; } 4972f167a42Smrg } || 4982f167a42Smrg { echo "$0: cannot unlink or rename $dst" >&2 4992f167a42Smrg (exit 1); exit 1 5002f167a42Smrg } 5012f167a42Smrg } && 5022f167a42Smrg 5032f167a42Smrg # Now rename the file to the real destination. 5042f167a42Smrg $doit $mvcmd "$dsttmp" "$dst" 5053e6697d4Smrg } 5063e6697d4Smrg fi || exit 1 5073e6697d4Smrg 5083e6697d4Smrg trap '' 0 5093e6697d4Smrg fi 5103e6697d4Smrgdone 5110e20ee16Smrg 5123e6697d4Smrg# Local variables: 513fc34a306Smrg# eval: (add-hook 'before-save-hook 'time-stamp) 5143e6697d4Smrg# time-stamp-start: "scriptversion=" 5153e6697d4Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 516fc34a306Smrg# time-stamp-time-zone: "UTC0" 5173e6697d4Smrg# time-stamp-end: "; # UTC" 5183e6697d4Smrg# End: 519