tazpanel rev 537

hosts.cgi: move state files (again) to /var/lib (previous /var/run is tmpfs); show diff after list update
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sun Aug 30 23:04:04 2015 +0300 (2015-08-30)
parents a53b0b636b98
children 082b622cb781
files hosts.cgi
line diff
     1.1 --- a/hosts.cgi	Sun Aug 30 18:52:48 2015 +0200
     1.2 +++ b/hosts.cgi	Sun Aug 30 23:04:04 2015 +0300
     1.3 @@ -26,7 +26,7 @@
     1.4  # now hosts='host1 host2 ... hostn'
     1.5  
     1.6  # Folder to save downloaded and installed hosts lists
     1.7 -HOSTSDIR='/var/run/tazpanel/hosts'
     1.8 +HOSTSDIR='/var/lib/tazpanel/hosts'
     1.9  mkdir -p "$HOSTSDIR"
    1.10  
    1.11  
    1.12 @@ -70,7 +70,7 @@
    1.13  	awk -vl="$list" '$1=="0.0.0.0"||$1=="127.0.0.1"{printf "0.0.0.0 %s #%s\n", $2, l}' "$file" | fgrep -v localhost >> /etc/hosts
    1.14  	# Clean the list
    1.15  	echo -n > "$file"
    1.16 -	touch "$file.up"
    1.17 +	touch "$file.checked"
    1.18  
    1.19  	# Remove the duplicate entries
    1.20  	hostsnew=$(mktemp)
    1.21 @@ -90,7 +90,7 @@
    1.22  	# Input: list=code letter
    1.23  	sed -i "/#$list$/d" /etc/hosts
    1.24  	file="$HOSTSDIR/$list"
    1.25 -	rm "$file" "$file.up" "$file.avail"
    1.26 +	rm "$file" "$file.checked" "$file.avail"
    1.27  }
    1.28  
    1.29  
    1.30 @@ -130,6 +130,8 @@
    1.31  		echo "<p>$(_ 'Installing the "%s"...' "$name") "
    1.32  		instlist
    1.33  		echo "$(_ 'Done')</p>"
    1.34 +		# Don't show diff because it's huge
    1.35 +		rm "$HOSTSDIR/diff"
    1.36  		;;
    1.37  
    1.38  	*\ uplist\ *)
    1.39 @@ -138,8 +140,30 @@
    1.40  		list="$(GET uplist)"
    1.41  		getlistspec "$list"
    1.42  		echo "<p>$(_ 'Updating the "%s"...' "$name") "
    1.43 +
    1.44 +		old_sublist=$(mktemp)
    1.45 +		# Note, old sublist already sorted. Only hostnames here
    1.46 +		awk -vlist="#$list" '$3 == list {print $2}' /etc/hosts > "$old_sublist"
    1.47 +
    1.48  		remlist; instlist
    1.49 +
    1.50 +		new_sublist=$(mktemp)
    1.51 +		awk -vlist="#$list" '$3 == list {print $2}' /etc/hosts > "$new_sublist"
    1.52 +
    1.53 +		# The diff: just '+' and '-', no header, no context
    1.54 +		diff -dU0 "$old_sublist" "$new_sublist" | sed '1,2d;/^@/d' > "$HOSTSDIR/diff"
    1.55 +
    1.56  		echo "$(_ 'Done')</p>"
    1.57 +
    1.58 +		# Show diff
    1.59 +		if [ -s "$HOSTSDIR/diff" ]; then
    1.60 +			echo '<section><pre class="scroll">'
    1.61 +			cat "$HOSTSDIR/diff" | syntax_highlighter diff
    1.62 +			echo '</pre></section>'
    1.63 +		fi
    1.64 +
    1.65 +		# Clean
    1.66 +		rm "$old_sublist" "$new_sublist" "$HOSTSDIR/diff"
    1.67  		;;
    1.68  
    1.69  	*\ remlist\ *)
    1.70 @@ -247,9 +271,9 @@
    1.71  		[ ! -f "$HOSTSDIR/$letter" ] && touch "$HOSTSDIR/$letter"
    1.72  
    1.73  		# Check for upgrades (once a day)
    1.74 -		if [ -f "$HOSTSDIR/$letter.up" ]; then
    1.75 +		if [ -f "$HOSTSDIR/$letter.checked" ]; then
    1.76  			# Update checked previously
    1.77 -			if [ "$(($(date -u +%s) - 86400))" -gt "$(date -ur "$HOSTSDIR/$letter.up" +%s)" ]; then
    1.78 +			if [ "$(($(date -u +%s) - 86400))" -gt "$(date -ur "$HOSTSDIR/$letter.checked" +%s)" ]; then
    1.79  				# Update checked more than one day (86400 seconds) ago
    1.80  				check='yes'
    1.81  			else
    1.82 @@ -271,7 +295,7 @@
    1.83  				# Update not available
    1.84  				rm "$HOSTSDIR/$letter.avail" 2>/dev/null
    1.85  			fi
    1.86 -			touch "$HOSTSDIR/$letter.up"
    1.87 +			touch "$HOSTSDIR/$letter.checked"
    1.88  		fi
    1.89  
    1.90  		if [ -f "$HOSTSDIR/$letter.avail" ]; then