summaryrefslogtreecommitdiffstats
path: root/lbu
blob: 96da259abd306501e9cbb0280fa800239879f081 (plain)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/bin/sh

# lbu - utility to create local backups.
# Copyright (c) 2006 Natanael Copa
# May be distributed under GPL2

PREFIX=
. $PREFIX/lib/libalpine.sh
# this one is from apk-tools
. $PREFIX/lib/apk/libutil.sh

VERSION=0.8
[ "$SFIC" ] && SFIC="$SFIC -i d"

# globals
CURRENT_TDB="$APK_DATA/current.tdb"
TMPCURRENT_TDB="$APK_DATA/tmp-current.tdb"
COMMITED_TDB="$APK_DATA/commited.tdb"

EXCLUDE_LIST=/etc/lbu/exclude
INCLUDE_LIST=/etc/lbu/include

MASK="Npugsh"

LBUDIRS=`echo "$APK_LBUDIRS" | sed 's/:/ /g'`

retcode=0
usage() {
	echo "$PROGRAM $VERSION"
	echo "usage: $PROGRAM <subcommand> [options] [args]

Available subcommands:
  commit (ci)
  exclude (ex, delete)
  include (inc, add)
  status (stat, st)
  update (up)

Common options:
 -h	Show help for subcommand.
 -q	Quiet mode.
 -v	Verbose mode.
"
	exit 1
}


#gen_temp_tdb() {
#	# generate temp tdb
#	local opwd="$PWD"
#	rm -f "$TMP_TDB"
#	cd "$ROOT"
#	$SFIC -R --add "$TMP_TDB" $LBUDIRS
#	cd "$opwd"
#}	

gen_current_tdb() {
#	[ -f "$TMP_TDB" ] || gen_temp_tdb
	# generate current tdb
	rm -f "$CURRENT_TDB"
	$SFIC -R -t --mask "$MASK" --old "$APK_DEFAULT_TDB" $LBUDIRS \
		| grep -v ^D | awk '{print $2}' | $SFIC --add $CURRENT_TDB --file -
}

# list_add(char *listfile, char* file...)
list_add() {
	local list="$1"
	shift
	mkdir -p `dirname "$list"`
	while [ $# -gt 0 ] ; do
		filename=`echo "$1" | sed 's:^/\+::'`
		if grep "^$filename$" "$list" >/dev/null 2>&1 ; then
			[ "$VERBOSE" ] echo "$filename is already in $list."
		else
			[ "$VERBOSE" ] && echo "Adding $filename to $list."
			echo "$filename" >> "$list"
		fi
		shift
	done
}

# list_delete(char *listfile, char *file...)
list_delete() {
	local list="$1"
	local tmp="$list.old"
	shift
	[ -f "$list" ] || return 1
	while [ $# -gt 0 ] ; do
		filename=`echo "$1" | sed 's:^/\+::'`
		mv "$list" "$tmp"
		[ "$VERBOSE" ] && echo "Removing $filename from list."
		grep -v "^$filename$" "$tmp" > "$list"
		rm "$tmp"
		shift
	done
}


		
#
# lbu_include - add/remove files to include list
#
usage_include() {
	echo "$PROGRAM $VERSION
usage: $PROGRAM include [-rv] <file> ...
       $PROGRAM include [-v] -l

Options:
  -l	List contents of include list.
  -r	Remove specified file(s) from include list instead of adding.
  -v	Verbose mode.
"
	exit 1
}

cmd_include() {
	if [ "$LIST" ] ; then
		[ $# -gt 0 ] && usage_include
		show_include
		return
	fi
	
	[ $# -lt 1 ] && usage_include
	if [ "$REMOVE" ] ; then
		list_delete "$INCLUDE_LIST" "$@"
	else
		list_add "$INCLUDE_LIST" "$@"
		list_delete "$EXCLUDE_LIST" "$@"
	fi
}

show_include() {
	if [ -f "$INCLUDE_LIST" ] ; then
		[ "$VERBOSE" ] && echo "Include files:"
		cat "$INCLUDE_LIST"
	fi
}


#
# lbu_commit - commit config files to writeable media
#
usage_commit() {
	echo "$PROGRAM $VERSION
usage: $PROGRAM commit [-nv] [<media>]

Options:
  -n	Don't commit, just show what would have been commited.
  -v	Verbose mode.

The following values for <media> is supported: floppy usb
If <media> is not specified, the environment variable LBU_MEDIA will be used.
"
	exit 1
}

cmd_commit() {
	local media mnt was_mounted statuslist tmplist currentlist 
	local incl excl outfile
	
	if [ "$1" ] ; then
		media="$1"
	else
		media="$LBU_MEDIA"
	fi
	[ -z "$media" ] && usage_commit

	mnt=/media/$media

	[ -d $mnt ] || usage
	was_mounted=`grep $mnt /proc/mounts`

	cd "$ROOT"

#	# for upgrading from packages.list times
#	mkdir -p $(dirname "$INCLUDE_LIST")
#	if ! grep "var/lib/apk/world" "$INCLUDE_LIST" >/dev/null 2>&1 ; then
#		echo "var/lib/apk/world" >> "$INCLUDE_LIST"
#	fi

	# generate the packages.list
	mkdir -p /etc/lbu
	echo "#This file is generated by 'lbu commit'" > /etc/lbu/packages.list
	for i in $(apk_glob '*') ; do
		reqby=$(apk_info -qr $i);
		# only add the packages who has no REQUIRED_BY
		[ -z "$reqby" ] && echo $i
	done | sed 's/-[0-9].*//' >> /etc/lbu/packages.list
	
	# Automatically add list and modified files
	currentlist=`VERBOSE="" USE_DEFAULT="-a" cmd_status -a | grep -v ^D | awk '{print $2}'`

	if [ -z "$was_mounted" ]; then
		mount $mnt || die "failed to mount $mnt."
	fi

	# we generate a tmpcurrent before we commit to avoid race condition
	[ -f "$CURRENT_TDB" ]  || $SFIC --add "$CURRENT_TDB" $currentlist
	
	# commit files to archive
	if [ "$DRYRUN" ] ; then
		outfile=/dev/null
		VERBOSE="-v"
	else
		outfile="$mnt/`hostname`.apkovl.tar.gz"
	fi
	
	[ -f "$EXCLUDE_LIST" ] && excl="-X $EXCLUDE_LIST"
	[ -f "$INCLUDE_LIST" ] && incl="-T $INCLUDE_LIST"
	
	if ! tar $VERBOSE $excl $incl -c $currentlist | gzip -c  >"$outfile" ; then
		rm -f "$CURRENT_TDB"
		[ -z "$was_mounted" ] && umount $mnt
		die "Problems creating archive. aborting"
	fi	

	# remove obsolete file
	if [ -z "$DRYRUN" ] && [ -f $mnt/packages.list ]; then
		echo "Note: Removing packages.list from $(basename $mnt)."
		echo "      /etc/lbu/packages.list will be used."
		rm -f $mnt/packages.list
	fi

	sync
	sleep 1
	[ -z "$was_mounted" ] && umount $mnt

	# move current to commited.
	[ "$DRYRUN" ] || mv "$CURRENT_TDB" "$COMMITED_TDB"
}

#---------------------------------------------------------------------------
# lbu_exclude - add remove file(s) from exclude list

usage_exclude() {
	echo "$PROGRAM $VERSION
usage: $PROGRAM exclude [-rv] <file> ...
       $PROGRAM exclude [-v] -l

Options:
  -l	List contents of exclude list.
  -r	Remove specified file(s) from exclude list instead of adding.
  -v	Verbose mode.
"
	exit 1
}

cmd_exclude() {
	if [ "$LIST" ] ; then
		[ $# -gt 0 ] && usage_exclude
		show_exclude
		return
	fi
	
	[ $# -lt 1 ] && usage_exclude
	if [ "$REMOVE" ] ; then
		list_delete "$EXCLUDE_LIST" "$@"
	else
		list_delete "$INCLUDE_LIST" "$@"
		list_add "$EXCLUDE_LIST" "$@"
	fi
}

show_exclude() {
	if [ -f "$EXCLUDE_LIST" ] ; then
		[ "$VERBOSE" ] && echo "Exclude files:"
		cat "$EXCLUDE_LIST"
	fi	
}

#---------------------------------------------------------------------------
# lbu_status - check what files have been changed since last save
usage_status() {
	echo "$PROGRAM $VERSION
Check what files have been changed since last commit.

usage: $PROGRAM status [-M <MASK>] [-av]

Options:
  -M	Use a different mask for comparing. (see sfic -h)
  -a	Compare all files, not just since last commit.
  -v	Also show include and exclude lists.
"
	exit 1
}

cmd_status() {
	cd "$ROOT"

	[ "$SFIC" ] || die "Need sfic."
	[ -f "$APK_DEFAULT_TDB" ] || die "$APK_DEFAULT_TDB not found."

	# genereate temp tdb
	#gen_temp_tdb
	
	if [ -f "$COMMITED_TDB" ] && [ -z "$USE_DEFAULT" ]; then
		# generate current tdb
		gen_current_tdb
		OLD="$COMMITED_TDB"
		NEW="--new $CURRENT_TDB"
	else
		OLD="$APK_DEFAULT_TDB"
		NEW="-R $LBUDIRS"
	fi

	$SFIC $QUIET $VERBOSE --mask "$MASK" -t --old "$OLD" $NEW
	retcode=$?

	if [ "$VERBOSE" ] ; then
		echo ""
		show_include
		echo ""
		show_exclude
	fi
}

#------------------------------------------------
# lbu_update - Update the database wihtout commit to media.
usage_update() {
	echo "$PROGRAM $VERSION
Update the commited database without commit to media.
	
usage: $PROGRAM update [-v] <file> ...

Options:
  -v	Verbose mode.
"
	exit 1
}

cmd_update() {
	[ $# -lt 1 ] && usage_update
	cd "$ROOT"
	for i in "$@" ; do
		echo $i | sed 's:^/::'
	done | lbu_filter | $SFIC $VERBOSE --add "$COMMITED_TDB" --file -
}

#-----------------------------------------------------------
# Main

cmd=`echo "$PROGRAM" | cut -s -d_ -f2`
PROGRAM=`echo "$PROGRAM" | cut -d_ -f1`
if [ -z "$cmd" ] ; then
	cmd="$1"
	[ -z "$cmd" ] && usage
	shift
fi

# check for valid sub command
case "$cmd" in
	include|inc|add)	SUBCMD="include";;
	commit|ci)		SUBCMD="commit";;
	exclude|ex|delete)	SUBCMD="exclude";;
	status|stat|st)		SUBCMD="status";;
	update|up)		SUBCMD="update";;
	*)			usage;;
esac

# parse common args
while getopts "ahlM:nqrv" opt ; do
	case "$opt" in
		a) 	[ $SUBCMD = status ] || usage_$SUBCMD
			USE_DEFAULT="-a"
			;;
		h) 	usage_$SUBCMD
			;;
		l)	LIST="-l"
			;;
		M) 	[ $SUBCMD = status ] || usage_$SUBCMD
			MASK="$OPTARG"
			;;
		n) 	[ $SUBCMD = commit ] || usage_$SUBCMD
			DRYRUN="-n"
			;;
		q)	QUIET="$QUIET -q"
			;;
		r)	REMOVE="-r"
			;;
		v) 	VERBOSE="$VERBOSE -v"
			;;
	esac
done
shift `expr $OPTIND - 1`
			
cmd_$SUBCMD "$@"		
# cleanup
rm -f "$CURRENT_TDB"
exit $retcode