File: apply

package info (click to toggle)
kernel-source-2.4.27 2.4.27-10sarge5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 191,224 kB
  • ctags: 610,077
  • sloc: ansic: 3,299,602; asm: 164,708; makefile: 10,962; sh: 3,725; perl: 2,273; yacc: 1,651; cpp: 820; lex: 752; tcl: 577; awk: 251; lisp: 218; sed: 79
file content (253 lines) | stat: -rw-r--r-- 5,501 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/sh 
# $Id: apply,v 1.4 2003/06/30 12:49:09 herbert Exp $

set -e

length=50

die() {
	echo "E: $@" >&2
	exit 1
}

warn() {
	echo "W: $@" >&2
}

uncompress_patch() {
	patch=$1
	case "$patch" in
		*.bz2) bzcat $patch ;;
		*.gz) zcat $patch ;;
		*) cat $patch ;;
	esac
}

find_patch() {
	patch=$1

	if [ -f "$patch" ]; then
		echo "$patch"
	elif [ -f "$patch.bz2" ]; then
		echo "$patch.bz2"
	elif [ -f "$patch.gz" ]; then
		echo "$patch.gz"
	else
		die "$patch is in the series, but doesn't exist!"
	fi
}
		
apply_patch() {
	patch=$(find_patch $home/$1)
	base=$1
	if uncompress_patch "$patch" | patch -p1 -s -t --no-backup-if-mismatch; then
		printf "%-${length}s\tOK (+)\n" "$base"
	else
		printf "%-${length}s\tFAIL (+)\n" "$base"
		exit 1
	fi
}

deapply_patch() {
	patch=$(find_patch $home/$1)
	base=$1
	if uncompress_patch "$patch" | patch -p1 -s -t -R --no-backup-if-mismatch; then
		printf "%-${length}s\tOK (-)\n" "$base"
	else
		printf "%-${length}s\tFAIL (-)\n" "$base"
		exit 1
	fi
}

unpatch_series() {
	series=$1
	[ -f "$series" ] || die "I wasn't passed a series!"

	tac $series | while read action patch; do
		case "$action" in
			+) deapply_patch $patch ;;
			-) apply_patch $patch ;;
			X)
				bakfile="$(dirname $patch)/.$(basename $patch).bak"
				if [ -f "$bakfile" ]; then
					mv -f "$bakfile" "$patch"
					printf "%-${length}s\tRESTORED (X)\n" "$patch"
				else
					printf "%-${length}s\tNO BACKUP (X)\n" "$patch"
				fi
			;;
		esac
	done 
	echo "--> $(basename $series) fully unapplied."
}

patch_series() {
	series=$1
	[ -f "$series" ] || die "I wasn't passed a series!"

	while read action patch; do
		case "$action" in
			+) apply_patch $patch ;;
			-) deapply_patch $patch ;;
			X)
				bakfile="$(dirname $patch)/.$(basename $patch).bak"
				if [ -f "$patch" ]; then
					mv -f "$patch" "$bakfile"
					printf "%-${length}s\tREMOVED (X)\n" "$patch"
				else
					printf "%-${length}s\tNO FILE (X)\n" "$patch"
				fi
			;;
		esac
	done < $series
	echo "--> $(basename $series) fully applied."
}

bubble_sort ()
{
	DIR=$1
	shift
	SORTED=$@

	SWAPED=1
	while [ $SWAPED = 1 ]; do
		X=0
		A=""
		SWAPED=0
		NEW=""
		for i in $SORTED; do
			if [ -z "$A" ]; then
				A="$i"
				continue
			fi
			B="$i"
		
			if dpkg --compare-versions "$A" "$DIR" "$B"; then
				SWAPED=1
				NEW="$NEW $B"
			else
				NEW="$NEW $A"
				A="$B"
			fi

			X=$(($X + 1))
		done
		SORTED="$NEW $A"
	done

	echo $SORTED
}

bubble_sort_fwd ()
{
	bubble_sort "lt" $@
}

bubble_sort_rev ()
{
	bubble_sort "gt" $@
}

if ! [ -d Documentation ] || ! [ -d kernel ]; then
	die 'Not in kernel top level directory.  Exiting'
	exit 1
fi

# for THIS particular version of the source package
version=${override_version:-@version@}
upstream=${version%-*}
revision=${version#*-}

home=${home:-/usr/src/kernel-patches/all/$upstream/debian}

if [ -f version.Debian ]; then
	current=$(cat version.Debian)
	current_rev=${current#*-}
	current_up=${current%-*}

	if [ "$current" = "$upstream" ]; then
		current_rev=0
	fi
else
	warn "No version.Debian file, assuming pristine Linux $upstream"
	current=$upstream
	current_rev=0
fi

target=${1:-$version}

target_rev=${target#*-}
target_up=${target%-*}

# Sanity checks
if dpkg --compare-versions "$target_up" ne "$upstream"; then
	die "Upstream $target_up doesn't match $upstream!"
# We don't have that version out yet!
elif [ ! -n "$target_rev" ] || 
		( dpkg --compare-versions "$target_rev" ne "$target" && 
			dpkg --compare-versions "$target_rev" gt "$revision" ); then
	year=$(($(date +%Y) + 1))
	die "Can't patch to nonexistent revision $target_rev (wait until $year)"
fi

# At this point, we must handle three cases.
# 1. $target_rev is greater than $current_rev. We must patch forward for this.
# 2. $target_rev is less than $current_rev. We must reverse the list of series,
#    reverse each used series (tac) and unapply applied patches and vice versa.
# 3. $target_rev is undefined, and $target is $upstream.

# Revert to upstream.
if [ "$target_rev" = "$target" ]; then
	# already reverted
	if [ "$current" = "$target" ]; then 
		echo "Nothing to do, exiting."
		exit 0
	fi

	for sver in $(bubble_sort_fwd $(ls -d $home/series/*) ); do
		base=$(basename "$sver")
		srev=${base#*-}
		if [ -n "$srev" ]; then
			if dpkg --compare-versions $srev "<=" $current_rev; then
				unpatch_series $sver
			fi
		else
			die "Series doesn't have a revision!"
		fi
	done
elif dpkg --compare-versions "$current_rev" eq "$upstream" ||
		dpkg --compare-versions "$target_rev" gt "$current_rev"; then
	for sver in $(bubble_sort_rev $(ls -d $home/series/*) ); do
		base=$(basename "$sver")
		srev=${base#*-}
		if [ -n "$srev" ]; then
			if dpkg --compare-versions "$srev" gt "$current_rev" && 
						dpkg --compare-versions "$srev" le "$target_rev"; then
				patch_series $sver
			fi
		else
			die "Series doesn't have a revision!"
		fi
	done
elif dpkg --compare-versions "$target_rev" eq "$current_rev"; then
	echo "Nothing to do, exiting."
	exit 0
elif dpkg --compare-versions "$target_rev" lt "$current_rev"; then
	for sver in $(bubble_sort_fwd $(ls -d $home/series/*) ); do
		base=$(basename "$sver")
		srev=${base#*-}
		if [ -n "$srev" ]; then
			# gt because you don't want to unapply the target series
			if dpkg --compare-versions "$srev" le "$current_rev" && 
						dpkg --compare-versions "$srev" gt "$target_rev"; then
				unpatch_series $sver
			fi
		else
			die "Series doesn't have a revision!"
		fi
	done
fi

echo $target > version.Debian

# vim:noet:ai:ts=4