common/rc: add _scratch_{u}mount_idmapped() helpers
[xfstests-dev.git] / src / trunc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2003 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <time.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #ifndef O_DIRECT
18 #define O_DIRECT        040000
19 #endif
20
21 #define WAITTIME        60
22 #define BUFSIZE         4096
23 #define ALIGNMENT       16384
24 #define TRUNCSIZE       1000
25
26 /* write data to disk - buffered/sync or direct
27  * write different buffered data to disk
28  * truncate
29  * direct read back, see if server puts stale data down
30  */
31
32 int
33 main(argc, argv)
34 int     argc;
35 char    **argv;
36 {
37         int fd, err, elapsed;
38         char *buf = NULL, *goodbuf = NULL;
39         time_t starttime;
40         char *filename="testfile";
41         int c;
42
43         if (argc != 3) {
44                 printf("Usage: trunc -f testfilename\n");
45                 exit(1);
46         }
47
48         while((c=getopt(argc,argv,"f:"))!=EOF) {
49                 switch (c) {
50                 case 'f':
51                         filename = optarg;
52                         break;
53                 default:
54                         fprintf(stderr,"Usage: trunc -f filename\n");
55                         exit(1);
56                 }
57         }
58
59         err = posix_memalign((void **)&buf, ALIGNMENT, BUFSIZE);
60         if (err)
61                 fprintf(stderr, "posix_memalign failed: %s\n", strerror(err));
62
63         err = posix_memalign((void **)&goodbuf, ALIGNMENT, BUFSIZE);
64         if (err)
65                 fprintf(stderr, "posix_memalign failed: %s\n", strerror(err));
66
67         err = unlink(filename);
68 /*      if (err < 0) perror("unlink failed");*/
69         
70         fd = open(filename, O_CREAT|O_RDWR|O_DIRECT, 0666);
71         if (fd < 0) perror("direct open failed");
72
73         memset(buf, 1, BUFSIZE);
74
75         printf("direct write of 1's into file\n");      
76         err = write(fd, buf, BUFSIZE);
77         if (err < 0) perror("direct write failed");
78
79         close(fd);
80         
81         fd = open(filename, O_CREAT|O_RDWR, 0666);
82         if (fd < 0) perror("buffered open failed");
83
84         /* 1 now on disk */
85
86         memset(buf, 2, BUFSIZE);
87         memset(goodbuf, 2, BUFSIZE);
88
89         printf("buffered write of 2's into file\n");    
90         err = write(fd, buf, BUFSIZE);
91         if (err < 0) perror("buffered write failed");
92
93         /* 1 now on disk, but 2 data is buffered */
94
95         printf("truncate file\n");
96         err = ftruncate(fd, TRUNCSIZE);
97         if (err < 0) perror("ftruncate failed");
98         starttime = time(NULL);
99
100         printf("sync buffered data (2's)\n");
101         err = fdatasync(fd);
102         if (err < 0) perror("fdatasync failed");
103
104         /* during truncate server may have read/modified/written last block */
105
106         close(fd);
107
108         fd = open(filename, O_CREAT|O_RDWR|O_DIRECT, 0666);
109         if (fd < 0) perror("direct open failed");
110
111         /* read what's really on disk now */
112
113         printf("iterate direct reads for %ds or until failure...\n", WAITTIME);
114
115         while ((elapsed = (time(NULL) - starttime)) <= WAITTIME) {
116
117                 /* printf(".");
118                 fflush(stdout);*/
119
120                 err = lseek(fd, 0, SEEK_SET);
121                 if (err < 0) perror("lseek failed");
122
123                 err = read(fd, buf, BUFSIZE);
124                 if (err < 0) perror("read failed");
125
126                 err = memcmp(buf, goodbuf, 100);
127                 if (err) {
128                         printf("\nFailed after %d secs: read %d's\n", elapsed, buf[0]); 
129                         return 1;
130                 }
131
132                 sleep(1);
133         }
134         
135         free(buf);
136         free(goodbuf);
137
138         printf("Passed\n");
139         return 0;
140 }
141
142