src/open_by_handle: verify dir content only with -r flag
[xfstests-dev.git] / src / trunc.c
1 /*
2  * Copyright (c) 2000-2003 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <time.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #ifndef O_DIRECT
30 #define O_DIRECT        040000
31 #endif
32
33 #define WAITTIME        60
34 #define BUFSIZE         4096
35 #define ALIGNMENT       16384
36 #define TRUNCSIZE       1000
37
38 /* write data to disk - buffered/sync or direct
39  * write different buffered data to disk
40  * truncate
41  * direct read back, see if server puts stale data down
42  */
43
44 int
45 main(argc, argv)
46 int     argc;
47 char    **argv;
48 {
49         int fd, err, elapsed;
50         char *buf = NULL, *goodbuf = NULL;
51         time_t starttime;
52         char *filename="testfile";
53         int c;
54
55         if (argc != 3) {
56                 printf("Usage: trunc -f testfilename\n");
57                 exit(1);
58         }
59
60         while((c=getopt(argc,argv,"f:"))!=EOF) {
61                 switch (c) {
62                 case 'f':
63                         filename = optarg;
64                         break;
65                 default:
66                         fprintf(stderr,"Usage: trunc -f filename\n");
67                         exit(1);
68                 }
69         }
70
71         err = posix_memalign((void **)&buf, ALIGNMENT, BUFSIZE);
72         if (err)
73                 fprintf(stderr, "posix_memalign failed: %s\n", strerror(err));
74
75         err = posix_memalign((void **)&goodbuf, ALIGNMENT, BUFSIZE);
76         if (err)
77                 fprintf(stderr, "posix_memalign failed: %s\n", strerror(err));
78
79         err = unlink(filename);
80 /*      if (err < 0) perror("unlink failed");*/
81         
82         fd = open(filename, O_CREAT|O_RDWR|O_DIRECT, 0666);
83         if (fd < 0) perror("direct open failed");
84
85         memset(buf, 1, BUFSIZE);
86
87         printf("direct write of 1's into file\n");      
88         err = write(fd, buf, BUFSIZE);
89         if (err < 0) perror("direct write failed");
90
91         close(fd);
92         
93         fd = open(filename, O_CREAT|O_RDWR, 0666);
94         if (fd < 0) perror("buffered open failed");
95
96         /* 1 now on disk */
97
98         memset(buf, 2, BUFSIZE);
99         memset(goodbuf, 2, BUFSIZE);
100
101         printf("buffered write of 2's into file\n");    
102         err = write(fd, buf, BUFSIZE);
103         if (err < 0) perror("buffered write failed");
104
105         /* 1 now on disk, but 2 data is buffered */
106
107         printf("truncate file\n");
108         err = ftruncate(fd, TRUNCSIZE);
109         if (err < 0) perror("ftruncate failed");
110         starttime = time(NULL);
111
112         printf("sync buffered data (2's)\n");
113         err = fdatasync(fd);
114         if (err < 0) perror("fdatasync failed");
115
116         /* during truncate server may have read/modified/written last block */
117
118         close(fd);
119
120         fd = open(filename, O_CREAT|O_RDWR|O_DIRECT, 0666);
121         if (fd < 0) perror("direct open failed");
122
123         /* read what's really on disk now */
124
125         printf("iterate direct reads for %ds or until failure...\n", WAITTIME);
126
127         while ((elapsed = (time(NULL) - starttime)) <= WAITTIME) {
128
129                 /* printf(".");
130                 fflush(stdout);*/
131
132                 err = lseek(fd, 0, SEEK_SET);
133                 if (err < 0) perror("lseek failed");
134
135                 err = read(fd, buf, BUFSIZE);
136                 if (err < 0) perror("read failed");
137
138                 err = memcmp(buf, goodbuf, 100);
139                 if (err) {
140                         printf("\nFailed after %d secs: read %d's\n", elapsed, buf[0]); 
141                         return 1;
142                 }
143
144                 sleep(1);
145         }
146         
147         free(buf);
148         free(goodbuf);
149
150         printf("Passed\n");
151         return 0;
152 }
153
154