• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2002
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  * NAME
22  *      pwrite04.c (ported from SPIE, section2/filesuite/pread_pwrite.c,
23  *      	        by Airong Zhang)
24  *
25  * TEST SUMMARY
26  *	Test the pwrite() system call with O_APPEND.
27  *
28  * USAGE
29  *  	pwrite04
30  *
31  */
32 
33 #define _XOPEN_SOURCE 500
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <memory.h>
40 #include <errno.h>
41 #include "test.h"
42 
43 char *TCID = "pwrite04";
44 int TST_TOTAL = 1;
45 int local_flag;
46 
47 #define PASSED 1
48 #define FAILED 0
49 
50 int block_cnt = 0;
51 
52 #define K1    		1024
53 #define K2    		(K1 * 2)
54 #define K3    		(K1 * 3)
55 #define K4    		(K1 * 4)
56 #define K5    		(K1 * 5)
57 #define	NBUFS 		4
58 #define DATA_FILE	"pwrite04_file"
59 
60 char name[256], fname[256];
61 
62 void init_buffers(char *[]);
63 void l_seek(int, off_t, int, off_t);
64 static void cleanup(void);
65 
main(int ac,char * av[])66 int main(int ac, char *av[])
67 {
68 	int fd;
69 	int nbytes;
70 	char *wbuf[NBUFS];
71 	struct stat statbuf;
72 	int lc;
73 
74 	strcpy(name, DATA_FILE);
75 	sprintf(fname, "%s.%d", name, getpid());
76 
77 	tst_parse_opts(ac, av, NULL, NULL);
78 
79 	tst_tmpdir();
80 	for (lc = 0; TEST_LOOPING(lc); lc++) {
81 
82 		init_buffers(wbuf);
83 		local_flag = PASSED;
84 
85 		if ((fd = open(fname, O_RDWR | O_CREAT, 0666)) < 0) {
86 			tst_resm(TBROK, "open failed: fname = %s, errno = %d",
87 				 fname, errno);
88 			cleanup();
89 		}
90 		/*
91 		 * pwrite() K1 of data (0's) at offset 0.
92 		 */
93 		if ((nbytes = pwrite(fd, wbuf[0], K1, 0)) != K1) {
94 			tst_resm(TFAIL,
95 				 "pwrite at 0 failed: nbytes=%d, errno=%d",
96 				 nbytes, errno);
97 			cleanup();
98 		}
99 
100 		/*
101 		 * We should still be at offset 0.
102 		 */
103 		l_seek(fd, 0, SEEK_CUR, 0);
104 
105 		/*
106 		 * lseek() to a non K boundary, just to be different.
107 		 */
108 		l_seek(fd, K1 / 2, SEEK_SET, K1 / 2);
109 
110 		/*
111 		 * pwrite() K1 of data (2's) at offset K2.
112 		 */
113 		if ((nbytes = pwrite(fd, wbuf[2], K1, K2)) != K1) {
114 			tst_resm(TFAIL,
115 				 "pwrite at K2 failed: nbytes=%d, errno=%d",
116 				 nbytes, errno);
117 			cleanup();
118 		}
119 
120 		/*
121 		 * We should still be at our non K boundary.
122 		 */
123 		l_seek(fd, 0, SEEK_CUR, K1 / 2);
124 
125 		/*
126 		 * lseek() to an offset of K3.
127 		 */
128 		l_seek(fd, K3, SEEK_SET, K3);
129 
130 		/*
131 		 * This time use a normal write() of K1 of data (3's) which should
132 		 * take place at an offset of K3, moving the file pointer to K4.
133 		 */
134 		if ((nbytes = write(fd, wbuf[3], K1)) != K1) {
135 			tst_resm(TFAIL, "write failed: nbytes=%d, errno=%d",
136 				 nbytes, errno);
137 			cleanup();
138 		}
139 
140 		/*
141 		 * We should be at offset K4.
142 		 */
143 		l_seek(fd, 0, SEEK_CUR, K4);
144 
145 		/*
146 		 * pwrite() K1 of data (1's) at offset K1.
147 		 */
148 		if ((nbytes = pwrite(fd, wbuf[1], K1, K1)) != K1) {
149 			tst_resm(TFAIL, "pwrite failed: nbytes=%d, errno=%d",
150 				 nbytes, errno);
151 			cleanup();
152 		}
153 
154 	/*--------------------------------------------------------------*/
155 
156 		/*
157 		 * Now test that O_APPEND takes precedence over any
158 		 * offset specified by pwrite(), but that the file
159 		 * pointer remains unchanged.  First, close then reopen
160 		 * the file and ensure it is already K4 in length and
161 		 * set the file pointer to it's midpoint, K2.
162 		 */
163 		close(fd);
164 		if ((fd = open(fname, O_RDWR | O_APPEND, 0666)) < 0) {
165 			tst_resm(TBROK, "open failed: fname = %s, errno = %d",
166 				 fname, errno);
167 			cleanup();
168 		}
169 		if (fstat(fd, &statbuf) == -1) {
170 			tst_resm(TFAIL, "fstat failed: errno = %d", errno);
171 			cleanup();
172 		}
173 		if (statbuf.st_size != K4) {
174 			tst_resm(TFAIL, "file size is %ld != K4",
175 				 statbuf.st_size);
176 			cleanup();
177 		}
178 		l_seek(fd, K2, SEEK_SET, K2);
179 
180 		/*
181 		 * Finally, pwrite() some K1 of data at offset 0.
182 		 * What we should end up with is:
183 		 *      -The file pointer should still be at K2.
184 		 *      -The data should have been written to the end
185 		 *       of the file (O_APPEND) and should be K5 in size.
186 		 */
187 		if ((nbytes = pwrite(fd, wbuf[0], K1, 0)) != K1) {
188 			tst_resm(TFAIL,
189 				 "pwrite at 0 failed: nbytes=%d, errno=%d",
190 				 nbytes, errno);
191 
192 		}
193 		l_seek(fd, 0, SEEK_CUR, K2);
194 		if (fstat(fd, &statbuf) == -1) {
195 			tst_resm(TFAIL, "fstat failed: errno = %d", errno);
196 
197 		}
198 		if (statbuf.st_size != K5) {
199 			tst_resm(TFAIL, "file size is %ld != K4",
200 				 statbuf.st_size);
201 
202 		}
203 		tst_resm(TPASS, "O_APPEND test passed.");
204 
205 	/*------------------------------------------------------------------------*/
206 
207 		close(fd);
208 		unlink(fname);
209 	}			/* end for */
210 	cleanup();
211 	tst_exit();
212 
213 }
214 
215 /*------------------------------------------------------------------------*/
216 
217 /*
218  * init_buffers() allocates wbuf[] array
219  * as follows:
220  * wbuf[0] has 0's, wbuf[1] has 1's, wbuf[2] has 2's, and wbuf[3] has 3's.
221  */
init_buffers(char * wbuf[])222 void init_buffers(char *wbuf[])
223 {
224 	int i;
225 
226 	for (i = 0; i < NBUFS; i++) {
227 		wbuf[i] = malloc(K1);
228 		if (wbuf[i] == NULL) {
229 			tst_brkm(TBROK, NULL, "ib: malloc failed: errno=%d",
230 				 errno);
231 		}
232 		memset(wbuf[i], i, K1);
233 	}
234 }
235 
236 /*
237  * l_seek() is a local front end to lseek().
238  * "checkoff" is the offset at which we believe we should be at.
239  * Used to validate pwrite doesn't move the offset.
240  */
l_seek(int fdesc,off_t offset,int whence,off_t checkoff)241 void l_seek(int fdesc, off_t offset, int whence, off_t checkoff)
242 {
243 	off_t offloc;
244 
245 	if ((offloc = lseek(fdesc, offset, whence)) != checkoff) {
246 		tst_brkm(TFAIL, NULL,
247 			 "(%ld = lseek(%d, %ld, %d)) != %ld) errno = %d",
248 			 offloc, fdesc, offset, whence, checkoff, errno);
249 	}
250 }
251 
252 /*
253  * cleanup() - Performs all ONE TIME cleanup for this test at
254  *             completion or premature exit.
255  *
256  *	Print test timing stats and errno log if test executed with options.
257  *	Close the testfile if still opened.
258  *	Remove temporary directory and sub-directories/files under it
259  *	created during setup().
260  *	Exit the test program with normal exit code.
261  */
cleanup(void)262 void cleanup(void)
263 {
264 
265 	tst_rmdir();
266 
267 }
268