ext4/306: Add -b blocksize parameter too to avoid failure with DAX config
[xfstests-dev.git] / include / install-sh
1 #! /bin/bash
2 #
3 # SPDX-License-Identifier: GPL-2.0
4 # Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
5 #
6 # This script emulates bsd install and also recognises
7 # two environment variables, with the following semantics :-
8 #
9 # $DIST_MANIFEST - if set, the name of the file to append manifest
10 #                  information in the following format:
11 #                  File     :  f mode owner group src target
12 #                  Directory:  d mode owner group target
13 #                  Symlink  :  l linkval target
14 #
15 # $DIST_ROOT     - if set, prepend to target
16 #
17 # The sematics of all combinations of these two variables
18 # are as follows:
19 #
20 # $DIST_MANIFEST?  $DIST_ROOT? |   Copy?  Append Manifest?
21 # -----------------------------+--------------------------
22 #       not set       not set  |    yes        no
23 #       not set       set      |    yes        no
24 #       set           not set  |    no         yes
25 #       set           set      |    yes        yes
26 #
27 _usage() {
28     echo "Usage: $prog [-o owner] [-g group] [-m mode] -d directory"
29     echo "or     $prog [-D] [-o owner] [-g group] [-m mode] file directory/file"
30     echo "or     $prog [-o owner] [-g group] [-m mode] file [file ...] directory"
31     echo "or     $prog -S file target  (creates \"target\" symlink)"
32     echo "or     $prog -T lt_arg [-o owner] [-g group] [-m mode] libtool.lai directory"
33     echo ""
34     echo "The \$DIST_MANIFEST and \$DIST_ROOT environment variables affect the"
35     echo "behaviour of this command - see comments in the script."
36     echo "The -D flag is only available for the second usage, and causes"
37     echo "the target directory to be created before installing the file."
38     echo ""
39     exit 1
40 }
41
42 _chown ()
43 {
44     _st=255
45     if [ $# -eq 3 ] ; then
46         chown $1:$2 $3
47         _st=$?
48         if [ $_st -ne 0 ] ; then
49             if [ $REAL_UID != '0' ] ; then
50                 if [ ! -f $DIST_ROOT/.chown.quiet ] ; then
51                     echo '==============================================='
52                     echo Ownership of files under ${DIST_ROOT:-/}
53                     echo cannot be changed
54                     echo '==============================================='
55                     if [ -n "$DIST_ROOT" ] ; then
56                         touch $DIST_ROOT/.chown.quiet
57                     fi
58                 fi
59                _st=0
60             fi
61         fi
62     fi
63
64     return $_st
65 }
66
67
68 _manifest ()
69 {
70     echo $* | sed -e 's/\/\//\//g' >>${DIST_MANIFEST:-/dev/null}
71 }
72
73 prog=`basename $0`
74 HERE=`pwd`
75 dflag=false
76 Dflag=false
77 Sflag=false
78 Tflag=false
79 DIRMODE=755
80 FILEMODE=644
81 OWNER=`id -u`
82 GROUP=`id -g`
83 REAL_UID=$OWNER
84
85 # default is to install and don't append manifest
86 INSTALL=true
87 MANIFEST=:
88
89 : ${DIST_ROOT:=${DESTDIR}}
90
91 [ -n "$DIST_MANIFEST" -a -z "$DIST_ROOT" ] && INSTALL=false
92 [ -n "$DIST_MANIFEST" ] && MANIFEST="_manifest"
93
94 [ $# -eq 0 ] && _usage
95
96 if $INSTALL
97 then
98     CP=cp; LN=ln; MKDIR=mkdir; CHMOD=chmod; CHOWN=_chown
99 else
100     CP=true; LN=true; MKDIR=true; CHMOD=true; CHOWN=true
101 fi
102
103 [ -n "$DIST_ROOT" -a $REAL_UID -ne 0 ] && CHOWN=true
104
105 while getopts "Dcm:d:S:o:g:T:" c $*
106 do
107    case $c in
108    c)
109         ;;
110    g)
111         GROUP=$OPTARG
112         ;;
113    o)
114         OWNER=$OPTARG
115         ;;
116    m)
117         DIRMODE=`expr $OPTARG`
118         FILEMODE=$DIRMODE
119         ;;
120    D)
121         Dflag=true
122         ;;
123    S)
124         symlink=$OPTARG
125         Sflag=true
126         ;;
127    d)
128         dir=$DIST_ROOT/$OPTARG
129         dflag=true
130         ;;
131    T)
132         lt_install=$OPTARG
133         Tflag=true
134         ;;
135    *)
136         _usage
137         ;;
138    esac
139 done
140
141 shift `expr $OPTIND - 1`
142
143 status=0
144 if $dflag
145 then
146     #
147     # first usage
148     #
149     $MKDIR -p $dir
150     status=$?
151     if [ $status -eq 0 ]
152     then
153         $CHMOD $DIRMODE $dir
154         status=$?
155     fi
156     if [ $status -eq 0 ]
157     then
158         $CHOWN $OWNER $GROUP $dir
159         status=$?
160     fi
161     $MANIFEST d $DIRMODE $OWNER $GROUP ${dir#$DIST_ROOT}
162 elif $Sflag
163 then
164     #
165     # fourth usage (symlink)
166     #
167     if [ $# -ne 1 ]
168     then
169         _usage
170     else
171         target=$DIST_ROOT/$1
172     fi
173     $LN -s -f $symlink $target
174     status=$?
175     $MANIFEST l $symlink ${target#$DIST_ROOT}
176 elif $Tflag
177 then
178     #
179     # -T (install libs built by libtool)
180     #
181     if [ $# -ne 2 ]
182     then
183         _usage
184     else
185         libtool_lai=$1
186         # source the libtool variables
187         if [ ! -f $libtool_lai ]
188         then
189                 echo "$prog: Unable to find libtool library file $libtool_lai"
190                 exit 2
191         fi
192         . ./$libtool_lai
193         target=$DIST_ROOT/$2
194     fi
195     case $lt_install in
196     so_dot_version)
197         # Loop until we find libfoo.so.x.y.z, then break out.
198         for solib in $library_names
199         do
200                 # does it have enough parts?  libfoo.so.x.y.z == 5
201                 cnt=`echo "$solib" | sed -e 's/\./ /g' | wc -w`
202                 if [ $cnt -eq 5 ]
203                 then
204                         install_name=$target/$solib
205                         $CP $solib $install_name
206                         status=$?
207                         $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$solib ${install_name#$DIST_ROOT}
208                         break
209                 fi
210         done
211         ;;
212
213     so_*)
214         case $lt_install in
215         so_dot_current)
216                 # ln -s libfoo.so.x.y.z to libfoo.so.x
217                 from_parts=5  # libfoo.so.x.y.z
218                 to_parts=3    # libfoo.so.x
219                 ;;
220         so_base)
221                 # ln -s libfoo.so.x to libfoo.so
222                 from_parts=3  # libfoo.so.x
223                 to_parts=2    # libfoo.so
224                 ;;
225         *)
226                 echo "$prog: -T $lt_install invalid"
227                 exit 2
228                 ;;
229         esac
230
231         # Loop until we find the names, then break out.
232         for solib in $library_names
233         do
234                 # does it have enough parts?
235                 cnt=`echo "$solib" | sed -e 's/\./ /g' | wc -w`
236                 if [ $cnt -eq $from_parts ]
237                 then
238                         from_name=$solib
239                 elif [ $cnt -eq $to_parts ]
240                 then
241                         to_name=$solib
242                 fi
243
244                 if [ -n "$from_name" ] && [ -n "$to_name" ]
245                 then
246                         install_name=$target/$to_name
247                         $LN -s -f $from_name $install_name
248                         status=$?
249                         $MANIFEST l $from_name ${install_name#$DIST_ROOT}
250                         break
251                 fi
252         done
253         ;;
254     old_lib)
255         install_name=$target/$old_library
256         $CP $old_library $install_name
257         status=$?
258         $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$old_library ${install_name#$DIST_ROOT}
259         ;;
260     *)
261         echo "$prog: -T $lt_install invalid"
262         exit 2
263         ;;
264     esac
265
266     case $lt_install in
267     old_lib|so_dot_version)
268         if [ $status -eq 0 ]
269         then
270                 $CHMOD $FILEMODE $install_name
271                 $CHOWN $OWNER $GROUP $install_name
272         fi
273         ;;
274     esac
275
276 else
277     list=""
278     dir=""
279     if [ $# -eq 2 ]
280     then
281         #
282         # second usage
283         #
284         f=$1
285         dir=$DIST_ROOT/$2
286         if $Dflag
287         then
288             mkdir -p `dirname $dir`
289         fi
290         $CP $f $dir
291         status=$?
292         if [ $status -eq 0 ]
293         then
294             if [ -f $dir/$f ]
295             then
296                 $CHMOD $FILEMODE $dir/$f
297                 status=$?
298                 if [ $status -eq 0 ]
299                 then
300                     $CHOWN $OWNER $GROUP $dir/$f
301                     status=$?
302                 fi
303                 $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$f ${dir#$DIST_ROOT}/$f
304             else
305                 $CHMOD $FILEMODE $dir
306                 status=$?
307                 if [ $status -eq 0 ]
308                 then
309                     $CHOWN $OWNER $GROUP $dir
310                     status=$?
311                 fi
312                 $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$dir ${dir#$DIST_ROOT}
313             fi
314         fi
315     else
316         #
317         # third usage
318         #
319         n=1
320         while [ $# -gt 0 ]
321         do
322             if [ $# -gt 1 ]
323             then
324                 list="$list $1"
325             else
326                 dir=$DIST_ROOT/$1
327             fi
328             shift
329         done
330
331         # echo DIR=$dir list=\"$list\"
332         for f in $list
333         do
334             $CP $f $dir
335             status=$?
336             if [ $status -eq 0 ]
337             then
338                 $CHMOD $FILEMODE $dir/$f
339                 status=$?
340                 if [ $status -eq 0 ]
341                 then
342                     $CHOWN $OWNER $GROUP $dir/$f
343                     status=$?
344                 fi
345                 $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$f ${dir#$DIST_ROOT}/$f
346             fi
347             [ $status -ne 0 ] && break
348         done
349     fi
350 fi
351
352 exit $status