Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

win: use win32 api for file reading and writing

  • Loading branch information...
piscisaureus committed Sep 5, 2011
1 parent 7b87ff7 commit 013d2a1916344df22f774430d322e8a24ad4d172
Showing with 60 additions and 15 deletions.
  1. +3 −3 src/uv-common.h
  2. +57 −12 src/win/fs.c
@@ -32,9 +32,9 @@
#define COUNTOF(a) (sizeof(a) / sizeof(a[0]))

/* Used for the uv_fs_ functions */
#define SET_REQ_RESULT(req, result) \
req->result = result; \
if (result == -1) { \
#define SET_REQ_RESULT(req, result_value) \
req->result = (result_value); \
if (req->result == -1) { \
req->errorno = errno; \
}

@@ -24,6 +24,7 @@
#include <direct.h>
#include <fcntl.h>
#include <io.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/utime.h>
#include <stdio.h>
@@ -117,33 +118,77 @@ void fs__close(uv_fs_t* req, uv_file file) {

void fs__read(uv_fs_t* req, uv_file file, void *buf, size_t length,
off_t offset) {
int result = 0;
HANDLE handle;
OVERLAPPED overlapped, *overlapped_ptr;
LARGE_INTEGER offset_;
DWORD bytes;

if (offset != -1) {
result = _lseek(file, offset, SEEK_SET);
handle = (HANDLE) _get_osfhandle(file);
if (handle == INVALID_HANDLE_VALUE) {
SET_REQ_RESULT(req, -1);
return;
}

if (result != -1) {
result = _read(file, buf, length);
if (length > INT_MAX) {
SET_REQ_ERROR(req, ERROR_INSUFFICIENT_BUFFER);
return;
}

SET_REQ_RESULT(req, result);
if (offset != -1) {
memset(&overlapped, 0, sizeof overlapped);

offset_.QuadPart = offset;
overlapped.Offset = offset_.LowPart;
overlapped.OffsetHigh = offset_.HighPart;

overlapped_ptr = &overlapped;
} else {
overlapped_ptr = NULL;
}

if (ReadFile(handle, buf, length, &bytes, overlapped_ptr)) {
SET_REQ_RESULT(req, bytes);
} else {
SET_REQ_ERROR(req, GetLastError());
}
}


void fs__write(uv_fs_t* req, uv_file file, void *buf, size_t length,
off_t offset) {
int result = 0;
HANDLE handle;
OVERLAPPED overlapped, *overlapped_ptr;
LARGE_INTEGER offset_;
DWORD bytes;

if (offset != -1) {
result = _lseek(file, offset, SEEK_SET);
handle = (HANDLE) _get_osfhandle(file);
if (handle == INVALID_HANDLE_VALUE) {
SET_REQ_RESULT(req, -1);
return;
}

if (result != -1) {
result = _write(file, buf, length);
if (length > INT_MAX) {
SET_REQ_ERROR(req, ERROR_INSUFFICIENT_BUFFER);
return;
}

SET_REQ_RESULT(req, result);
if (offset != -1) {
memset(&overlapped, 0, sizeof overlapped);

offset_.QuadPart = offset;
overlapped.Offset = offset_.LowPart;
overlapped.OffsetHigh = offset_.HighPart;

overlapped_ptr = &overlapped;
} else {
overlapped_ptr = NULL;
}

if (WriteFile(handle, buf, length, &bytes, overlapped_ptr)) {
SET_REQ_RESULT(req, bytes);
} else {
SET_REQ_ERROR(req, GetLastError());
}
}


0 comments on commit 013d2a1

Please sign in to comment.
You can’t perform that action at this time.