forked from dckesler/opened
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 55ced1e
Showing
8 changed files
with
709 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.DS_Store | ||
.jshintrc | ||
build | ||
node_modules | ||
utimes_fixtures | ||
binding.node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
); | ||
} | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.