Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
jorangreef committed Jun 26, 2017
0 parents commit 55ced1e
Show file tree
Hide file tree
Showing 8 changed files with 709 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
.jshintrc
build
node_modules
utimes_fixtures
binding.node
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Ronomon

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# utimes
Check if a file is open in another application on Windows, macOS and Linux. Linux requires privileges.

## Installation
```
npm install @ronomon/opened
```

## Usage

```javascript
var Opened = require('@ronomon/opened');
var paths = [...];
Opened.files(paths,
function(error, hashTable) {
if (error) throw error;
paths.forEach(
function(path) {
console.log(path + ' open=' + hashTable.hasOwnProperty(path));
}
);
}
);
```
82 changes: 82 additions & 0 deletions binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <nan.h>
#if defined(_WIN32)
#include <io.h>
#include <windows.h>
#endif

int open(const char* path) {
#if defined(_WIN32)
int chars = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
if (chars == 0) return GetLastError();
WCHAR* pathw = (WCHAR*) malloc(chars * sizeof(WCHAR));
if (pathw == NULL) return ERROR_OUTOFMEMORY;
MultiByteToWideChar(CP_UTF8, 0, path, -1, pathw, chars);
HANDLE handle = CreateFileW(
pathw,
FILE_GENERIC_READ | FILE_GENERIC_WRITE,
0L,
NULL,
OPEN_EXISTING,
NULL,
NULL
);
free(pathw);
if (handle == INVALID_HANDLE_VALUE) return GetLastError();
CloseHandle(handle);
return 0;
#else
return -1;
#endif
}

class OpenWorker : public Nan::AsyncWorker {
public:
OpenWorker(
v8::Local<v8::Object> &pathHandle,
Nan::Callback *callback
) : Nan::AsyncWorker(callback) {
SaveToPersistent("pathHandle", pathHandle);
path = node::Buffer::Data(pathHandle);
}

~OpenWorker() {}

void Execute() {
error = open(path);
}

void HandleOKCallback () {
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::New<v8::Number>(error)
};
callback->Call(1, argv);
}

private:
const char* path;
int error;
};

NAN_METHOD(opened) {
if (
info.Length() != 2 ||
!node::Buffer::HasInstance(info[0]) ||
!info[1]->IsFunction()
) {
return Nan::ThrowError(
"bad arguments, expected: (buffer path, function callback)"
);
}
v8::Local<v8::Object> pathHandle = info[0].As<v8::Object>();
Nan::Callback *callback = new Nan::Callback(info[1].As<v8::Function>());
Nan::AsyncQueueWorker(new OpenWorker(pathHandle, callback));
}

NAN_MODULE_INIT(Init) {
NAN_EXPORT(target, opened);
}

NODE_MODULE(binding, Init)

// S.D.G.
20 changes: 20 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"targets": [
{
"target_name": "binding",
"sources": [ "binding.cc" ],
"include_dirs": [ "<!(node -e \"require('nan')\")" ]
},
{
"target_name": "copy",
"type": "none",
"dependencies": [ "binding" ],
"copies": [
{
'destination': '<(module_root_dir)',
'files': ['<(module_root_dir)/build/Release/binding.node']
}
]
}
]
}
Loading

0 comments on commit 55ced1e

Please sign in to comment.