rx: Remove RX_CALL_BUSY
[openafs.git] / src / WINNT / tests / largefiles / lftest.c
1 #include <windows.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <process.h>
6
7 char * teststr = "This is a test string.";
8
9
10
11 void usage(void) {
12     fprintf(stderr, "lftest <path>\n");
13     exit(1);
14 }
15
16 int test_write(HANDLE hf, LARGE_INTEGER offset) {
17     DWORD dwWritten;
18     int ret = 0;
19
20     if (!LockFile(hf, offset.u.LowPart, offset.u.HighPart,
21                    4096, 0)) {
22         fprintf(stderr, "Unable to lock offset 0x%08x:%08x gle = 0x%08x\n",
23                  offset.u.HighPart, offset.u.LowPart, GetLastError());
24         return -1;
25     }
26
27     if (!SetFilePointerEx(hf, offset, NULL, FILE_BEGIN)) {
28         fprintf(stderr, "Unable to set file pointer to offset 0x%08x:%08x gle = 0x%08x\n",
29                  offset.u.HighPart, offset.u.LowPart, GetLastError());
30         ret = -1;
31         goto unlock;
32     }
33
34     if (!WriteFile(hf, teststr, strlen(teststr)+1, &dwWritten, NULL)) {
35         fprintf(stderr, "Unable to write test string at offset 0x%08x:%08x gle = 0x%08x\n",
36                  offset.u.HighPart, offset.u.LowPart, GetLastError());
37         ret = -1;
38         goto unlock;
39     } else {
40         printf("wrote '%s' at offset 0x%08x:%08x\n", teststr, offset.u.HighPart, offset.u.LowPart);
41     }
42
43   unlock:
44     if (!UnlockFile(hf, offset.u.LowPart, offset.u.HighPart,
45                    4096, 0)) {
46         fprintf(stderr, "Unable to unlock offset 0x%08x:%08x gle = 0x%08x\n",
47                  offset.u.HighPart, offset.u.LowPart, GetLastError());
48         ret = -1;
49     }
50
51 #if 0
52     if (!FlushFileBuffers(hf)) {
53         fprintf(stderr, "Flush buffers fails at offset 0x%08x:%08x gle = 0x%08x\n",
54                  offset.u.HighPart, offset.u.LowPart, GetLastError());
55     }
56 #endif
57     return ret;
58 }
59
60 int test_read(HANDLE hf, LARGE_INTEGER offset) {
61     char buffer[256];
62     DWORD dwRead;
63     int ret = 0;
64
65     if (!LockFile(hf, offset.u.LowPart, offset.u.HighPart,
66                    4096, 0)) {
67         fprintf(stderr, "Unable to lock offset 0x%08x:%08x gle = 0x%08x\n",
68                  offset.u.HighPart, offset.u.LowPart, GetLastError());
69         return -1;
70     }
71
72     if (!SetFilePointerEx(hf, offset, NULL, FILE_BEGIN)) {
73         fprintf(stderr, "Unable to set file pointer to offset 0x%08x:%08x gle = 0x%08x\n",
74                  offset.u.HighPart, offset.u.LowPart, GetLastError());
75         ret = -1;
76         goto unlock;
77     }
78
79     if (!ReadFile(hf, buffer, strlen(teststr)+1, &dwRead, NULL)) {
80         fprintf(stderr, "Unable to read test string at offset 0x%08x:%08x gle = 0x%08x\n",
81                  offset.u.HighPart, offset.u.LowPart, GetLastError());
82         ret = -1;
83         goto unlock;
84     } else {
85         printf("read '%s' (%d bytes) at offset 0x%08x:%08x\n", buffer, dwRead, offset.u.HighPart, offset.u.LowPart);
86     }
87
88     if (strcmp(buffer, teststr)) {
89         fprintf(stderr, "Test string comparison failure at offset 0x%08x:%08x\n",
90                  offset.u.HighPart, offset.u.LowPart);
91         ret = -1;
92         goto unlock;
93     }
94
95   unlock:
96     if (!UnlockFile(hf, offset.u.LowPart, offset.u.HighPart,
97                    4096, 0)) {
98         fprintf(stderr, "Unable to unlock offset 0x%08x:%08x gle = 0x%08x\n",
99                  offset.u.HighPart, offset.u.LowPart, GetLastError());
100         ret = -1;
101     }
102
103     return ret;
104 }
105
106 int
107 main(int argc, char *argv[]) {
108     HANDLE fh;
109     __int64 i;
110     char cmdline[512];
111     LARGE_INTEGER large;
112
113     if (argc == 1)
114         usage();
115
116     if (!SetCurrentDirectory(argv[1])) {
117         fprintf(stderr, "unable to set directory to %s\n", argv[1]);
118         return 2;
119     }
120
121     fh = CreateFile("largefile.test",
122                      GENERIC_READ | GENERIC_WRITE | STANDARD_RIGHTS_READ | STANDARD_RIGHTS_WRITE,
123                      FILE_SHARE_READ | FILE_SHARE_WRITE,
124                      NULL /* default security */,
125                      OPEN_ALWAYS,
126                      FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS | FILE_FLAG_WRITE_THROUGH,
127                      NULL );
128
129     if (fh == INVALID_HANDLE_VALUE) {
130         fprintf(stderr, "unable to create/open the test file\n");
131         return 3;
132     }
133
134     for ( i=0; i<7; i++ ) {
135
136         large.QuadPart = i * (0x40000000-4);
137         test_write(fh, large);
138
139     }
140
141     CloseHandle(fh);
142
143     sprintf(cmdline, "fs.exe flushvolume %s", argv[1]);
144     system(cmdline);
145
146     fh = CreateFile("largefile.test",
147                      GENERIC_READ | GENERIC_WRITE | STANDARD_RIGHTS_READ | STANDARD_RIGHTS_WRITE,
148                      FILE_SHARE_READ | FILE_SHARE_WRITE,
149                      NULL /* default security */,
150                      OPEN_ALWAYS,
151                      FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS | FILE_FLAG_WRITE_THROUGH,
152                      NULL );
153
154     if (fh == INVALID_HANDLE_VALUE) {
155         fprintf(stderr, "unable to create/open the test file\n");
156         return 3;
157     }
158
159     for ( i=0; i<7; i++ ) {
160
161         large.QuadPart = i * (0x40000000-4);
162         test_read(fh, large);
163
164     }
165
166     CloseHandle(fh);
167
168     return 0;
169 }