blob: 4c2fe52450f757e532d14c7ff593e1295c0eb715 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
subrata_modak4bb656a2009-02-26 12:02:09 +000020/*
plars865695b2001-08-27 22:15:12 +000021 * 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:
subrata_modak4bb656a2009-02-26 12:02:09 +000028 * ftruncate(2) should return a value 0 and the length of the file after
plars865695b2001-08-27 22:15:12 +000029 * truncation should be equal to the length it is truncated to.
subrata_modakbdbaec52009-02-26 12:14:51 +000030 *
plars865695b2001-08-27 22:15:12 +000031 * 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)
subrata_modak56207ce2009-03-23 13:35:39 +000041 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000042 * Otherwise,
subrata_modak56207ce2009-03-23 13:35:39 +000043 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000044 * if successful,
subrata_modak56207ce2009-03-23 13:35:39 +000045 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000046 * 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 <sys/fcntl.h>
72#include <errno.h>
73#include <string.h>
74#include <signal.h>
vapierc20c04b2009-08-28 13:00:33 +000075#include <inttypes.h>
plars865695b2001-08-27 22:15:12 +000076
77#include "test.h"
plars865695b2001-08-27 22:15:12 +000078
subrata_modak56207ce2009-03-23 13:35:39 +000079#define TESTFILE "testfile" /* file under test */
plars865695b2001-08-27 22:15:12 +000080#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
subrata_modak56207ce2009-03-23 13:35:39 +000081#define BUF_SIZE 256 /* buffer size */
82#define FILE_SIZE 1024 /* test file size */
83#define TRUNC_LEN 256 /* truncation length */
plars865695b2001-08-27 22:15:12 +000084
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020085TCID_DEFINE(ftruncate01);
subrata_modak56207ce2009-03-23 13:35:39 +000086int TST_TOTAL = 1; /* Total number of test conditions */
plars865695b2001-08-27 22:15:12 +000087int fildes; /* file descriptor for test file */
88
89void setup(); /* setup function for the test */
90void cleanup(); /* cleanup function for the test */
91
subrata_modak56207ce2009-03-23 13:35:39 +000092int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000093{
94 struct stat stat_buf; /* stat(2) struct contents */
Cyril Hrubis89af32a2012-10-24 16:39:11 +020095 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020096 const char *msg;
plars865695b2001-08-27 22:15:12 +000097 off_t file_length; /* test file length */
subrata_modak56207ce2009-03-23 13:35:39 +000098
Garrett Cooper45e285d2010-11-22 12:19:25 -080099 msg = parse_opts(ac, av, NULL, NULL);
100 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000101 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800102
plars865695b2001-08-27 22:15:12 +0000103 }
104
plars865695b2001-08-27 22:15:12 +0000105 setup();
106
plars865695b2001-08-27 22:15:12 +0000107 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800108
Caspar Zhangd59a6592013-03-07 14:59:12 +0800109 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000110
subrata_modak4bb656a2009-02-26 12:02:09 +0000111 /*
112 * Call ftruncate(2) to truncate a test file to a
plars865695b2001-08-27 22:15:12 +0000113 * specified length.
114 */
115 TEST(ftruncate(fildes, TRUNC_LEN));
116
plars865695b2001-08-27 22:15:12 +0000117 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800118 tst_resm(TFAIL | TTERRNO, "ftruncate(%s) failed",
119 TESTFILE);
plars865695b2001-08-27 22:15:12 +0000120 continue;
121 }
122 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200123 * Get the testfile information using
124 * fstat(2).
plars865695b2001-08-27 22:15:12 +0000125 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200126 if (fstat(fildes, &stat_buf) < 0) {
127 tst_brkm(TFAIL, cleanup,
128 "stat(2) of %s failed, error:%d",
129 TESTFILE, errno);
130 }
131 stat_buf.st_mode &= ~S_IFREG;
132 file_length = stat_buf.st_size;
plars865695b2001-08-27 22:15:12 +0000133
Cyril Hrubise38b9612014-06-02 17:20:57 +0200134 /*
135 * Check for expected size of testfile after
136 * truncate(2) on it.
137 */
138 if (file_length != TRUNC_LEN) {
139 tst_resm(TFAIL,
140 "%s: Incorrect file size %" PRId64 ", "
141 "Expected %d", TESTFILE,
142 (int64_t) file_length, TRUNC_LEN);
plars865695b2001-08-27 22:15:12 +0000143 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200144 tst_resm(TPASS, "Functionality of ftruncate() "
145 "on %s successful", TESTFILE);
plars865695b2001-08-27 22:15:12 +0000146 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800147 }
plars865695b2001-08-27 22:15:12 +0000148
plars865695b2001-08-27 22:15:12 +0000149 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800150 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800151}
plars865695b2001-08-27 22:15:12 +0000152
153/*
154 * void
155 * setup() - performs all ONE TIME setup for this test.
156 * Create a temporary directory and change directory to it.
157 * Create a test file under temporary directory and write some
158 * data into it.
159 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400160void setup(void)
plars865695b2001-08-27 22:15:12 +0000161{
Garrett Cooper2c282152010-12-16 00:55:50 -0800162 int i;
subrata_modak56207ce2009-03-23 13:35:39 +0000163 int c, c_total = 0; /* bytes to be written to file */
plars865695b2001-08-27 22:15:12 +0000164 char tst_buff[BUF_SIZE]; /* buffer to hold data */
165
plars865695b2001-08-27 22:15:12 +0000166 tst_sig(NOFORK, DEF_HANDLER, cleanup);
167
plars865695b2001-08-27 22:15:12 +0000168 TEST_PAUSE;
169
plars865695b2001-08-27 22:15:12 +0000170 tst_tmpdir();
171
172 /* Fill the test buffer with the known data */
173 for (i = 0; i < BUF_SIZE; i++) {
174 tst_buff[i] = 'a';
175 }
176
177 /* open a file for reading/writing */
vapierc20c04b2009-08-28 13:00:33 +0000178 fildes = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
179 if (fildes == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 tst_brkm(TBROK | TERRNO, cleanup,
vapierc20c04b2009-08-28 13:00:33 +0000181 "open(%s, O_RDWR|O_CREAT, %o) failed",
182 TESTFILE, FILE_MODE);
plars865695b2001-08-27 22:15:12 +0000183
184 /* Write to the file 1k data from the buffer */
185 while (c_total < FILE_SIZE) {
186 if ((c = write(fildes, tst_buff, sizeof(tst_buff))) <= 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 tst_brkm(TBROK | TERRNO, cleanup, "write(%s) failed",
188 TESTFILE);
plars865695b2001-08-27 22:15:12 +0000189 } else {
190 c_total += c;
191 }
192 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800193}
plars865695b2001-08-27 22:15:12 +0000194
195/*
196 * void
197 * cleanup() - performs all ONE TIME cleanup for this test at
198 * completion or premature exit.
199 * Close the temporary file.
200 * Remove the test directory and testfile created in the setup.
201 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400202void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000203{
plars865695b2001-08-27 22:15:12 +0000204
205 /* Close the testfile after writing data into it */
vapierc20c04b2009-08-28 13:00:33 +0000206 if (close(fildes) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800207 tst_brkm(TFAIL | TERRNO, NULL, "close(%s) failed", TESTFILE);
plars865695b2001-08-27 22:15:12 +0000208
plars865695b2001-08-27 22:15:12 +0000209 tst_rmdir();
210
Chris Dearmanec6edca2012-10-17 19:54:01 -0700211}