2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /* I/O operations for the Windows NT platforms. */
12 #include <afsconfig.h>
13 #include <afs/param.h>
23 #include <afs/afsutil.h>
25 #include <afs/afsint.h>
29 #include "viceinode.h"
30 #include <afs/errmap_nt.h>
32 #define BASEFILEATTRIBUTE FILE_ATTRIBUTE_NORMAL
34 /* nt_unlink - unlink a case sensitive name.
36 * nt_unlink supports the nt_dec call.
38 * This nt_unlink has the delete on last close semantics of the Unix unlink
39 * with a minor twist. Subsequent CreateFile calls on this file can succeed
40 * if they open for delete. If a CreateFile call tries to create a new file
41 * with the same name it will fail. Fortunately, neither case should occur
50 GENERIC_READ | GENERIC_WRITE | DELETE,
51 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
53 BASEFILEATTRIBUTE | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_POSIX_SEMANTICS,
55 if (fh != INVALID_HANDLE_VALUE)
58 errno = nterr_nt2unix(GetLastError(), ENOENT);
64 /* nt_open - open an NT handle for a file.
67 * the handle or -1 on error.
70 nt_open(const char *name, int flags, int mode)
74 DWORD nt_share = FILE_SHARE_READ;
76 /* Really use the sequential one for data files, random for meta data. */
77 DWORD FandA = BASEFILEATTRIBUTE | FILE_FLAG_SEQUENTIAL_SCAN;
80 if ((flags & O_RDWR) || (flags & O_WRONLY)) {
81 nt_access |= GENERIC_WRITE;
82 nt_share |= FILE_SHARE_WRITE | FILE_SHARE_DELETE;
84 if ((flags & O_RDWR) || (flags == O_RDONLY))
85 nt_access |= GENERIC_READ;
88 switch (flags & (O_CREAT | O_EXCL | O_TRUNC)) {
90 nt_create = OPEN_EXISTING;
93 nt_create = OPEN_ALWAYS;
95 case O_CREAT | O_TRUNC:
96 nt_create = CREATE_ALWAYS;
98 case O_CREAT | O_EXCL:
99 case O_CREAT | O_EXCL | O_TRUNC:
100 nt_create = CREATE_NEW;
103 nt_create = TRUNCATE_EXISTING;
105 case O_TRUNC | O_EXCL:
113 fh = CreateFile(name, nt_access, nt_share, NULL, nt_create, FandA, NULL);
114 if (fh == INVALID_HANDLE_VALUE) {
115 DWORD gle = GetLastError();
116 errno = nterr_nt2unix(gle, EBADF);
126 code = CloseHandle(fd);
128 errno = nterr_nt2unix(GetLastError(), EBADF);
135 nt_write(FD_t fd, void *buf, afs_sfsize_t size)
140 code = WriteFile((HANDLE) fd, buf, (DWORD) size, &nbytes, NULL);
143 errno = nterr_nt2unix(GetLastError(), EBADF);
150 nt_pwrite(FD_t fd, const void * buf, afs_sfsize_t count, afs_foff_t offset)
153 * same comment as read
158 OVERLAPPED overlap = {0};
159 LARGE_INTEGER liOffset;
161 liOffset.QuadPart = offset;
162 overlap.Offset = liOffset.LowPart;
163 overlap.OffsetHigh = liOffset.HighPart;
165 code = WriteFile((HANDLE) fd, (void *)buf, (DWORD) count, &nbytes, &overlap);
168 errno = nterr_nt2unix(GetLastError(), EBADF);
171 return (ssize_t)nbytes;
175 nt_read(FD_t fd, void *buf, afs_sfsize_t size)
180 code = ReadFile((HANDLE) fd, buf, (DWORD) size, &nbytes, NULL);
183 DWORD gle = GetLastError();
184 if (gle != ERROR_HANDLE_EOF) {
185 errno = nterr_nt2unix(GetLastError(), EBADF);
193 nt_pread(FD_t fd, void * buf, afs_sfsize_t count, afs_foff_t offset)
196 * This really ought to call NtReadFile
200 OVERLAPPED overlap = {0};
201 LARGE_INTEGER liOffset;
203 * Cast through a LARGE_INTEGER - make no assumption about
204 * byte ordering and leave that to the compiler..
206 liOffset.QuadPart = offset;
207 overlap.Offset = liOffset.LowPart;
208 overlap.OffsetHigh = liOffset.HighPart;
210 code = ReadFile((HANDLE) fd, (void *)buf, (DWORD) count, &nbytes, &overlap);
213 DWORD gle = GetLastError();
214 if (gle != ERROR_HANDLE_EOF) {
215 errno = nterr_nt2unix(GetLastError(), EBADF);
219 return (ssize_t)nbytes;
225 BY_HANDLE_FILE_INFORMATION finfo;
226 LARGE_INTEGER FileSize;
228 if (!GetFileInformationByHandle(fd, &finfo))
231 FileSize.HighPart = finfo.nFileSizeHigh;
232 FileSize.LowPart = finfo.nFileSizeLow;
233 return FileSize.QuadPart;
238 nt_getFileCreationTime(FD_t fd, FILETIME * ftime)
240 BY_HANDLE_FILE_INFORMATION finfo;
242 if (!GetFileInformationByHandle(fd, &finfo))
245 *ftime = finfo.ftCreationTime;
251 nt_setFileCreationTime(FD_t fd, FILETIME * ftime)
253 return !SetFileTime(fd, ftime, NULL, NULL);
265 cdrive = 'A' + (n - 1);
268 cdrive = _toupper(cdrive);
270 (void)sprintf(sdrive, "\\\\.\\%c:", cdrive);
271 drive_fd = nt_open(sdrive, O_RDWR, 0666);
272 if (drive_fd == INVALID_FD) {
276 if (!FlushFileBuffers((HANDLE) drive_fd)) {
277 errno = nterr_nt2unix(GetLastError(), EBADF);
286 /* Currently nt_ftruncate only tested to shrink a file. */
288 nt_ftruncate(FD_t fd, afs_foff_t len)
290 LARGE_INTEGER length;
292 length.QuadPart = len;
294 if (SetFilePointerEx(fd, length, NULL, FILE_BEGIN)
296 errno = nterr_nt2unix(GetLastError(), EBADF);
299 if (!SetEndOfFile(fd)) {
300 errno = nterr_nt2unix(GetLastError(), EBADF);
310 int code = FlushFileBuffers(fd);
311 return code == 0 ? -1 : 0;
316 nt_seek(FD_t fd, afs_foff_t off, int whence)
319 LARGE_INTEGER offset;
322 if (SEEK_SET == whence) {
324 } else if (SEEK_END == whence) {
326 } else if (SEEK_CUR == whence) {
327 where = FILE_CURRENT;
332 offset.QuadPart = off;
334 code = SetFilePointerEx(fd, offset, NULL, where);
336 errno = nterr_nt2unix(GetLastError(), EBADF);
343 * converts a device number (2-25) into a drive letter name.
346 * drive - assumes drive is a pointer to a string at least 3 bytes long.
347 * dev - drive number 2-25, since A-C already in use.
350 * Returns pointer to end of drive if successful, else NULL.
354 nt_DevToDrive(char *drive, int dev)
356 if (dev < 2 || dev > 25) {
358 return; /* Invalid drive */
360 drive[0] = (char)('A' + dev);
369 * converts a drive letter to a device number (2-25)
372 * drive - assumes drive is a pointer to a string at least 3 bytes long.
375 * dev - drive number 2-25 if successful (A-C already in use), else -1
379 nt_DriveToDev(char *drive)
384 dev = toupper(*drive) - 'A';
385 if ((dev < 2) || (dev > 25))