Whamcloud - gitweb
merge b_devel into HEAD, which will become 0.7.3
[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_LIBDIR != x ]
15 then
16         P=$PATCHSCRIPTS_LIBDIR
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 check_pc_match()
73 {
74         if [ -f /usr/bin/lsdiff ]; then
75                 tmpfile=$(mktemp /tmp/p_XXXXXX) || exit 1
76                 lsdiff --strip=1 $1 > $tmpfile 
77                 diff $2 $tmpfile > /dev/null
78                 if [ $? != 0 ]; then
79                         echo " $1 do not match with $2 "
80                         echo " $2 will be changed to match $2"
81                         # cat $tmpfile > $P/pc/$PATCH_NAME.pc
82                 fi
83                 rm -rf $tmpfile
84         fi
85
86 can_apply()
87 {
88         if patch -p1 --dry-run -i "$1" -f
89         then
90                 return 0
91         else
92                 return 1
93         fi
94 }
95
96 can_remove()
97 {
98         if patch -R -p1 --dry-run -i $P/patches/"$1".patch -f
99         then
100                 return 0
101         else
102                 return 1
103         fi
104 }
105
106 remove_from_db()
107 {
108         tmpfile=$(mktemp /tmp/p_XXXXXX)
109         name="$1"
110         sed -e "/^$name$/d" < "$DB" > $tmpfile
111         mv $tmpfile "$DB"
112 }
113
114 stripit()
115 {
116         ret=$(basename $1)
117         ret=$(echo $ret | sed -e 's/\.patch$//')
118         ret=$(echo $ret | sed -e 's/\.pc$//')
119         ret=$(echo $ret | sed -e 's/\.txt$//')
120         echo $ret
121 }
122
123 top_is_current()
124 {
125         patch_name=$(top_patch)
126         if [ x$patch_name == x ]
127         then
128                 return 1
129         else
130                 patch_file=$P/patches/"$patch_name".patch
131                 files=$(cat $P/pc/$patch_name.pc)
132                 for file in $files
133                 do
134                         if [ $file -nt $patch_file ]
135                         then
136                                 echo $file newer than $patch_file
137                                 return 0
138                         fi
139                 done
140         fi
141         return 1
142 }
143
144 need_top_current()
145 {
146         if top_is_current
147         then
148                 echo "Error: Top patch is not up-to-date"
149                 exit 1
150         fi
151 }
152
153 warn_top_current()
154 {
155         if top_is_current
156         then
157                 echo "Warning: Top patch is not up-to-date"
158         fi
159 }
160
161 file_in_patch()
162 {
163         file=$1
164         patch=$2
165
166         if [ -e $P/pc/$patch.pc ]
167         then
168                 if grep "^"$file"$" $P/pc/$patch.pc > /dev/null
169                 then
170                         return 0
171                 fi
172         fi
173         return 1
174 }
175
176 # copy_file_to_bup filename patchname
177 copy_file_to_bup()
178 {
179         file=$1
180         patch=$2
181         bup="$file"~"$patch"
182         orig="$file"~"orig"
183         src_dir=`pwd`
184
185         if [ -e $bup ]
186         then
187                 echo "Cannot install file $file in patch $patch: backup $bup exists"
188                 exit 1
189         fi
190         if [ -e $file ]
191         then
192                 cp -p $file "$file"~"$patch"
193         else
194                 echo "file $file appears to be newly added"
195         fi
196         if [ ! -L "$orig" ]; then
197                 ln -s "$src_dir/$bup" $orig
198         fi      
199 }
200
201 install_file_in_patch()
202 {
203         file=$1
204         patch=$2
205
206         copy_file_to_bup $file $patch
207         echo $file >> $P/pc/$patch.pc
208 #       touch $P/txt/$patch.txt
209 }
210
211 need_file_there()
212 {
213         if [ ! -e $1 ]
214         then
215                 echo "File $1 does not exist"
216                 exit 1
217         fi
218 }
219
220 desc()
221 {
222         state=0
223         while read x
224         do
225                 if [ x"$x" = xDESC ]
226                 then
227                         state=1
228                 elif [ x"$x" = xEDESC ]
229                 then
230                         state=0
231                 elif [ $state = 1 ]
232                 then
233                         echo "  $x"
234                 fi
235         done
236 }
237
238 body()
239 {
240         file=$1
241
242         did_stuff=0
243         while read x
244         do
245                 if [ x"$x" = xEDESC ]
246                 then
247                         cat
248                         did_stuff=1
249                 fi
250         done < $file
251
252         if [ $did_stuff = 0 ]
253         then
254                 cat $file
255         fi
256 }