/
squash_dir
executable file
·851 lines (784 loc) · 19.2 KB
/
squash_dir
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
#!/sbin/runscript
# (C) Martin V\"ath <martin@mvath.de>
depend() {
need localmount
}
description='Mount a squashed directory in a writable manner'
extra_commands='START STOP RESTART'
extra_commands=$extra_commands' will_squash need_squash have_magic have_kill'
extra_commands=$extra_commands' print_dir_change print_dir_squash'
extra_commands=$extra_commands' print_ignore_threshold print_magic_file'
extra_commands=$extra_commands' print_kill_file'
description_START='Execute actions of start without actually starting. Dangerous!'
description_STOP='Execute actions of stop without actually stopping. Dangerous!'
description_RESTART='Execute STOP && START without actually restarting. Dangerous!'
description_will_squash='print 1 to stderr if stop operation would re-squash'
description_need_squash='print 1 to stderr if new data was written'
description_have_magic='print 1 to stderr if $MAGIC_FILE exists'
description_have_kill='print 1 to stderr if $KILL_FILE exists'
description_print_dir_change='print effective $DIR_CHANGE to stderr'
description_print_dir_squash='print effective $DIR_SQUASH to stderr'
description_print_ignore_threshold='print effective $IGNORE_THRESHOLD to stderr; empty if unused'
description_print_magic_file='print effective $MAGIC_FILE to stderr'
description_print_kill_file='print effective $KILL_FILE to stderr'
Which() {
command -v "$1" 2>/dev/null
}
Warn() {
[ $1 -eq 0 ] && return
warnstat=$1
shift
ewarn "$* [exit with $warnstat]"
return $warnstat
}
Error() {
[ $1 -eq 0 ] && return
errorstat=$1
shift
eerror "$* [exit with $errorstat]"
return $errorstat
}
Mkdir() {
[ -z "${1:++}" ] && return
test -d "$1" && return
mkdir -p -m 755 -- "$1" || Error $? "cannot create $1"
}
Normalize() {
normalizestat=0
for normalizei
do eval normalizen=\$$normalizei
if [ -z "${normalizen:++}" ]
then eval $normalizei=
normalizestat=1
continue
fi
if [ -n "${readlink:++}" ]
then normalizep=`"$readlink" -f -- "$normalizen"`
else normalizep=`cd -P -- "$normalizen" >/dev/null 2>&1 \
&& printf '%sA' "$PWD"` \
&& normalizep=${normalizep%A}
fi && [ -n "${normalizep:++}" ] && normalizen=$normalizep
while ! case $normalizen in
*//*)
false;;
esac
do normalizen=${normalizen%%//*}/${normalizen#*//}
done
[ "$normalizen" != '/' ] && normalizen=${normalizen%/}
eval $normalizei=\$normalizen
done
return $normalizestat
}
Mktemp() {
mktempstat=0
eval mktempn=\$$1
Mkdir "${mktempn%/*}" || mktempstat=$?
if [ $mktempstat -eq 0 ]
then mktempn=`mktemp $2 -- "$mktempn"` || \
Error $? "mktemp $2 $mktempn failed"
mktempstat=$?
[ -n "${2:++}" ] && chmod 755 -- "$mktempn"
eval $1=\$mktempn
fi
Normalize $1 && return $mktempstat
}
Mktempdir() {
if ! $1
then Normalize $2
eval "Mkdir \"\$$2\""
return
fi
Mktemp $2 -d
mktempdirtstat=$?
eval mktempdirv=\$$2
mktempdirv=${mktempdirv%/}
case $mktempdirv in
/*)
:;;
*)
return $mktempdirtstat;;
esac
if $have_name_file
then printf "%s='%s'\n" $3 "${mktempdirv##*/}" >>"$NAME_FILE"
else have_name_file=:
printf "%s='%s'\n" $3 "${mktempdirv##*/}" >"$NAME_FILE"
fi
mktempdirwstat=$?
if [ $mktempdirwstat -ne 0 ]
then Error $mktempdirwstat "cannot write to $NAME_FILE"
[ $mktempdirtstat -eq 0 ] && return $mktempdirwstat
fi
return $mktempdirtstat
}
Lock() {
case ${LOCKFILE-auto} in
auto)
if test -h /etc/mtab
then LOCKFILE=
else LOCKFILE='/etc/mtab.lock'
false
fi;;
/*) false;;
esac && {
"$@"
return
}
flock -w 20 -- "$LOCKFILE" "$@"
}
Umount() {
if [ -z "$umount" ]
then umount=`Which umount` || umount=/bin/umount
fi
Lock "$umount" "$UMOUNT_OPTS" "$@" >/dev/null && return
umountstat=$?
case ${LAZY_UMOUNT-y} in
[yYtT:1]*)
Warn $umountstat "Non-lazy umount $* failed"
Lock umount -l "$UMOUNT_OPTS" "$@" >/dev/null && return
umountstat=$?;;
esac
Error $umountstat "umount $* failed"
}
Mount() {
Lock mount "$@"
}
# Usage: Cleardir directory [keep] [action-before-first-removal]
Cleardir() {
cleardird=$1
test -d "$cleardird" || return
cleardire=${2:+|}$2
cleardirt=${3:-:}
eval "for cleardiri in \"\$cleardird\"/* \"\$cleardird\"/.*
do case \${cleardiri##*/} in
''|.|..$cleardire)
continue;;
'*'|'.*')
test -e \"\$cleardiri\" || continue;;
esac
eval \"\$cleardirt\"
cleardirt=:
rm -rf -- \"\$cleardiri\" || return
done"
return 0
}
Compare() {
compareori=$DIR_SQUASH/${1##*/}
if test -f "$1"
then test -f "$compareori" && \
diff -q -- "$1" "$compareori" >/dev/null 2>&1
return
fi
test -d "$1" || return 0
test -d "$compareori" && \
diff -r -q -- "$1" "$compareori" >/dev/null 2>&1
}
Checktemp() {
eval "case \$$1 in
*XXXXXX)
${1}_temp=:
[ $# -eq 2 ] && : \${RM_$1:=$2};;
*)
${1}_temp=false;;
esac"
}
Readtemps() {
[ -n "${NAME_FILE:++}" ] \
&& readtempsc=`cat -- "$NAME_FILE 2>/dev/null"` \
&& [ -n "${readtempsc:++}" ] || {
eend $? "cannot read $NAME_FILE"
return
}
readtempsc="
$readtempsc
"
for readtempsi in DIR_SQUASH DIR_CHANGE
do eval "readtempsj=\$$readtempsi
readtempsk=\${readtempsc##*\"
\${readtempsi}_name='\"}
\${${readtempsi}_temp}" || continue
readtempsk=${readtempsk%%"'
"*}
case $readtempsk in
''|*/*)
eend 1 "broken ${readtempsi}_name in $NAME_FILE"
return;;
esac
eval $readtempsi=\${readtempsj%/*}/\$readtempsk
done
}
CreateFirstTime() {
einfo "It seems you start $SVCNAME for the first time with this configuration:"
einfo "The squashed file $FILE_SQFS does not exist yet."
einfo "So I will try to initialize..."
test -d "$DIRECTORY" || {
eerror "The DIRECTORY \"$DIRECTORY\" does not exist."
eerror "Probably something is wrong with the configuration."
eerror "Exiting without doing anything."
return 2
}
case $FILE_SQFS in
"$DIRECTORY"/*|"$DIR_CHANGE"/*|"$DIR_SQUASH"/*|'/.sqfs'|'/')
false;;
/*)
! test -e "$FILE_SQFS";;
*)
false;;
esac || {
eerror "Something is strange with this configuration:"
eerror "You used a bad filename for FILE_SQFS or it appears in a bad path."
eerror "Exiting without doing anything."
return 2
}
einfo "Squashing $DIRECTORY to $FILE_SQFS"
mksquashfs "$DIRECTORY" "$FILE_SQFS" \
${COMPRESSION:+-comp "$COMPRESSION"} $MKSQUASHFS || {
Error $? "mksquashfs failed. Exiting."
return
}
test -f "$FILE_SQFS" || {
eerror "mksquashfs succeeded but there is no $FILE_SQFS"
eerror "This is very strange. Exiting."
return
}
chmod -- 644 "$FILE_SQFS" || {
Error $? "chmod failed for $FILE_SQFS. Exiting."
return
}
einfo "Removing all in $DIRECTORY"
Cleardir "$DIRECTORY" || {
error "Cannot properly clean $DIRECTORY. Exiting."
return
}
einfo "Now its content is only available as squashfs."
einfo "Name of the squashfs file: $FILE_SQFS"
}
Check() {
readlink=`Which readlink` || readlink=
[ -n "${DIRECTORY:++}" ] && : ${FILE_SQFS:=$DIRECTORY.sqfs}
for checki in "$DIRECTORY" "$DIR_CHANGE" "$DIR_SQUASH" \
"$TMPDIR" "$FILE_SQFS" "$FILE_SQFS_OLD" \
"$FILE_TBZ" "$FILE_TBZ_OLD" "$NAME_FILE"
do case $checki in
''|/*)
continue;;
esac
eend 2 "$checki is not an absolute path"
return
done
Normalize TMPDIR || TMPDIR='/tmp'
: ${RUNPATH:=/run}
Normalize RUNPATH && test -d "$RUNPATH" || {
RUNPATH=/var/run
Normalize RUNPATH && test -d "$RUNPATH" || RUNPATH=/
}
Normalize NAME_FILE || NAME_FILE=${RUNPATH%/}/$SVCNAME
if [ -n "${DIR_CHANGE:++}" ] && [ -z "${DIR_SQUASH:++}" ]
then DIR_SQUASH=${RUNPATH%/}/$SVCNAME.readonly
: ${RM_DIR_SQUASH=:}
fi
Checktemp DIR_SQUASH parents
Checktemp DIR_CHANGE parents
if $DIR_SQUASH_temp || $DIR_CHANGE_temp
then use_name_file=:
have_name_file=false
else use_name_file=false
fi
if [ "$1" != start ]
then if $use_name_file
then Readtemps || {
eend $?
return
}
else Normalize DIR_SQUASH DIR_CHANGE
fi
fi
if ! Normalize DIRECTORY FILE_SQFS
then eerror "You must set paths in /etc/conf.d/$SVCNAME"
eerror "(See /etc/init.d/$SVCNAME for further details)"
eend 2
return
fi
: ${UMOUNT_OPTS=-i}
: ${COMPRESSION=xz}
if ! test -f "$FILE_SQFS"
then if [ "$1" = start ]
then CreateFirstTime || {
eend $?
return
}
else eerror "The squashed file $FILE_SQFS does not exist."
eerror "This is very strange; probably it is better to exit."
eend 2
return
fi
fi
}
Aufs() {
eval "Mount -t aufs ${MOUNT_AUFS--o noatime} \
-o \"br:\$DIR_CHANGE=rw:\$mount_squash=rr\" \
-- aufs \"\$DIRECTORY\"" \
|| Warn $? "Failed mounting $DIR_CHANGE with aufs"
}
Overlayfs() {
eval "Mount -t overlayfs ${MOUNT_OVERLAYFS--o noatime} \
-o \"upperdir=\$DIR_CHANGE\" \
-o \"lowerdir=\$mount_squash\" \
-- overlayfs \"\$DIRECTORY\"" \
|| Warn $? "Failed mounting $DIR_CHANGE with overlayfs"
}
Unionfs() {
eval "Mount -t unionfs ${MOUNT_UNIONFS--o noatime} \
-o \"dirs=\$DIR_CHANGE=rw:\$mount_squash=ro\" \
-- unionfs \"\$DIRECTORY\"" \
|| Warn $? "Failed mounting $DIR_CHANGE with unionfs"
}
UnionfsFuse() {
if ! unionfsfuse=`Which unionfs` || \
[ -z "${unionfsfuse:++}" ] || ! test -e "$unionfsfuse"
then ewarn "unionfs-fuse appears not to be installed"
return 1
fi
eval "\"\$unionfsfuse\" \
${MOUNT_UNIONFS_FUSE--o cow -o allow_other -o use_ino -o nonempty -o noatime -o hide_meta_files} \
\"\$DIR_CHANGE=RW:\$mount_squash=RO\" \
\"\$DIRECTORY\"" || \
Warn $? "Failed mounting $DIR_CHANGE with unionfs-fuse"
}
Funionfs() {
if ! funionfs=`Which funionfs` || \
[ -z "${funionfs:++}" ] || ! test -e "$funionfs"
then ewarn "funionfs appears not to be installed"
return 1
fi
eval "\"\$funionfs\" \"\$DIR_CHANGE\" \"\$DIRECTORY\" \
${MOUNT_FUNIONFS--o allow_other -o nonempty} \
-o \"dirs=\$mount_squash=ro\"" || \
Warn $? "Failed mounting $DIR_CHANGE with funionfs"
}
Mountunion() {
for mountunion in ${ORDER-overlayfs aufs unionfs-fuse funionfs unionfs}
do case $mountunion in
aufs)
Aufs && return;;
overlayfs)
Overlayfs && return;;
unionfs)
Unionfs && return;;
unionfs*)
UnionfsFuse && return;;
funionfs)
Funionfs && return;;
*)
ewarn "Strange entry in ORDER: $mountunion";;
esac
done
return 1
}
MountRW() {
Mkdir "$DIRECTORY" || return
Mktempdir $DIR_CHANGE_temp DIR_CHANGE DIR_CHANGE_name || return
Mountunion && return
eerror "All attempts to mount $DIRECTORY writable failed"
ewarn "rbinding $DIRECTORY as a read-only fallback"
if Mount --rbind "$mount_squash" -- "$DIRECTORY"
then mountrwstat=0
einfo "Mounted $DIRECTORY readonly"
else mountrwstat=$?
Error $? "Failed even rbinding $DIRECTORY"
fi
return $mountrwstat
}
MountRO() {
Mkdir "$DIRECTORY" || return
Mount --rbind "$mount_squash" -- "$DIRECTORY"
Error $? "Failed rbinding $DIRECTORY"
}
Mksquash() {
mount_squash=
if [ -z "${DIR_SQUASH:++}" ]
then Mkdir "$DIRECTORY" && mount_squash=$DIRECTORY
else Mktempdir $DIR_SQUASH_temp DIR_SQUASH DIR_SQUASH_name && \
mount_squash=$DIR_SQUASH
fi
}
START() {
Check start || return
Mksquash || return
ebegin "Mounting $FILE_SQFS as $DIRECTORY"
if Mount -t squashfs -o loop,ro,noatime -- "$FILE_SQFS" "$mount_squash"
then if [ -n "${DIR_CHANGE:++}" ]
then MountRW
startstat=$?
elif [ -n "${DIR_SQUASH:++}" ]
then MountRO
startstat=$?
else startstat=0
fi
else startstat=$?
Error $startstat \
"Failed mounting $FILE_SQFS as $mount_squash"
rmdir -- "$mount_squash"
fi
eend $startstat
}
start() {
START
}
MvTbzOld() {
[ -n "${FILE_TBZ_OLD:++}" ] || return 0
test -e "$FILE_TBZ" || return 0
${MV_FILE_TBZ:-mv --} "$FILE_TBZ" "$FILE_TBZ_OLD"
}
MvSqfsOld() {
[ -n "${FILE_SQFS_OLD:++}" ] || return 0
test -e "$FILE_SQFS" || return 0
${MV_FILE_SQFS:-mv --} "$FILE_SQFS" "$FILE_SQFS_OLD"
}
Rmdir() {
case ${1:-0} in
[nNfF]*|0|-)
return 0;;
*p*)
rmdirparent=:;;
*)
rmdirparent=false;;
esac
[ -z "${2:++}" ] || ! test -d "$2" && return
rmdir -- "$2" || {
ewarn "could not remove $2"
return 1
}
$rmdirparent && rmdir -p -- "${2%/*}" >/dev/null 2>&1
return 0
}
# Usage: ClearRw [keep] [action-before-first-removal]
ClearRw() {
[ -z "${DIR_CHANGE:++}" ] || ! test -d "$DIR_CHANGE" && return
Cleardir "$DIR_CHANGE" "$@" || return
Rmdir "$RM_DIR_CHANGE" "$DIR_CHANGE"
}
Tbzd() {
[ -z "${FILE_TBZ:++}" ] && return
MvTbzOld
Error $? "Failed moving $FILE_SQFS -> $FILE_SQFS_OLD"
${TARCMD:-tar -cjf} "$FILE_TBZ" "$DIRECTORY/" >/dev/null
}
Sqfsd() {
: ${TMP_SQFS:=$TMPDIR/$SVCNAME.sqfs.XXXXXXXX}
Checktemp TMP_SQFS
$TMP_SQFS_temp && Mktemp TMP_SQFS
chmod -- 644 "$TMP_SQFS"
sqfsdaddopt='-noappend'
sqfsdredir='>/dev/null'
case $VERBOSE_MODE in
0)
sqfsdaddopt=$sqfsdaddopt' -no-progress';;
2)
sqfsdredir=;;
esac
[ -n "${COMPRESSION:++}" ] && \
sqfsdaddopt=$sqfsdaddopt' -comp "$COMPRESSION"'
eval "mksquashfs \"\$DIRECTORY\" \"\$TMP_SQFS\" \
$sqfsdaddopt \$MKSQUASHFS $sqfsdredir" \
&& MvSqfsOld \
&& mv -f -- "$TMP_SQFS" "$FILE_SQFS"
}
CalcMagic() {
: ${MAGIC_FILE:=$DIR_CHANGE/.no-save}
}
HaveMagic() {
CalcMagic
test -e "$MAGIC_FILE"
}
CalcKill() {
: ${KILL_FILE:=$DIR_CHANGE/.No-save}
}
HaveKill() {
CalcKill
test -e "$KILL_FILE"
}
CalcIgnore() {
calcignorev=$1
shift
calcignorer=$1
case $calcignorer in
*' '*)
calcignorer="'(' $calcignorer ')'";;
esac
shift
calcignoreb=
for calcignorei
do [ -n "${calcignorer:++}" ] && calcignoreb=' -o '
[ -n "${calcignorei:++}" ] && calcignorei=/$calcignorei
calcignorer=$calcignorer$calcignoreb-path
calcignorer="$calcignorer \"\$DIR_CHANGE$calcignorei\""
done
case $calcignorev in
'@'*)
calcignorev=${calcignorev#?}
[ -n "${calcignoreb:++}" ] && \
calcignorer="'(' $calcignorer ')'";;
esac
eval $calcignorev=\$calcignorer
}
NeedSquash() {
[ -n "${DIR_CHANGE:++}" ] && test -d "$DIR_CHANGE" || return
[ -n "${DIRECTORY:++}" ] && test -d "$DIRECTORY" || return
eval "CalcIgnore @needsquashif \"\$IGNOREFILE\" \
$IGNORETOPFILE \
\"._funionfs*\" \
.wh..wh.aufs"
eval "CalcIgnore @needsquashid \"\$IGNOREDIR\" \
$IGNORETOPDIR \
.wh..wh.plink \
.wh..wh.plnk \
.wh..wh..tmp \
.wh..wh.orph \
.unionfs"
eval "CalcIgnore needsquashc \"\$IGNORE\" \
$IGNORETOP \
\"\""
[ -n "${needsquashif:++}" ] && \
needsquashc="$needsquashc -o '(' $needsquashif -type f ')'"
[ -n "${needsquashid:++}" ] && {
needsquashc="$needsquashc -o '(' $needsquashid"
needsquashc="$needsquashc -type d -prune ')'"
}
needsquashc="find \"\$DIR_CHANGE\" $needsquashc -o -exec "
eval "$needsquashc/bin/false '{}' '+'" && return 1
[ -z "${IGNORETOUCH:++}" ] && return
# Do the time-consuming file-comparison only if it appears necessary:
( d="for i
do case \${i#\"\$DIR_CHANGE/\"} in
$IGNORETOUCH)
c=\$DIR_SQUASH/\${i#\"\$DIR_CHANGE/\"}
if test -L \"\$i\"
then test -L \"\$c\"
else ! test -L \"\$c\"
fi || exit 1
test -d \"\$i\" && test -d \"\$c\" && continue
test -f \"\$i\" && test -f \"\$c\" \
&& diff -q -- \"\$i\" \"\$c\" >/dev/null 2>&1 && continue;;
esac
exit 1
done
:"
export DIR_CHANGE DIR_SQUASH
eval "! $needsquashc\"\${SHELL:-/bin/sh}\" -c -- \"\$d\" sh '{}' '+'"
)
}
CalcIgnoreThreshold() {
: ${THRESHOLD:=0}
[ $THRESHOLD -gt 0 ] || return
Normalize IGNORE_THRESHOLD || \
IGNORE_THRESHOLD=$DIR_CHANGE/.do-save
}
ExceedThreshold() {
CalcIgnoreThreshold || return 0
test -e "$IGNORE_THRESHOLD" && return
exceedthresholdn=`du -s -k -- "$DIR_CHANGE" | cut -f 1`
[ "$THRESHOLD" -le "$exceedthresholdn" ]
}
RemoveThreshold() {
case $IGNORE_THRESHOLD in
"$DIR_CHANGE/"*)
test -f "$IGNORE_THRESHOLD" || return 0
rm -- "$IGNORE_THRESHOLD";;
esac
}
Store() {
if HaveKill
then will_kill=kill
return 0
else will_kill=false
fi
HaveMagic && return
if ! NeedSquash
then will_kill=keep
return 0
fi
ExceedThreshold || return 0
Normalize FILE_SQFS_OLD FILE_TBZ FILE_TBZ_OLD
ebegin "Squashing $DIRECTORY to $FILE_SQFS"
einfo "(this may take a while)"
RemoveThreshold
Sqfsd
storestat=$?
if [ $storestat -eq 0 ]
then if [ -n "${FILE_TBZ:++}" ]
then eend $storestat
ebegin "Updating archive $FILE_TBZ"
einfo "(this may take a while)"
Tbzd
storestat=$?
Error $storestat \
"Creating $FILE_TBZ failed"
fi
will_kill=stored
else Error $storestat "Squashing $DIRECTORY failed"
fi
eend $storestat
return $storestat
}
IsUmounted() {
if [ -z "${mountinfo:++}" ]
then mountinfo=`Which mountinfo` && [ -n "${mountinfo:++}" ] \
|| mountinfo=:
fi
! "$mountinfo" -q "$1"
}
UmountSquash() {
[ -n "${1:++}" ] && sleep $1
IsUmounted "$DIR_SQUASH" || Umount -d -- "$DIR_SQUASH" && return
umountstatsquash=$?
Umount -d -- "$FILE_SQFS" && IsUmounted "$DIR_SQUASH" && return
return $umountstatsquash
}
UmountAll() {
ebegin "Umounting $DIRECTORY"
IsUmounted "$DIRECTORY" || Umount -- "$DIRECTORY"
umountallstat=$?
if [ -z "${DIR_SQUASH:++}" ]
then eend $umountallstat
return $umountallstat
fi
UmountSquash >/dev/null 2>&1 \
|| UmountSquash 1s >/dev/null 2>&1 \
|| UmountSquash 1s \
|| umountallstat=$?
Rmdir "$RM_DIR_SQUASH" "$DIR_SQUASH"
eend $umountallstat
return $umountallstat
}
RmNamefile() {
$use_name_file || return 0
[ -n "${NAME_FILE:++}" ] && rm -- "$NAME_FILE" && return
Error $? "cannot remove file $NAME_FILE"
}
KillBegin() {
ebegin "$1 $DIR_CHANGE"
kill_eend=eend
}
DoKill() {
kill_eend=:
case $will_kill in
kill)
ClearRw "$KILL_NOT" 'KillBegin "Killing"';;
keep)
ClearRw "$KEEP";;
stored)
ClearRw;;
esac
kill_stat=$?
Error $kill_stat "Clearing $DIR_CHANGE failed"
$kill_eend $kill_stat
return $kill_stat
}
STOP() {
Check && Store && UmountAll && DoKill && RmNamefile
}
stop() {
STOP
}
RESTART() {
STOP && START
}
will_squash() {
Check || return $?
if HaveKill
then echo 1 >&2
einfo "Have found kill-file $KILL_FILE"
einfo "Will delete $DIR_CHANGE"
return 0
fi
HaveMagic && nomagic=false || nomagic=:
ExceedThreshold && exceed=: || exceed=false
if $nomagic && $exceed && NeedSquash
then s="Will squash $DIRECTORY to $FILE_SQFS"
echo 1 >&2
ret=0
einfo "Have found modified $DIR_CHANGE"
else s="Will not squash $DIRECTORY to $FILE_SQFS"
echo 0 >&2
ret=1
$nomagic && $exceed && einfo "No modified $DIR_CHANGE"
fi
if $nomagic
then einfo "No magic file $MAGIC_FILE"
else einfo "Have found magic file $MAGIC_FILE"
fi
if $exceed
then [ $THRESHOLD -gt 0 ] && \
einfo "Exceeded threshold in $DIR_CHANGE"
else einfo "Threshold not exceeded in $DIR_CHANGE"
fi
einfo "$s"
return $ret
}
need_squash() {
Check || return $?
if NeedSquash
then echo 1 >&2
if HaveMagic
then einfo "Have found magic file $MAGIC_FILE"
else einfo "No magic file $MAGIC_FILE"
fi
einfo "Have found modified $DIR_CHANGE"
einfo "Need to squash $DIRECTORY to $FILE_SQFS"
return 0
fi
echo 0 >&2
einfo "No modified $DIR_CHANGE"
einfo "Need not squash $DIRECTORY to $FILE_SQFS"
return 1
}
have_magic() {
Check || return $?
if HaveMagic
then echo 1 >&2
einfo "Magic file $MAGIC_FILE exists."
return 0
fi
echo 0 >&2
einfo "No magic file $MAGIC_FILE exists."
return 1
}
have_kill() {
Check || return $?
if HaveKill
then echo 1 >&2
einfo "Kill-file $KILL_FILE exists."
return 0
fi
echo 0 >&2
einfo "No kill-file $KILL_FILE exists."
return 1
}
print_ignore_threshold() {
Check || return $?
CalcIgnoreThreshold && printf '%s' "$IGNORE_THRESHOLD" >&2
return 0
}
print_magic_file() {
Check || return $?
CalcMagic && printf '%s' "$MAGIC_FILE" >&2
return 0
}
print_kill_file() {
Check || return $?
CalcKill && printf '%s' "$KILL_FILE" >&2
return 0
}
print_dir_change() {
Check || return $?
printf '%s/' "$DIR_CHANGE" >&2
return 0
}
print_dir_squash() {
Check || return $?
printf '%s/' "$DIR_SQUASH" >&2
return 0
}
test -r '/etc/conf.d/squash_dir' && \
. '/etc/conf.d/squash_dir'
: