Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / tests / invalidate-file.c
1 /*
2 * Copyright (c) 2000 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <sys/mman.h>
41 #include <sys/stat.h>
42
43 #include <unistd.h>
44 #include <fcntl.h>
45
46 #include <atypes.h>
47
48 #include <kafs.h>
49
50 #include <fs.h>
51 #include <arlalib.h>
52
53 #include <err.h>
54
55
56 #ifndef MAP_FAILED
57 #define MAP_FAILED ((void *)-1)
58 #endif
59
60 #ifdef KERBEROS
61
62 static void
63 create_write_file(char *filename)
64 {
65 int ret;
66 int fd;
67
68 fs_invalidate(filename);
69
70 fd = open(filename, O_RDWR | O_CREAT, 0666);
71 if (fd < 0)
72 err(1, "open(rw): %s", filename);
73
74 ret = write(fd, "foo", 3);
75 if (ret < 0)
76 err(1, "write");
77
78 fs_invalidate(filename);
79
80 ret = write(fd, "foo", 3);
81 if (ret < 0)
82 err(1, "write2");
83
84 ret = close(fd);
85 if (ret < 0)
86 err(1, "close");
87 }
88
89 static void
90 read_file(char *filename)
91 {
92 int ret;
93 int fd;
94 char buf[3];
95
96 fs_invalidate(filename);
97
98 fd = open(filename, O_RDONLY, 0666);
99 if (fd < 0)
100 err(1, "open(ro)");
101
102 ret = read(fd, buf, sizeof(buf));
103 if (ret < 0)
104 err(1, "read");
105
106 fs_invalidate(filename);
107
108 ret = read(fd, buf, sizeof(buf));
109 if (ret < 0)
110 err(1, "read");
111
112 ret = close(fd);
113 if (ret < 0)
114 err(1, "close");
115 }
116
117 static void
118 mmap_read_file(char *filename)
119 {
120 int fd;
121 void *v;
122 char buf[6];
123
124 fs_invalidate(filename);
125
126 fd = open(filename, O_RDONLY, 0666);
127 if (fd < 0)
128 err(1, "open(ro-mmap)");
129
130 v = mmap(NULL, 6, PROT_READ, MAP_SHARED, fd, 0);
131 if (v == (void *)MAP_FAILED)
132 err(1, "mmap(ro) %s", filename);
133
134 memcpy(buf, v, 3);
135 fs_invalidate(filename);
136 memcpy(buf, v, 3);
137
138 munmap(v, 6);
139 }
140
141 static void
142 mmap_write_file(char *filename)
143 {
144 int fd;
145 void *v;
146
147 fs_invalidate(filename);
148
149 fd = open(filename, O_RDWR, 0666);
150 if (fd < 0)
151 err(1, "open(rw-mmap)");
152
153 v = mmap(NULL, 6, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
154 if (v == (void *)MAP_FAILED)
155 err(1, "mmap(rw) %s", filename);
156
157 memcpy(v, "foo", 3);
158 fs_invalidate(filename);
159 memcpy(v, "foo", 3);
160
161 munmap(v, 6);
162 close(fd);
163 }
164
165 int
166 main(int argc, char **argv)
167 {
168 char *filename = "foo";
169
170
171 #ifdef KERBEROS
172 if (!k_hasafs())
173 #endif
174 exit(1);
175
176 create_write_file(filename);
177 read_file(filename);
178 read_file(filename);
179 read_file(filename);
180 read_file(filename);
181 mmap_read_file(filename);
182 mmap_read_file(filename);
183 mmap_read_file(filename);
184 mmap_read_file(filename);
185 mmap_write_file(filename);
186 mmap_write_file(filename);
187 mmap_write_file(filename);
188 mmap_write_file(filename);
189
190 return 0;
191 }
192
193 #else
194
195 int
196 main(int argc, char **argv)
197 {
198
199 errx(1, "no kafs support");
200 }
201 #endif