-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
131 lines (98 loc) · 2.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const util = require('util')
const binding = require('node-gyp-build')(__dirname)
function onwork (errno) {
if (errno < 0) this.reject(toError(errno))
else this.resolve()
}
exports.tryLock = function tryLock (fd, offset = 0, length = 0, opts = {}) {
if (typeof offset === 'object') {
opts = offset
offset = 0
}
if (typeof length === 'object') {
opts = length
length = 0
}
if (typeof opts !== 'object') {
opts = {}
}
const errno = binding.fsctl_napi_try_lock(fd, offset, length, opts.shared ? 0 : 1)
if (errno < 0) {
const err = toError(errno)
if (err.code === 'EAGAIN') return false
throw err
}
return true
}
// Alias for backwards compatibility
exports.lock = exports.tryLock
exports.waitForLock = function waitForLock (fd, offset = 0, length = 0, opts = {}) {
if (typeof offset === 'object') {
opts = offset
offset = 0
}
if (typeof length === 'object') {
opts = length
length = 0
}
if (typeof opts !== 'object') {
opts = {}
}
const req = Buffer.alloc(binding.sizeof_fsctl_napi_lock_t)
const ctx = {
req,
resolve: null,
reject: null
}
const promise = new Promise((resolve, reject) => {
ctx.resolve = resolve
ctx.reject = reject
})
const errno = binding.fsctl_napi_wait_for_lock(req, fd, offset, length, opts.shared ? 0 : 1, ctx, onwork)
if (errno < 0) return Promise.reject(toError(errno))
return promise
}
exports.unlock = function unlock (fd, offset = 0, length = 0) {
const errno = binding.fsctl_napi_unlock(fd, offset, length)
if (errno < 0) throw toError(errno)
}
exports.punchHole = function punchHole (fd, offset, length) {
const req = Buffer.alloc(binding.sizeof_fsctl_napi_punch_hole_t)
const ctx = {
req,
resolve: null,
reject: null
}
const promise = new Promise((resolve, reject) => {
ctx.resolve = resolve
ctx.reject = reject
})
const errno = binding.fsctl_napi_punch_hole(req, fd, offset, length, ctx, onwork)
if (errno < 0) return Promise.reject(toError(errno))
return promise
}
exports.sparse = function sparse (fd) {
// Short circuit on everything but Windows
if (process.platform !== 'win32') return Promise.resolve()
const req = Buffer.alloc(binding.sizeof_fsctl_napi_sparse_t)
const ctx = {
req,
resolve: null,
reject: null
}
const promise = new Promise((resolve, reject) => {
ctx.resolve = resolve
ctx.reject = reject
})
const errno = binding.fsctl_napi_sparse(req, fd, ctx, onwork)
if (errno < 0) return Promise.reject(toError(errno))
return promise
}
function toError (errno) {
const [code, msg] = util.getSystemErrorMap().get(errno)
const err = new Error(`${code}: ${msg}`)
err.errno = errno
err.code = code
Error.captureStackTrace(err, toError)
return err
}