15cf555074045d4c0cffa25fc3a228f98c207b8a
[openafs.git] / src / vol / ntops.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* I/O operations for the Windows NT platforms. */
11
12 #include <afsconfig.h>
13 #include <afs/param.h>
14
15 #include <roken.h>
16
17 #ifdef AFS_NT40_ENV
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <direct.h>
23 #include <io.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #include <windows.h>
27 #include <winnt.h>
28 #include <winbase.h>
29 #include <lock.h>
30 #include <afs/afsutil.h>
31 #include "nfs.h"
32 #include <afs/afsint.h>
33 #include "ihandle.h"
34 #include "vnode.h"
35 #include "volume.h"
36 #include "viceinode.h"
37 #include <dirent.h>
38 #include <afs/afs_assert.h>
39 #include <afs/errmap_nt.h>
40
41 #define BASEFILEATTRIBUTE FILE_ATTRIBUTE_NORMAL
42
43 /* nt_unlink - unlink a case sensitive name.
44  *
45  * nt_unlink supports the nt_dec call.
46  *
47  * This nt_unlink has the delete on last close semantics of the Unix unlink
48  * with a minor twist. Subsequent CreateFile calls on this file can succeed
49  * if they open for delete. If a CreateFile call tries to create a new file
50  * with the same name it will fail. Fortunately, neither case should occur
51  * as part of nt_dec.
52  */
53 int
54 nt_unlink(char *name)
55 {
56     HANDLE fh;
57
58     fh = CreateFile(name,
59                     GENERIC_READ | GENERIC_WRITE | DELETE,
60                     FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
61                     NULL, OPEN_EXISTING,
62                     BASEFILEATTRIBUTE | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_POSIX_SEMANTICS,
63                     NULL);
64     if (fh != INVALID_HANDLE_VALUE)
65         CloseHandle(fh);
66     else {
67         errno = nterr_nt2unix(GetLastError(), ENOENT);
68         return -1;
69     }
70     return 0;
71 }
72
73 /* nt_open - open an NT handle for a file.
74  *
75  * Return Value:
76  *      the handle or -1 on error.
77  */
78 FD_t
79 nt_open(char *name, int flags, int mode)
80 {
81     HANDLE fh;
82     DWORD nt_access = 0;
83     DWORD nt_share = FILE_SHARE_READ;
84     DWORD nt_create = 0;
85     /* Really use the sequential one for data files, random for meta data. */
86     DWORD FandA = BASEFILEATTRIBUTE | FILE_FLAG_SEQUENTIAL_SCAN;
87
88     /* set access */
89     if ((flags & O_RDWR) || (flags & O_WRONLY)) {
90         nt_access |= GENERIC_WRITE;
91         nt_share  |= FILE_SHARE_WRITE | FILE_SHARE_DELETE;
92     }
93     if ((flags & O_RDWR) || (flags == O_RDONLY))
94         nt_access |= GENERIC_READ;
95
96     /* set creation */
97     switch (flags & (O_CREAT | O_EXCL | O_TRUNC)) {
98     case 0:
99         nt_create = OPEN_EXISTING;
100         break;
101     case O_CREAT:
102         nt_create = OPEN_ALWAYS;
103         break;
104     case O_CREAT | O_TRUNC:
105         nt_create = CREATE_ALWAYS;
106         break;
107     case O_CREAT | O_EXCL:
108     case O_CREAT | O_EXCL | O_TRUNC:
109         nt_create = CREATE_NEW;
110         break;
111     case O_TRUNC:
112         nt_create = TRUNCATE_EXISTING;
113         break;
114     case O_TRUNC | O_EXCL:
115     case O_EXCL:
116     default:
117         errno = EINVAL;
118         return INVALID_FD;
119         break;
120     }
121
122   retry:
123     fh = CreateFile(name, nt_access, nt_share, NULL, nt_create, FandA, NULL);
124     if (fh == INVALID_HANDLE_VALUE) {
125         DWORD gle = GetLastError();
126         errno = nterr_nt2unix(gle, EBADF);
127     }
128     return fh;
129 }
130
131 int
132 nt_close(FD_t fd)
133 {
134     BOOL code;
135
136     code = CloseHandle(fd);
137     if (!code) {
138         errno = nterr_nt2unix(GetLastError(), EBADF);
139         return -1;
140     }
141     return 0;
142 }
143
144 int
145 nt_write(FD_t fd, char *buf, afs_sfsize_t size)
146 {
147     BOOL code;
148     DWORD nbytes;
149
150     code = WriteFile((HANDLE) fd, (void *)buf, (DWORD) size, &nbytes, NULL);
151
152     if (!code) {
153         errno = nterr_nt2unix(GetLastError(), EBADF);
154         return -1;
155     }
156     return (int)nbytes;
157 }
158
159 int
160 nt_pwrite(FD_t fd, const void * buf, afs_sfsize_t count, afs_foff_t offset)
161 {
162     /*
163      * same comment as read
164      */
165
166     DWORD nbytes;
167     BOOL code;
168     OVERLAPPED overlap = {0};
169     LARGE_INTEGER liOffset;
170
171     liOffset.QuadPart = offset;
172     overlap.Offset = liOffset.LowPart;
173     overlap.OffsetHigh = liOffset.HighPart;
174
175     code = WriteFile((HANDLE) fd, (void *)buf, (DWORD) count, &nbytes, &overlap);
176
177     if (!code) {
178         errno = nterr_nt2unix(GetLastError(), EBADF);
179         return -1;
180     }
181     return (ssize_t)nbytes;
182 }
183
184 int
185 nt_read(FD_t fd, char *buf, afs_sfsize_t size)
186 {
187     BOOL code;
188     DWORD nbytes;
189
190     code = ReadFile((HANDLE) fd, (void *)buf, (DWORD) size, &nbytes, NULL);
191
192     if (!code) {
193         DWORD gle = GetLastError();
194         if (gle != ERROR_HANDLE_EOF) {
195                 errno = nterr_nt2unix(GetLastError(), EBADF);
196                 return -1;
197         }
198     }
199     return (int)nbytes;
200 }
201
202 int
203 nt_pread(FD_t fd, void * buf, afs_sfsize_t count, afs_foff_t offset)
204 {
205     /*
206      * This really ought to call NtReadFile
207      */
208     DWORD nbytes;
209     BOOL code;
210     OVERLAPPED overlap = {0};
211     LARGE_INTEGER liOffset;
212     /*
213      * Cast through a LARGE_INTEGER - make no assumption about
214      * byte ordering and leave that to the compiler..
215      */
216     liOffset.QuadPart = offset;
217     overlap.Offset = liOffset.LowPart;
218     overlap.OffsetHigh = liOffset.HighPart;
219
220     code = ReadFile((HANDLE) fd, (void *)buf, (DWORD) count, &nbytes, &overlap);
221
222     if (!code) {
223         DWORD gle = GetLastError();
224         if (gle != ERROR_HANDLE_EOF) {
225                 errno = nterr_nt2unix(GetLastError(), EBADF);
226                 return -1;
227         }
228     }
229     return (ssize_t)nbytes;
230 }
231
232 afs_sfsize_t
233 nt_size(FD_t fd)
234 {
235     BY_HANDLE_FILE_INFORMATION finfo;
236     LARGE_INTEGER FileSize;
237
238     if (!GetFileInformationByHandle(fd, &finfo))
239         return -1;
240
241     FileSize.HighPart = finfo.nFileSizeHigh;
242     FileSize.LowPart = finfo.nFileSizeLow;
243     return FileSize.QuadPart;
244 }
245
246
247 int
248 nt_getFileCreationTime(FD_t fd, FILETIME * ftime)
249 {
250     BY_HANDLE_FILE_INFORMATION finfo;
251
252     if (!GetFileInformationByHandle(fd, &finfo))
253         return -1;
254
255     *ftime = finfo.ftCreationTime;
256
257     return 0;
258 }
259
260 int
261 nt_setFileCreationTime(FD_t fd, FILETIME * ftime)
262 {
263     return !SetFileTime(fd, ftime, NULL, NULL);
264 }
265
266 int
267 nt_sync(int cdrive)
268 {
269     FD_t drive_fd;
270     char sdrive[32];
271     int n;
272
273     n = cdrive;
274     if (n <= 26) {
275         cdrive = 'A' + (n - 1);
276     }
277
278     cdrive = _toupper(cdrive);
279
280     (void)sprintf(sdrive, "\\\\.\\%c:", cdrive);
281     drive_fd = nt_open(sdrive, O_RDWR, 0666);
282     if (drive_fd == INVALID_FD) {
283         return -1;
284     }
285
286     if (!FlushFileBuffers((HANDLE) drive_fd)) {
287         errno = nterr_nt2unix(GetLastError(), EBADF);
288         nt_close(drive_fd);
289         return -1;
290     }
291     nt_close(drive_fd);
292     return 0;
293 }
294
295
296 /* Currently nt_ftruncate only tested to shrink a file. */
297 int
298 nt_ftruncate(FD_t fd, afs_foff_t len)
299 {
300     LARGE_INTEGER length;
301
302     length.QuadPart = len;
303
304     if (SetFilePointerEx(fd, length, NULL, FILE_BEGIN)
305         == 0xffffffff) {
306         errno = nterr_nt2unix(GetLastError(), EBADF);
307         return -1;
308     }
309     if (!SetEndOfFile(fd)) {
310         errno = nterr_nt2unix(GetLastError(), EBADF);
311         return -1;
312     }
313     return 0;
314 }
315
316
317 int
318 nt_fsync(FD_t fd)
319 {
320     int code = FlushFileBuffers(fd);
321     return code == 0 ? -1 : 0;
322 }
323
324
325 int
326 nt_seek(FD_t fd, afs_foff_t off, int where)
327 {
328     int code;
329     LARGE_INTEGER offset;
330
331     offset.QuadPart = off;
332
333     code = SetFilePointerEx(fd, offset, NULL, where);
334     return code;
335 }
336
337 /* nt_DevToDrive
338  * converts a device number (2-25) into a drive letter name.
339  *
340  * Arguments:
341  * drive - assumes drive is a pointer to a string at least 3 bytes long.
342  * dev   - drive number 2-25, since A-C already in use.
343  *
344  * Return Value:
345  * Returns pointer to end of drive if successful, else NULL.
346  *
347  */
348 void
349 nt_DevToDrive(char *drive, int dev)
350 {
351     if (dev < 2 || dev > 25) {
352         errno = EINVAL;
353         return;         /* Invalid drive */
354     }
355     drive[0] = (char)('A' + dev);
356     drive[1] = ':';
357     drive[2] = '\0';
358
359     return;
360
361 }
362
363 /* nt_DriveToDev
364  * converts a drive letter to a device number (2-25)
365  *
366  * Arguments:
367  * drive - assumes drive is a pointer to a string at least 3 bytes long.
368  *
369  * Return Value:
370  * dev   - drive number 2-25 if successful (A-C already in use), else -1
371  *
372  */
373 int
374 nt_DriveToDev(char *drive)
375 {
376     int dev = -1;
377
378     if (drive)
379         dev = toupper(*drive) - 'A';
380     if ((dev < 2) || (dev > 25))
381         return -1;
382
383     return dev;
384 }
385 #endif