xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ftruncate/ftruncate01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Wayne Boyer
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
9*49cdfc7eSAndroid Build Coastguard Worker  *  Verify that, ftruncate() succeeds to truncate a file to a certain length,
10*49cdfc7eSAndroid Build Coastguard Worker  *  if the file previously is smaller than the truncated size, ftruncate()
11*49cdfc7eSAndroid Build Coastguard Worker  *  shall increase the size of the file.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE	"testfile"
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #define TRUNC_LEN1	256
26*49cdfc7eSAndroid Build Coastguard Worker #define TRUNC_LEN2	512
27*49cdfc7eSAndroid Build Coastguard Worker #define FILE_SIZE	1024
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker static int fd;
30*49cdfc7eSAndroid Build Coastguard Worker 
check_and_report(off_t offset,char data,off_t trunc_len)31*49cdfc7eSAndroid Build Coastguard Worker static void check_and_report(off_t offset, char data, off_t trunc_len)
32*49cdfc7eSAndroid Build Coastguard Worker {
33*49cdfc7eSAndroid Build Coastguard Worker 	int i, file_length;
34*49cdfc7eSAndroid Build Coastguard Worker 	char buf[FILE_SIZE];
35*49cdfc7eSAndroid Build Coastguard Worker 	struct stat stat_buf;
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	memset(buf, '*', sizeof(buf));
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FSTAT(fd, &stat_buf);
40*49cdfc7eSAndroid Build Coastguard Worker 	file_length = stat_buf.st_size;
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	if (file_length != trunc_len) {
43*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "ftruncate() got incorrected size: %d",
44*49cdfc7eSAndroid Build Coastguard Worker 			file_length);
45*49cdfc7eSAndroid Build Coastguard Worker 		return;
46*49cdfc7eSAndroid Build Coastguard Worker 	}
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSEEK(fd, offset, SEEK_SET);
49*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_READ(0, fd, buf, sizeof(buf));
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < TRUNC_LEN1; i++) {
52*49cdfc7eSAndroid Build Coastguard Worker 		if (buf[i] != data) {
53*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL,
54*49cdfc7eSAndroid Build Coastguard Worker 				"ftruncate() got incorrect data %i, expected %i",
55*49cdfc7eSAndroid Build Coastguard Worker 				buf[i], data);
56*49cdfc7eSAndroid Build Coastguard Worker 			return;
57*49cdfc7eSAndroid Build Coastguard Worker 		}
58*49cdfc7eSAndroid Build Coastguard Worker 	}
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "ftruncate() succeeded");
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker 
verify_ftruncate(void)63*49cdfc7eSAndroid Build Coastguard Worker static void verify_ftruncate(void)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Truncated length smaller than file size");
66*49cdfc7eSAndroid Build Coastguard Worker 	TEST(ftruncate(fd, TRUNC_LEN1));
67*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
68*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "ftruncate() failed");
69*49cdfc7eSAndroid Build Coastguard Worker 		return;
70*49cdfc7eSAndroid Build Coastguard Worker 	}
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	check_and_report(0, 'a', TRUNC_LEN1);
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Truncated length exceeds file size");
75*49cdfc7eSAndroid Build Coastguard Worker 	TEST(ftruncate(fd, TRUNC_LEN2));
76*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
77*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "ftruncate() failed");
78*49cdfc7eSAndroid Build Coastguard Worker 		return;
79*49cdfc7eSAndroid Build Coastguard Worker 	}
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	check_and_report(TRUNC_LEN1, 0, TRUNC_LEN2);
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)84*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
85*49cdfc7eSAndroid Build Coastguard Worker {
86*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_fill_file(TESTFILE, 'a', FILE_SIZE, 1))
87*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Failed to create test file");
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(TESTFILE, O_RDWR);
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)92*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
93*49cdfc7eSAndroid Build Coastguard Worker {
94*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
95*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
96*49cdfc7eSAndroid Build Coastguard Worker }
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
99*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_ftruncate,
100*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
101*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
102*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
103*49cdfc7eSAndroid Build Coastguard Worker };
104