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>
30 #include <afs/afsutil.h>
32 #include <afs/afsint.h>
36 #include "viceinode.h"
38 #include <afs/afs_assert.h>
39 #include <afs/errmap_nt.h>
41 #define BASEFILEATTRIBUTE FILE_ATTRIBUTE_NORMAL
43 /* nt_unlink - unlink a case sensitive name.
45 * nt_unlink supports the nt_dec call.
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
59 GENERIC_READ | GENERIC_WRITE | DELETE,
60 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
62 BASEFILEATTRIBUTE | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_POSIX_SEMANTICS,
64 if (fh != INVALID_HANDLE_VALUE)
67 errno = nterr_nt2unix(GetLastError(), ENOENT);
73 /* nt_open - open an NT handle for a file.
76 * the handle or -1 on error.
79 nt_open(char *name, int flags, int mode)
83 DWORD nt_share = FILE_SHARE_READ;
85 /* Really use the sequential one for data files, random for meta data. */
86 DWORD FandA = BASEFILEATTRIBUTE | FILE_FLAG_SEQUENTIAL_SCAN;
89 if ((flags & O_RDWR) || (flags & O_WRONLY)) {
90 nt_access |= GENERIC_WRITE;
91 nt_share |= FILE_SHARE_WRITE | FILE_SHARE_DELETE;
93 if ((flags & O_RDWR) || (flags == O_RDONLY))
94 nt_access |= GENERIC_READ;
97 switch (flags & (O_CREAT | O_EXCL | O_TRUNC)) {
99 nt_create = OPEN_EXISTING;
102 nt_create = OPEN_ALWAYS;
104 case O_CREAT | O_TRUNC:
105 nt_create = CREATE_ALWAYS;
107 case O_CREAT | O_EXCL:
108 case O_CREAT | O_EXCL | O_TRUNC:
109 nt_create = CREATE_NEW;
112 nt_create = TRUNCATE_EXISTING;
114 case O_TRUNC | O_EXCL:
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);
136 code = CloseHandle(fd);
138 errno = nterr_nt2unix(GetLastError(), EBADF);
145 nt_write(FD_t fd, char *buf, afs_sfsize_t size)
150 code = WriteFile((HANDLE) fd, (void *)buf, (DWORD) size, &nbytes, NULL);
153 errno = nterr_nt2unix(GetLastError(), EBADF);
160 nt_pwrite(FD_t fd, const void * buf, afs_sfsize_t count, afs_foff_t offset)
163 * same comment as read
168 OVERLAPPED overlap = {0};
169 LARGE_INTEGER liOffset;
171 liOffset.QuadPart = offset;
172 overlap.Offset = liOffset.LowPart;
173 overlap.OffsetHigh = liOffset.HighPart;
175 code = WriteFile((HANDLE) fd, (void *)buf, (DWORD) count, &nbytes, &overlap);
178 errno = nterr_nt2unix(GetLastError(), EBADF);
181 return (ssize_t)nbytes;
185 nt_read(FD_t fd, char *buf, afs_sfsize_t size)
190 code = ReadFile((HANDLE) fd, (void *)buf, (DWORD) size, &nbytes, NULL);
193 DWORD gle = GetLastError();
194 if (gle != ERROR_HANDLE_EOF) {
195 errno = nterr_nt2unix(GetLastError(), EBADF);
203 nt_pread(FD_t fd, void * buf, afs_sfsize_t count, afs_foff_t offset)
206 * This really ought to call NtReadFile
210 OVERLAPPED overlap = {0};
211 LARGE_INTEGER liOffset;
213 * Cast through a LARGE_INTEGER - make no assumption about
214 * byte ordering and leave that to the compiler..
216 liOffset.QuadPart = offset;
217 overlap.Offset = liOffset.LowPart;
218 overlap.OffsetHigh = liOffset.HighPart;
220 code = ReadFile((HANDLE) fd, (void *)buf, (DWORD) count, &nbytes, &overlap);
223 DWORD gle = GetLastError();
224 if (gle != ERROR_HANDLE_EOF) {
225 errno = nterr_nt2unix(GetLastError(), EBADF);
229 return (ssize_t)nbytes;
235 BY_HANDLE_FILE_INFORMATION finfo;
236 LARGE_INTEGER FileSize;
238 if (!GetFileInformationByHandle(fd, &finfo))
241 FileSize.HighPart = finfo.nFileSizeHigh;
242 FileSize.LowPart = finfo.nFileSizeLow;
243 return FileSize.QuadPart;
248 nt_getFileCreationTime(FD_t fd, FILETIME * ftime)
250 BY_HANDLE_FILE_INFORMATION finfo;
252 if (!GetFileInformationByHandle(fd, &finfo))
255 *ftime = finfo.ftCreationTime;
261 nt_setFileCreationTime(FD_t fd, FILETIME * ftime)
263 return !SetFileTime(fd, ftime, NULL, NULL);
275 cdrive = 'A' + (n - 1);
278 cdrive = _toupper(cdrive);
280 (void)sprintf(sdrive, "\\\\.\\%c:", cdrive);
281 drive_fd = nt_open(sdrive, O_RDWR, 0666);
282 if (drive_fd == INVALID_FD) {
286 if (!FlushFileBuffers((HANDLE) drive_fd)) {
287 errno = nterr_nt2unix(GetLastError(), EBADF);
296 /* Currently nt_ftruncate only tested to shrink a file. */
298 nt_ftruncate(FD_t fd, afs_foff_t len)
300 LARGE_INTEGER length;
302 length.QuadPart = len;
304 if (SetFilePointerEx(fd, length, NULL, FILE_BEGIN)
306 errno = nterr_nt2unix(GetLastError(), EBADF);
309 if (!SetEndOfFile(fd)) {
310 errno = nterr_nt2unix(GetLastError(), EBADF);
320 int code = FlushFileBuffers(fd);
321 return code == 0 ? -1 : 0;
326 nt_seek(FD_t fd, afs_foff_t off, int where)
329 LARGE_INTEGER offset;
331 offset.QuadPart = off;
333 code = SetFilePointerEx(fd, offset, NULL, where);
338 * converts a device number (2-25) into a drive letter name.
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.
345 * Returns pointer to end of drive if successful, else NULL.
349 nt_DevToDrive(char *drive, int dev)
351 if (dev < 2 || dev > 25) {
353 return; /* Invalid drive */
355 drive[0] = (char)('A' + dev);
364 * converts a drive letter to a device number (2-25)
367 * drive - assumes drive is a pointer to a string at least 3 bytes long.
370 * dev - drive number 2-25 if successful (A-C already in use), else -1
374 nt_DriveToDev(char *drive)
379 dev = toupper(*drive) - 'A';
380 if ((dev < 2) || (dev > 25))