]> review.fuel-infra Code Review - packages/trusty/cirros-testvm.git/blob - cirros-testvm/src-cirros/bin/xmakedevs
Fix for file injection is broken on centos computes with Cirros images LP #1587960
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / bin / xmakedevs
1 #!/bin/sh -e
2 # vi: ts=4 noexpandtab
3 #
4 # this mimics function of buildroot's makedev
5 # read a file like:
6 #  <name>    <type>      <mode>  <uid>   <gid>   <major> <minor> <start> <inc>   <count>
7 # and make files
8 DEBUG=${XMAKEDEV_DEBUG}
9 Usage() {
10    cat <<EOF
11 Usage: ${0##*/} file root
12   read file, and make devs apropriately under 'root'.
13
14 file looks like:
15   <name>    <type>      <mode>  <uid>   <gid>   <major> <minor> <start> <inc>   <count>
16   where name is the file name,  type can be one of:
17    f       A regular file
18    d       Directory
19    c       Character special device file
20    b       Block special device file
21    p       Fifo (named pipe)
22 EOF
23 }
24
25 error() { echo "$@" 1>&2; exit 1; }
26 fail() { [ $# -eq 0 ] || error "$@" ; exit 1; }
27
28 [ "$1" = "-h" -o "$1" = "--help" ] && { Usage; exit 0; }
29 [ $# -eq 2 ] || { Usage 1>&2; exit 1; }
30
31 root="$2"
32
33 if [ "$1" != "-" ]; then
34         exec < "$1" || fail "failed to redirect input from $1"
35 fi
36
37 chmodchown() {
38         local mode=$1 uid=$2 gid=$3
39         shift 3;
40         [ "$mode" = "-" ] || chmod "$mode" "$@" || return
41         [ "$uid" = "-" ] || chown "$uid" "$@" || return
42         [ "$gid" = "-" ] || chgrp "$gid" "$@" || return
43 }
44 iswild() {
45         local star="\\*"
46         case "$1" in
47                 *$star*) return 0;;
48         esac
49         return 1
50 }
51
52 cd "$root" || fail "failed cd $root"
53
54 while read line; do
55         line=${line%#*}
56         [ -n "$line" ] || continue
57         set -f
58         set -- $line
59         name=$1; type=$2; mode=$3; uid=$4; gid=$5; major=$6; minor=$7; start=$8; inc=$9; count="${10}"
60         set +f
61         files="./${name#/}"
62         if iswild "$name"; then
63                 files=$(echo ./$name)
64                 iswild "$files" && fail "wildcards in wildcards: $files"
65         fi
66         case "$type" in
67                 f) 
68                         for f in $files; do
69                                 $DEBUG mkdir -p "${f%/*}" || fail "failed to make ${f%/*}";
70                         done
71                         $DEBUG touch $files &&
72                         $DEBUG chmodchown "$mode" "$uid" "$gid" $files ||
73                         fail "failed create file $files"
74                         ;;
75                 d)
76                         $DEBUG mkdir -p $files &&
77                         $DEBUG chmodchown "$mode" "$uid" "$gid" $files ||
78                         fail "failed create dir $files"
79                         ;;
80                 c|b)
81                         for f in $files; do
82                                 $DEBUG mkdir -p "${f%/*}" || fail "failed to make ${f%/*}";
83                         done
84                         if [ "$count" = "-" ]; then
85                                 $DEBUG mknod $files $type $major $minor ||
86                                         fail "failed mknod"
87                                 $DEBUG chmodchown "$mode" "$uid" "$gid" $files ||
88                                         fail "failed chmodchown '$mode' '$uid' '$gid' $files"
89                         else
90                                 [ "$count" = "-" ] && count=1;
91                                 i=0
92                                 while [ $i -lt $count ]; do
93                                         fname="./$name"
94                                         $DEBUG mknod "$fname" "$type" "$major" "$(($minor+($i*$inc)))"
95                                         $DEBUG chmodchown "$fname" "$mode" "$uid" "$gid"
96                                         i=$(($i+1))
97                                 done
98                         fi
99                         ;;
100         esac
101 done