113498266Sopenharmony_ci#!/bin/sh 213498266Sopenharmony_ci# install - install a program, script, or datafile 313498266Sopenharmony_ci 413498266Sopenharmony_ciscriptversion=2020-11-14.01; # UTC 513498266Sopenharmony_ci 613498266Sopenharmony_ci# This originates from X11R5 (mit/util/scripts/install.sh), which was 713498266Sopenharmony_ci# later released in X11R6 (xc/config/util/install.sh) with the 813498266Sopenharmony_ci# following copyright and license. 913498266Sopenharmony_ci# 1013498266Sopenharmony_ci# Copyright (C) 1994 X Consortium 1113498266Sopenharmony_ci# 1213498266Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a copy 1313498266Sopenharmony_ci# of this software and associated documentation files (the "Software"), to 1413498266Sopenharmony_ci# deal in the Software without restriction, including without limitation the 1513498266Sopenharmony_ci# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 1613498266Sopenharmony_ci# sell copies of the Software, and to permit persons to whom the Software is 1713498266Sopenharmony_ci# furnished to do so, subject to the following conditions: 1813498266Sopenharmony_ci# 1913498266Sopenharmony_ci# The above copyright notice and this permission notice shall be included in 2013498266Sopenharmony_ci# all copies or substantial portions of the Software. 2113498266Sopenharmony_ci# 2213498266Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2313498266Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2413498266Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2513498266Sopenharmony_ci# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 2613498266Sopenharmony_ci# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 2713498266Sopenharmony_ci# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2813498266Sopenharmony_ci# 2913498266Sopenharmony_ci# Except as contained in this notice, the name of the X Consortium shall not 3013498266Sopenharmony_ci# be used in advertising or otherwise to promote the sale, use or other deal- 3113498266Sopenharmony_ci# ings in this Software without prior written authorization from the X Consor- 3213498266Sopenharmony_ci# tium. 3313498266Sopenharmony_ci# 3413498266Sopenharmony_ci# 3513498266Sopenharmony_ci# FSF changes to this file are in the public domain. 3613498266Sopenharmony_ci# 3713498266Sopenharmony_ci# Calling this script install-sh is preferred over install.sh, to prevent 3813498266Sopenharmony_ci# 'make' implicit rules from creating a file called install from it 3913498266Sopenharmony_ci# when there is no Makefile. 4013498266Sopenharmony_ci# 4113498266Sopenharmony_ci# This script is compatible with the BSD install script, but was written 4213498266Sopenharmony_ci# from scratch. 4313498266Sopenharmony_ci 4413498266Sopenharmony_citab=' ' 4513498266Sopenharmony_cinl=' 4613498266Sopenharmony_ci' 4713498266Sopenharmony_ciIFS=" $tab$nl" 4813498266Sopenharmony_ci 4913498266Sopenharmony_ci# Set DOITPROG to "echo" to test this script. 5013498266Sopenharmony_ci 5113498266Sopenharmony_cidoit=${DOITPROG-} 5213498266Sopenharmony_cidoit_exec=${doit:-exec} 5313498266Sopenharmony_ci 5413498266Sopenharmony_ci# Put in absolute file names if you don't have them in your path; 5513498266Sopenharmony_ci# or use environment vars. 5613498266Sopenharmony_ci 5713498266Sopenharmony_cichgrpprog=${CHGRPPROG-chgrp} 5813498266Sopenharmony_cichmodprog=${CHMODPROG-chmod} 5913498266Sopenharmony_cichownprog=${CHOWNPROG-chown} 6013498266Sopenharmony_cicmpprog=${CMPPROG-cmp} 6113498266Sopenharmony_cicpprog=${CPPROG-cp} 6213498266Sopenharmony_cimkdirprog=${MKDIRPROG-mkdir} 6313498266Sopenharmony_cimvprog=${MVPROG-mv} 6413498266Sopenharmony_cirmprog=${RMPROG-rm} 6513498266Sopenharmony_cistripprog=${STRIPPROG-strip} 6613498266Sopenharmony_ci 6713498266Sopenharmony_ciposix_mkdir= 6813498266Sopenharmony_ci 6913498266Sopenharmony_ci# Desired mode of installed file. 7013498266Sopenharmony_cimode=0755 7113498266Sopenharmony_ci 7213498266Sopenharmony_ci# Create dirs (including intermediate dirs) using mode 755. 7313498266Sopenharmony_ci# This is like GNU 'install' as of coreutils 8.32 (2020). 7413498266Sopenharmony_cimkdir_umask=22 7513498266Sopenharmony_ci 7613498266Sopenharmony_cibackupsuffix= 7713498266Sopenharmony_cichgrpcmd= 7813498266Sopenharmony_cichmodcmd=$chmodprog 7913498266Sopenharmony_cichowncmd= 8013498266Sopenharmony_cimvcmd=$mvprog 8113498266Sopenharmony_cirmcmd="$rmprog -f" 8213498266Sopenharmony_cistripcmd= 8313498266Sopenharmony_ci 8413498266Sopenharmony_cisrc= 8513498266Sopenharmony_cidst= 8613498266Sopenharmony_cidir_arg= 8713498266Sopenharmony_cidst_arg= 8813498266Sopenharmony_ci 8913498266Sopenharmony_cicopy_on_change=false 9013498266Sopenharmony_ciis_target_a_directory=possibly 9113498266Sopenharmony_ci 9213498266Sopenharmony_ciusage="\ 9313498266Sopenharmony_ciUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE 9413498266Sopenharmony_ci or: $0 [OPTION]... SRCFILES... DIRECTORY 9513498266Sopenharmony_ci or: $0 [OPTION]... -t DIRECTORY SRCFILES... 9613498266Sopenharmony_ci or: $0 [OPTION]... -d DIRECTORIES... 9713498266Sopenharmony_ci 9813498266Sopenharmony_ciIn the 1st form, copy SRCFILE to DSTFILE. 9913498266Sopenharmony_ciIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 10013498266Sopenharmony_ciIn the 4th, create DIRECTORIES. 10113498266Sopenharmony_ci 10213498266Sopenharmony_ciOptions: 10313498266Sopenharmony_ci --help display this help and exit. 10413498266Sopenharmony_ci --version display version info and exit. 10513498266Sopenharmony_ci 10613498266Sopenharmony_ci -c (ignored) 10713498266Sopenharmony_ci -C install only if different (preserve data modification time) 10813498266Sopenharmony_ci -d create directories instead of installing files. 10913498266Sopenharmony_ci -g GROUP $chgrpprog installed files to GROUP. 11013498266Sopenharmony_ci -m MODE $chmodprog installed files to MODE. 11113498266Sopenharmony_ci -o USER $chownprog installed files to USER. 11213498266Sopenharmony_ci -p pass -p to $cpprog. 11313498266Sopenharmony_ci -s $stripprog installed files. 11413498266Sopenharmony_ci -S SUFFIX attempt to back up existing files, with suffix SUFFIX. 11513498266Sopenharmony_ci -t DIRECTORY install into DIRECTORY. 11613498266Sopenharmony_ci -T report an error if DSTFILE is a directory. 11713498266Sopenharmony_ci 11813498266Sopenharmony_ciEnvironment variables override the default commands: 11913498266Sopenharmony_ci CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG 12013498266Sopenharmony_ci RMPROG STRIPPROG 12113498266Sopenharmony_ci 12213498266Sopenharmony_ciBy default, rm is invoked with -f; when overridden with RMPROG, 12313498266Sopenharmony_ciit's up to you to specify -f if you want it. 12413498266Sopenharmony_ci 12513498266Sopenharmony_ciIf -S is not specified, no backups are attempted. 12613498266Sopenharmony_ci 12713498266Sopenharmony_ciEmail bug reports to bug-automake@gnu.org. 12813498266Sopenharmony_ciAutomake home page: https://www.gnu.org/software/automake/ 12913498266Sopenharmony_ci" 13013498266Sopenharmony_ci 13113498266Sopenharmony_ciwhile test $# -ne 0; do 13213498266Sopenharmony_ci case $1 in 13313498266Sopenharmony_ci -c) ;; 13413498266Sopenharmony_ci 13513498266Sopenharmony_ci -C) copy_on_change=true;; 13613498266Sopenharmony_ci 13713498266Sopenharmony_ci -d) dir_arg=true;; 13813498266Sopenharmony_ci 13913498266Sopenharmony_ci -g) chgrpcmd="$chgrpprog $2" 14013498266Sopenharmony_ci shift;; 14113498266Sopenharmony_ci 14213498266Sopenharmony_ci --help) echo "$usage"; exit $?;; 14313498266Sopenharmony_ci 14413498266Sopenharmony_ci -m) mode=$2 14513498266Sopenharmony_ci case $mode in 14613498266Sopenharmony_ci *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*) 14713498266Sopenharmony_ci echo "$0: invalid mode: $mode" >&2 14813498266Sopenharmony_ci exit 1;; 14913498266Sopenharmony_ci esac 15013498266Sopenharmony_ci shift;; 15113498266Sopenharmony_ci 15213498266Sopenharmony_ci -o) chowncmd="$chownprog $2" 15313498266Sopenharmony_ci shift;; 15413498266Sopenharmony_ci 15513498266Sopenharmony_ci -p) cpprog="$cpprog -p";; 15613498266Sopenharmony_ci 15713498266Sopenharmony_ci -s) stripcmd=$stripprog;; 15813498266Sopenharmony_ci 15913498266Sopenharmony_ci -S) backupsuffix="$2" 16013498266Sopenharmony_ci shift;; 16113498266Sopenharmony_ci 16213498266Sopenharmony_ci -t) 16313498266Sopenharmony_ci is_target_a_directory=always 16413498266Sopenharmony_ci dst_arg=$2 16513498266Sopenharmony_ci # Protect names problematic for 'test' and other utilities. 16613498266Sopenharmony_ci case $dst_arg in 16713498266Sopenharmony_ci -* | [=\(\)!]) dst_arg=./$dst_arg;; 16813498266Sopenharmony_ci esac 16913498266Sopenharmony_ci shift;; 17013498266Sopenharmony_ci 17113498266Sopenharmony_ci -T) is_target_a_directory=never;; 17213498266Sopenharmony_ci 17313498266Sopenharmony_ci --version) echo "$0 $scriptversion"; exit $?;; 17413498266Sopenharmony_ci 17513498266Sopenharmony_ci --) shift 17613498266Sopenharmony_ci break;; 17713498266Sopenharmony_ci 17813498266Sopenharmony_ci -*) echo "$0: invalid option: $1" >&2 17913498266Sopenharmony_ci exit 1;; 18013498266Sopenharmony_ci 18113498266Sopenharmony_ci *) break;; 18213498266Sopenharmony_ci esac 18313498266Sopenharmony_ci shift 18413498266Sopenharmony_cidone 18513498266Sopenharmony_ci 18613498266Sopenharmony_ci# We allow the use of options -d and -T together, by making -d 18713498266Sopenharmony_ci# take the precedence; this is for compatibility with GNU install. 18813498266Sopenharmony_ci 18913498266Sopenharmony_ciif test -n "$dir_arg"; then 19013498266Sopenharmony_ci if test -n "$dst_arg"; then 19113498266Sopenharmony_ci echo "$0: target directory not allowed when installing a directory." >&2 19213498266Sopenharmony_ci exit 1 19313498266Sopenharmony_ci fi 19413498266Sopenharmony_cifi 19513498266Sopenharmony_ci 19613498266Sopenharmony_ciif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then 19713498266Sopenharmony_ci # When -d is used, all remaining arguments are directories to create. 19813498266Sopenharmony_ci # When -t is used, the destination is already specified. 19913498266Sopenharmony_ci # Otherwise, the last argument is the destination. Remove it from $@. 20013498266Sopenharmony_ci for arg 20113498266Sopenharmony_ci do 20213498266Sopenharmony_ci if test -n "$dst_arg"; then 20313498266Sopenharmony_ci # $@ is not empty: it contains at least $arg. 20413498266Sopenharmony_ci set fnord "$@" "$dst_arg" 20513498266Sopenharmony_ci shift # fnord 20613498266Sopenharmony_ci fi 20713498266Sopenharmony_ci shift # arg 20813498266Sopenharmony_ci dst_arg=$arg 20913498266Sopenharmony_ci # Protect names problematic for 'test' and other utilities. 21013498266Sopenharmony_ci case $dst_arg in 21113498266Sopenharmony_ci -* | [=\(\)!]) dst_arg=./$dst_arg;; 21213498266Sopenharmony_ci esac 21313498266Sopenharmony_ci done 21413498266Sopenharmony_cifi 21513498266Sopenharmony_ci 21613498266Sopenharmony_ciif test $# -eq 0; then 21713498266Sopenharmony_ci if test -z "$dir_arg"; then 21813498266Sopenharmony_ci echo "$0: no input file specified." >&2 21913498266Sopenharmony_ci exit 1 22013498266Sopenharmony_ci fi 22113498266Sopenharmony_ci # It's OK to call 'install-sh -d' without argument. 22213498266Sopenharmony_ci # This can happen when creating conditional directories. 22313498266Sopenharmony_ci exit 0 22413498266Sopenharmony_cifi 22513498266Sopenharmony_ci 22613498266Sopenharmony_ciif test -z "$dir_arg"; then 22713498266Sopenharmony_ci if test $# -gt 1 || test "$is_target_a_directory" = always; then 22813498266Sopenharmony_ci if test ! -d "$dst_arg"; then 22913498266Sopenharmony_ci echo "$0: $dst_arg: Is not a directory." >&2 23013498266Sopenharmony_ci exit 1 23113498266Sopenharmony_ci fi 23213498266Sopenharmony_ci fi 23313498266Sopenharmony_cifi 23413498266Sopenharmony_ci 23513498266Sopenharmony_ciif test -z "$dir_arg"; then 23613498266Sopenharmony_ci do_exit='(exit $ret); exit $ret' 23713498266Sopenharmony_ci trap "ret=129; $do_exit" 1 23813498266Sopenharmony_ci trap "ret=130; $do_exit" 2 23913498266Sopenharmony_ci trap "ret=141; $do_exit" 13 24013498266Sopenharmony_ci trap "ret=143; $do_exit" 15 24113498266Sopenharmony_ci 24213498266Sopenharmony_ci # Set umask so as not to create temps with too-generous modes. 24313498266Sopenharmony_ci # However, 'strip' requires both read and write access to temps. 24413498266Sopenharmony_ci case $mode in 24513498266Sopenharmony_ci # Optimize common cases. 24613498266Sopenharmony_ci *644) cp_umask=133;; 24713498266Sopenharmony_ci *755) cp_umask=22;; 24813498266Sopenharmony_ci 24913498266Sopenharmony_ci *[0-7]) 25013498266Sopenharmony_ci if test -z "$stripcmd"; then 25113498266Sopenharmony_ci u_plus_rw= 25213498266Sopenharmony_ci else 25313498266Sopenharmony_ci u_plus_rw='% 200' 25413498266Sopenharmony_ci fi 25513498266Sopenharmony_ci cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 25613498266Sopenharmony_ci *) 25713498266Sopenharmony_ci if test -z "$stripcmd"; then 25813498266Sopenharmony_ci u_plus_rw= 25913498266Sopenharmony_ci else 26013498266Sopenharmony_ci u_plus_rw=,u+rw 26113498266Sopenharmony_ci fi 26213498266Sopenharmony_ci cp_umask=$mode$u_plus_rw;; 26313498266Sopenharmony_ci esac 26413498266Sopenharmony_cifi 26513498266Sopenharmony_ci 26613498266Sopenharmony_cifor src 26713498266Sopenharmony_cido 26813498266Sopenharmony_ci # Protect names problematic for 'test' and other utilities. 26913498266Sopenharmony_ci case $src in 27013498266Sopenharmony_ci -* | [=\(\)!]) src=./$src;; 27113498266Sopenharmony_ci esac 27213498266Sopenharmony_ci 27313498266Sopenharmony_ci if test -n "$dir_arg"; then 27413498266Sopenharmony_ci dst=$src 27513498266Sopenharmony_ci dstdir=$dst 27613498266Sopenharmony_ci test -d "$dstdir" 27713498266Sopenharmony_ci dstdir_status=$? 27813498266Sopenharmony_ci # Don't chown directories that already exist. 27913498266Sopenharmony_ci if test $dstdir_status = 0; then 28013498266Sopenharmony_ci chowncmd="" 28113498266Sopenharmony_ci fi 28213498266Sopenharmony_ci else 28313498266Sopenharmony_ci 28413498266Sopenharmony_ci # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 28513498266Sopenharmony_ci # might cause directories to be created, which would be especially bad 28613498266Sopenharmony_ci # if $src (and thus $dsttmp) contains '*'. 28713498266Sopenharmony_ci if test ! -f "$src" && test ! -d "$src"; then 28813498266Sopenharmony_ci echo "$0: $src does not exist." >&2 28913498266Sopenharmony_ci exit 1 29013498266Sopenharmony_ci fi 29113498266Sopenharmony_ci 29213498266Sopenharmony_ci if test -z "$dst_arg"; then 29313498266Sopenharmony_ci echo "$0: no destination specified." >&2 29413498266Sopenharmony_ci exit 1 29513498266Sopenharmony_ci fi 29613498266Sopenharmony_ci dst=$dst_arg 29713498266Sopenharmony_ci 29813498266Sopenharmony_ci # If destination is a directory, append the input filename. 29913498266Sopenharmony_ci if test -d "$dst"; then 30013498266Sopenharmony_ci if test "$is_target_a_directory" = never; then 30113498266Sopenharmony_ci echo "$0: $dst_arg: Is a directory" >&2 30213498266Sopenharmony_ci exit 1 30313498266Sopenharmony_ci fi 30413498266Sopenharmony_ci dstdir=$dst 30513498266Sopenharmony_ci dstbase=`basename "$src"` 30613498266Sopenharmony_ci case $dst in 30713498266Sopenharmony_ci */) dst=$dst$dstbase;; 30813498266Sopenharmony_ci *) dst=$dst/$dstbase;; 30913498266Sopenharmony_ci esac 31013498266Sopenharmony_ci dstdir_status=0 31113498266Sopenharmony_ci else 31213498266Sopenharmony_ci dstdir=`dirname "$dst"` 31313498266Sopenharmony_ci test -d "$dstdir" 31413498266Sopenharmony_ci dstdir_status=$? 31513498266Sopenharmony_ci fi 31613498266Sopenharmony_ci fi 31713498266Sopenharmony_ci 31813498266Sopenharmony_ci case $dstdir in 31913498266Sopenharmony_ci */) dstdirslash=$dstdir;; 32013498266Sopenharmony_ci *) dstdirslash=$dstdir/;; 32113498266Sopenharmony_ci esac 32213498266Sopenharmony_ci 32313498266Sopenharmony_ci obsolete_mkdir_used=false 32413498266Sopenharmony_ci 32513498266Sopenharmony_ci if test $dstdir_status != 0; then 32613498266Sopenharmony_ci case $posix_mkdir in 32713498266Sopenharmony_ci '') 32813498266Sopenharmony_ci # With -d, create the new directory with the user-specified mode. 32913498266Sopenharmony_ci # Otherwise, rely on $mkdir_umask. 33013498266Sopenharmony_ci if test -n "$dir_arg"; then 33113498266Sopenharmony_ci mkdir_mode=-m$mode 33213498266Sopenharmony_ci else 33313498266Sopenharmony_ci mkdir_mode= 33413498266Sopenharmony_ci fi 33513498266Sopenharmony_ci 33613498266Sopenharmony_ci posix_mkdir=false 33713498266Sopenharmony_ci # The $RANDOM variable is not portable (e.g., dash). Use it 33813498266Sopenharmony_ci # here however when possible just to lower collision chance. 33913498266Sopenharmony_ci tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 34013498266Sopenharmony_ci 34113498266Sopenharmony_ci trap ' 34213498266Sopenharmony_ci ret=$? 34313498266Sopenharmony_ci rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null 34413498266Sopenharmony_ci exit $ret 34513498266Sopenharmony_ci ' 0 34613498266Sopenharmony_ci 34713498266Sopenharmony_ci # Because "mkdir -p" follows existing symlinks and we likely work 34813498266Sopenharmony_ci # directly in world-writeable /tmp, make sure that the '$tmpdir' 34913498266Sopenharmony_ci # directory is successfully created first before we actually test 35013498266Sopenharmony_ci # 'mkdir -p'. 35113498266Sopenharmony_ci if (umask $mkdir_umask && 35213498266Sopenharmony_ci $mkdirprog $mkdir_mode "$tmpdir" && 35313498266Sopenharmony_ci exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1 35413498266Sopenharmony_ci then 35513498266Sopenharmony_ci if test -z "$dir_arg" || { 35613498266Sopenharmony_ci # Check for POSIX incompatibilities with -m. 35713498266Sopenharmony_ci # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 35813498266Sopenharmony_ci # other-writable bit of parent directory when it shouldn't. 35913498266Sopenharmony_ci # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 36013498266Sopenharmony_ci test_tmpdir="$tmpdir/a" 36113498266Sopenharmony_ci ls_ld_tmpdir=`ls -ld "$test_tmpdir"` 36213498266Sopenharmony_ci case $ls_ld_tmpdir in 36313498266Sopenharmony_ci d????-?r-*) different_mode=700;; 36413498266Sopenharmony_ci d????-?--*) different_mode=755;; 36513498266Sopenharmony_ci *) false;; 36613498266Sopenharmony_ci esac && 36713498266Sopenharmony_ci $mkdirprog -m$different_mode -p -- "$test_tmpdir" && { 36813498266Sopenharmony_ci ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"` 36913498266Sopenharmony_ci test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 37013498266Sopenharmony_ci } 37113498266Sopenharmony_ci } 37213498266Sopenharmony_ci then posix_mkdir=: 37313498266Sopenharmony_ci fi 37413498266Sopenharmony_ci rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 37513498266Sopenharmony_ci else 37613498266Sopenharmony_ci # Remove any dirs left behind by ancient mkdir implementations. 37713498266Sopenharmony_ci rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null 37813498266Sopenharmony_ci fi 37913498266Sopenharmony_ci trap '' 0;; 38013498266Sopenharmony_ci esac 38113498266Sopenharmony_ci 38213498266Sopenharmony_ci if 38313498266Sopenharmony_ci $posix_mkdir && ( 38413498266Sopenharmony_ci umask $mkdir_umask && 38513498266Sopenharmony_ci $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 38613498266Sopenharmony_ci ) 38713498266Sopenharmony_ci then : 38813498266Sopenharmony_ci else 38913498266Sopenharmony_ci 39013498266Sopenharmony_ci # mkdir does not conform to POSIX, 39113498266Sopenharmony_ci # or it failed possibly due to a race condition. Create the 39213498266Sopenharmony_ci # directory the slow way, step by step, checking for races as we go. 39313498266Sopenharmony_ci 39413498266Sopenharmony_ci case $dstdir in 39513498266Sopenharmony_ci /*) prefix='/';; 39613498266Sopenharmony_ci [-=\(\)!]*) prefix='./';; 39713498266Sopenharmony_ci *) prefix='';; 39813498266Sopenharmony_ci esac 39913498266Sopenharmony_ci 40013498266Sopenharmony_ci oIFS=$IFS 40113498266Sopenharmony_ci IFS=/ 40213498266Sopenharmony_ci set -f 40313498266Sopenharmony_ci set fnord $dstdir 40413498266Sopenharmony_ci shift 40513498266Sopenharmony_ci set +f 40613498266Sopenharmony_ci IFS=$oIFS 40713498266Sopenharmony_ci 40813498266Sopenharmony_ci prefixes= 40913498266Sopenharmony_ci 41013498266Sopenharmony_ci for d 41113498266Sopenharmony_ci do 41213498266Sopenharmony_ci test X"$d" = X && continue 41313498266Sopenharmony_ci 41413498266Sopenharmony_ci prefix=$prefix$d 41513498266Sopenharmony_ci if test -d "$prefix"; then 41613498266Sopenharmony_ci prefixes= 41713498266Sopenharmony_ci else 41813498266Sopenharmony_ci if $posix_mkdir; then 41913498266Sopenharmony_ci (umask $mkdir_umask && 42013498266Sopenharmony_ci $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 42113498266Sopenharmony_ci # Don't fail if two instances are running concurrently. 42213498266Sopenharmony_ci test -d "$prefix" || exit 1 42313498266Sopenharmony_ci else 42413498266Sopenharmony_ci case $prefix in 42513498266Sopenharmony_ci *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 42613498266Sopenharmony_ci *) qprefix=$prefix;; 42713498266Sopenharmony_ci esac 42813498266Sopenharmony_ci prefixes="$prefixes '$qprefix'" 42913498266Sopenharmony_ci fi 43013498266Sopenharmony_ci fi 43113498266Sopenharmony_ci prefix=$prefix/ 43213498266Sopenharmony_ci done 43313498266Sopenharmony_ci 43413498266Sopenharmony_ci if test -n "$prefixes"; then 43513498266Sopenharmony_ci # Don't fail if two instances are running concurrently. 43613498266Sopenharmony_ci (umask $mkdir_umask && 43713498266Sopenharmony_ci eval "\$doit_exec \$mkdirprog $prefixes") || 43813498266Sopenharmony_ci test -d "$dstdir" || exit 1 43913498266Sopenharmony_ci obsolete_mkdir_used=true 44013498266Sopenharmony_ci fi 44113498266Sopenharmony_ci fi 44213498266Sopenharmony_ci fi 44313498266Sopenharmony_ci 44413498266Sopenharmony_ci if test -n "$dir_arg"; then 44513498266Sopenharmony_ci { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 44613498266Sopenharmony_ci { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 44713498266Sopenharmony_ci { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 44813498266Sopenharmony_ci test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 44913498266Sopenharmony_ci else 45013498266Sopenharmony_ci 45113498266Sopenharmony_ci # Make a couple of temp file names in the proper directory. 45213498266Sopenharmony_ci dsttmp=${dstdirslash}_inst.$$_ 45313498266Sopenharmony_ci rmtmp=${dstdirslash}_rm.$$_ 45413498266Sopenharmony_ci 45513498266Sopenharmony_ci # Trap to clean up those temp files at exit. 45613498266Sopenharmony_ci trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 45713498266Sopenharmony_ci 45813498266Sopenharmony_ci # Copy the file name to the temp name. 45913498266Sopenharmony_ci (umask $cp_umask && 46013498266Sopenharmony_ci { test -z "$stripcmd" || { 46113498266Sopenharmony_ci # Create $dsttmp read-write so that cp doesn't create it read-only, 46213498266Sopenharmony_ci # which would cause strip to fail. 46313498266Sopenharmony_ci if test -z "$doit"; then 46413498266Sopenharmony_ci : >"$dsttmp" # No need to fork-exec 'touch'. 46513498266Sopenharmony_ci else 46613498266Sopenharmony_ci $doit touch "$dsttmp" 46713498266Sopenharmony_ci fi 46813498266Sopenharmony_ci } 46913498266Sopenharmony_ci } && 47013498266Sopenharmony_ci $doit_exec $cpprog "$src" "$dsttmp") && 47113498266Sopenharmony_ci 47213498266Sopenharmony_ci # and set any options; do chmod last to preserve setuid bits. 47313498266Sopenharmony_ci # 47413498266Sopenharmony_ci # If any of these fail, we abort the whole thing. If we want to 47513498266Sopenharmony_ci # ignore errors from any of these, just make sure not to ignore 47613498266Sopenharmony_ci # errors from the above "$doit $cpprog $src $dsttmp" command. 47713498266Sopenharmony_ci # 47813498266Sopenharmony_ci { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && 47913498266Sopenharmony_ci { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && 48013498266Sopenharmony_ci { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && 48113498266Sopenharmony_ci { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 48213498266Sopenharmony_ci 48313498266Sopenharmony_ci # If -C, don't bother to copy if it wouldn't change the file. 48413498266Sopenharmony_ci if $copy_on_change && 48513498266Sopenharmony_ci old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && 48613498266Sopenharmony_ci new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && 48713498266Sopenharmony_ci set -f && 48813498266Sopenharmony_ci set X $old && old=:$2:$4:$5:$6 && 48913498266Sopenharmony_ci set X $new && new=:$2:$4:$5:$6 && 49013498266Sopenharmony_ci set +f && 49113498266Sopenharmony_ci test "$old" = "$new" && 49213498266Sopenharmony_ci $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 49313498266Sopenharmony_ci then 49413498266Sopenharmony_ci rm -f "$dsttmp" 49513498266Sopenharmony_ci else 49613498266Sopenharmony_ci # If $backupsuffix is set, and the file being installed 49713498266Sopenharmony_ci # already exists, attempt a backup. Don't worry if it fails, 49813498266Sopenharmony_ci # e.g., if mv doesn't support -f. 49913498266Sopenharmony_ci if test -n "$backupsuffix" && test -f "$dst"; then 50013498266Sopenharmony_ci $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null 50113498266Sopenharmony_ci fi 50213498266Sopenharmony_ci 50313498266Sopenharmony_ci # Rename the file to the real destination. 50413498266Sopenharmony_ci $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || 50513498266Sopenharmony_ci 50613498266Sopenharmony_ci # The rename failed, perhaps because mv can't rename something else 50713498266Sopenharmony_ci # to itself, or perhaps because mv is so ancient that it does not 50813498266Sopenharmony_ci # support -f. 50913498266Sopenharmony_ci { 51013498266Sopenharmony_ci # Now remove or move aside any old file at destination location. 51113498266Sopenharmony_ci # We try this two ways since rm can't unlink itself on some 51213498266Sopenharmony_ci # systems and the destination file might be busy for other 51313498266Sopenharmony_ci # reasons. In this case, the final cleanup might fail but the new 51413498266Sopenharmony_ci # file should still install successfully. 51513498266Sopenharmony_ci { 51613498266Sopenharmony_ci test ! -f "$dst" || 51713498266Sopenharmony_ci $doit $rmcmd "$dst" 2>/dev/null || 51813498266Sopenharmony_ci { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && 51913498266Sopenharmony_ci { $doit $rmcmd "$rmtmp" 2>/dev/null; :; } 52013498266Sopenharmony_ci } || 52113498266Sopenharmony_ci { echo "$0: cannot unlink or rename $dst" >&2 52213498266Sopenharmony_ci (exit 1); exit 1 52313498266Sopenharmony_ci } 52413498266Sopenharmony_ci } && 52513498266Sopenharmony_ci 52613498266Sopenharmony_ci # Now rename the file to the real destination. 52713498266Sopenharmony_ci $doit $mvcmd "$dsttmp" "$dst" 52813498266Sopenharmony_ci } 52913498266Sopenharmony_ci fi || exit 1 53013498266Sopenharmony_ci 53113498266Sopenharmony_ci trap '' 0 53213498266Sopenharmony_ci fi 53313498266Sopenharmony_cidone 53413498266Sopenharmony_ci 53513498266Sopenharmony_ci# Local variables: 53613498266Sopenharmony_ci# eval: (add-hook 'before-save-hook 'time-stamp) 53713498266Sopenharmony_ci# time-stamp-start: "scriptversion=" 53813498266Sopenharmony_ci# time-stamp-format: "%:y-%02m-%02d.%02H" 53913498266Sopenharmony_ci# time-stamp-time-zone: "UTC0" 54013498266Sopenharmony_ci# time-stamp-end: "; # UTC" 54113498266Sopenharmony_ci# End: 542