Whamcloud - gitweb
- bring in akpm's patch management scripts and a first cut at
[fs/lustre-release.git] / lustre / kernel_patches / scripts / patchfns
1 DB=applied-patches
2
3 #
4 # Work out where the user's pc/, patch/ and txt/ directories live.
5 #
6 # If the user specified PATCHSCRIPTS in environment then use that (it's
7 # probably a relative path)
8 #
9 # If there is a directory ./patch-scripts then use that
10 #
11 # Otherwise use "."
12 #
13
14 if [ x$PATCHSCRIPTS != x ]
15 then
16         P=$PATCHSCRIPTS
17 elif [ -d ./patch-scripts ]
18 then
19         P=./patch-scripts
20 elif [ -d ./patches ]
21 then
22         P=.
23 else
24         echo "could not locate your pc/ and patches/ directories"
25         exit 1
26 fi
27
28 top_patch()
29 {
30         tail -1 $DB
31 }
32
33 die()
34 {
35         echo error: $*
36         exit 1
37 }
38
39 is_numeric()
40 {
41         if echo $1 | egrep '^[0-9]*$' > /dev/null
42         then
43                 return 0
44         fi
45         return 1
46 }
47
48 is_applied_last()
49 {
50         name="$(stripit $1)"
51         top_patch >$DB.1
52         if grep "^$name$" "$DB.1" > /dev/null 2>&1
53         then
54                 rm $DB.1
55                 return 0
56         else
57                 rm $DB.1
58                 return 1
59         fi
60 }
61
62 is_applied()
63 {
64         name=$(stripit "$1")
65         if grep "^$name$" "$DB" > /dev/null 2>&1
66         then
67                 return 0
68         else
69                 return 1
70         fi
71 }
72
73 can_apply()
74 {
75         if patch -p1 --dry-run -i "$1" -f
76         then
77                 return 0
78         else
79                 return 1
80         fi
81 }
82
83 can_remove()
84 {
85         if patch -R -p1 --dry-run -i $P/patches/"$1".patch -f
86         then
87                 return 0
88         else
89                 return 1
90         fi
91 }
92
93 remove_from_db()
94 {
95         tmpfile=$(mktemp /tmp/p_XXXXXX)
96         name="$1"
97         sed -e "/^$name$/d" < "$DB" > $tmpfile
98         mv $tmpfile "$DB"
99 }
100
101 stripit()
102 {
103         ret=$(basename $1)
104         ret=$(echo $ret | sed -e 's/\.patch$//')
105         ret=$(echo $ret | sed -e 's/\.pc$//')
106         ret=$(echo $ret | sed -e 's/\.txt$//')
107         echo $ret
108 }
109
110 top_is_current()
111 {
112         patch_name=$(top_patch)
113         if [ x$patch_name == x ]
114         then
115                 return 1
116         else
117                 patch_file=$P/patches/"$patch_name".patch
118                 files=$(cat $P/pc/$patch_name.pc)
119                 for file in $files
120                 do
121                         if [ $file -nt $patch_file ]
122                         then
123                                 echo $file newer than $patch_file
124                                 return 0
125                         fi
126                 done
127         fi
128         return 1
129 }
130
131 need_top_current()
132 {
133         if top_is_current
134         then
135                 echo "Error: Top patch is not up-to-date"
136                 exit 1
137         fi
138 }
139
140 warn_top_current()
141 {
142         if top_is_current
143         then
144                 echo "Warning: Top patch is not up-to-date"
145         fi
146 }
147
148 file_in_patch()
149 {
150         file=$1
151         patch=$2
152
153         if [ -e $P/pc/$patch.pc ]
154         then
155                 if grep "^"$file"$" $P/pc/$patch.pc > /dev/null
156                 then
157                         return 0
158                 fi
159         fi
160         return 1
161 }
162
163 # copy_file_to_bup filename patchname
164 copy_file_to_bup()
165 {
166         file=$1
167         patch=$2
168         bup="$file"~"$patch"
169
170         if [ -e $bup ]
171         then
172                 echo "Cannot install file $file in patch $patch: backup $bup exists"
173                 exit 1
174         fi
175
176         if [ -e $file ]
177         then
178                 cp $file "$file"~"$patch"
179         else
180                 echo "file $file appears to be newly added"
181         fi
182 }
183
184 install_file_in_patch()
185 {
186         file=$1
187         patch=$2
188
189         copy_file_to_bup $file $patch
190         echo $file >> $P/pc/$patch.pc
191 #       touch $P/txt/$patch.txt
192 }
193
194 need_file_there()
195 {
196         if [ ! -e $1 ]
197         then
198                 echo "File $1 does not exist"
199                 exit 1
200         fi
201 }
202
203 desc()
204 {
205         state=0
206         while read x
207         do
208                 if [ x"$x" = xDESC ]
209                 then
210                         state=1
211                 elif [ x"$x" = xEDESC ]
212                 then
213                         state=0
214                 elif [ $state = 1 ]
215                 then
216                         echo "  $x"
217                 fi
218         done
219 }
220
221 body()
222 {
223         file=$1
224
225         did_stuff=0
226         while read x
227         do
228                 if [ x"$x" = xEDESC ]
229                 then
230                         cat
231                         did_stuff=1
232                 fi
233         done < $file
234
235         if [ $did_stuff = 0 ]
236         then
237                 cat $file
238         fi
239 }