install-sh revision 6c3c2bce
17a0395d0Smrg#!/bin/sh 27a0395d0Smrg# install - install a program, script, or datafile 37a0395d0Smrg 46c3c2bceSmrgscriptversion=2018-03-11.20; # UTC 57a0395d0Smrg 67a0395d0Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 77a0395d0Smrg# later released in X11R6 (xc/config/util/install.sh) with the 87a0395d0Smrg# following copyright and license. 97a0395d0Smrg# 107a0395d0Smrg# Copyright (C) 1994 X Consortium 117a0395d0Smrg# 127a0395d0Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 137a0395d0Smrg# of this software and associated documentation files (the "Software"), to 147a0395d0Smrg# deal in the Software without restriction, including without limitation the 157a0395d0Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 167a0395d0Smrg# sell copies of the Software, and to permit persons to whom the Software is 177a0395d0Smrg# furnished to do so, subject to the following conditions: 187a0395d0Smrg# 197a0395d0Smrg# The above copyright notice and this permission notice shall be included in 207a0395d0Smrg# all copies or substantial portions of the Software. 217a0395d0Smrg# 227a0395d0Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 237a0395d0Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 247a0395d0Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 257a0395d0Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 267a0395d0Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 277a0395d0Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 287a0395d0Smrg# 297a0395d0Smrg# Except as contained in this notice, the name of the X Consortium shall not 307a0395d0Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 317a0395d0Smrg# ings in this Software without prior written authorization from the X Consor- 327a0395d0Smrg# tium. 337a0395d0Smrg# 347a0395d0Smrg# 357a0395d0Smrg# FSF changes to this file are in the public domain. 367a0395d0Smrg# 377a0395d0Smrg# Calling this script install-sh is preferred over install.sh, to prevent 388abc0ccfSmrg# 'make' implicit rules from creating a file called install from it 397a0395d0Smrg# when there is no Makefile. 407a0395d0Smrg# 417a0395d0Smrg# This script is compatible with the BSD install script, but was written 427a0395d0Smrg# from scratch. 437a0395d0Smrg 4440c5344fSmrgtab=' ' 457a0395d0Smrgnl=' 467a0395d0Smrg' 4740c5344fSmrgIFS=" $tab$nl" 487a0395d0Smrg 4940c5344fSmrg# Set DOITPROG to "echo" to test this script. 507a0395d0Smrg 517a0395d0Smrgdoit=${DOITPROG-} 5240c5344fSmrgdoit_exec=${doit:-exec} 537a0395d0Smrg 547a0395d0Smrg# Put in absolute file names if you don't have them in your path; 557a0395d0Smrg# or use environment vars. 567a0395d0Smrg 577a0395d0Smrgchgrpprog=${CHGRPPROG-chgrp} 587a0395d0Smrgchmodprog=${CHMODPROG-chmod} 597a0395d0Smrgchownprog=${CHOWNPROG-chown} 607a0395d0Smrgcmpprog=${CMPPROG-cmp} 617a0395d0Smrgcpprog=${CPPROG-cp} 627a0395d0Smrgmkdirprog=${MKDIRPROG-mkdir} 637a0395d0Smrgmvprog=${MVPROG-mv} 647a0395d0Smrgrmprog=${RMPROG-rm} 657a0395d0Smrgstripprog=${STRIPPROG-strip} 667a0395d0Smrg 677a0395d0Smrgposix_mkdir= 687a0395d0Smrg 697a0395d0Smrg# Desired mode of installed file. 707a0395d0Smrgmode=0755 717a0395d0Smrg 727a0395d0Smrgchgrpcmd= 737a0395d0Smrgchmodcmd=$chmodprog 747a0395d0Smrgchowncmd= 757a0395d0Smrgmvcmd=$mvprog 767a0395d0Smrgrmcmd="$rmprog -f" 777a0395d0Smrgstripcmd= 787a0395d0Smrg 797a0395d0Smrgsrc= 807a0395d0Smrgdst= 817a0395d0Smrgdir_arg= 827a0395d0Smrgdst_arg= 837a0395d0Smrg 847a0395d0Smrgcopy_on_change=false 8540c5344fSmrgis_target_a_directory=possibly 867a0395d0Smrg 877a0395d0Smrgusage="\ 887a0395d0SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE 897a0395d0Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 907a0395d0Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 917a0395d0Smrg or: $0 [OPTION]... -d DIRECTORIES... 927a0395d0Smrg 937a0395d0SmrgIn the 1st form, copy SRCFILE to DSTFILE. 947a0395d0SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 957a0395d0SmrgIn the 4th, create DIRECTORIES. 967a0395d0Smrg 977a0395d0SmrgOptions: 987a0395d0Smrg --help display this help and exit. 997a0395d0Smrg --version display version info and exit. 1007a0395d0Smrg 1017a0395d0Smrg -c (ignored) 1027a0395d0Smrg -C install only if different (preserve the last data modification time) 1037a0395d0Smrg -d create directories instead of installing files. 1047a0395d0Smrg -g GROUP $chgrpprog installed files to GROUP. 1057a0395d0Smrg -m MODE $chmodprog installed files to MODE. 1067a0395d0Smrg -o USER $chownprog installed files to USER. 1077a0395d0Smrg -s $stripprog installed files. 1087a0395d0Smrg -t DIRECTORY install into DIRECTORY. 1097a0395d0Smrg -T report an error if DSTFILE is a directory. 1107a0395d0Smrg 1117a0395d0SmrgEnvironment variables override the default commands: 1127a0395d0Smrg CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG 1137a0395d0Smrg RMPROG STRIPPROG 1147a0395d0Smrg" 1157a0395d0Smrg 1167a0395d0Smrgwhile test $# -ne 0; do 1177a0395d0Smrg case $1 in 1187a0395d0Smrg -c) ;; 1197a0395d0Smrg 1207a0395d0Smrg -C) copy_on_change=true;; 1217a0395d0Smrg 1227a0395d0Smrg -d) dir_arg=true;; 1237a0395d0Smrg 1247a0395d0Smrg -g) chgrpcmd="$chgrpprog $2" 12540c5344fSmrg shift;; 1267a0395d0Smrg 1277a0395d0Smrg --help) echo "$usage"; exit $?;; 1287a0395d0Smrg 1297a0395d0Smrg -m) mode=$2 13040c5344fSmrg case $mode in 13140c5344fSmrg *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*) 13240c5344fSmrg echo "$0: invalid mode: $mode" >&2 13340c5344fSmrg exit 1;; 13440c5344fSmrg esac 13540c5344fSmrg shift;; 1367a0395d0Smrg 1377a0395d0Smrg -o) chowncmd="$chownprog $2" 13840c5344fSmrg shift;; 1397a0395d0Smrg 1407a0395d0Smrg -s) stripcmd=$stripprog;; 1417a0395d0Smrg 14240c5344fSmrg -t) 14340c5344fSmrg is_target_a_directory=always 14440c5344fSmrg dst_arg=$2 14540c5344fSmrg # Protect names problematic for 'test' and other utilities. 14640c5344fSmrg case $dst_arg in 14740c5344fSmrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 14840c5344fSmrg esac 14940c5344fSmrg shift;; 1507a0395d0Smrg 15140c5344fSmrg -T) is_target_a_directory=never;; 1527a0395d0Smrg 1537a0395d0Smrg --version) echo "$0 $scriptversion"; exit $?;; 1547a0395d0Smrg 15540c5344fSmrg --) shift 15640c5344fSmrg break;; 1577a0395d0Smrg 15840c5344fSmrg -*) echo "$0: invalid option: $1" >&2 15940c5344fSmrg exit 1;; 1607a0395d0Smrg 1617a0395d0Smrg *) break;; 1627a0395d0Smrg esac 1637a0395d0Smrg shift 1647a0395d0Smrgdone 1657a0395d0Smrg 16640c5344fSmrg# We allow the use of options -d and -T together, by making -d 16740c5344fSmrg# take the precedence; this is for compatibility with GNU install. 16840c5344fSmrg 16940c5344fSmrgif test -n "$dir_arg"; then 17040c5344fSmrg if test -n "$dst_arg"; then 17140c5344fSmrg echo "$0: target directory not allowed when installing a directory." >&2 17240c5344fSmrg exit 1 17340c5344fSmrg fi 17440c5344fSmrgfi 17540c5344fSmrg 1767a0395d0Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then 1777a0395d0Smrg # When -d is used, all remaining arguments are directories to create. 1787a0395d0Smrg # When -t is used, the destination is already specified. 1797a0395d0Smrg # Otherwise, the last argument is the destination. Remove it from $@. 1807a0395d0Smrg for arg 1817a0395d0Smrg do 1827a0395d0Smrg if test -n "$dst_arg"; then 1837a0395d0Smrg # $@ is not empty: it contains at least $arg. 1847a0395d0Smrg set fnord "$@" "$dst_arg" 1857a0395d0Smrg shift # fnord 1867a0395d0Smrg fi 1877a0395d0Smrg shift # arg 1887a0395d0Smrg dst_arg=$arg 1898abc0ccfSmrg # Protect names problematic for 'test' and other utilities. 1908abc0ccfSmrg case $dst_arg in 1918abc0ccfSmrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 1928abc0ccfSmrg esac 1937a0395d0Smrg done 1947a0395d0Smrgfi 1957a0395d0Smrg 1967a0395d0Smrgif test $# -eq 0; then 1977a0395d0Smrg if test -z "$dir_arg"; then 1987a0395d0Smrg echo "$0: no input file specified." >&2 1997a0395d0Smrg exit 1 2007a0395d0Smrg fi 2018abc0ccfSmrg # It's OK to call 'install-sh -d' without argument. 2027a0395d0Smrg # This can happen when creating conditional directories. 2037a0395d0Smrg exit 0 2047a0395d0Smrgfi 2057a0395d0Smrg 20640c5344fSmrgif test -z "$dir_arg"; then 20740c5344fSmrg if test $# -gt 1 || test "$is_target_a_directory" = always; then 20840c5344fSmrg if test ! -d "$dst_arg"; then 20940c5344fSmrg echo "$0: $dst_arg: Is not a directory." >&2 21040c5344fSmrg exit 1 21140c5344fSmrg fi 21240c5344fSmrg fi 21340c5344fSmrgfi 21440c5344fSmrg 2157a0395d0Smrgif test -z "$dir_arg"; then 2168abc0ccfSmrg do_exit='(exit $ret); exit $ret' 2178abc0ccfSmrg trap "ret=129; $do_exit" 1 2188abc0ccfSmrg trap "ret=130; $do_exit" 2 2198abc0ccfSmrg trap "ret=141; $do_exit" 13 2208abc0ccfSmrg trap "ret=143; $do_exit" 15 2217a0395d0Smrg 2227a0395d0Smrg # Set umask so as not to create temps with too-generous modes. 2237a0395d0Smrg # However, 'strip' requires both read and write access to temps. 2247a0395d0Smrg case $mode in 2257a0395d0Smrg # Optimize common cases. 2267a0395d0Smrg *644) cp_umask=133;; 2277a0395d0Smrg *755) cp_umask=22;; 2287a0395d0Smrg 2297a0395d0Smrg *[0-7]) 2307a0395d0Smrg if test -z "$stripcmd"; then 23140c5344fSmrg u_plus_rw= 2327a0395d0Smrg else 23340c5344fSmrg u_plus_rw='% 200' 2347a0395d0Smrg fi 2357a0395d0Smrg cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 2367a0395d0Smrg *) 2377a0395d0Smrg if test -z "$stripcmd"; then 23840c5344fSmrg u_plus_rw= 2397a0395d0Smrg else 24040c5344fSmrg u_plus_rw=,u+rw 2417a0395d0Smrg fi 2427a0395d0Smrg cp_umask=$mode$u_plus_rw;; 2437a0395d0Smrg esac 2447a0395d0Smrgfi 2457a0395d0Smrg 2467a0395d0Smrgfor src 2477a0395d0Smrgdo 2488abc0ccfSmrg # Protect names problematic for 'test' and other utilities. 2497a0395d0Smrg case $src in 2508abc0ccfSmrg -* | [=\(\)!]) src=./$src;; 2517a0395d0Smrg esac 2527a0395d0Smrg 2537a0395d0Smrg if test -n "$dir_arg"; then 2547a0395d0Smrg dst=$src 2557a0395d0Smrg dstdir=$dst 2567a0395d0Smrg test -d "$dstdir" 2577a0395d0Smrg dstdir_status=$? 2587a0395d0Smrg else 2597a0395d0Smrg 2607a0395d0Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 2617a0395d0Smrg # might cause directories to be created, which would be especially bad 2627a0395d0Smrg # if $src (and thus $dsttmp) contains '*'. 2637a0395d0Smrg if test ! -f "$src" && test ! -d "$src"; then 2647a0395d0Smrg echo "$0: $src does not exist." >&2 2657a0395d0Smrg exit 1 2667a0395d0Smrg fi 2677a0395d0Smrg 2687a0395d0Smrg if test -z "$dst_arg"; then 2697a0395d0Smrg echo "$0: no destination specified." >&2 2707a0395d0Smrg exit 1 2717a0395d0Smrg fi 2727a0395d0Smrg dst=$dst_arg 2737a0395d0Smrg 2746c3c2bceSmrg # If destination is a directory, append the input filename. 2757a0395d0Smrg if test -d "$dst"; then 27640c5344fSmrg if test "$is_target_a_directory" = never; then 27740c5344fSmrg echo "$0: $dst_arg: Is a directory" >&2 27840c5344fSmrg exit 1 2797a0395d0Smrg fi 2807a0395d0Smrg dstdir=$dst 2816c3c2bceSmrg dstbase=`basename "$src"` 2826c3c2bceSmrg case $dst in 2836c3c2bceSmrg */) dst=$dst$dstbase;; 2846c3c2bceSmrg *) dst=$dst/$dstbase;; 2856c3c2bceSmrg esac 2867a0395d0Smrg dstdir_status=0 2877a0395d0Smrg else 28840c5344fSmrg dstdir=`dirname "$dst"` 2897a0395d0Smrg test -d "$dstdir" 2907a0395d0Smrg dstdir_status=$? 2917a0395d0Smrg fi 2927a0395d0Smrg fi 2937a0395d0Smrg 2946c3c2bceSmrg case $dstdir in 2956c3c2bceSmrg */) dstdirslash=$dstdir;; 2966c3c2bceSmrg *) dstdirslash=$dstdir/;; 2976c3c2bceSmrg esac 2986c3c2bceSmrg 2997a0395d0Smrg obsolete_mkdir_used=false 3007a0395d0Smrg 3017a0395d0Smrg if test $dstdir_status != 0; then 3027a0395d0Smrg case $posix_mkdir in 3037a0395d0Smrg '') 30440c5344fSmrg # Create intermediate dirs using mode 755 as modified by the umask. 30540c5344fSmrg # This is like FreeBSD 'install' as of 1997-10-28. 30640c5344fSmrg umask=`umask` 30740c5344fSmrg case $stripcmd.$umask in 30840c5344fSmrg # Optimize common cases. 30940c5344fSmrg *[2367][2367]) mkdir_umask=$umask;; 31040c5344fSmrg .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;; 31140c5344fSmrg 31240c5344fSmrg *[0-7]) 31340c5344fSmrg mkdir_umask=`expr $umask + 22 \ 31440c5344fSmrg - $umask % 100 % 40 + $umask % 20 \ 31540c5344fSmrg - $umask % 10 % 4 + $umask % 2 31640c5344fSmrg `;; 31740c5344fSmrg *) mkdir_umask=$umask,go-w;; 31840c5344fSmrg esac 31940c5344fSmrg 32040c5344fSmrg # With -d, create the new directory with the user-specified mode. 32140c5344fSmrg # Otherwise, rely on $mkdir_umask. 32240c5344fSmrg if test -n "$dir_arg"; then 32340c5344fSmrg mkdir_mode=-m$mode 32440c5344fSmrg else 32540c5344fSmrg mkdir_mode= 32640c5344fSmrg fi 32740c5344fSmrg 32840c5344fSmrg posix_mkdir=false 32940c5344fSmrg case $umask in 33040c5344fSmrg *[123567][0-7][0-7]) 33140c5344fSmrg # POSIX mkdir -p sets u+wx bits regardless of umask, which 33240c5344fSmrg # is incompatible with FreeBSD 'install' when (umask & 300) != 0. 33340c5344fSmrg ;; 33440c5344fSmrg *) 3356c3c2bceSmrg # Note that $RANDOM variable is not portable (e.g. dash); Use it 3366c3c2bceSmrg # here however when possible just to lower collision chance. 33740c5344fSmrg tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 33840c5344fSmrg 3396c3c2bceSmrg trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0 3406c3c2bceSmrg 3416c3c2bceSmrg # Because "mkdir -p" follows existing symlinks and we likely work 3426c3c2bceSmrg # directly in world-writeable /tmp, make sure that the '$tmpdir' 3436c3c2bceSmrg # directory is successfully created first before we actually test 3446c3c2bceSmrg # 'mkdir -p' feature. 34540c5344fSmrg if (umask $mkdir_umask && 3466c3c2bceSmrg $mkdirprog $mkdir_mode "$tmpdir" && 3476c3c2bceSmrg exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1 34840c5344fSmrg then 34940c5344fSmrg if test -z "$dir_arg" || { 35040c5344fSmrg # Check for POSIX incompatibilities with -m. 35140c5344fSmrg # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 35240c5344fSmrg # other-writable bit of parent directory when it shouldn't. 35340c5344fSmrg # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 3546c3c2bceSmrg test_tmpdir="$tmpdir/a" 3556c3c2bceSmrg ls_ld_tmpdir=`ls -ld "$test_tmpdir"` 35640c5344fSmrg case $ls_ld_tmpdir in 35740c5344fSmrg d????-?r-*) different_mode=700;; 35840c5344fSmrg d????-?--*) different_mode=755;; 35940c5344fSmrg *) false;; 36040c5344fSmrg esac && 3616c3c2bceSmrg $mkdirprog -m$different_mode -p -- "$test_tmpdir" && { 3626c3c2bceSmrg ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"` 36340c5344fSmrg test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 36440c5344fSmrg } 36540c5344fSmrg } 36640c5344fSmrg then posix_mkdir=: 36740c5344fSmrg fi 3686c3c2bceSmrg rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 36940c5344fSmrg else 37040c5344fSmrg # Remove any dirs left behind by ancient mkdir implementations. 3716c3c2bceSmrg rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null 37240c5344fSmrg fi 37340c5344fSmrg trap '' 0;; 37440c5344fSmrg esac;; 3757a0395d0Smrg esac 3767a0395d0Smrg 3777a0395d0Smrg if 3787a0395d0Smrg $posix_mkdir && ( 37940c5344fSmrg umask $mkdir_umask && 38040c5344fSmrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 3817a0395d0Smrg ) 3827a0395d0Smrg then : 3837a0395d0Smrg else 3847a0395d0Smrg 3857a0395d0Smrg # The umask is ridiculous, or mkdir does not conform to POSIX, 3867a0395d0Smrg # or it failed possibly due to a race condition. Create the 3877a0395d0Smrg # directory the slow way, step by step, checking for races as we go. 3887a0395d0Smrg 3897a0395d0Smrg case $dstdir in 39040c5344fSmrg /*) prefix='/';; 39140c5344fSmrg [-=\(\)!]*) prefix='./';; 39240c5344fSmrg *) prefix='';; 3937a0395d0Smrg esac 3947a0395d0Smrg 3957a0395d0Smrg oIFS=$IFS 3967a0395d0Smrg IFS=/ 39740c5344fSmrg set -f 3987a0395d0Smrg set fnord $dstdir 3997a0395d0Smrg shift 40040c5344fSmrg set +f 4017a0395d0Smrg IFS=$oIFS 4027a0395d0Smrg 4037a0395d0Smrg prefixes= 4047a0395d0Smrg 4057a0395d0Smrg for d 4067a0395d0Smrg do 40740c5344fSmrg test X"$d" = X && continue 40840c5344fSmrg 40940c5344fSmrg prefix=$prefix$d 41040c5344fSmrg if test -d "$prefix"; then 41140c5344fSmrg prefixes= 41240c5344fSmrg else 41340c5344fSmrg if $posix_mkdir; then 41440c5344fSmrg (umask=$mkdir_umask && 41540c5344fSmrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 41640c5344fSmrg # Don't fail if two instances are running concurrently. 41740c5344fSmrg test -d "$prefix" || exit 1 41840c5344fSmrg else 41940c5344fSmrg case $prefix in 42040c5344fSmrg *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 42140c5344fSmrg *) qprefix=$prefix;; 42240c5344fSmrg esac 42340c5344fSmrg prefixes="$prefixes '$qprefix'" 42440c5344fSmrg fi 42540c5344fSmrg fi 42640c5344fSmrg prefix=$prefix/ 4277a0395d0Smrg done 4287a0395d0Smrg 4297a0395d0Smrg if test -n "$prefixes"; then 43040c5344fSmrg # Don't fail if two instances are running concurrently. 43140c5344fSmrg (umask $mkdir_umask && 43240c5344fSmrg eval "\$doit_exec \$mkdirprog $prefixes") || 43340c5344fSmrg test -d "$dstdir" || exit 1 43440c5344fSmrg obsolete_mkdir_used=true 4357a0395d0Smrg fi 4367a0395d0Smrg fi 4377a0395d0Smrg fi 4387a0395d0Smrg 4397a0395d0Smrg if test -n "$dir_arg"; then 4407a0395d0Smrg { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 4417a0395d0Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 4427a0395d0Smrg { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 4437a0395d0Smrg test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 4447a0395d0Smrg else 4457a0395d0Smrg 4467a0395d0Smrg # Make a couple of temp file names in the proper directory. 4476c3c2bceSmrg dsttmp=${dstdirslash}_inst.$$_ 4486c3c2bceSmrg rmtmp=${dstdirslash}_rm.$$_ 4497a0395d0Smrg 4507a0395d0Smrg # Trap to clean up those temp files at exit. 4517a0395d0Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 4527a0395d0Smrg 4537a0395d0Smrg # Copy the file name to the temp name. 4547a0395d0Smrg (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") && 4557a0395d0Smrg 4567a0395d0Smrg # and set any options; do chmod last to preserve setuid bits. 4577a0395d0Smrg # 4587a0395d0Smrg # If any of these fail, we abort the whole thing. If we want to 4597a0395d0Smrg # ignore errors from any of these, just make sure not to ignore 4607a0395d0Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 4617a0395d0Smrg # 4627a0395d0Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && 4637a0395d0Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && 4647a0395d0Smrg { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && 4657a0395d0Smrg { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 4667a0395d0Smrg 4677a0395d0Smrg # If -C, don't bother to copy if it wouldn't change the file. 4687a0395d0Smrg if $copy_on_change && 46940c5344fSmrg old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && 47040c5344fSmrg new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && 47140c5344fSmrg set -f && 4727a0395d0Smrg set X $old && old=:$2:$4:$5:$6 && 4737a0395d0Smrg set X $new && new=:$2:$4:$5:$6 && 47440c5344fSmrg set +f && 4757a0395d0Smrg test "$old" = "$new" && 4767a0395d0Smrg $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 4777a0395d0Smrg then 4787a0395d0Smrg rm -f "$dsttmp" 4797a0395d0Smrg else 4807a0395d0Smrg # Rename the file to the real destination. 4817a0395d0Smrg $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || 4827a0395d0Smrg 4837a0395d0Smrg # The rename failed, perhaps because mv can't rename something else 4847a0395d0Smrg # to itself, or perhaps because mv is so ancient that it does not 4857a0395d0Smrg # support -f. 4867a0395d0Smrg { 48740c5344fSmrg # Now remove or move aside any old file at destination location. 48840c5344fSmrg # We try this two ways since rm can't unlink itself on some 48940c5344fSmrg # systems and the destination file might be busy for other 49040c5344fSmrg # reasons. In this case, the final cleanup might fail but the new 49140c5344fSmrg # file should still install successfully. 49240c5344fSmrg { 49340c5344fSmrg test ! -f "$dst" || 49440c5344fSmrg $doit $rmcmd -f "$dst" 2>/dev/null || 49540c5344fSmrg { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && 49640c5344fSmrg { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; } 49740c5344fSmrg } || 49840c5344fSmrg { echo "$0: cannot unlink or rename $dst" >&2 49940c5344fSmrg (exit 1); exit 1 50040c5344fSmrg } 50140c5344fSmrg } && 50240c5344fSmrg 50340c5344fSmrg # Now rename the file to the real destination. 50440c5344fSmrg $doit $mvcmd "$dsttmp" "$dst" 5057a0395d0Smrg } 5067a0395d0Smrg fi || exit 1 5077a0395d0Smrg 5087a0395d0Smrg trap '' 0 5097a0395d0Smrg fi 5107a0395d0Smrgdone 5117a0395d0Smrg 5127a0395d0Smrg# Local variables: 5136c3c2bceSmrg# eval: (add-hook 'before-save-hook 'time-stamp) 5147a0395d0Smrg# time-stamp-start: "scriptversion=" 5157a0395d0Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 5166c3c2bceSmrg# time-stamp-time-zone: "UTC0" 5177366012aSmrg# time-stamp-end: "; # UTC" 5187a0395d0Smrg# End: 519