Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for Windows #70

Merged
merged 11 commits into from
Jul 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ notifications:
language: node_js
dist: trusty
node_js:
- "0.10.45"
- "7"
- "6"
- "8"
script:
- npm run lint
- npm test
Expand Down
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
environment:
nodejs_version: "6"
nodejs_version: "8"

install:
- ps: Install-Product node $env:nodejs_version
- npm install

before_build:
Expand Down
81 changes: 72 additions & 9 deletions fs-ext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#include <node.h>
#ifndef _WIN32
#include <fcntl.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
Expand All @@ -37,6 +39,8 @@
#include <io.h>
#include <windows.h>
#include <sys/utime.h>
#define off_t LONGLONG
#define _LARGEFILE_SOURCE
#endif

using namespace v8;
Expand Down Expand Up @@ -87,7 +91,9 @@ enum
FS_OP_SEEK,
FS_OP_UTIME,
FS_OP_STATVFS,
#ifndef _WIN32
FS_OP_FCNTL,
#endif
};

static void EIO_After(uv_work_t *req) {
Expand Down Expand Up @@ -119,7 +125,7 @@ static void EIO_After(uv_work_t *req) {

case FS_OP_SEEK:
argc = 2;
argv[1] = Nan::New<Number>(store_data->offset);
argv[1] = Nan::New<Number>(static_cast<double>(store_data->offset));
break;
case FS_OP_STATVFS:
#ifndef _WIN32
Expand All @@ -139,10 +145,12 @@ static void EIO_After(uv_work_t *req) {
argc = 1;
#endif
break;
#ifndef _WIN32
case FS_OP_FCNTL:
argc = 2;
argv[1] = Nan::New<Number>(store_data->result);
break;
#endif
default:
assert(0 && "Unhandled op type value");
}
Expand Down Expand Up @@ -176,10 +184,53 @@ static void EIO_StatVFS(uv_work_t *req) {
;
}

#ifdef _WIN32
static off_t _win32_lseek(int fd, off_t offset, int whence) {
HANDLE fh;
LARGE_INTEGER new_offset;

fh = (HANDLE)uv_get_osfhandle(fd);
if (fh == (HANDLE)-1) {
errno = EBADF;
return -1;
}

DWORD method;
switch (whence) {
case SEEK_SET:
method = FILE_BEGIN;
break;
case SEEK_CUR:
method = FILE_CURRENT;
break;
case SEEK_END:
method = FILE_END;
break;
default:
errno = EINVAL;
return -1;
}

LARGE_INTEGER distance;
distance.QuadPart = offset;

if (SetFilePointerEx(fh, distance, &new_offset, method)) {
return new_offset.QuadPart;
}

errno = EINVAL;
return -1;
}
#endif

static void EIO_Seek(uv_work_t *req) {
store_data_t* seek_data = static_cast<store_data_t *>(req->data);

#ifdef _WIN32
off_t offs = _win32_lseek(seek_data->fd, seek_data->offset, seek_data->oper);
#else
off_t offs = lseek(seek_data->fd, seek_data->offset, seek_data->oper);
#endif

if (offs == -1) {
seek_data->result = -1;
Expand All @@ -190,6 +241,7 @@ static void EIO_Seek(uv_work_t *req) {

}

#ifndef _WIN32
static void EIO_Fcntl(uv_work_t *req) {
store_data_t* data = static_cast<store_data_t *>(req->data);

Expand All @@ -214,6 +266,7 @@ static void EIO_Fcntl(uv_work_t *req) {
data->error = errno;
}
}
#endif

#ifdef _WIN32

Expand All @@ -231,9 +284,11 @@ static int _win32_flock(int fd, int oper) {

int i = -1;

fh = (HANDLE)_get_osfhandle(fd);
if (fh == (HANDLE)-1)
fh = (HANDLE)uv_get_osfhandle(fd);
if (fh == (HANDLE)-1) {
errno = EBADF;
return -1;
}

memset(&o, 0, sizeof(o));

Expand All @@ -256,16 +311,17 @@ static int _win32_flock(int fd, int oper) {
i = 0;
break;
case LOCK_UN: /* unlock lock */
if (UnlockFileEx(fh, 0, LK_LEN, 0, &o))
if (UnlockFileEx(fh, 0, LK_LEN, 0, &o) ||
GetLastError() == ERROR_NOT_LOCKED)
i = 0;
break;
default: /* unknown */
errno = EINVAL;
return -1;
}
if (i == -1) {
if (GetLastError() == ERROR_LOCK_VIOLATION)
errno = WSAEWOULDBLOCK;
if (GetLastError() == ERROR_LOCK_VIOLATION)
errno = EWOULDBLOCK;
else
errno = EINVAL;
}
Expand Down Expand Up @@ -352,9 +408,13 @@ static NAN_METHOD(Seek) {
int whence = info[2]->Int32Value();

if ( ! info[3]->IsFunction()) {
#ifdef _WIN32
off_t offs_result = _win32_lseek(fd, offs, whence);
#else
off_t offs_result = lseek(fd, offs, whence);
#endif
if (offs_result == -1) return Nan::ThrowError(Nan::ErrnoException(errno, "Seek", ""));
info.GetReturnValue().Set(Nan::New<Number>(offs_result));
info.GetReturnValue().Set(Nan::New<Number>(static_cast<double>(offs_result)));
return;
}

Expand All @@ -375,6 +435,7 @@ static NAN_METHOD(Seek) {

// fs.fcntl(fd, cmd, [arg])

#ifndef _WIN32
static NAN_METHOD(Fcntl) {
if (info.Length() < 3 ||
!info[0]->IsInt32() ||
Expand Down Expand Up @@ -408,12 +469,12 @@ static NAN_METHOD(Fcntl) {

info.GetReturnValue().SetUndefined();
}

#endif

static void EIO_UTime(uv_work_t *req) {
store_data_t* utime_data = static_cast<store_data_t *>(req->data);

off_t i = utime(utime_data->path, &utime_data->utime_buf);
int i = utime(utime_data->path, &utime_data->utime_buf);
free( utime_data->path );

if (i == (off_t)-1) {
Expand Down Expand Up @@ -589,7 +650,9 @@ NAN_MODULE_INIT(init)
NODE_DEFINE_CONSTANT(target, F_SETLKW);
#endif
target->Set(Nan::New<String>("seek").ToLocalChecked(), Nan::New<FunctionTemplate>(Seek)->GetFunction());
#ifndef _WIN32
target->Set(Nan::New<String>("fcntl").ToLocalChecked(), Nan::New<FunctionTemplate>(Fcntl)->GetFunction());
#endif
target->Set(Nan::New<String>("flock").ToLocalChecked(), Nan::New<FunctionTemplate>(Flock)->GetFunction());
target->Set(Nan::New<String>("utime").ToLocalChecked(), Nan::New<FunctionTemplate>(UTime)->GetFunction());
target->Set(Nan::New<String>("statVFS").ToLocalChecked(), Nan::New<FunctionTemplate>(StatVFS)->GetFunction());
Expand Down
33 changes: 21 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"author": "Matt Sergeant <helpme@gmail.com> (http://baudehlo.wordpress.com/)",
"name": "fs-ext",
"description": "Extensions to core 'fs' module.",
"keywords": ["fs", "filesystem", "flock", "seek"],
"keywords": [
"fs",
"filesystem",
"flock",
"seek"
],
"version": "0.6.0",
"homepage": "https://github.com/baudehlo/node-fs-ext/",
"repository": {
Expand All @@ -11,23 +16,27 @@
},
"main": "fs-ext.js",
"engines": {
"node": ">= v0.8.0"
"node": ">= 6.0.0"
},
"dependencies": { "nan": "^2.0" },
"licenses": [ {
"type": "MIT"
} ],
"dependencies": {
"nan": "^2.0"
},
"licenses": [
{
"type": "MIT"
}
],
"bugs": {
"mail" : "helpme@gmail.com",
"web" : "https://github.com/baudehlo/node-fs-ext/issues"
"mail": "helpme@gmail.com",
"web": "https://github.com/baudehlo/node-fs-ext/issues"
},
"scripts": {
"scripts": {
"install": "node-gyp configure build",
"test": "./run_tests",
"test": "node ./run_tests",
"lint": "node ./node_modules/eslint/bin/eslint \"*.js\" \"tests/**/*.js\"",
"cover": "NODE_ENV=cov ./node_modules/.bin/istanbul cover ./run_tests"
"cover": "NODE_ENV=cov ./node_modules/.bin/istanbul cover node ./run_tests"
},
"devDependencies": {
"eslint" : "^2.13.0"
"eslint": "^2.13.0"
}
}
15 changes: 0 additions & 15 deletions run_tests

This file was deleted.

17 changes: 17 additions & 0 deletions run_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";

if (process.platform !== 'win32') {
require('./tests/test-fs-fcntl');
}

require('./tests/test-fs-seek');

require('./tests/test-fs-flock');

require('./tests/test-fs-utime');

// for stress testing only
if (process.argv[2] == '--stress') {
require('./tests/test-fs-seek_stress');
require('./tests/test-fs-flock_stress');
}
9 changes: 5 additions & 4 deletions tests/test-fs-fcntl.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
var assert = require('assert'),
path = require('path'),
util = require('util'),
fs = require('../fs-ext');
fs = require('../fs-ext'),
os = require('os');

var tests_ok = 0,
tests_run = 0;

var debug_me = false;

var tmp_dir = "/tmp",
var tmp_dir = os.tmpdir(),
file_path = path.join(tmp_dir, 'what.when.fcntl.test');

var file_fd,
Expand Down Expand Up @@ -70,7 +71,7 @@ function expect_errno(api_name, resource, err, expected_errno) {
if (debug_me) console.log(' FAILED OK: ' + api_name );
}
else {
console.log('FAILURE: ' + arguments.callee.name + ': ' + fault_msg);
console.log('FAILURE: ' + fault_msg);
console.log(' ARGS: ', util.inspect(arguments));
}
}
Expand All @@ -87,7 +88,7 @@ function expect_ok(api_name, resource, err) {
if (debug_me) console.log(' OK: ' + api_name );
}
else {
console.log('FAILURE: ' + arguments.callee.name + ': ' + fault_msg);
console.log('FAILURE: ' + fault_msg);
console.log(' ARGS: ', util.inspect(arguments));
}
}
Expand Down
17 changes: 12 additions & 5 deletions tests/test-fs-flock.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
var assert = require('assert'),
path = require('path'),
util = require('util'),
fs = require('../fs-ext');
fs = require('../fs-ext'),
os = require('os');

var tests_ok = 0,
tests_run = 0;

var debug_me = false;

var tmp_dir = "/tmp",
var tmp_dir = os.tmpdir(),
file_path = path.join(tmp_dir, 'what.when.flock.test'),
file_path_not = path.join(tmp_dir, 'what.not.flock.test');

Expand Down Expand Up @@ -88,7 +89,7 @@ function expect_errno(api_name, resource, err, expected_errno) {
if (debug_me) console.log(' FAILED OK: ' + api_name );
}
else {
console.log('FAILURE: ' + arguments.callee.name + ': ' + fault_msg);
console.log('FAILURE: ' + fault_msg);
console.log(' ARGS: ', util.inspect(arguments));
}
}
Expand All @@ -105,7 +106,7 @@ function expect_ok(api_name, resource, err) {
if (debug_me) console.log(' OK: ' + api_name );
}
else {
console.log('FAILURE: ' + arguments.callee.name + ': ' + fault_msg);
console.log('FAILURE: ' + fault_msg);
console.log(' ARGS: ', util.inspect(arguments));
}
}
Expand Down Expand Up @@ -392,7 +393,13 @@ fs.flock(file_fd, 'sh', function(err, extra) {

tests_run++;
fs.flock(file_fd, 'exnb', function(err) {
expect_ok('flock', file_fd, err);
if (process.platform === 'win32') {
// Windows doesn't support lock upgrades
expect_errno('flock', 10035, err, 'EWOULDBLOCK');
}
else {
expect_ok('flock', file_fd, err);
}

tests_run++;
fs.flock(file_fd, 'un', function(err) {
Expand Down
Loading