Skip to content

Commit 55ced1e

Browse files
committed
Init
0 parents  commit 55ced1e

8 files changed

+709
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
.jshintrc
3+
build
4+
node_modules
5+
utimes_fixtures
6+
binding.node

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Ronomon
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# utimes
2+
Check if a file is open in another application on Windows, macOS and Linux. Linux requires privileges.
3+
4+
## Installation
5+
```
6+
npm install @ronomon/opened
7+
```
8+
9+
## Usage
10+
11+
```javascript
12+
var Opened = require('@ronomon/opened');
13+
var paths = [...];
14+
Opened.files(paths,
15+
function(error, hashTable) {
16+
if (error) throw error;
17+
paths.forEach(
18+
function(path) {
19+
console.log(path + ' open=' + hashTable.hasOwnProperty(path));
20+
}
21+
);
22+
}
23+
);
24+
```

binding.cc

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <nan.h>
2+
#if defined(_WIN32)
3+
#include <io.h>
4+
#include <windows.h>
5+
#endif
6+
7+
int open(const char* path) {
8+
#if defined(_WIN32)
9+
int chars = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
10+
if (chars == 0) return GetLastError();
11+
WCHAR* pathw = (WCHAR*) malloc(chars * sizeof(WCHAR));
12+
if (pathw == NULL) return ERROR_OUTOFMEMORY;
13+
MultiByteToWideChar(CP_UTF8, 0, path, -1, pathw, chars);
14+
HANDLE handle = CreateFileW(
15+
pathw,
16+
FILE_GENERIC_READ | FILE_GENERIC_WRITE,
17+
0L,
18+
NULL,
19+
OPEN_EXISTING,
20+
NULL,
21+
NULL
22+
);
23+
free(pathw);
24+
if (handle == INVALID_HANDLE_VALUE) return GetLastError();
25+
CloseHandle(handle);
26+
return 0;
27+
#else
28+
return -1;
29+
#endif
30+
}
31+
32+
class OpenWorker : public Nan::AsyncWorker {
33+
public:
34+
OpenWorker(
35+
v8::Local<v8::Object> &pathHandle,
36+
Nan::Callback *callback
37+
) : Nan::AsyncWorker(callback) {
38+
SaveToPersistent("pathHandle", pathHandle);
39+
path = node::Buffer::Data(pathHandle);
40+
}
41+
42+
~OpenWorker() {}
43+
44+
void Execute() {
45+
error = open(path);
46+
}
47+
48+
void HandleOKCallback () {
49+
Nan::HandleScope scope;
50+
v8::Local<v8::Value> argv[] = {
51+
Nan::New<v8::Number>(error)
52+
};
53+
callback->Call(1, argv);
54+
}
55+
56+
private:
57+
const char* path;
58+
int error;
59+
};
60+
61+
NAN_METHOD(opened) {
62+
if (
63+
info.Length() != 2 ||
64+
!node::Buffer::HasInstance(info[0]) ||
65+
!info[1]->IsFunction()
66+
) {
67+
return Nan::ThrowError(
68+
"bad arguments, expected: (buffer path, function callback)"
69+
);
70+
}
71+
v8::Local<v8::Object> pathHandle = info[0].As<v8::Object>();
72+
Nan::Callback *callback = new Nan::Callback(info[1].As<v8::Function>());
73+
Nan::AsyncQueueWorker(new OpenWorker(pathHandle, callback));
74+
}
75+
76+
NAN_MODULE_INIT(Init) {
77+
NAN_EXPORT(target, opened);
78+
}
79+
80+
NODE_MODULE(binding, Init)
81+
82+
// S.D.G.

binding.gyp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "binding",
5+
"sources": [ "binding.cc" ],
6+
"include_dirs": [ "<!(node -e \"require('nan')\")" ]
7+
},
8+
{
9+
"target_name": "copy",
10+
"type": "none",
11+
"dependencies": [ "binding" ],
12+
"copies": [
13+
{
14+
'destination': '<(module_root_dir)',
15+
'files': ['<(module_root_dir)/build/Release/binding.node']
16+
}
17+
]
18+
}
19+
]
20+
}

0 commit comments

Comments
 (0)