#!/bin/sh

# Copyright (C) 2006, 2007 Junjiro Okajima
#
# This program, aufs is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# $Id: auplink,v 1.6 2007/04/16 01:19:49 sfjro Exp $

#set -x
tmp=/tmp/$$
set -e

Usage()
{
	cat <<- EOF 1>&2
	usage: $0 aufs_mount_point list|cpup|flush
	'list' shows the pseudo-linked inode numbers and filenames.
	'cpup' copies-up all pseudo-link to the writeble branch.
	'flush' calls 'cpup', and then "mount -o remount,clean_plink=inum"
	and remove the whiteouted plink.
	EOF
	rm -f $tmp $tmp.*
	exit 1
}

Find() # dir [ find_args ]
{
	local dir="$1"
	shift
	find "$dir" -xdev -noleaf $@
}

List()
{
	sed -e 's/^\([0-9]*\) .*$/-o -inum \1/' |
	xargs > $tmp.args
	Find "$mntpnt" \( `cut -c3- $tmp.args` \) $@
}

Cpup()
{
	List \( \( -type l -fprint $tmp.slink \) \
		-o ! -type l -fprint $tmp.other  \)

	# do nothing but update something. it causes copyup.
	xargs -r touch -ac -r $tmp.other < $tmp.other
	xargs -r aulchown < $tmp.slink
}

test $# -eq 2 || Usage
cd "$1"
mntpnt=`readlink -f "$PWD"`
cd "$OLDPWD"

do_flush=0
f=${AUPLINK_CONFIG:-/etc/default/auplink}
if [ -r $f ]
then
	. $f
	for i in $FLUSH
	do
		test "$i" = "$mntpnt" -o "$i" = "ALL" || continue
		do_flush=1
		break
	done
fi
case $2 in
cpup|flush) test $do_flush -eq 0 && exit 0;;
esac

brs=/sys/fs/aufs/brs
if [ -r $brs ]
then
	grep " $mntpnt " $brs |
	tail -n 1 |
	tr '[, ]' '\n'
else
	grep " $mntpnt " /proc/mounts |
	tail -n 1 |
	tr ',' '\n'
fi |
sed -e 's/\\/\\\\/g' |
egrep '^(dirs=|br:)' |
tr ':' '\n' |
grep '=rw$' |
sed -e 's/=rw$//' |
while read wbr
do
	d=`echo $wbr/.wh..wh.plink`
	# -printf '%n %p\n'
	test -d "$d" && Find "$d" -maxdepth 1 ! -type d -printf '%n\t%p\n'
done |
while read nlink pname
do
	inum=`basename "$pname" | cut -f1 -d.`
	echo $inum $nlink "$pname"
done > $tmp
test -w $brs && >> $brs
test -s $tmp || { rm -f $tmp $tmp.*; exit 0; }

# debug
#mount -o remount,list_plink "$mntpnt"

case $2 in
list)
	cut -f1 -d' ' $tmp | tr '\n' ' '
	echo
	List < $tmp
	;;
cpup)
	Cpup < $tmp
	;;
flush)
	#echo -n Flushing pseudo-links on "$mntpnt"...
	Cpup < $tmp

	# all are copied-up, and then remove all pseudo-links.
	mount -o remount,clean_plink "$mntpnt"
	cut -f3- -d' ' $tmp | xargs -r rm
	# debug
	#mount -o remount,list_plink "$mntpnt"
	#echo done.
	;;
*)
	Usage;;
esac

rm -f $tmp $tmp.*
