0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00

fix "offset out of bounds" issue

This commit is contained in:
Alex Kocharin 2014-10-03 07:51:56 +04:00
parent 1cb1e91ca1
commit 20f748be12

View file

@ -163,20 +163,33 @@ function open_flock(name, opmod, flmod, tries, backoff, cb) {
// this function neither unlocks file nor closes it
// it'll have to be done manually later
function lock_and_read(name, callback) {
function lock_and_read(name, _callback) {
open_flock(name, 'r', 'exnb', 4, 10, function(err, fd) {
function callback(err) {
if (err && fd) {
fs.close(fd, function(err2) {
_callback(err)
})
} else {
_callback.apply(null, arguments)
}
}
if (err) return callback(err, fd)
fs.fstat(fd, function(err, st) {
if (err) return callback(err, fd)
var buffer = new Buffer(st.size)
fs.read(fd, buffer, 0, st.size, null, function(err, bytesRead, buffer) {
if (err) return callback(err)
if (bytesRead != st.size) return callback(Error('st.size != bytesRead'), fd)
if (st.size === 0) return onRead(null, 0, buffer)
fs.read(fd, buffer, 0, st.size, null, onRead)
function onRead(err, bytesRead, buffer) {
if (err) return callback(err, fd)
if (bytesRead != st.size) return callback(new Error('st.size != bytesRead'), fd)
callback(null, fd, buffer)
})
}
})
})
}