install-sh revision ef7198c0
156f6b22bSmrg#!/bin/sh 256f6b22bSmrg# install - install a program, script, or datafile 3ef7198c0Smrg 4ef7198c0Smrgscriptversion=2009-04-28.21; # UTC 5ef7198c0Smrg 656f6b22bSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 756f6b22bSmrg# later released in X11R6 (xc/config/util/install.sh) with the 856f6b22bSmrg# following copyright and license. 956f6b22bSmrg# 1056f6b22bSmrg# Copyright (C) 1994 X Consortium 1156f6b22bSmrg# 1256f6b22bSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy 1356f6b22bSmrg# of this software and associated documentation files (the "Software"), to 1456f6b22bSmrg# deal in the Software without restriction, including without limitation the 1556f6b22bSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 1656f6b22bSmrg# sell copies of the Software, and to permit persons to whom the Software is 1756f6b22bSmrg# furnished to do so, subject to the following conditions: 1856f6b22bSmrg# 1956f6b22bSmrg# The above copyright notice and this permission notice shall be included in 2056f6b22bSmrg# all copies or substantial portions of the Software. 2156f6b22bSmrg# 2256f6b22bSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2356f6b22bSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2456f6b22bSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2556f6b22bSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 2656f6b22bSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 2756f6b22bSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2856f6b22bSmrg# 2956f6b22bSmrg# Except as contained in this notice, the name of the X Consortium shall not 3056f6b22bSmrg# be used in advertising or otherwise to promote the sale, use or other deal- 3156f6b22bSmrg# ings in this Software without prior written authorization from the X Consor- 3256f6b22bSmrg# tium. 3356f6b22bSmrg# 3456f6b22bSmrg# 3556f6b22bSmrg# FSF changes to this file are in the public domain. 3656f6b22bSmrg# 3756f6b22bSmrg# Calling this script install-sh is preferred over install.sh, to prevent 3856f6b22bSmrg# `make' implicit rules from creating a file called install from it 3956f6b22bSmrg# when there is no Makefile. 4056f6b22bSmrg# 4156f6b22bSmrg# This script is compatible with the BSD install script, but was written 42ef7198c0Smrg# from scratch. 4356f6b22bSmrg 44ef7198c0Smrgnl=' 45ef7198c0Smrg' 46ef7198c0SmrgIFS=" "" $nl" 4756f6b22bSmrg 4856f6b22bSmrg# set DOITPROG to echo to test this script 4956f6b22bSmrg 5056f6b22bSmrg# Don't use :- since 4.3BSD and earlier shells don't like it. 51ef7198c0Smrgdoit=${DOITPROG-} 52ef7198c0Smrgif test -z "$doit"; then 53ef7198c0Smrg doit_exec=exec 5456f6b22bSmrgelse 55ef7198c0Smrg doit_exec=$doit 5656f6b22bSmrgfi 5756f6b22bSmrg 58ef7198c0Smrg# Put in absolute file names if you don't have them in your path; 59ef7198c0Smrg# or use environment vars. 60ef7198c0Smrg 61ef7198c0Smrgchgrpprog=${CHGRPPROG-chgrp} 62ef7198c0Smrgchmodprog=${CHMODPROG-chmod} 63ef7198c0Smrgchownprog=${CHOWNPROG-chown} 64ef7198c0Smrgcmpprog=${CMPPROG-cmp} 65ef7198c0Smrgcpprog=${CPPROG-cp} 66ef7198c0Smrgmkdirprog=${MKDIRPROG-mkdir} 67ef7198c0Smrgmvprog=${MVPROG-mv} 68ef7198c0Smrgrmprog=${RMPROG-rm} 69ef7198c0Smrgstripprog=${STRIPPROG-strip} 70ef7198c0Smrg 71ef7198c0Smrgposix_glob='?' 72ef7198c0Smrginitialize_posix_glob=' 73ef7198c0Smrg test "$posix_glob" != "?" || { 74ef7198c0Smrg if (set -f) 2>/dev/null; then 75ef7198c0Smrg posix_glob= 76ef7198c0Smrg else 77ef7198c0Smrg posix_glob=: 78ef7198c0Smrg fi 79ef7198c0Smrg } 80ef7198c0Smrg' 81ef7198c0Smrg 82ef7198c0Smrgposix_mkdir= 83ef7198c0Smrg 84ef7198c0Smrg# Desired mode of installed file. 85ef7198c0Smrgmode=0755 86ef7198c0Smrg 87ef7198c0Smrgchgrpcmd= 88ef7198c0Smrgchmodcmd=$chmodprog 89ef7198c0Smrgchowncmd= 90ef7198c0Smrgmvcmd=$mvprog 91ef7198c0Smrgrmcmd="$rmprog -f" 92ef7198c0Smrgstripcmd= 9356f6b22bSmrg 94ef7198c0Smrgsrc= 95ef7198c0Smrgdst= 96ef7198c0Smrgdir_arg= 97ef7198c0Smrgdst_arg= 9856f6b22bSmrg 99ef7198c0Smrgcopy_on_change=false 100ef7198c0Smrgno_target_directory= 10156f6b22bSmrg 102ef7198c0Smrgusage="\ 103ef7198c0SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE 104ef7198c0Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 105ef7198c0Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 106ef7198c0Smrg or: $0 [OPTION]... -d DIRECTORIES... 10756f6b22bSmrg 108ef7198c0SmrgIn the 1st form, copy SRCFILE to DSTFILE. 109ef7198c0SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 110ef7198c0SmrgIn the 4th, create DIRECTORIES. 11156f6b22bSmrg 112ef7198c0SmrgOptions: 113ef7198c0Smrg --help display this help and exit. 114ef7198c0Smrg --version display version info and exit. 11556f6b22bSmrg 116ef7198c0Smrg -c (ignored) 117ef7198c0Smrg -C install only if different (preserve the last data modification time) 118ef7198c0Smrg -d create directories instead of installing files. 119ef7198c0Smrg -g GROUP $chgrpprog installed files to GROUP. 120ef7198c0Smrg -m MODE $chmodprog installed files to MODE. 121ef7198c0Smrg -o USER $chownprog installed files to USER. 122ef7198c0Smrg -s $stripprog installed files. 123ef7198c0Smrg -t DIRECTORY install into DIRECTORY. 124ef7198c0Smrg -T report an error if DSTFILE is a directory. 12556f6b22bSmrg 126ef7198c0SmrgEnvironment variables override the default commands: 127ef7198c0Smrg CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG 128ef7198c0Smrg RMPROG STRIPPROG 129ef7198c0Smrg" 13056f6b22bSmrg 131ef7198c0Smrgwhile test $# -ne 0; do 132ef7198c0Smrg case $1 in 133ef7198c0Smrg -c) ;; 13456f6b22bSmrg 135ef7198c0Smrg -C) copy_on_change=true;; 13656f6b22bSmrg 137ef7198c0Smrg -d) dir_arg=true;; 13856f6b22bSmrg 139ef7198c0Smrg -g) chgrpcmd="$chgrpprog $2" 140ef7198c0Smrg shift;; 14156f6b22bSmrg 142ef7198c0Smrg --help) echo "$usage"; exit $?;; 14356f6b22bSmrg 144ef7198c0Smrg -m) mode=$2 145ef7198c0Smrg case $mode in 146ef7198c0Smrg *' '* | *' '* | *' 147ef7198c0Smrg'* | *'*'* | *'?'* | *'['*) 148ef7198c0Smrg echo "$0: invalid mode: $mode" >&2 149ef7198c0Smrg exit 1;; 150ef7198c0Smrg esac 151ef7198c0Smrg shift;; 15256f6b22bSmrg 153ef7198c0Smrg -o) chowncmd="$chownprog $2" 154ef7198c0Smrg shift;; 15556f6b22bSmrg 156ef7198c0Smrg -s) stripcmd=$stripprog;; 15756f6b22bSmrg 158ef7198c0Smrg -t) dst_arg=$2 159ef7198c0Smrg shift;; 16056f6b22bSmrg 161ef7198c0Smrg -T) no_target_directory=true;; 16256f6b22bSmrg 163ef7198c0Smrg --version) echo "$0 $scriptversion"; exit $?;; 16456f6b22bSmrg 165ef7198c0Smrg --) shift 166ef7198c0Smrg break;; 16756f6b22bSmrg 168ef7198c0Smrg -*) echo "$0: invalid option: $1" >&2 169ef7198c0Smrg exit 1;; 17056f6b22bSmrg 171ef7198c0Smrg *) break;; 172ef7198c0Smrg esac 173ef7198c0Smrg shift 174ef7198c0Smrgdone 17556f6b22bSmrg 176ef7198c0Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then 177ef7198c0Smrg # When -d is used, all remaining arguments are directories to create. 178ef7198c0Smrg # When -t is used, the destination is already specified. 179ef7198c0Smrg # Otherwise, the last argument is the destination. Remove it from $@. 180ef7198c0Smrg for arg 181ef7198c0Smrg do 182ef7198c0Smrg if test -n "$dst_arg"; then 183ef7198c0Smrg # $@ is not empty: it contains at least $arg. 184ef7198c0Smrg set fnord "$@" "$dst_arg" 185ef7198c0Smrg shift # fnord 186ef7198c0Smrg fi 187ef7198c0Smrg shift # arg 188ef7198c0Smrg dst_arg=$arg 189ef7198c0Smrg done 190ef7198c0Smrgfi 19156f6b22bSmrg 192ef7198c0Smrgif test $# -eq 0; then 193ef7198c0Smrg if test -z "$dir_arg"; then 194ef7198c0Smrg echo "$0: no input file specified." >&2 195ef7198c0Smrg exit 1 196ef7198c0Smrg fi 197ef7198c0Smrg # It's OK to call `install-sh -d' without argument. 198ef7198c0Smrg # This can happen when creating conditional directories. 199ef7198c0Smrg exit 0 200ef7198c0Smrgfi 20156f6b22bSmrg 202ef7198c0Smrgif test -z "$dir_arg"; then 203ef7198c0Smrg trap '(exit $?); exit' 1 2 13 15 204ef7198c0Smrg 205ef7198c0Smrg # Set umask so as not to create temps with too-generous modes. 206ef7198c0Smrg # However, 'strip' requires both read and write access to temps. 207ef7198c0Smrg case $mode in 208ef7198c0Smrg # Optimize common cases. 209ef7198c0Smrg *644) cp_umask=133;; 210ef7198c0Smrg *755) cp_umask=22;; 211ef7198c0Smrg 212ef7198c0Smrg *[0-7]) 213ef7198c0Smrg if test -z "$stripcmd"; then 214ef7198c0Smrg u_plus_rw= 215ef7198c0Smrg else 216ef7198c0Smrg u_plus_rw='% 200' 217ef7198c0Smrg fi 218ef7198c0Smrg cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 219ef7198c0Smrg *) 220ef7198c0Smrg if test -z "$stripcmd"; then 221ef7198c0Smrg u_plus_rw= 222ef7198c0Smrg else 223ef7198c0Smrg u_plus_rw=,u+rw 224ef7198c0Smrg fi 225ef7198c0Smrg cp_umask=$mode$u_plus_rw;; 226ef7198c0Smrg esac 227ef7198c0Smrgfi 22856f6b22bSmrg 229ef7198c0Smrgfor src 230ef7198c0Smrgdo 231ef7198c0Smrg # Protect names starting with `-'. 232ef7198c0Smrg case $src in 233ef7198c0Smrg -*) src=./$src;; 234ef7198c0Smrg esac 235ef7198c0Smrg 236ef7198c0Smrg if test -n "$dir_arg"; then 237ef7198c0Smrg dst=$src 238ef7198c0Smrg dstdir=$dst 239ef7198c0Smrg test -d "$dstdir" 240ef7198c0Smrg dstdir_status=$? 241ef7198c0Smrg else 242ef7198c0Smrg 243ef7198c0Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 244ef7198c0Smrg # might cause directories to be created, which would be especially bad 245ef7198c0Smrg # if $src (and thus $dsttmp) contains '*'. 246ef7198c0Smrg if test ! -f "$src" && test ! -d "$src"; then 247ef7198c0Smrg echo "$0: $src does not exist." >&2 248ef7198c0Smrg exit 1 249ef7198c0Smrg fi 250ef7198c0Smrg 251ef7198c0Smrg if test -z "$dst_arg"; then 252ef7198c0Smrg echo "$0: no destination specified." >&2 253ef7198c0Smrg exit 1 254ef7198c0Smrg fi 255ef7198c0Smrg 256ef7198c0Smrg dst=$dst_arg 257ef7198c0Smrg # Protect names starting with `-'. 258ef7198c0Smrg case $dst in 259ef7198c0Smrg -*) dst=./$dst;; 260ef7198c0Smrg esac 26156f6b22bSmrg 262ef7198c0Smrg # If destination is a directory, append the input filename; won't work 263ef7198c0Smrg # if double slashes aren't ignored. 264ef7198c0Smrg if test -d "$dst"; then 265ef7198c0Smrg if test -n "$no_target_directory"; then 266ef7198c0Smrg echo "$0: $dst_arg: Is a directory" >&2 267ef7198c0Smrg exit 1 268ef7198c0Smrg fi 269ef7198c0Smrg dstdir=$dst 270ef7198c0Smrg dst=$dstdir/`basename "$src"` 271ef7198c0Smrg dstdir_status=0 272ef7198c0Smrg else 273ef7198c0Smrg # Prefer dirname, but fall back on a substitute if dirname fails. 274ef7198c0Smrg dstdir=` 275ef7198c0Smrg (dirname "$dst") 2>/dev/null || 276ef7198c0Smrg expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ 277ef7198c0Smrg X"$dst" : 'X\(//\)[^/]' \| \ 278ef7198c0Smrg X"$dst" : 'X\(//\)$' \| \ 279ef7198c0Smrg X"$dst" : 'X\(/\)' \| . 2>/dev/null || 280ef7198c0Smrg echo X"$dst" | 281ef7198c0Smrg sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ 282ef7198c0Smrg s//\1/ 283ef7198c0Smrg q 284ef7198c0Smrg } 285ef7198c0Smrg /^X\(\/\/\)[^/].*/{ 286ef7198c0Smrg s//\1/ 287ef7198c0Smrg q 288ef7198c0Smrg } 289ef7198c0Smrg /^X\(\/\/\)$/{ 290ef7198c0Smrg s//\1/ 291ef7198c0Smrg q 292ef7198c0Smrg } 293ef7198c0Smrg /^X\(\/\).*/{ 294ef7198c0Smrg s//\1/ 295ef7198c0Smrg q 296ef7198c0Smrg } 297ef7198c0Smrg s/.*/./; q' 298ef7198c0Smrg ` 299ef7198c0Smrg 300ef7198c0Smrg test -d "$dstdir" 301ef7198c0Smrg dstdir_status=$? 302ef7198c0Smrg fi 303ef7198c0Smrg fi 304ef7198c0Smrg 305ef7198c0Smrg obsolete_mkdir_used=false 306ef7198c0Smrg 307ef7198c0Smrg if test $dstdir_status != 0; then 308ef7198c0Smrg case $posix_mkdir in 309ef7198c0Smrg '') 310ef7198c0Smrg # Create intermediate dirs using mode 755 as modified by the umask. 311ef7198c0Smrg # This is like FreeBSD 'install' as of 1997-10-28. 312ef7198c0Smrg umask=`umask` 313ef7198c0Smrg case $stripcmd.$umask in 314ef7198c0Smrg # Optimize common cases. 315ef7198c0Smrg *[2367][2367]) mkdir_umask=$umask;; 316ef7198c0Smrg .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;; 317ef7198c0Smrg 318ef7198c0Smrg *[0-7]) 319ef7198c0Smrg mkdir_umask=`expr $umask + 22 \ 320ef7198c0Smrg - $umask % 100 % 40 + $umask % 20 \ 321ef7198c0Smrg - $umask % 10 % 4 + $umask % 2 322ef7198c0Smrg `;; 323ef7198c0Smrg *) mkdir_umask=$umask,go-w;; 324ef7198c0Smrg esac 325ef7198c0Smrg 326ef7198c0Smrg # With -d, create the new directory with the user-specified mode. 327ef7198c0Smrg # Otherwise, rely on $mkdir_umask. 328ef7198c0Smrg if test -n "$dir_arg"; then 329ef7198c0Smrg mkdir_mode=-m$mode 33056f6b22bSmrg else 331ef7198c0Smrg mkdir_mode= 33256f6b22bSmrg fi 33356f6b22bSmrg 334ef7198c0Smrg posix_mkdir=false 335ef7198c0Smrg case $umask in 336ef7198c0Smrg *[123567][0-7][0-7]) 337ef7198c0Smrg # POSIX mkdir -p sets u+wx bits regardless of umask, which 338ef7198c0Smrg # is incompatible with FreeBSD 'install' when (umask & 300) != 0. 339ef7198c0Smrg ;; 340ef7198c0Smrg *) 341ef7198c0Smrg tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 342ef7198c0Smrg trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0 343ef7198c0Smrg 344ef7198c0Smrg if (umask $mkdir_umask && 345ef7198c0Smrg exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1 346ef7198c0Smrg then 347ef7198c0Smrg if test -z "$dir_arg" || { 348ef7198c0Smrg # Check for POSIX incompatibilities with -m. 349ef7198c0Smrg # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 350ef7198c0Smrg # other-writeable bit of parent directory when it shouldn't. 351ef7198c0Smrg # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 352ef7198c0Smrg ls_ld_tmpdir=`ls -ld "$tmpdir"` 353ef7198c0Smrg case $ls_ld_tmpdir in 354ef7198c0Smrg d????-?r-*) different_mode=700;; 355ef7198c0Smrg d????-?--*) different_mode=755;; 356ef7198c0Smrg *) false;; 357ef7198c0Smrg esac && 358ef7198c0Smrg $mkdirprog -m$different_mode -p -- "$tmpdir" && { 359ef7198c0Smrg ls_ld_tmpdir_1=`ls -ld "$tmpdir"` 360ef7198c0Smrg test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 361ef7198c0Smrg } 362ef7198c0Smrg } 363ef7198c0Smrg then posix_mkdir=: 364ef7198c0Smrg fi 365ef7198c0Smrg rmdir "$tmpdir/d" "$tmpdir" 366ef7198c0Smrg else 367ef7198c0Smrg # Remove any dirs left behind by ancient mkdir implementations. 368ef7198c0Smrg rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null 369ef7198c0Smrg fi 370ef7198c0Smrg trap '' 0;; 371ef7198c0Smrg esac;; 372ef7198c0Smrg esac 37356f6b22bSmrg 374ef7198c0Smrg if 375ef7198c0Smrg $posix_mkdir && ( 376ef7198c0Smrg umask $mkdir_umask && 377ef7198c0Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 378ef7198c0Smrg ) 379ef7198c0Smrg then : 380ef7198c0Smrg else 381ef7198c0Smrg 382ef7198c0Smrg # The umask is ridiculous, or mkdir does not conform to POSIX, 383ef7198c0Smrg # or it failed possibly due to a race condition. Create the 384ef7198c0Smrg # directory the slow way, step by step, checking for races as we go. 385ef7198c0Smrg 386ef7198c0Smrg case $dstdir in 387ef7198c0Smrg /*) prefix='/';; 388ef7198c0Smrg -*) prefix='./';; 389ef7198c0Smrg *) prefix='';; 390ef7198c0Smrg esac 391ef7198c0Smrg 392ef7198c0Smrg eval "$initialize_posix_glob" 393ef7198c0Smrg 394ef7198c0Smrg oIFS=$IFS 395ef7198c0Smrg IFS=/ 396ef7198c0Smrg $posix_glob set -f 397ef7198c0Smrg set fnord $dstdir 398ef7198c0Smrg shift 399ef7198c0Smrg $posix_glob set +f 400ef7198c0Smrg IFS=$oIFS 401ef7198c0Smrg 402ef7198c0Smrg prefixes= 403ef7198c0Smrg 404ef7198c0Smrg for d 405ef7198c0Smrg do 406ef7198c0Smrg test -z "$d" && continue 407ef7198c0Smrg 408ef7198c0Smrg prefix=$prefix$d 409ef7198c0Smrg if test -d "$prefix"; then 410ef7198c0Smrg prefixes= 411ef7198c0Smrg else 412ef7198c0Smrg if $posix_mkdir; then 413ef7198c0Smrg (umask=$mkdir_umask && 414ef7198c0Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 415ef7198c0Smrg # Don't fail if two instances are running concurrently. 416ef7198c0Smrg test -d "$prefix" || exit 1 417ef7198c0Smrg else 418ef7198c0Smrg case $prefix in 419ef7198c0Smrg *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 420ef7198c0Smrg *) qprefix=$prefix;; 421ef7198c0Smrg esac 422ef7198c0Smrg prefixes="$prefixes '$qprefix'" 423ef7198c0Smrg fi 424ef7198c0Smrg fi 425ef7198c0Smrg prefix=$prefix/ 426ef7198c0Smrg done 427ef7198c0Smrg 428ef7198c0Smrg if test -n "$prefixes"; then 429ef7198c0Smrg # Don't fail if two instances are running concurrently. 430ef7198c0Smrg (umask $mkdir_umask && 431ef7198c0Smrg eval "\$doit_exec \$mkdirprog $prefixes") || 432ef7198c0Smrg test -d "$dstdir" || exit 1 433ef7198c0Smrg obsolete_mkdir_used=true 434ef7198c0Smrg fi 435ef7198c0Smrg fi 436ef7198c0Smrg fi 437ef7198c0Smrg 438ef7198c0Smrg if test -n "$dir_arg"; then 439ef7198c0Smrg { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 440ef7198c0Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 441ef7198c0Smrg { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 442ef7198c0Smrg test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 443ef7198c0Smrg else 444ef7198c0Smrg 445ef7198c0Smrg # Make a couple of temp file names in the proper directory. 446ef7198c0Smrg dsttmp=$dstdir/_inst.$$_ 447ef7198c0Smrg rmtmp=$dstdir/_rm.$$_ 448ef7198c0Smrg 449ef7198c0Smrg # Trap to clean up those temp files at exit. 450ef7198c0Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 451ef7198c0Smrg 452ef7198c0Smrg # Copy the file name to the temp name. 453ef7198c0Smrg (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") && 454ef7198c0Smrg 455ef7198c0Smrg # and set any options; do chmod last to preserve setuid bits. 456ef7198c0Smrg # 457ef7198c0Smrg # If any of these fail, we abort the whole thing. If we want to 458ef7198c0Smrg # ignore errors from any of these, just make sure not to ignore 459ef7198c0Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 460ef7198c0Smrg # 461ef7198c0Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && 462ef7198c0Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && 463ef7198c0Smrg { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && 464ef7198c0Smrg { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 465ef7198c0Smrg 466ef7198c0Smrg # If -C, don't bother to copy if it wouldn't change the file. 467ef7198c0Smrg if $copy_on_change && 468ef7198c0Smrg old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && 469ef7198c0Smrg new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && 470ef7198c0Smrg 471ef7198c0Smrg eval "$initialize_posix_glob" && 472ef7198c0Smrg $posix_glob set -f && 473ef7198c0Smrg set X $old && old=:$2:$4:$5:$6 && 474ef7198c0Smrg set X $new && new=:$2:$4:$5:$6 && 475ef7198c0Smrg $posix_glob set +f && 476ef7198c0Smrg 477ef7198c0Smrg test "$old" = "$new" && 478ef7198c0Smrg $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 479ef7198c0Smrg then 480ef7198c0Smrg rm -f "$dsttmp" 481ef7198c0Smrg else 482ef7198c0Smrg # Rename the file to the real destination. 483ef7198c0Smrg $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || 484ef7198c0Smrg 485ef7198c0Smrg # The rename failed, perhaps because mv can't rename something else 486ef7198c0Smrg # to itself, or perhaps because mv is so ancient that it does not 487ef7198c0Smrg # support -f. 488ef7198c0Smrg { 489ef7198c0Smrg # Now remove or move aside any old file at destination location. 490ef7198c0Smrg # We try this two ways since rm can't unlink itself on some 491ef7198c0Smrg # systems and the destination file might be busy for other 492ef7198c0Smrg # reasons. In this case, the final cleanup might fail but the new 493ef7198c0Smrg # file should still install successfully. 494ef7198c0Smrg { 495ef7198c0Smrg test ! -f "$dst" || 496ef7198c0Smrg $doit $rmcmd -f "$dst" 2>/dev/null || 497ef7198c0Smrg { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && 498ef7198c0Smrg { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; } 499ef7198c0Smrg } || 500ef7198c0Smrg { echo "$0: cannot unlink or rename $dst" >&2 501ef7198c0Smrg (exit 1); exit 1 502ef7198c0Smrg } 503ef7198c0Smrg } && 504ef7198c0Smrg 505ef7198c0Smrg # Now rename the file to the real destination. 506ef7198c0Smrg $doit $mvcmd "$dsttmp" "$dst" 507ef7198c0Smrg } 508ef7198c0Smrg fi || exit 1 509ef7198c0Smrg 510ef7198c0Smrg trap '' 0 511ef7198c0Smrg fi 512ef7198c0Smrgdone 51356f6b22bSmrg 514ef7198c0Smrg# Local variables: 515ef7198c0Smrg# eval: (add-hook 'write-file-hooks 'time-stamp) 516ef7198c0Smrg# time-stamp-start: "scriptversion=" 517ef7198c0Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 518ef7198c0Smrg# time-stamp-time-zone: "UTC" 519ef7198c0Smrg# time-stamp-end: "; # UTC" 520ef7198c0Smrg# End: 521