xref: /aosp_15_r20/external/ltp/lib/tst_fs_has_free.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2014 Fujitsu Ltd.
3*49cdfc7eSAndroid Build Coastguard Worker  * Author: Xiaoguang Wang <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
6*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
7*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
10*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License along
14*49cdfc7eSAndroid Build Coastguard Worker  * with this program; if not, write the Free Software Foundation, Inc.,
15*49cdfc7eSAndroid Build Coastguard Worker  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker /*
19*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
20*49cdfc7eSAndroid Build Coastguard Worker  *	Check if the mounted file system has enough free space,
21*49cdfc7eSAndroid Build Coastguard Worker  *	if it is, tst_fs_has_free() returns 1, otherwise 0.
22*49cdfc7eSAndroid Build Coastguard Worker  */
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #include <stdint.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <sys/vfs.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_fs.h"
28*49cdfc7eSAndroid Build Coastguard Worker 
tst_fs_has_free_(void (* cleanup)(void),const char * path,uint64_t size,unsigned int mult)29*49cdfc7eSAndroid Build Coastguard Worker int tst_fs_has_free_(void (*cleanup)(void), const char *path,
30*49cdfc7eSAndroid Build Coastguard Worker 		     uint64_t size, unsigned int mult)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker 	struct statfs sf;
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	if (statfs(path, &sf)) {
35*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup,
36*49cdfc7eSAndroid Build Coastguard Worker 			 "tst_fs_has_free: failed to statfs(%s)", path);
37*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
38*49cdfc7eSAndroid Build Coastguard Worker 	}
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	if ((uint64_t)sf.f_bavail * sf.f_bsize >= (uint64_t)size * mult)
41*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
44*49cdfc7eSAndroid Build Coastguard Worker }
45