From aa85d7a8b17193627d1517a2af17abfd72289260 Mon Sep 17 00:00:00 2001 From: "cantab.net!aia21" Date: Thu, 12 Aug 2004 08:21:50 +0000 Subject: [PATCH] The long waited device-level write support on windows. (Yuval) (Logical change 1.491) --- libntfs/win32_io.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/libntfs/win32_io.c b/libntfs/win32_io.c index 679bd7c7..d4e09f20 100644 --- a/libntfs/win32_io.c +++ b/libntfs/win32_io.c @@ -826,14 +826,44 @@ static int ntfs_device_win32_sync(struct ntfs_device *dev) * @count: How many bytes should be written. * * On success returns the amount of bytes actually written. - * On fail returns -errno. + * On fail returns -1 and errno set. */ -static s64 ntfs_device_win32_write(struct ntfs_device *dev, const void *buffer, +static s64 ntfs_device_win32_write(struct ntfs_device *dev, const void *buf, s64 count) { - fprintf(stderr, "win32_write() unimplemented\n"); - errno = ENOTSUP; - return -1; + s64 bytes_written = 0; + HANDLE handle = ((win32_fd *)dev->d_private)->handle; + + Dprintf("win32_write: Writing %ll bytes\n",count); + + if (NDevReadOnly(dev)) { + Dputs("win32_write: Device R/O, exiting."); + errno = EROFS; + return -1; + } + NDevSetDirty(dev); + + while (count>0) { + DWORD cur_written; + DWORD cur_count = (count>32768)?32768:count; + + if (WriteFile(handle, buf, cur_count, &cur_written, NULL) && + (cur_written==cur_count)) { + Dprintf("win32_write: Written %u bytes.",bytes_written); + bytes_written += cur_written; + count -= cur_written; + } else { + /* error */ + errno = ntfs_w32error_to_errno(GetLastError()); + return -1; + } + } + if (count) { + errno = EIO; + return -1; + } else { + return bytes_written; + } } /**