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