RHBZ 1403095, RHBZ 1411720: Archive interp mode performance:
[pcp.git] / install-sh
blobac6ff0bcc335e4404e2d310a2198242e299e627d
1 #!/bin/sh
3 # Copyright (c) 2013 Red Hat.
4 # Copyright (c) 2004 Silicon Graphics, Inc. All Rights Reserved.
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; either version 2 of the License, or (at your
9 # option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 # for more details.
16 # This script emulates bsd install and also recognises
17 # two environment variables, with the following semanitcs :-
19 # $DIST_MANIFEST - if set, the name of the file to append manifest
20 # information in the following format:
21 # File : f mode owner group src target
22 # Diretory: d mode owner group target
23 # Symlink : l linkval target
25 # $DIST_ROOT - if set, prepend to target
27 # The sematics of all combinations of these two variables
28 # are as follows:
30 # $DIST_MANIFEST? $DIST_ROOT? | Copy? Append Manifest?
31 # -----------------------------+--------------------------
32 # not set not set | yes no
33 # not set set | yes no
34 # set not set | no yes
35 # set set | yes yes
37 _usage() {
38 echo "Usage: $prog [-o owner] [-g group] [-m mode] -d directory"
39 echo "or $prog [-o owner] [-g group] [-m mode] file directory/file"
40 echo "or $prog [-o owner] [-g group] [-m mode] file file [file ...] directory"
41 echo "or $prog -S file target (creates \"target\" symlink)"
42 echo ""
43 echo "The \$DIST_MANIFEST and \$DIST_ROOT environment variables affect the"
44 echo "behaviour of this command - see comments in the script."
45 echo ""
46 exit 1
49 _chown ()
51 _st=255
52 if [ $# -eq 3 ] ; then
53 chown $1:$2 $3
54 _st=$?
55 if [ $_st -ne 0 ] ; then
56 if [ $REAL_UID != '0' ] ; then
57 if [ ! -f $DIST_ROOT/.chown.quiet ] ; then
58 echo '==============================================='
59 echo Ownership of files under ${DIST_ROOT:-/}
60 echo cannot be changed
61 echo '==============================================='
62 if [ -n "$DIST_ROOT" ] ; then
63 touch $DIST_ROOT/.chown.quiet
66 _st=0
67 fi
71 return $_st
75 _manifest ()
77 echo $* | sed -e 's/\/\//\//g' >>${DIST_MANIFEST:-/dev/null}
80 ID=id
81 # Test to use the functional id on Solaris
82 test -f /usr/xpg4/bin/id && ID=/usr/xpg4/bin/id
84 prog=`basename $0`
85 HERE=`pwd`
86 dflag=false
87 Sflag=false
88 DIRMODE=755
89 FILEMODE=644
90 OWNER=`$ID -u 2>/dev/null`
91 GROUP=`$ID -g 2>/dev/null`
92 if [ -z "$OWNER" -o -z "$GROUP" ]
93 then
94 # do it the hardway
96 OWNER=`$ID | sed -e 's/.*uid=[^(]*(//' -e 's/).*//'`
97 GROUP=`$ID | sed -e 's/.*gid=[^(]*(//' -e 's/).*//'`
99 REAL_UID=$OWNER
101 # default is to install and don't append manifest
102 INSTALL=true
103 MANIFEST=:
105 [ -n "$DIST_MANIFEST" -a -z "$DIST_ROOT" ] && INSTALL=false
106 [ -n "$DIST_MANIFEST" ] && MANIFEST="_manifest"
108 [ $# -eq 0 ] && _usage
110 if $INSTALL
111 then
112 RM=rm; CP=cp; LN=ln; MKDIR=mkdir; CHMOD=chmod; CHOWN=_chown
113 else
114 RM=true; CP=true; LN=true; MKDIR=true; CHMOD=true; CHOWN=true
118 test $REAL_UID -ne 0 && CHOWN=true
119 test "$NO_CHOWN" = "true" && CHOWN=true
121 while getopts "d:g:m:o:S:" c $*
123 case $c in
125 dir=`echo $DIST_ROOT/$OPTARG | sed -e 's;//*;/;g'`
126 dflag=true
129 GROUP=$OPTARG
132 DIRMODE=`expr $OPTARG`
133 FILEMODE=$DIRMODE
136 OWNER=$OPTARG
139 symlink=$OPTARG
140 Sflag=true
143 _usage
145 esac
146 done
148 shift `expr $OPTIND - 1`
150 status=0
151 if $dflag
152 then
154 # first usage
156 $MKDIR -p $dir
157 status=$?
158 if [ $status -eq 0 ]
159 then
160 $CHMOD $DIRMODE $dir
161 status=$?
163 if [ $status -eq 0 ]
164 then
165 $CHOWN $OWNER $GROUP $dir
166 status=$?
168 $MANIFEST d $DIRMODE $OWNER $GROUP `echo "$dir" | sed -e "s;^$DIST_ROOT;;"`
169 elif $Sflag
170 then
172 # fourth usage (symlink)
174 if [ $# -ne 1 ]
175 then
176 _usage
177 else
178 target=`echo $DIST_ROOT/$1 | sed -e 's;//*;/;g'`
180 $RM -f $target
181 dir=`dirname $target | sed -e 's;//*;/;g'`
182 [ ! -d $dir ] && mkdir -p $dir
183 $LN -s -f $symlink $target
184 status=$?
185 $MANIFEST l $symlink `echo "$target" | sed -e "s;^$DIST_ROOT;;"`
186 else
187 list=""
188 dir=""
189 if [ $# -eq 2 ]
190 then
192 # second usage: install file dir/name
193 # target 'name' is not necessarily the same as 'file'
194 # (Note: install file dir is ambiguous and not supported
196 f=$1
197 dir=$DIST_ROOT/`dirname $2`
198 dir=`echo $dir | sed -e 's;//*;/;g'`
199 t_dir=`echo "$dir" | sed -e "s;^$DIST_ROOT;;" | sed -e 's;//*;/;g'`
200 destfile=`basename $2`
201 if [ -f $f -a -f $f.exe ]
202 then
203 # Windows hack -- executables present as foo and foo.exe,
204 # the foo one is visible to test(1) and ls(1), but not to
205 # cat(1)/cp(1) ... need to $RM/$CP the foo.exe one
207 if cat $f >/dev/null 2>&1
208 then
210 else
211 f=$f.exe
212 destfile=$destfile.exe
215 [ ! -d $dir ] && mkdir -p $dir
216 if [ -d $dir/$destfile ]
217 then
218 echo ERROR: unsupported install usage
219 _usage
220 exit 1
221 else
222 $RM -f $dir/$destfile
224 $CP $f $dir/$destfile
225 status=$?
227 if [ $status -eq 0 ]
228 then
229 [ ! -d $dir/$destfile ] && $CHMOD $FILEMODE $dir/$destfile
230 status=$?
231 if [ $status -eq 0 ]
232 then
233 $CHOWN $OWNER $GROUP $dir/$destfile
234 status=$?
236 $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$f $t_dir/$destfile
238 [ $status -ne 0 ] && break
240 else
242 # third usage: install file file [file ...] directory
245 while [ $# -gt 0 ]
247 if [ $# -gt 1 ]
248 then
249 list="$list $1"
250 else
251 dir=`echo $DIST_ROOT/$1 | sed -e 's;//*;/;g'`
253 shift
254 done
256 # At this stage, $list is the list of files to install
257 # in $dir. We create $dir if necessary
259 # t_dir is for the manifest, i.e. without $DIST_ROOT prefix
260 t_dir=`echo "$dir" | sed -e "s;^$DIST_ROOT;;"`
262 for f in $list
264 if [ -f $f -a -f $f.exe ]
265 then
266 # Windows hack -- executables present as foo and foo.exe,
267 # the foo one is visible to test(1) and ls(1), but not to
268 # cat(1)/cp(1) ... need to $RM/$CP the foo.exe one
270 if cat $f >/dev/null 2>&1
271 then
273 else
274 f=$f.exe
278 [ ! -d $dir ] && mkdir -p $dir
279 $RM -f $dir/$f
280 $CP $f $dir/$f
281 status=$?
283 if [ $status -eq 0 ]
284 then
285 $CHMOD $FILEMODE $dir/$f
286 status=$?
287 if [ $status -eq 0 ]
288 then
289 $CHOWN $OWNER $GROUP $dir/$f
290 status=$?
292 $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$f $t_dir/$f
294 [ $status -ne 0 ] && break
295 done
299 exit $status