Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
win: use win32 api for file reading and writing
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Sep 5, 2011
1 parent 7b87ff7 commit 013d2a1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/uv-common.h
Expand Up @@ -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; \
}

Expand Down
69 changes: 57 additions & 12 deletions src/win/fs.c
Expand Up @@ -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>
Expand Down Expand Up @@ -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());
}
}


Expand Down

0 comments on commit 013d2a1

Please sign in to comment.