• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * Test Name: ftruncate01
22  *
23  * Test Description:
24  *  Verify that, ftruncate(2) succeeds to truncate a file to a specified
25  *  length if the file indicated by file descriptor opened for writing.
26  *
27  * Expected Result:
28  *  ftruncate(2) should return a value 0 and the length of the file after
29  *  truncation should be equal to the length it is truncated to.
30  *
31  * Algorithm:
32  *  Setup:
33  *   Setup signal handling.
34  *   Create temporary directory.
35  *   Pause for SIGUSR1 if option specified.
36  *
37  *  Test:
38  *   Loop if the proper options are given.
39  *   Execute system call
40  *   Check return code, if system call failed (return=-1)
41  *	Log the errno and Issue a FAIL message.
42  *   Otherwise,
43  *	Verify the Functionality of system call
44  *      if successful,
45  *		Issue Functionality-Pass message.
46  *      Otherwise,
47  *		Issue Functionality-Fail message.
48  *  Cleanup:
49  *   Print errno log and/or timing stats if options given
50  *   Delete the temporary directory created.
51  *
52  * Usage:  <for command-line>
53  *  ftruncate01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
54  *     where,  -c n : Run n copies concurrently.
55  *             -f   : Turn off functionality Testing.
56  *	       -i n : Execute test n times.
57  *	       -I x : Execute test for x seconds.
58  *	       -P x : Pause for x seconds between iterations.
59  *	       -t   : Turn on syscall timing.
60  *
61  * HISTORY
62  *	07/2001 Ported by Wayne Boyer
63  *
64  * RESTRICTIONS:
65  *  This test should be run by 'non-super-user' only.
66  *
67  */
68 
69 #include <sys/types.h>
70 #include <sys/stat.h>
71 #include <fcntl.h>
72 #include <errno.h>
73 #include <string.h>
74 #include <signal.h>
75 #include <inttypes.h>
76 
77 #include "test.h"
78 
79 #define TESTFILE	"testfile"	/* file under test */
80 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
81 #define BUF_SIZE	256	/* buffer size */
82 #define FILE_SIZE	1024	/* test file size */
83 #define TRUNC_LEN	256	/* truncation length */
84 
85 TCID_DEFINE(ftruncate01);
86 int TST_TOTAL = 1;		/* Total number of test conditions */
87 int fildes;			/* file descriptor for test file */
88 
89 void setup();			/* setup function for the test */
90 void cleanup();			/* cleanup function for the test */
91 
main(int ac,char ** av)92 int main(int ac, char **av)
93 {
94 	struct stat stat_buf;	/* stat(2) struct contents */
95 	int lc;
96 	off_t file_length;	/* test file length */
97 
98 	tst_parse_opts(ac, av, NULL, NULL);
99 
100 	setup();
101 
102 	for (lc = 0; TEST_LOOPING(lc); lc++) {
103 
104 		tst_count = 0;
105 
106 		/*
107 		 * Call ftruncate(2) to truncate a test file to a
108 		 * specified length.
109 		 */
110 		TEST(ftruncate(fildes, TRUNC_LEN));
111 
112 		if (TEST_RETURN == -1) {
113 			tst_resm(TFAIL | TTERRNO, "ftruncate(%s) failed",
114 				 TESTFILE);
115 			continue;
116 		}
117 		/*
118 		 * Get the testfile information using
119 		 * fstat(2).
120 		 */
121 		if (fstat(fildes, &stat_buf) < 0) {
122 			tst_brkm(TFAIL, cleanup,
123 				 "stat(2) of %s failed, error:%d",
124 				 TESTFILE, errno);
125 		}
126 		stat_buf.st_mode &= ~S_IFREG;
127 		file_length = stat_buf.st_size;
128 
129 		/*
130 		 * Check for expected size of testfile after
131 		 * truncate(2) on it.
132 		 */
133 		if (file_length != TRUNC_LEN) {
134 			tst_resm(TFAIL,
135 				 "%s: Incorrect file size %" PRId64 ", "
136 				 "Expected %d", TESTFILE,
137 				 (int64_t) file_length, TRUNC_LEN);
138 		} else {
139 			tst_resm(TPASS, "Functionality of ftruncate() "
140 				 "on %s successful", TESTFILE);
141 		}
142 	}
143 
144 	cleanup();
145 	tst_exit();
146 }
147 
148 /*
149  * void
150  * setup() - performs all ONE TIME setup for this test.
151  *  Create a temporary directory and change directory to it.
152  *  Create a test file under temporary directory and write some
153  *  data into it.
154  */
setup(void)155 void setup(void)
156 {
157 	int i;
158 	int c, c_total = 0;	/* bytes to be written to file */
159 	char tst_buff[BUF_SIZE];	/* buffer to hold data */
160 
161 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
162 
163 	TEST_PAUSE;
164 
165 	tst_tmpdir();
166 
167 	/* Fill the test buffer with the known data */
168 	for (i = 0; i < BUF_SIZE; i++) {
169 		tst_buff[i] = 'a';
170 	}
171 
172 	/* open a file for reading/writing */
173 	fildes = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
174 	if (fildes == -1)
175 		tst_brkm(TBROK | TERRNO, cleanup,
176 			 "open(%s, O_RDWR|O_CREAT, %o) failed",
177 			 TESTFILE, FILE_MODE);
178 
179 	/* Write to the file 1k data from the buffer */
180 	while (c_total < FILE_SIZE) {
181 		if ((c = write(fildes, tst_buff, sizeof(tst_buff))) <= 0) {
182 			tst_brkm(TBROK | TERRNO, cleanup, "write(%s) failed",
183 				 TESTFILE);
184 		} else {
185 			c_total += c;
186 		}
187 	}
188 }
189 
190 /*
191  * void
192  * cleanup() - performs all ONE TIME cleanup for this test at
193  *	       completion or premature exit.
194  *  Close the temporary file.
195  *  Remove the test directory and testfile created in the setup.
196  */
cleanup(void)197 void cleanup(void)
198 {
199 
200 	/* Close the testfile after writing data into it */
201 	if (close(fildes) == -1)
202 		tst_brkm(TFAIL | TERRNO, NULL, "close(%s) failed", TESTFILE);
203 
204 	tst_rmdir();
205 
206 }
207