install-sh revision 4940c694
1c43cc173Smrg#!/bin/sh 2c43cc173Smrg# install - install a program, script, or datafile 3c43cc173Smrg 44940c694Smrgscriptversion=2018-03-11.20; # UTC 5c43cc173Smrg 6c43cc173Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 7c43cc173Smrg# later released in X11R6 (xc/config/util/install.sh) with the 8c43cc173Smrg# following copyright and license. 9c43cc173Smrg# 10c43cc173Smrg# Copyright (C) 1994 X Consortium 11c43cc173Smrg# 12c43cc173Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 13c43cc173Smrg# of this software and associated documentation files (the "Software"), to 14c43cc173Smrg# deal in the Software without restriction, including without limitation the 15c43cc173Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 16c43cc173Smrg# sell copies of the Software, and to permit persons to whom the Software is 17c43cc173Smrg# furnished to do so, subject to the following conditions: 18c43cc173Smrg# 19c43cc173Smrg# The above copyright notice and this permission notice shall be included in 20c43cc173Smrg# all copies or substantial portions of the Software. 21c43cc173Smrg# 22c43cc173Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23c43cc173Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24c43cc173Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25c43cc173Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26c43cc173Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 27c43cc173Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28c43cc173Smrg# 29c43cc173Smrg# Except as contained in this notice, the name of the X Consortium shall not 30c43cc173Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 31c43cc173Smrg# ings in this Software without prior written authorization from the X Consor- 32c43cc173Smrg# tium. 33c43cc173Smrg# 34c43cc173Smrg# 35c43cc173Smrg# FSF changes to this file are in the public domain. 36c43cc173Smrg# 37c43cc173Smrg# Calling this script install-sh is preferred over install.sh, to prevent 38f1ee322dSmrg# 'make' implicit rules from creating a file called install from it 39c43cc173Smrg# when there is no Makefile. 40c43cc173Smrg# 41c43cc173Smrg# This script is compatible with the BSD install script, but was written 42c43cc173Smrg# from scratch. 43c43cc173Smrg 4448e5ce07Smrgtab=' ' 45c43cc173Smrgnl=' 46c43cc173Smrg' 4748e5ce07SmrgIFS=" $tab$nl" 48c43cc173Smrg 4948e5ce07Smrg# Set DOITPROG to "echo" to test this script. 50c43cc173Smrg 51c27c18e8Smrgdoit=${DOITPROG-} 5248e5ce07Smrgdoit_exec=${doit:-exec} 53c43cc173Smrg 54c43cc173Smrg# Put in absolute file names if you don't have them in your path; 55c43cc173Smrg# or use environment vars. 56c43cc173Smrg 57c27c18e8Smrgchgrpprog=${CHGRPPROG-chgrp} 58c27c18e8Smrgchmodprog=${CHMODPROG-chmod} 59c27c18e8Smrgchownprog=${CHOWNPROG-chown} 60c27c18e8Smrgcmpprog=${CMPPROG-cmp} 61c27c18e8Smrgcpprog=${CPPROG-cp} 62c27c18e8Smrgmkdirprog=${MKDIRPROG-mkdir} 63c27c18e8Smrgmvprog=${MVPROG-mv} 64c27c18e8Smrgrmprog=${RMPROG-rm} 65c27c18e8Smrgstripprog=${STRIPPROG-strip} 66c27c18e8Smrg 67c43cc173Smrgposix_mkdir= 68c43cc173Smrg 69c43cc173Smrg# Desired mode of installed file. 70c43cc173Smrgmode=0755 71c43cc173Smrg 72c27c18e8Smrgchgrpcmd= 73c43cc173Smrgchmodcmd=$chmodprog 74c43cc173Smrgchowncmd= 75c27c18e8Smrgmvcmd=$mvprog 76c43cc173Smrgrmcmd="$rmprog -f" 77c27c18e8Smrgstripcmd= 78c27c18e8Smrg 79c43cc173Smrgsrc= 80c43cc173Smrgdst= 81c43cc173Smrgdir_arg= 82c27c18e8Smrgdst_arg= 83c27c18e8Smrg 84c27c18e8Smrgcopy_on_change=false 8548e5ce07Smrgis_target_a_directory=possibly 86c43cc173Smrg 87c27c18e8Smrgusage="\ 88c27c18e8SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE 89c43cc173Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 90c43cc173Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 91c43cc173Smrg or: $0 [OPTION]... -d DIRECTORIES... 92c43cc173Smrg 93c43cc173SmrgIn the 1st form, copy SRCFILE to DSTFILE. 94c43cc173SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 95c43cc173SmrgIn the 4th, create DIRECTORIES. 96c43cc173Smrg 97c43cc173SmrgOptions: 98c27c18e8Smrg --help display this help and exit. 99c27c18e8Smrg --version display version info and exit. 100c27c18e8Smrg 101c27c18e8Smrg -c (ignored) 102c27c18e8Smrg -C install only if different (preserve the last data modification time) 103c27c18e8Smrg -d create directories instead of installing files. 104c27c18e8Smrg -g GROUP $chgrpprog installed files to GROUP. 105c27c18e8Smrg -m MODE $chmodprog installed files to MODE. 106c27c18e8Smrg -o USER $chownprog installed files to USER. 107c27c18e8Smrg -s $stripprog installed files. 108c27c18e8Smrg -t DIRECTORY install into DIRECTORY. 109c27c18e8Smrg -T report an error if DSTFILE is a directory. 110c43cc173Smrg 111c43cc173SmrgEnvironment variables override the default commands: 112c27c18e8Smrg CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG 113c27c18e8Smrg RMPROG STRIPPROG 114c43cc173Smrg" 115c43cc173Smrg 116c43cc173Smrgwhile test $# -ne 0; do 117c43cc173Smrg case $1 in 118c27c18e8Smrg -c) ;; 119c27c18e8Smrg 120c27c18e8Smrg -C) copy_on_change=true;; 121c43cc173Smrg 122c27c18e8Smrg -d) dir_arg=true;; 123c43cc173Smrg 124c43cc173Smrg -g) chgrpcmd="$chgrpprog $2" 12548e5ce07Smrg shift;; 126c43cc173Smrg 127c43cc173Smrg --help) echo "$usage"; exit $?;; 128c43cc173Smrg 129c43cc173Smrg -m) mode=$2 13048e5ce07Smrg case $mode in 13148e5ce07Smrg *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*) 13248e5ce07Smrg echo "$0: invalid mode: $mode" >&2 13348e5ce07Smrg exit 1;; 13448e5ce07Smrg esac 13548e5ce07Smrg shift;; 136c43cc173Smrg 137c43cc173Smrg -o) chowncmd="$chownprog $2" 13848e5ce07Smrg shift;; 139c43cc173Smrg 140c27c18e8Smrg -s) stripcmd=$stripprog;; 141c43cc173Smrg 14248e5ce07Smrg -t) 14348e5ce07Smrg is_target_a_directory=always 14448e5ce07Smrg dst_arg=$2 14548e5ce07Smrg # Protect names problematic for 'test' and other utilities. 14648e5ce07Smrg case $dst_arg in 14748e5ce07Smrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 14848e5ce07Smrg esac 14948e5ce07Smrg shift;; 150c43cc173Smrg 15148e5ce07Smrg -T) is_target_a_directory=never;; 152c43cc173Smrg 153c43cc173Smrg --version) echo "$0 $scriptversion"; exit $?;; 154c43cc173Smrg 15548e5ce07Smrg --) shift 15648e5ce07Smrg break;; 157c43cc173Smrg 15848e5ce07Smrg -*) echo "$0: invalid option: $1" >&2 15948e5ce07Smrg exit 1;; 160c43cc173Smrg 161c43cc173Smrg *) break;; 162c43cc173Smrg esac 163c27c18e8Smrg shift 164c43cc173Smrgdone 165c43cc173Smrg 16648e5ce07Smrg# We allow the use of options -d and -T together, by making -d 16748e5ce07Smrg# take the precedence; this is for compatibility with GNU install. 16848e5ce07Smrg 16948e5ce07Smrgif test -n "$dir_arg"; then 17048e5ce07Smrg if test -n "$dst_arg"; then 17148e5ce07Smrg echo "$0: target directory not allowed when installing a directory." >&2 17248e5ce07Smrg exit 1 17348e5ce07Smrg fi 17448e5ce07Smrgfi 17548e5ce07Smrg 176c27c18e8Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then 177c43cc173Smrg # When -d is used, all remaining arguments are directories to create. 178c43cc173Smrg # When -t is used, the destination is already specified. 179c43cc173Smrg # Otherwise, the last argument is the destination. Remove it from $@. 180c43cc173Smrg for arg 181c43cc173Smrg do 182c27c18e8Smrg if test -n "$dst_arg"; then 183c43cc173Smrg # $@ is not empty: it contains at least $arg. 184c27c18e8Smrg set fnord "$@" "$dst_arg" 185c43cc173Smrg shift # fnord 186c43cc173Smrg fi 187c43cc173Smrg shift # arg 188c27c18e8Smrg dst_arg=$arg 189f1ee322dSmrg # Protect names problematic for 'test' and other utilities. 190f1ee322dSmrg case $dst_arg in 191f1ee322dSmrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 192f1ee322dSmrg esac 193c43cc173Smrg done 194c43cc173Smrgfi 195c43cc173Smrg 196c43cc173Smrgif test $# -eq 0; then 197c43cc173Smrg if test -z "$dir_arg"; then 198c43cc173Smrg echo "$0: no input file specified." >&2 199c43cc173Smrg exit 1 200c43cc173Smrg fi 201f1ee322dSmrg # It's OK to call 'install-sh -d' without argument. 202c43cc173Smrg # This can happen when creating conditional directories. 203c43cc173Smrg exit 0 204c43cc173Smrgfi 205c43cc173Smrg 20648e5ce07Smrgif test -z "$dir_arg"; then 20748e5ce07Smrg if test $# -gt 1 || test "$is_target_a_directory" = always; then 20848e5ce07Smrg if test ! -d "$dst_arg"; then 20948e5ce07Smrg echo "$0: $dst_arg: Is not a directory." >&2 21048e5ce07Smrg exit 1 21148e5ce07Smrg fi 21248e5ce07Smrg fi 21348e5ce07Smrgfi 21448e5ce07Smrg 215c43cc173Smrgif test -z "$dir_arg"; then 216f1ee322dSmrg do_exit='(exit $ret); exit $ret' 217f1ee322dSmrg trap "ret=129; $do_exit" 1 218f1ee322dSmrg trap "ret=130; $do_exit" 2 219f1ee322dSmrg trap "ret=141; $do_exit" 13 220f1ee322dSmrg trap "ret=143; $do_exit" 15 221c43cc173Smrg 222c43cc173Smrg # Set umask so as not to create temps with too-generous modes. 223c43cc173Smrg # However, 'strip' requires both read and write access to temps. 224c43cc173Smrg case $mode in 225c43cc173Smrg # Optimize common cases. 226c43cc173Smrg *644) cp_umask=133;; 227c43cc173Smrg *755) cp_umask=22;; 228c43cc173Smrg 229c43cc173Smrg *[0-7]) 230c43cc173Smrg if test -z "$stripcmd"; then 23148e5ce07Smrg u_plus_rw= 232c43cc173Smrg else 23348e5ce07Smrg u_plus_rw='% 200' 234c43cc173Smrg fi 235c43cc173Smrg cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 236c43cc173Smrg *) 237c43cc173Smrg if test -z "$stripcmd"; then 23848e5ce07Smrg u_plus_rw= 239c43cc173Smrg else 24048e5ce07Smrg u_plus_rw=,u+rw 241c43cc173Smrg fi 242c43cc173Smrg cp_umask=$mode$u_plus_rw;; 243c43cc173Smrg esac 244c43cc173Smrgfi 245c43cc173Smrg 246c43cc173Smrgfor src 247c43cc173Smrgdo 248f1ee322dSmrg # Protect names problematic for 'test' and other utilities. 249c43cc173Smrg case $src in 250f1ee322dSmrg -* | [=\(\)!]) src=./$src;; 251c43cc173Smrg esac 252c43cc173Smrg 253c43cc173Smrg if test -n "$dir_arg"; then 254c43cc173Smrg dst=$src 255c43cc173Smrg dstdir=$dst 256c43cc173Smrg test -d "$dstdir" 257c43cc173Smrg dstdir_status=$? 258c43cc173Smrg else 259c43cc173Smrg 260c43cc173Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 261c43cc173Smrg # might cause directories to be created, which would be especially bad 262c43cc173Smrg # if $src (and thus $dsttmp) contains '*'. 263c43cc173Smrg if test ! -f "$src" && test ! -d "$src"; then 264c43cc173Smrg echo "$0: $src does not exist." >&2 265c43cc173Smrg exit 1 266c43cc173Smrg fi 267c43cc173Smrg 268c27c18e8Smrg if test -z "$dst_arg"; then 269c43cc173Smrg echo "$0: no destination specified." >&2 270c43cc173Smrg exit 1 271c43cc173Smrg fi 272c27c18e8Smrg dst=$dst_arg 273c43cc173Smrg 2744940c694Smrg # If destination is a directory, append the input filename. 275c43cc173Smrg if test -d "$dst"; then 27648e5ce07Smrg if test "$is_target_a_directory" = never; then 27748e5ce07Smrg echo "$0: $dst_arg: Is a directory" >&2 27848e5ce07Smrg exit 1 279c43cc173Smrg fi 280c43cc173Smrg dstdir=$dst 2814940c694Smrg dstbase=`basename "$src"` 2824940c694Smrg case $dst in 2834940c694Smrg */) dst=$dst$dstbase;; 2844940c694Smrg *) dst=$dst/$dstbase;; 2854940c694Smrg esac 286c43cc173Smrg dstdir_status=0 287c43cc173Smrg else 28848e5ce07Smrg dstdir=`dirname "$dst"` 289c43cc173Smrg test -d "$dstdir" 290c43cc173Smrg dstdir_status=$? 291c43cc173Smrg fi 292c43cc173Smrg fi 293c43cc173Smrg 2944940c694Smrg case $dstdir in 2954940c694Smrg */) dstdirslash=$dstdir;; 2964940c694Smrg *) dstdirslash=$dstdir/;; 2974940c694Smrg esac 2984940c694Smrg 299c43cc173Smrg obsolete_mkdir_used=false 300c43cc173Smrg 301c43cc173Smrg if test $dstdir_status != 0; then 302c43cc173Smrg case $posix_mkdir in 303c43cc173Smrg '') 30448e5ce07Smrg # Create intermediate dirs using mode 755 as modified by the umask. 30548e5ce07Smrg # This is like FreeBSD 'install' as of 1997-10-28. 30648e5ce07Smrg umask=`umask` 30748e5ce07Smrg case $stripcmd.$umask in 30848e5ce07Smrg # Optimize common cases. 30948e5ce07Smrg *[2367][2367]) mkdir_umask=$umask;; 31048e5ce07Smrg .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;; 31148e5ce07Smrg 31248e5ce07Smrg *[0-7]) 31348e5ce07Smrg mkdir_umask=`expr $umask + 22 \ 31448e5ce07Smrg - $umask % 100 % 40 + $umask % 20 \ 31548e5ce07Smrg - $umask % 10 % 4 + $umask % 2 31648e5ce07Smrg `;; 31748e5ce07Smrg *) mkdir_umask=$umask,go-w;; 31848e5ce07Smrg esac 31948e5ce07Smrg 32048e5ce07Smrg # With -d, create the new directory with the user-specified mode. 32148e5ce07Smrg # Otherwise, rely on $mkdir_umask. 32248e5ce07Smrg if test -n "$dir_arg"; then 32348e5ce07Smrg mkdir_mode=-m$mode 32448e5ce07Smrg else 32548e5ce07Smrg mkdir_mode= 32648e5ce07Smrg fi 32748e5ce07Smrg 32848e5ce07Smrg posix_mkdir=false 32948e5ce07Smrg case $umask in 33048e5ce07Smrg *[123567][0-7][0-7]) 33148e5ce07Smrg # POSIX mkdir -p sets u+wx bits regardless of umask, which 33248e5ce07Smrg # is incompatible with FreeBSD 'install' when (umask & 300) != 0. 33348e5ce07Smrg ;; 33448e5ce07Smrg *) 3354940c694Smrg # Note that $RANDOM variable is not portable (e.g. dash); Use it 3364940c694Smrg # here however when possible just to lower collision chance. 33748e5ce07Smrg tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 33848e5ce07Smrg 3394940c694Smrg trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0 3404940c694Smrg 3414940c694Smrg # Because "mkdir -p" follows existing symlinks and we likely work 3424940c694Smrg # directly in world-writeable /tmp, make sure that the '$tmpdir' 3434940c694Smrg # directory is successfully created first before we actually test 3444940c694Smrg # 'mkdir -p' feature. 34548e5ce07Smrg if (umask $mkdir_umask && 3464940c694Smrg $mkdirprog $mkdir_mode "$tmpdir" && 3474940c694Smrg exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1 34848e5ce07Smrg then 34948e5ce07Smrg if test -z "$dir_arg" || { 35048e5ce07Smrg # Check for POSIX incompatibilities with -m. 35148e5ce07Smrg # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 35248e5ce07Smrg # other-writable bit of parent directory when it shouldn't. 35348e5ce07Smrg # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 3544940c694Smrg test_tmpdir="$tmpdir/a" 3554940c694Smrg ls_ld_tmpdir=`ls -ld "$test_tmpdir"` 35648e5ce07Smrg case $ls_ld_tmpdir in 35748e5ce07Smrg d????-?r-*) different_mode=700;; 35848e5ce07Smrg d????-?--*) different_mode=755;; 35948e5ce07Smrg *) false;; 36048e5ce07Smrg esac && 3614940c694Smrg $mkdirprog -m$different_mode -p -- "$test_tmpdir" && { 3624940c694Smrg ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"` 36348e5ce07Smrg test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 36448e5ce07Smrg } 36548e5ce07Smrg } 36648e5ce07Smrg then posix_mkdir=: 36748e5ce07Smrg fi 3684940c694Smrg rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 36948e5ce07Smrg else 37048e5ce07Smrg # Remove any dirs left behind by ancient mkdir implementations. 3714940c694Smrg rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null 37248e5ce07Smrg fi 37348e5ce07Smrg trap '' 0;; 37448e5ce07Smrg esac;; 375c43cc173Smrg esac 376c43cc173Smrg 377c43cc173Smrg if 378c43cc173Smrg $posix_mkdir && ( 37948e5ce07Smrg umask $mkdir_umask && 38048e5ce07Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 381c43cc173Smrg ) 382c43cc173Smrg then : 383c43cc173Smrg else 384c43cc173Smrg 385c43cc173Smrg # The umask is ridiculous, or mkdir does not conform to POSIX, 386c43cc173Smrg # or it failed possibly due to a race condition. Create the 387c43cc173Smrg # directory the slow way, step by step, checking for races as we go. 388c43cc173Smrg 389c43cc173Smrg case $dstdir in 39048e5ce07Smrg /*) prefix='/';; 39148e5ce07Smrg [-=\(\)!]*) prefix='./';; 39248e5ce07Smrg *) prefix='';; 393c43cc173Smrg esac 394c43cc173Smrg 395c43cc173Smrg oIFS=$IFS 396c43cc173Smrg IFS=/ 39748e5ce07Smrg set -f 398c43cc173Smrg set fnord $dstdir 399c43cc173Smrg shift 40048e5ce07Smrg set +f 401c43cc173Smrg IFS=$oIFS 402c43cc173Smrg 403c43cc173Smrg prefixes= 404c43cc173Smrg 405c43cc173Smrg for d 406c43cc173Smrg do 40748e5ce07Smrg test X"$d" = X && continue 40848e5ce07Smrg 40948e5ce07Smrg prefix=$prefix$d 41048e5ce07Smrg if test -d "$prefix"; then 41148e5ce07Smrg prefixes= 41248e5ce07Smrg else 41348e5ce07Smrg if $posix_mkdir; then 41448e5ce07Smrg (umask=$mkdir_umask && 41548e5ce07Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 41648e5ce07Smrg # Don't fail if two instances are running concurrently. 41748e5ce07Smrg test -d "$prefix" || exit 1 41848e5ce07Smrg else 41948e5ce07Smrg case $prefix in 42048e5ce07Smrg *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 42148e5ce07Smrg *) qprefix=$prefix;; 42248e5ce07Smrg esac 42348e5ce07Smrg prefixes="$prefixes '$qprefix'" 42448e5ce07Smrg fi 42548e5ce07Smrg fi 42648e5ce07Smrg prefix=$prefix/ 427c43cc173Smrg done 428c43cc173Smrg 429c43cc173Smrg if test -n "$prefixes"; then 43048e5ce07Smrg # Don't fail if two instances are running concurrently. 43148e5ce07Smrg (umask $mkdir_umask && 43248e5ce07Smrg eval "\$doit_exec \$mkdirprog $prefixes") || 43348e5ce07Smrg test -d "$dstdir" || exit 1 43448e5ce07Smrg obsolete_mkdir_used=true 435c43cc173Smrg fi 436c43cc173Smrg fi 437c43cc173Smrg fi 438c43cc173Smrg 439c43cc173Smrg if test -n "$dir_arg"; then 440c43cc173Smrg { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 441c43cc173Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 442c43cc173Smrg { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 443c43cc173Smrg test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 444c43cc173Smrg else 445c43cc173Smrg 446c43cc173Smrg # Make a couple of temp file names in the proper directory. 4474940c694Smrg dsttmp=${dstdirslash}_inst.$$_ 4484940c694Smrg rmtmp=${dstdirslash}_rm.$$_ 449c43cc173Smrg 450c43cc173Smrg # Trap to clean up those temp files at exit. 451c43cc173Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 452c43cc173Smrg 453c43cc173Smrg # Copy the file name to the temp name. 454c43cc173Smrg (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") && 455c43cc173Smrg 456c43cc173Smrg # and set any options; do chmod last to preserve setuid bits. 457c43cc173Smrg # 458c43cc173Smrg # If any of these fail, we abort the whole thing. If we want to 459c43cc173Smrg # ignore errors from any of these, just make sure not to ignore 460c43cc173Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 461c43cc173Smrg # 462c27c18e8Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && 463c27c18e8Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && 464c27c18e8Smrg { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && 465c27c18e8Smrg { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 466c27c18e8Smrg 467c27c18e8Smrg # If -C, don't bother to copy if it wouldn't change the file. 468c27c18e8Smrg if $copy_on_change && 46948e5ce07Smrg old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && 47048e5ce07Smrg new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && 47148e5ce07Smrg set -f && 472c27c18e8Smrg set X $old && old=:$2:$4:$5:$6 && 473c27c18e8Smrg set X $new && new=:$2:$4:$5:$6 && 47448e5ce07Smrg set +f && 475c27c18e8Smrg test "$old" = "$new" && 476c27c18e8Smrg $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 477c27c18e8Smrg then 478c27c18e8Smrg rm -f "$dsttmp" 479c27c18e8Smrg else 480c27c18e8Smrg # Rename the file to the real destination. 481c27c18e8Smrg $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || 482c27c18e8Smrg 483c27c18e8Smrg # The rename failed, perhaps because mv can't rename something else 484c27c18e8Smrg # to itself, or perhaps because mv is so ancient that it does not 485c27c18e8Smrg # support -f. 486c27c18e8Smrg { 48748e5ce07Smrg # Now remove or move aside any old file at destination location. 48848e5ce07Smrg # We try this two ways since rm can't unlink itself on some 48948e5ce07Smrg # systems and the destination file might be busy for other 49048e5ce07Smrg # reasons. In this case, the final cleanup might fail but the new 49148e5ce07Smrg # file should still install successfully. 49248e5ce07Smrg { 49348e5ce07Smrg test ! -f "$dst" || 49448e5ce07Smrg $doit $rmcmd -f "$dst" 2>/dev/null || 49548e5ce07Smrg { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && 49648e5ce07Smrg { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; } 49748e5ce07Smrg } || 49848e5ce07Smrg { echo "$0: cannot unlink or rename $dst" >&2 49948e5ce07Smrg (exit 1); exit 1 50048e5ce07Smrg } 50148e5ce07Smrg } && 50248e5ce07Smrg 50348e5ce07Smrg # Now rename the file to the real destination. 50448e5ce07Smrg $doit $mvcmd "$dsttmp" "$dst" 505c27c18e8Smrg } 506c27c18e8Smrg fi || exit 1 507c43cc173Smrg 508c43cc173Smrg trap '' 0 509c43cc173Smrg fi 510c43cc173Smrgdone 511c43cc173Smrg 512c43cc173Smrg# Local variables: 5134940c694Smrg# eval: (add-hook 'before-save-hook 'time-stamp) 514c43cc173Smrg# time-stamp-start: "scriptversion=" 515c43cc173Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 5164940c694Smrg# time-stamp-time-zone: "UTC0" 517c27c18e8Smrg# time-stamp-end: "; # UTC" 518c43cc173Smrg# End: 519