Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/util/__tests__/utimes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,26 @@ describe('utimes', () => {
})
})
})

describe('utimesMillisSync()', () => {
it('should close open file descriptors after encountering an error', () => {
const fakeFd = Math.random()

gracefulFsStub.openSync = (pathIgnored, flagsIgnored) => fakeFd

let closeCalled = false
gracefulFsStub.closeSync = (fd) => {
assert.strictEqual(fd, fakeFd)
closeCalled = true
}

const testError = new Error('A test error')
gracefulFsStub.futimesSync = (fd, atimeIgnored, mtimeIgnored) => {
throw testError
}

assert.throws(() => utimes.utimesMillisSync('ignored', 'ignored', 'ignored'), testError)
assert(closeCalled)
})
})
})
20 changes: 18 additions & 2 deletions lib/util/utimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,24 @@ async function utimesMillis (path, atime, mtime) {

function utimesMillisSync (path, atime, mtime) {
const fd = fs.openSync(path, 'r+')
fs.futimesSync(fd, atime, mtime)
return fs.closeSync(fd)

let error = null

try {
fs.futimesSync(fd, atime, mtime)
} catch (futimesErr) {
error = futimesErr
} finally {
try {
fs.closeSync(fd)
} catch (closeErr) {
error = closeErr
}
}

if (error) {
throw error
}
}

module.exports = {
Expand Down
Loading