xref: /aosp_15_r20/external/e2fsprogs/config/install-sh (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker#!/bin/sh
2*6a54128fSAndroid Build Coastguard Worker# install - install a program, script, or datafile
3*6a54128fSAndroid Build Coastguard Worker
4*6a54128fSAndroid Build Coastguard Workerscriptversion=2020-11-14.01; # UTC
5*6a54128fSAndroid Build Coastguard Worker
6*6a54128fSAndroid Build Coastguard Worker# This originates from X11R5 (mit/util/scripts/install.sh), which was
7*6a54128fSAndroid Build Coastguard Worker# later released in X11R6 (xc/config/util/install.sh) with the
8*6a54128fSAndroid Build Coastguard Worker# following copyright and license.
9*6a54128fSAndroid Build Coastguard Worker#
10*6a54128fSAndroid Build Coastguard Worker# Copyright (C) 1994 X Consortium
11*6a54128fSAndroid Build Coastguard Worker#
12*6a54128fSAndroid Build Coastguard Worker# Permission is hereby granted, free of charge, to any person obtaining a copy
13*6a54128fSAndroid Build Coastguard Worker# of this software and associated documentation files (the "Software"), to
14*6a54128fSAndroid Build Coastguard Worker# deal in the Software without restriction, including without limitation the
15*6a54128fSAndroid Build Coastguard Worker# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16*6a54128fSAndroid Build Coastguard Worker# sell copies of the Software, and to permit persons to whom the Software is
17*6a54128fSAndroid Build Coastguard Worker# furnished to do so, subject to the following conditions:
18*6a54128fSAndroid Build Coastguard Worker#
19*6a54128fSAndroid Build Coastguard Worker# The above copyright notice and this permission notice shall be included in
20*6a54128fSAndroid Build Coastguard Worker# all copies or substantial portions of the Software.
21*6a54128fSAndroid Build Coastguard Worker#
22*6a54128fSAndroid Build Coastguard Worker# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23*6a54128fSAndroid Build Coastguard Worker# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24*6a54128fSAndroid Build Coastguard Worker# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25*6a54128fSAndroid Build Coastguard Worker# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26*6a54128fSAndroid Build Coastguard Worker# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27*6a54128fSAndroid Build Coastguard Worker# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28*6a54128fSAndroid Build Coastguard Worker#
29*6a54128fSAndroid Build Coastguard Worker# Except as contained in this notice, the name of the X Consortium shall not
30*6a54128fSAndroid Build Coastguard Worker# be used in advertising or otherwise to promote the sale, use or other deal-
31*6a54128fSAndroid Build Coastguard Worker# ings in this Software without prior written authorization from the X Consor-
32*6a54128fSAndroid Build Coastguard Worker# tium.
33*6a54128fSAndroid Build Coastguard Worker#
34*6a54128fSAndroid Build Coastguard Worker#
35*6a54128fSAndroid Build Coastguard Worker# FSF changes to this file are in the public domain.
36*6a54128fSAndroid Build Coastguard Worker#
37*6a54128fSAndroid Build Coastguard Worker# Calling this script install-sh is preferred over install.sh, to prevent
38*6a54128fSAndroid Build Coastguard Worker# 'make' implicit rules from creating a file called install from it
39*6a54128fSAndroid Build Coastguard Worker# when there is no Makefile.
40*6a54128fSAndroid Build Coastguard Worker#
41*6a54128fSAndroid Build Coastguard Worker# This script is compatible with the BSD install script, but was written
42*6a54128fSAndroid Build Coastguard Worker# from scratch.
43*6a54128fSAndroid Build Coastguard Worker
44*6a54128fSAndroid Build Coastguard Workertab='	'
45*6a54128fSAndroid Build Coastguard Workernl='
46*6a54128fSAndroid Build Coastguard Worker'
47*6a54128fSAndroid Build Coastguard WorkerIFS=" $tab$nl"
48*6a54128fSAndroid Build Coastguard Worker
49*6a54128fSAndroid Build Coastguard Worker# Set DOITPROG to "echo" to test this script.
50*6a54128fSAndroid Build Coastguard Worker
51*6a54128fSAndroid Build Coastguard Workerdoit=${DOITPROG-}
52*6a54128fSAndroid Build Coastguard Workerdoit_exec=${doit:-exec}
53*6a54128fSAndroid Build Coastguard Worker
54*6a54128fSAndroid Build Coastguard Worker# Put in absolute file names if you don't have them in your path;
55*6a54128fSAndroid Build Coastguard Worker# or use environment vars.
56*6a54128fSAndroid Build Coastguard Worker
57*6a54128fSAndroid Build Coastguard Workerchgrpprog=${CHGRPPROG-chgrp}
58*6a54128fSAndroid Build Coastguard Workerchmodprog=${CHMODPROG-chmod}
59*6a54128fSAndroid Build Coastguard Workerchownprog=${CHOWNPROG-chown}
60*6a54128fSAndroid Build Coastguard Workercmpprog=${CMPPROG-cmp}
61*6a54128fSAndroid Build Coastguard Workercpprog=${CPPROG-cp}
62*6a54128fSAndroid Build Coastguard Workermkdirprog=${MKDIRPROG-mkdir}
63*6a54128fSAndroid Build Coastguard Workermvprog=${MVPROG-mv}
64*6a54128fSAndroid Build Coastguard Workerrmprog=${RMPROG-rm}
65*6a54128fSAndroid Build Coastguard Workerstripprog=${STRIPPROG-strip}
66*6a54128fSAndroid Build Coastguard Worker
67*6a54128fSAndroid Build Coastguard Workerposix_mkdir=
68*6a54128fSAndroid Build Coastguard Worker
69*6a54128fSAndroid Build Coastguard Worker# Desired mode of installed file.
70*6a54128fSAndroid Build Coastguard Workermode=0755
71*6a54128fSAndroid Build Coastguard Worker
72*6a54128fSAndroid Build Coastguard Worker# Create dirs (including intermediate dirs) using mode 755.
73*6a54128fSAndroid Build Coastguard Worker# This is like GNU 'install' as of coreutils 8.32 (2020).
74*6a54128fSAndroid Build Coastguard Workermkdir_umask=22
75*6a54128fSAndroid Build Coastguard Worker
76*6a54128fSAndroid Build Coastguard Workerbackupsuffix=
77*6a54128fSAndroid Build Coastguard Workerchgrpcmd=
78*6a54128fSAndroid Build Coastguard Workerchmodcmd=$chmodprog
79*6a54128fSAndroid Build Coastguard Workerchowncmd=
80*6a54128fSAndroid Build Coastguard Workermvcmd=$mvprog
81*6a54128fSAndroid Build Coastguard Workerrmcmd="$rmprog -f"
82*6a54128fSAndroid Build Coastguard Workerstripcmd=
83*6a54128fSAndroid Build Coastguard Worker
84*6a54128fSAndroid Build Coastguard Workersrc=
85*6a54128fSAndroid Build Coastguard Workerdst=
86*6a54128fSAndroid Build Coastguard Workerdir_arg=
87*6a54128fSAndroid Build Coastguard Workerdst_arg=
88*6a54128fSAndroid Build Coastguard Worker
89*6a54128fSAndroid Build Coastguard Workercopy_on_change=false
90*6a54128fSAndroid Build Coastguard Workeris_target_a_directory=possibly
91*6a54128fSAndroid Build Coastguard Worker
92*6a54128fSAndroid Build Coastguard Workerusage="\
93*6a54128fSAndroid Build Coastguard WorkerUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94*6a54128fSAndroid Build Coastguard Worker   or: $0 [OPTION]... SRCFILES... DIRECTORY
95*6a54128fSAndroid Build Coastguard Worker   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96*6a54128fSAndroid Build Coastguard Worker   or: $0 [OPTION]... -d DIRECTORIES...
97*6a54128fSAndroid Build Coastguard Worker
98*6a54128fSAndroid Build Coastguard WorkerIn the 1st form, copy SRCFILE to DSTFILE.
99*6a54128fSAndroid Build Coastguard WorkerIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100*6a54128fSAndroid Build Coastguard WorkerIn the 4th, create DIRECTORIES.
101*6a54128fSAndroid Build Coastguard Worker
102*6a54128fSAndroid Build Coastguard WorkerOptions:
103*6a54128fSAndroid Build Coastguard Worker     --help     display this help and exit.
104*6a54128fSAndroid Build Coastguard Worker     --version  display version info and exit.
105*6a54128fSAndroid Build Coastguard Worker
106*6a54128fSAndroid Build Coastguard Worker  -c            (ignored)
107*6a54128fSAndroid Build Coastguard Worker  -C            install only if different (preserve data modification time)
108*6a54128fSAndroid Build Coastguard Worker  -d            create directories instead of installing files.
109*6a54128fSAndroid Build Coastguard Worker  -g GROUP      $chgrpprog installed files to GROUP.
110*6a54128fSAndroid Build Coastguard Worker  -m MODE       $chmodprog installed files to MODE.
111*6a54128fSAndroid Build Coastguard Worker  -o USER       $chownprog installed files to USER.
112*6a54128fSAndroid Build Coastguard Worker  -p            pass -p to $cpprog.
113*6a54128fSAndroid Build Coastguard Worker  -s            $stripprog installed files.
114*6a54128fSAndroid Build Coastguard Worker  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115*6a54128fSAndroid Build Coastguard Worker  -t DIRECTORY  install into DIRECTORY.
116*6a54128fSAndroid Build Coastguard Worker  -T            report an error if DSTFILE is a directory.
117*6a54128fSAndroid Build Coastguard Worker
118*6a54128fSAndroid Build Coastguard WorkerEnvironment variables override the default commands:
119*6a54128fSAndroid Build Coastguard Worker  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120*6a54128fSAndroid Build Coastguard Worker  RMPROG STRIPPROG
121*6a54128fSAndroid Build Coastguard Worker
122*6a54128fSAndroid Build Coastguard WorkerBy default, rm is invoked with -f; when overridden with RMPROG,
123*6a54128fSAndroid Build Coastguard Workerit's up to you to specify -f if you want it.
124*6a54128fSAndroid Build Coastguard Worker
125*6a54128fSAndroid Build Coastguard WorkerIf -S is not specified, no backups are attempted.
126*6a54128fSAndroid Build Coastguard Worker
127*6a54128fSAndroid Build Coastguard WorkerEmail bug reports to [email protected].
128*6a54128fSAndroid Build Coastguard WorkerAutomake home page: https://www.gnu.org/software/automake/
129*6a54128fSAndroid Build Coastguard Worker"
130*6a54128fSAndroid Build Coastguard Worker
131*6a54128fSAndroid Build Coastguard Workerwhile test $# -ne 0; do
132*6a54128fSAndroid Build Coastguard Worker  case $1 in
133*6a54128fSAndroid Build Coastguard Worker    -c) ;;
134*6a54128fSAndroid Build Coastguard Worker
135*6a54128fSAndroid Build Coastguard Worker    -C) copy_on_change=true;;
136*6a54128fSAndroid Build Coastguard Worker
137*6a54128fSAndroid Build Coastguard Worker    -d) dir_arg=true;;
138*6a54128fSAndroid Build Coastguard Worker
139*6a54128fSAndroid Build Coastguard Worker    -g) chgrpcmd="$chgrpprog $2"
140*6a54128fSAndroid Build Coastguard Worker        shift;;
141*6a54128fSAndroid Build Coastguard Worker
142*6a54128fSAndroid Build Coastguard Worker    --help) echo "$usage"; exit $?;;
143*6a54128fSAndroid Build Coastguard Worker
144*6a54128fSAndroid Build Coastguard Worker    -m) mode=$2
145*6a54128fSAndroid Build Coastguard Worker        case $mode in
146*6a54128fSAndroid Build Coastguard Worker          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147*6a54128fSAndroid Build Coastguard Worker            echo "$0: invalid mode: $mode" >&2
148*6a54128fSAndroid Build Coastguard Worker            exit 1;;
149*6a54128fSAndroid Build Coastguard Worker        esac
150*6a54128fSAndroid Build Coastguard Worker        shift;;
151*6a54128fSAndroid Build Coastguard Worker
152*6a54128fSAndroid Build Coastguard Worker    -o) chowncmd="$chownprog $2"
153*6a54128fSAndroid Build Coastguard Worker        shift;;
154*6a54128fSAndroid Build Coastguard Worker
155*6a54128fSAndroid Build Coastguard Worker    -p) cpprog="$cpprog -p";;
156*6a54128fSAndroid Build Coastguard Worker
157*6a54128fSAndroid Build Coastguard Worker    -s) stripcmd=$stripprog;;
158*6a54128fSAndroid Build Coastguard Worker
159*6a54128fSAndroid Build Coastguard Worker    -S) backupsuffix="$2"
160*6a54128fSAndroid Build Coastguard Worker        shift;;
161*6a54128fSAndroid Build Coastguard Worker
162*6a54128fSAndroid Build Coastguard Worker    -t)
163*6a54128fSAndroid Build Coastguard Worker        is_target_a_directory=always
164*6a54128fSAndroid Build Coastguard Worker        dst_arg=$2
165*6a54128fSAndroid Build Coastguard Worker        # Protect names problematic for 'test' and other utilities.
166*6a54128fSAndroid Build Coastguard Worker        case $dst_arg in
167*6a54128fSAndroid Build Coastguard Worker          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168*6a54128fSAndroid Build Coastguard Worker        esac
169*6a54128fSAndroid Build Coastguard Worker        shift;;
170*6a54128fSAndroid Build Coastguard Worker
171*6a54128fSAndroid Build Coastguard Worker    -T) is_target_a_directory=never;;
172*6a54128fSAndroid Build Coastguard Worker
173*6a54128fSAndroid Build Coastguard Worker    --version) echo "$0 $scriptversion"; exit $?;;
174*6a54128fSAndroid Build Coastguard Worker
175*6a54128fSAndroid Build Coastguard Worker    --) shift
176*6a54128fSAndroid Build Coastguard Worker        break;;
177*6a54128fSAndroid Build Coastguard Worker
178*6a54128fSAndroid Build Coastguard Worker    -*) echo "$0: invalid option: $1" >&2
179*6a54128fSAndroid Build Coastguard Worker        exit 1;;
180*6a54128fSAndroid Build Coastguard Worker
181*6a54128fSAndroid Build Coastguard Worker    *)  break;;
182*6a54128fSAndroid Build Coastguard Worker  esac
183*6a54128fSAndroid Build Coastguard Worker  shift
184*6a54128fSAndroid Build Coastguard Workerdone
185*6a54128fSAndroid Build Coastguard Worker
186*6a54128fSAndroid Build Coastguard Worker# We allow the use of options -d and -T together, by making -d
187*6a54128fSAndroid Build Coastguard Worker# take the precedence; this is for compatibility with GNU install.
188*6a54128fSAndroid Build Coastguard Worker
189*6a54128fSAndroid Build Coastguard Workerif test -n "$dir_arg"; then
190*6a54128fSAndroid Build Coastguard Worker  if test -n "$dst_arg"; then
191*6a54128fSAndroid Build Coastguard Worker    echo "$0: target directory not allowed when installing a directory." >&2
192*6a54128fSAndroid Build Coastguard Worker    exit 1
193*6a54128fSAndroid Build Coastguard Worker  fi
194*6a54128fSAndroid Build Coastguard Workerfi
195*6a54128fSAndroid Build Coastguard Worker
196*6a54128fSAndroid Build Coastguard Workerif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197*6a54128fSAndroid Build Coastguard Worker  # When -d is used, all remaining arguments are directories to create.
198*6a54128fSAndroid Build Coastguard Worker  # When -t is used, the destination is already specified.
199*6a54128fSAndroid Build Coastguard Worker  # Otherwise, the last argument is the destination.  Remove it from $@.
200*6a54128fSAndroid Build Coastguard Worker  for arg
201*6a54128fSAndroid Build Coastguard Worker  do
202*6a54128fSAndroid Build Coastguard Worker    if test -n "$dst_arg"; then
203*6a54128fSAndroid Build Coastguard Worker      # $@ is not empty: it contains at least $arg.
204*6a54128fSAndroid Build Coastguard Worker      set fnord "$@" "$dst_arg"
205*6a54128fSAndroid Build Coastguard Worker      shift # fnord
206*6a54128fSAndroid Build Coastguard Worker    fi
207*6a54128fSAndroid Build Coastguard Worker    shift # arg
208*6a54128fSAndroid Build Coastguard Worker    dst_arg=$arg
209*6a54128fSAndroid Build Coastguard Worker    # Protect names problematic for 'test' and other utilities.
210*6a54128fSAndroid Build Coastguard Worker    case $dst_arg in
211*6a54128fSAndroid Build Coastguard Worker      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212*6a54128fSAndroid Build Coastguard Worker    esac
213*6a54128fSAndroid Build Coastguard Worker  done
214*6a54128fSAndroid Build Coastguard Workerfi
215*6a54128fSAndroid Build Coastguard Worker
216*6a54128fSAndroid Build Coastguard Workerif test $# -eq 0; then
217*6a54128fSAndroid Build Coastguard Worker  if test -z "$dir_arg"; then
218*6a54128fSAndroid Build Coastguard Worker    echo "$0: no input file specified." >&2
219*6a54128fSAndroid Build Coastguard Worker    exit 1
220*6a54128fSAndroid Build Coastguard Worker  fi
221*6a54128fSAndroid Build Coastguard Worker  # It's OK to call 'install-sh -d' without argument.
222*6a54128fSAndroid Build Coastguard Worker  # This can happen when creating conditional directories.
223*6a54128fSAndroid Build Coastguard Worker  exit 0
224*6a54128fSAndroid Build Coastguard Workerfi
225*6a54128fSAndroid Build Coastguard Worker
226*6a54128fSAndroid Build Coastguard Workerif test -z "$dir_arg"; then
227*6a54128fSAndroid Build Coastguard Worker  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228*6a54128fSAndroid Build Coastguard Worker    if test ! -d "$dst_arg"; then
229*6a54128fSAndroid Build Coastguard Worker      echo "$0: $dst_arg: Is not a directory." >&2
230*6a54128fSAndroid Build Coastguard Worker      exit 1
231*6a54128fSAndroid Build Coastguard Worker    fi
232*6a54128fSAndroid Build Coastguard Worker  fi
233*6a54128fSAndroid Build Coastguard Workerfi
234*6a54128fSAndroid Build Coastguard Worker
235*6a54128fSAndroid Build Coastguard Workerif test -z "$dir_arg"; then
236*6a54128fSAndroid Build Coastguard Worker  do_exit='(exit $ret); exit $ret'
237*6a54128fSAndroid Build Coastguard Worker  trap "ret=129; $do_exit" 1
238*6a54128fSAndroid Build Coastguard Worker  trap "ret=130; $do_exit" 2
239*6a54128fSAndroid Build Coastguard Worker  trap "ret=141; $do_exit" 13
240*6a54128fSAndroid Build Coastguard Worker  trap "ret=143; $do_exit" 15
241*6a54128fSAndroid Build Coastguard Worker
242*6a54128fSAndroid Build Coastguard Worker  # Set umask so as not to create temps with too-generous modes.
243*6a54128fSAndroid Build Coastguard Worker  # However, 'strip' requires both read and write access to temps.
244*6a54128fSAndroid Build Coastguard Worker  case $mode in
245*6a54128fSAndroid Build Coastguard Worker    # Optimize common cases.
246*6a54128fSAndroid Build Coastguard Worker    *644) cp_umask=133;;
247*6a54128fSAndroid Build Coastguard Worker    *755) cp_umask=22;;
248*6a54128fSAndroid Build Coastguard Worker
249*6a54128fSAndroid Build Coastguard Worker    *[0-7])
250*6a54128fSAndroid Build Coastguard Worker      if test -z "$stripcmd"; then
251*6a54128fSAndroid Build Coastguard Worker        u_plus_rw=
252*6a54128fSAndroid Build Coastguard Worker      else
253*6a54128fSAndroid Build Coastguard Worker        u_plus_rw='% 200'
254*6a54128fSAndroid Build Coastguard Worker      fi
255*6a54128fSAndroid Build Coastguard Worker      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256*6a54128fSAndroid Build Coastguard Worker    *)
257*6a54128fSAndroid Build Coastguard Worker      if test -z "$stripcmd"; then
258*6a54128fSAndroid Build Coastguard Worker        u_plus_rw=
259*6a54128fSAndroid Build Coastguard Worker      else
260*6a54128fSAndroid Build Coastguard Worker        u_plus_rw=,u+rw
261*6a54128fSAndroid Build Coastguard Worker      fi
262*6a54128fSAndroid Build Coastguard Worker      cp_umask=$mode$u_plus_rw;;
263*6a54128fSAndroid Build Coastguard Worker  esac
264*6a54128fSAndroid Build Coastguard Workerfi
265*6a54128fSAndroid Build Coastguard Worker
266*6a54128fSAndroid Build Coastguard Workerfor src
267*6a54128fSAndroid Build Coastguard Workerdo
268*6a54128fSAndroid Build Coastguard Worker  # Protect names problematic for 'test' and other utilities.
269*6a54128fSAndroid Build Coastguard Worker  case $src in
270*6a54128fSAndroid Build Coastguard Worker    -* | [=\(\)!]) src=./$src;;
271*6a54128fSAndroid Build Coastguard Worker  esac
272*6a54128fSAndroid Build Coastguard Worker
273*6a54128fSAndroid Build Coastguard Worker  if test -n "$dir_arg"; then
274*6a54128fSAndroid Build Coastguard Worker    dst=$src
275*6a54128fSAndroid Build Coastguard Worker    dstdir=$dst
276*6a54128fSAndroid Build Coastguard Worker    test -d "$dstdir"
277*6a54128fSAndroid Build Coastguard Worker    dstdir_status=$?
278*6a54128fSAndroid Build Coastguard Worker    # Don't chown directories that already exist.
279*6a54128fSAndroid Build Coastguard Worker    if test $dstdir_status = 0; then
280*6a54128fSAndroid Build Coastguard Worker      chowncmd=""
281*6a54128fSAndroid Build Coastguard Worker    fi
282*6a54128fSAndroid Build Coastguard Worker  else
283*6a54128fSAndroid Build Coastguard Worker
284*6a54128fSAndroid Build Coastguard Worker    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285*6a54128fSAndroid Build Coastguard Worker    # might cause directories to be created, which would be especially bad
286*6a54128fSAndroid Build Coastguard Worker    # if $src (and thus $dsttmp) contains '*'.
287*6a54128fSAndroid Build Coastguard Worker    if test ! -f "$src" && test ! -d "$src"; then
288*6a54128fSAndroid Build Coastguard Worker      echo "$0: $src does not exist." >&2
289*6a54128fSAndroid Build Coastguard Worker      exit 1
290*6a54128fSAndroid Build Coastguard Worker    fi
291*6a54128fSAndroid Build Coastguard Worker
292*6a54128fSAndroid Build Coastguard Worker    if test -z "$dst_arg"; then
293*6a54128fSAndroid Build Coastguard Worker      echo "$0: no destination specified." >&2
294*6a54128fSAndroid Build Coastguard Worker      exit 1
295*6a54128fSAndroid Build Coastguard Worker    fi
296*6a54128fSAndroid Build Coastguard Worker    dst=$dst_arg
297*6a54128fSAndroid Build Coastguard Worker
298*6a54128fSAndroid Build Coastguard Worker    # If destination is a directory, append the input filename.
299*6a54128fSAndroid Build Coastguard Worker    if test -d "$dst"; then
300*6a54128fSAndroid Build Coastguard Worker      if test "$is_target_a_directory" = never; then
301*6a54128fSAndroid Build Coastguard Worker        echo "$0: $dst_arg: Is a directory" >&2
302*6a54128fSAndroid Build Coastguard Worker        exit 1
303*6a54128fSAndroid Build Coastguard Worker      fi
304*6a54128fSAndroid Build Coastguard Worker      dstdir=$dst
305*6a54128fSAndroid Build Coastguard Worker      dstbase=`basename "$src"`
306*6a54128fSAndroid Build Coastguard Worker      case $dst in
307*6a54128fSAndroid Build Coastguard Worker	*/) dst=$dst$dstbase;;
308*6a54128fSAndroid Build Coastguard Worker	*)  dst=$dst/$dstbase;;
309*6a54128fSAndroid Build Coastguard Worker      esac
310*6a54128fSAndroid Build Coastguard Worker      dstdir_status=0
311*6a54128fSAndroid Build Coastguard Worker    else
312*6a54128fSAndroid Build Coastguard Worker      dstdir=`dirname "$dst"`
313*6a54128fSAndroid Build Coastguard Worker      test -d "$dstdir"
314*6a54128fSAndroid Build Coastguard Worker      dstdir_status=$?
315*6a54128fSAndroid Build Coastguard Worker    fi
316*6a54128fSAndroid Build Coastguard Worker  fi
317*6a54128fSAndroid Build Coastguard Worker
318*6a54128fSAndroid Build Coastguard Worker  case $dstdir in
319*6a54128fSAndroid Build Coastguard Worker    */) dstdirslash=$dstdir;;
320*6a54128fSAndroid Build Coastguard Worker    *)  dstdirslash=$dstdir/;;
321*6a54128fSAndroid Build Coastguard Worker  esac
322*6a54128fSAndroid Build Coastguard Worker
323*6a54128fSAndroid Build Coastguard Worker  obsolete_mkdir_used=false
324*6a54128fSAndroid Build Coastguard Worker
325*6a54128fSAndroid Build Coastguard Worker  if test $dstdir_status != 0; then
326*6a54128fSAndroid Build Coastguard Worker    case $posix_mkdir in
327*6a54128fSAndroid Build Coastguard Worker      '')
328*6a54128fSAndroid Build Coastguard Worker        # With -d, create the new directory with the user-specified mode.
329*6a54128fSAndroid Build Coastguard Worker        # Otherwise, rely on $mkdir_umask.
330*6a54128fSAndroid Build Coastguard Worker        if test -n "$dir_arg"; then
331*6a54128fSAndroid Build Coastguard Worker          mkdir_mode=-m$mode
332*6a54128fSAndroid Build Coastguard Worker        else
333*6a54128fSAndroid Build Coastguard Worker          mkdir_mode=
334*6a54128fSAndroid Build Coastguard Worker        fi
335*6a54128fSAndroid Build Coastguard Worker
336*6a54128fSAndroid Build Coastguard Worker        posix_mkdir=false
337*6a54128fSAndroid Build Coastguard Worker	# The $RANDOM variable is not portable (e.g., dash).  Use it
338*6a54128fSAndroid Build Coastguard Worker	# here however when possible just to lower collision chance.
339*6a54128fSAndroid Build Coastguard Worker	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340*6a54128fSAndroid Build Coastguard Worker
341*6a54128fSAndroid Build Coastguard Worker	trap '
342*6a54128fSAndroid Build Coastguard Worker	  ret=$?
343*6a54128fSAndroid Build Coastguard Worker	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344*6a54128fSAndroid Build Coastguard Worker	  exit $ret
345*6a54128fSAndroid Build Coastguard Worker	' 0
346*6a54128fSAndroid Build Coastguard Worker
347*6a54128fSAndroid Build Coastguard Worker	# Because "mkdir -p" follows existing symlinks and we likely work
348*6a54128fSAndroid Build Coastguard Worker	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349*6a54128fSAndroid Build Coastguard Worker	# directory is successfully created first before we actually test
350*6a54128fSAndroid Build Coastguard Worker	# 'mkdir -p'.
351*6a54128fSAndroid Build Coastguard Worker	if (umask $mkdir_umask &&
352*6a54128fSAndroid Build Coastguard Worker	    $mkdirprog $mkdir_mode "$tmpdir" &&
353*6a54128fSAndroid Build Coastguard Worker	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354*6a54128fSAndroid Build Coastguard Worker	then
355*6a54128fSAndroid Build Coastguard Worker	  if test -z "$dir_arg" || {
356*6a54128fSAndroid Build Coastguard Worker	       # Check for POSIX incompatibilities with -m.
357*6a54128fSAndroid Build Coastguard Worker	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358*6a54128fSAndroid Build Coastguard Worker	       # other-writable bit of parent directory when it shouldn't.
359*6a54128fSAndroid Build Coastguard Worker	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360*6a54128fSAndroid Build Coastguard Worker	       test_tmpdir="$tmpdir/a"
361*6a54128fSAndroid Build Coastguard Worker	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362*6a54128fSAndroid Build Coastguard Worker	       case $ls_ld_tmpdir in
363*6a54128fSAndroid Build Coastguard Worker		 d????-?r-*) different_mode=700;;
364*6a54128fSAndroid Build Coastguard Worker		 d????-?--*) different_mode=755;;
365*6a54128fSAndroid Build Coastguard Worker		 *) false;;
366*6a54128fSAndroid Build Coastguard Worker	       esac &&
367*6a54128fSAndroid Build Coastguard Worker	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368*6a54128fSAndroid Build Coastguard Worker		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369*6a54128fSAndroid Build Coastguard Worker		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370*6a54128fSAndroid Build Coastguard Worker	       }
371*6a54128fSAndroid Build Coastguard Worker	     }
372*6a54128fSAndroid Build Coastguard Worker	  then posix_mkdir=:
373*6a54128fSAndroid Build Coastguard Worker	  fi
374*6a54128fSAndroid Build Coastguard Worker	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375*6a54128fSAndroid Build Coastguard Worker	else
376*6a54128fSAndroid Build Coastguard Worker	  # Remove any dirs left behind by ancient mkdir implementations.
377*6a54128fSAndroid Build Coastguard Worker	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378*6a54128fSAndroid Build Coastguard Worker	fi
379*6a54128fSAndroid Build Coastguard Worker	trap '' 0;;
380*6a54128fSAndroid Build Coastguard Worker    esac
381*6a54128fSAndroid Build Coastguard Worker
382*6a54128fSAndroid Build Coastguard Worker    if
383*6a54128fSAndroid Build Coastguard Worker      $posix_mkdir && (
384*6a54128fSAndroid Build Coastguard Worker        umask $mkdir_umask &&
385*6a54128fSAndroid Build Coastguard Worker        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386*6a54128fSAndroid Build Coastguard Worker      )
387*6a54128fSAndroid Build Coastguard Worker    then :
388*6a54128fSAndroid Build Coastguard Worker    else
389*6a54128fSAndroid Build Coastguard Worker
390*6a54128fSAndroid Build Coastguard Worker      # mkdir does not conform to POSIX,
391*6a54128fSAndroid Build Coastguard Worker      # or it failed possibly due to a race condition.  Create the
392*6a54128fSAndroid Build Coastguard Worker      # directory the slow way, step by step, checking for races as we go.
393*6a54128fSAndroid Build Coastguard Worker
394*6a54128fSAndroid Build Coastguard Worker      case $dstdir in
395*6a54128fSAndroid Build Coastguard Worker        /*) prefix='/';;
396*6a54128fSAndroid Build Coastguard Worker        [-=\(\)!]*) prefix='./';;
397*6a54128fSAndroid Build Coastguard Worker        *)  prefix='';;
398*6a54128fSAndroid Build Coastguard Worker      esac
399*6a54128fSAndroid Build Coastguard Worker
400*6a54128fSAndroid Build Coastguard Worker      oIFS=$IFS
401*6a54128fSAndroid Build Coastguard Worker      IFS=/
402*6a54128fSAndroid Build Coastguard Worker      set -f
403*6a54128fSAndroid Build Coastguard Worker      set fnord $dstdir
404*6a54128fSAndroid Build Coastguard Worker      shift
405*6a54128fSAndroid Build Coastguard Worker      set +f
406*6a54128fSAndroid Build Coastguard Worker      IFS=$oIFS
407*6a54128fSAndroid Build Coastguard Worker
408*6a54128fSAndroid Build Coastguard Worker      prefixes=
409*6a54128fSAndroid Build Coastguard Worker
410*6a54128fSAndroid Build Coastguard Worker      for d
411*6a54128fSAndroid Build Coastguard Worker      do
412*6a54128fSAndroid Build Coastguard Worker        test X"$d" = X && continue
413*6a54128fSAndroid Build Coastguard Worker
414*6a54128fSAndroid Build Coastguard Worker        prefix=$prefix$d
415*6a54128fSAndroid Build Coastguard Worker        if test -d "$prefix"; then
416*6a54128fSAndroid Build Coastguard Worker          prefixes=
417*6a54128fSAndroid Build Coastguard Worker        else
418*6a54128fSAndroid Build Coastguard Worker          if $posix_mkdir; then
419*6a54128fSAndroid Build Coastguard Worker            (umask $mkdir_umask &&
420*6a54128fSAndroid Build Coastguard Worker             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421*6a54128fSAndroid Build Coastguard Worker            # Don't fail if two instances are running concurrently.
422*6a54128fSAndroid Build Coastguard Worker            test -d "$prefix" || exit 1
423*6a54128fSAndroid Build Coastguard Worker          else
424*6a54128fSAndroid Build Coastguard Worker            case $prefix in
425*6a54128fSAndroid Build Coastguard Worker              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426*6a54128fSAndroid Build Coastguard Worker              *) qprefix=$prefix;;
427*6a54128fSAndroid Build Coastguard Worker            esac
428*6a54128fSAndroid Build Coastguard Worker            prefixes="$prefixes '$qprefix'"
429*6a54128fSAndroid Build Coastguard Worker          fi
430*6a54128fSAndroid Build Coastguard Worker        fi
431*6a54128fSAndroid Build Coastguard Worker        prefix=$prefix/
432*6a54128fSAndroid Build Coastguard Worker      done
433*6a54128fSAndroid Build Coastguard Worker
434*6a54128fSAndroid Build Coastguard Worker      if test -n "$prefixes"; then
435*6a54128fSAndroid Build Coastguard Worker        # Don't fail if two instances are running concurrently.
436*6a54128fSAndroid Build Coastguard Worker        (umask $mkdir_umask &&
437*6a54128fSAndroid Build Coastguard Worker         eval "\$doit_exec \$mkdirprog $prefixes") ||
438*6a54128fSAndroid Build Coastguard Worker          test -d "$dstdir" || exit 1
439*6a54128fSAndroid Build Coastguard Worker        obsolete_mkdir_used=true
440*6a54128fSAndroid Build Coastguard Worker      fi
441*6a54128fSAndroid Build Coastguard Worker    fi
442*6a54128fSAndroid Build Coastguard Worker  fi
443*6a54128fSAndroid Build Coastguard Worker
444*6a54128fSAndroid Build Coastguard Worker  if test -n "$dir_arg"; then
445*6a54128fSAndroid Build Coastguard Worker    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446*6a54128fSAndroid Build Coastguard Worker    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447*6a54128fSAndroid Build Coastguard Worker    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448*6a54128fSAndroid Build Coastguard Worker      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449*6a54128fSAndroid Build Coastguard Worker  else
450*6a54128fSAndroid Build Coastguard Worker
451*6a54128fSAndroid Build Coastguard Worker    # Make a couple of temp file names in the proper directory.
452*6a54128fSAndroid Build Coastguard Worker    dsttmp=${dstdirslash}_inst.$$_
453*6a54128fSAndroid Build Coastguard Worker    rmtmp=${dstdirslash}_rm.$$_
454*6a54128fSAndroid Build Coastguard Worker
455*6a54128fSAndroid Build Coastguard Worker    # Trap to clean up those temp files at exit.
456*6a54128fSAndroid Build Coastguard Worker    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457*6a54128fSAndroid Build Coastguard Worker
458*6a54128fSAndroid Build Coastguard Worker    # Copy the file name to the temp name.
459*6a54128fSAndroid Build Coastguard Worker    (umask $cp_umask &&
460*6a54128fSAndroid Build Coastguard Worker     { test -z "$stripcmd" || {
461*6a54128fSAndroid Build Coastguard Worker	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462*6a54128fSAndroid Build Coastguard Worker	 # which would cause strip to fail.
463*6a54128fSAndroid Build Coastguard Worker	 if test -z "$doit"; then
464*6a54128fSAndroid Build Coastguard Worker	   : >"$dsttmp" # No need to fork-exec 'touch'.
465*6a54128fSAndroid Build Coastguard Worker	 else
466*6a54128fSAndroid Build Coastguard Worker	   $doit touch "$dsttmp"
467*6a54128fSAndroid Build Coastguard Worker	 fi
468*6a54128fSAndroid Build Coastguard Worker       }
469*6a54128fSAndroid Build Coastguard Worker     } &&
470*6a54128fSAndroid Build Coastguard Worker     $doit_exec $cpprog "$src" "$dsttmp") &&
471*6a54128fSAndroid Build Coastguard Worker
472*6a54128fSAndroid Build Coastguard Worker    # and set any options; do chmod last to preserve setuid bits.
473*6a54128fSAndroid Build Coastguard Worker    #
474*6a54128fSAndroid Build Coastguard Worker    # If any of these fail, we abort the whole thing.  If we want to
475*6a54128fSAndroid Build Coastguard Worker    # ignore errors from any of these, just make sure not to ignore
476*6a54128fSAndroid Build Coastguard Worker    # errors from the above "$doit $cpprog $src $dsttmp" command.
477*6a54128fSAndroid Build Coastguard Worker    #
478*6a54128fSAndroid Build Coastguard Worker    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479*6a54128fSAndroid Build Coastguard Worker    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480*6a54128fSAndroid Build Coastguard Worker    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481*6a54128fSAndroid Build Coastguard Worker    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482*6a54128fSAndroid Build Coastguard Worker
483*6a54128fSAndroid Build Coastguard Worker    # If -C, don't bother to copy if it wouldn't change the file.
484*6a54128fSAndroid Build Coastguard Worker    if $copy_on_change &&
485*6a54128fSAndroid Build Coastguard Worker       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486*6a54128fSAndroid Build Coastguard Worker       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487*6a54128fSAndroid Build Coastguard Worker       set -f &&
488*6a54128fSAndroid Build Coastguard Worker       set X $old && old=:$2:$4:$5:$6 &&
489*6a54128fSAndroid Build Coastguard Worker       set X $new && new=:$2:$4:$5:$6 &&
490*6a54128fSAndroid Build Coastguard Worker       set +f &&
491*6a54128fSAndroid Build Coastguard Worker       test "$old" = "$new" &&
492*6a54128fSAndroid Build Coastguard Worker       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493*6a54128fSAndroid Build Coastguard Worker    then
494*6a54128fSAndroid Build Coastguard Worker      rm -f "$dsttmp"
495*6a54128fSAndroid Build Coastguard Worker    else
496*6a54128fSAndroid Build Coastguard Worker      # If $backupsuffix is set, and the file being installed
497*6a54128fSAndroid Build Coastguard Worker      # already exists, attempt a backup.  Don't worry if it fails,
498*6a54128fSAndroid Build Coastguard Worker      # e.g., if mv doesn't support -f.
499*6a54128fSAndroid Build Coastguard Worker      if test -n "$backupsuffix" && test -f "$dst"; then
500*6a54128fSAndroid Build Coastguard Worker        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501*6a54128fSAndroid Build Coastguard Worker      fi
502*6a54128fSAndroid Build Coastguard Worker
503*6a54128fSAndroid Build Coastguard Worker      # Rename the file to the real destination.
504*6a54128fSAndroid Build Coastguard Worker      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505*6a54128fSAndroid Build Coastguard Worker
506*6a54128fSAndroid Build Coastguard Worker      # The rename failed, perhaps because mv can't rename something else
507*6a54128fSAndroid Build Coastguard Worker      # to itself, or perhaps because mv is so ancient that it does not
508*6a54128fSAndroid Build Coastguard Worker      # support -f.
509*6a54128fSAndroid Build Coastguard Worker      {
510*6a54128fSAndroid Build Coastguard Worker        # Now remove or move aside any old file at destination location.
511*6a54128fSAndroid Build Coastguard Worker        # We try this two ways since rm can't unlink itself on some
512*6a54128fSAndroid Build Coastguard Worker        # systems and the destination file might be busy for other
513*6a54128fSAndroid Build Coastguard Worker        # reasons.  In this case, the final cleanup might fail but the new
514*6a54128fSAndroid Build Coastguard Worker        # file should still install successfully.
515*6a54128fSAndroid Build Coastguard Worker        {
516*6a54128fSAndroid Build Coastguard Worker          test ! -f "$dst" ||
517*6a54128fSAndroid Build Coastguard Worker          $doit $rmcmd "$dst" 2>/dev/null ||
518*6a54128fSAndroid Build Coastguard Worker          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519*6a54128fSAndroid Build Coastguard Worker            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520*6a54128fSAndroid Build Coastguard Worker          } ||
521*6a54128fSAndroid Build Coastguard Worker          { echo "$0: cannot unlink or rename $dst" >&2
522*6a54128fSAndroid Build Coastguard Worker            (exit 1); exit 1
523*6a54128fSAndroid Build Coastguard Worker          }
524*6a54128fSAndroid Build Coastguard Worker        } &&
525*6a54128fSAndroid Build Coastguard Worker
526*6a54128fSAndroid Build Coastguard Worker        # Now rename the file to the real destination.
527*6a54128fSAndroid Build Coastguard Worker        $doit $mvcmd "$dsttmp" "$dst"
528*6a54128fSAndroid Build Coastguard Worker      }
529*6a54128fSAndroid Build Coastguard Worker    fi || exit 1
530*6a54128fSAndroid Build Coastguard Worker
531*6a54128fSAndroid Build Coastguard Worker    trap '' 0
532*6a54128fSAndroid Build Coastguard Worker  fi
533*6a54128fSAndroid Build Coastguard Workerdone
534*6a54128fSAndroid Build Coastguard Worker
535*6a54128fSAndroid Build Coastguard Worker# Local variables:
536*6a54128fSAndroid Build Coastguard Worker# eval: (add-hook 'before-save-hook 'time-stamp)
537*6a54128fSAndroid Build Coastguard Worker# time-stamp-start: "scriptversion="
538*6a54128fSAndroid Build Coastguard Worker# time-stamp-format: "%:y-%02m-%02d.%02H"
539*6a54128fSAndroid Build Coastguard Worker# time-stamp-time-zone: "UTC0"
540*6a54128fSAndroid Build Coastguard Worker# time-stamp-end: "; # UTC"
541*6a54128fSAndroid Build Coastguard Worker# End:
542