1#!/bin/sh
2# $1: filesystem type, $2: device path.
3
4
5ret_dir=/tmp/fsck_ret
6
7
8# $1: device path, $2: error code.
9_set_fsck_code(){
10	pool_name=`echo "$1" |awk '{FS="/"; print $NF}'`
11
12	if [ ! -d "$ret_dir" ]; then
13		rm -rf $ret_dir
14		mkdir $ret_dir
15	fi
16
17	rm -f $ret_dir/$pool_name.[0-3]
18	touch "$ret_dir/$pool_name.$2"
19}
20
21# $1: device path.
22_get_fsck_logfile(){
23	pool_name=`echo "$1" |awk '{FS="/"; print $NF}'`
24
25	if [ ! -d "$ret_dir" ]; then
26		rm -rf $ret_dir
27		mkdir $ret_dir
28	fi
29
30	echo "$ret_dir/$pool_name.log"
31}
32
33
34if [ -z "$1" ] || [ -z "$2" ]; then
35	echo "Usage: app_fsck.sh [filesystem type] [device's path]"
36	exit 0
37elif [ "$1" == "vfat" ] || [ "$1" == "msdos" ] || [ "$1" == "fuseblk" ]; then
38	_set_fsck_code $2 3
39	exit 0
40fi
41
42autocheck_option=
43autofix_option=
44autofix=`nvram get apps_state_autofix`
45ntfs_mod=`nvram get usb_ntfs_mod`
46hfs_mod=`nvram get usb_hfs_mod`
47
48log_file=`_get_fsck_logfile $2`
49log_option="> $log_file 2>&1"
50
51if [ "$autofix" == "1" ]; then
52	if [ "$1" == "tfat" ]; then
53		autocheck_option="-s"
54		autofix_option="-r"
55	elif [ "$1" == "ntfs" ] || [ "$1" == "tntfs" ]; then
56		if [ "$ntfs_mod" == "open" ]; then
57			autocheck_option=
58			autofix_option="-f"
59		elif [ "$ntfs_mod" == "paragon" ]; then
60			autocheck_option=
61			autofix_option="-f"
62		elif [ "$ntfs_mod" == "tuxera" ]; then
63			autocheck_option=
64			autofix_option="-f"
65		fi
66	elif [ "$1" == "hfs" ] || [ "$1" == "hfsplus" ] || [ "$1" == "thfsplus" ] || [ "$1" == "hfs+j" ] || [ "$1" == "hfs+jx" ]; then
67		if [ "$hfs_mod" == "open" ]; then
68			autocheck_option=
69			autofix_option="-f"
70		elif [ "$hfs_mod" == "paragon" ]; then
71			autocheck_option=
72			autofix_option="-f"
73		elif [ "$hfs_mod" == "tuxera" ]; then
74			autocheck_option=
75			autofix_option="-fy"
76		fi
77	else
78		autocheck_option=
79		autofix_option=p
80	fi
81else
82	if [ "$1" == "tfat" ]; then
83		autocheck_option="-s"
84		autofix_option=
85	elif [ "$1" == "ntfs" ] || [ "$1" == "tntfs" ]; then
86		if [ "$ntfs_mod" == "open" ]; then
87			autocheck_option=
88			autofix_option="-f"
89		elif [ "$ntfs_mod" == "paragon" ]; then
90			autocheck_option=
91			autofix_option=
92		elif [ "$ntfs_mod" == "tuxera" ]; then
93			autocheck_option="-c"
94			autofix_option=
95		fi
96	elif [ "$1" == "hfs" ] || [ "$1" == "hfsplus" ] || [ "$1" == "thfsplus" ] || [ "$1" == "hfs+j" ] || [ "$1" == "hfs+jx" ]; then
97		if [ "$hfs_mod" == "open" ]; then
98			autocheck_option="-q"
99			autofix_option=
100		elif [ "$hfs_mod" == "paragon" ]; then
101			autocheck_option=
102			autofix_option=
103		elif [ "$hfs_mod" == "tuxera" ]; then
104			autocheck_option="-qy"
105			autofix_option=
106		fi
107	else
108		autocheck_option=n
109		autofix_option=
110	fi
111fi
112
113free_caches -w 0
114set -o pipefail
115_set_fsck_code $2 2
116if [ "$1" == "tfat" ]; then
117	# return value = 0: FS be ok.
118	eval fatfsck $autocheck_option $autofix_option $2 $log_option
119
120	if [ "$?" == "0" ]; then
121		_set_fsck_code $2 0
122	else
123		_set_fsck_code $2 1
124	fi
125elif [ "$1" == "ntfs" ] || [ "$1" == "tntfs" ]; then
126	if [ "$ntfs_mod" == "paragon" ]; then
127		# return value = 1: FS be ok.
128		c=0
129		RET=0
130		while [ ${c} -lt 4 -a ${RET} -ne 1 ] ; do
131			c=$((c+1))
132			eval chkntfs $autocheck_option $autofix_option --verbose $2 $log_option
133			RET=$?
134			if [ ${RET} -ge 251 -a ${RET} -le 254 ] ; then
135				break
136			fi
137		done
138
139		if [ "${RET}" == "1" ]; then
140			_set_fsck_code $2 0
141		else
142			_set_fsck_code $2 1
143		fi
144
145		# remove "x%", "\r\r", "\r".
146		sed -i 's/[0-9]*%//g' $log_file
147		tr -d '\r' < $log_file > $log_file.tmp
148		mv $log_file.tmp $log_file
149	elif [ "$ntfs_mod" == "tuxera" ]; then
150		# return value = 0: FS be ok.
151		eval ntfsck $autocheck_option $autofix_option $2 $log_option
152
153		if [ "$?" == "0" ]; then
154			_set_fsck_code $2 0
155		else
156			_set_fsck_code $2 1
157		fi
158
159		# remove the logs. e.q. "Record 676 has wrong SeqNo (378 <> 405)"
160		# The characters: '<', '>' would cause the ajax error.
161		mv -f $log_file $log_file.orig ; cat $log_file.orig | \
162			grep -v "has wrong SeqNo" | \
163			grep -v "ntfs_attr_pread partial read" | \
164			grep -v "ntfs_attr_pwrite partial write" | \
165			grep -v "Unexpected attrlist size" | \
166			grep -v "has corrupt allocation size" | \
167			grep -v "^$" > $log_file
168		rm -f $log_file.orig
169	fi
170elif [ "$1" == "hfs" ] || [ "$1" == "hfsplus" ] || [ "$1" == "thfsplus" ] || [ "$1" == "hfs+j" ] || [ "$1" == "hfs+jx" ]; then
171	if [ "$hfs_mod" == "open" ]; then
172		eval fsck.hfsplus $autocheck_option $autofix_option $2 $log_option
173
174		if [ "$?" == "0" ]; then
175			_set_fsck_code $2 0
176		else
177			_set_fsck_code $2 1
178		fi
179	elif [ "$hfs_mod" == "paragon" ]; then
180		# return value = 1: FS be ok.
181		eval chkhfs $autocheck_option $autofix_option --verbose $2 $log_option
182
183		if [ "$?" == "1" ]; then
184			_set_fsck_code $2 0
185		else
186			_set_fsck_code $2 1
187		fi
188
189		# remove "x%", "\r\r", "\r".
190		sed -i 's/[0-9]*%//g' $log_file
191		tr -d '\r' < $log_file > $log_file.tmp
192		mv $log_file.tmp $log_file
193	elif [ "$hfs_mod" == "tuxera" ]; then
194		# return value = 0: FS be ok.
195		eval fsck_hfs $autocheck_option $autofix_option $2 $log_option
196
197		if [ "$?" == "0" ]; then
198			_set_fsck_code $2 0
199		else
200			_set_fsck_code $2 1
201		fi
202	fi
203else
204	eval fsck.$1 -$autocheck_option$autofix_option -v $2 $log_option
205
206	if [ "$?" == "0" ]; then
207		_set_fsck_code $2 0
208	else
209		_set_fsck_code $2 1
210	fi
211fi
212free_caches -w 0 -c 0
213
214