Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Trust the filesystem to move files #195

Merged
merged 2 commits into from May 1, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/content/write.js
Expand Up @@ -4,7 +4,7 @@ const events = require('events')

const contentPath = require('./path')
const fs = require('fs/promises')
const moveFile = require('../util/move-file')
const { moveFile } = require('@npmcli/fs')
const { Minipass } = require('minipass')
const Pipeline = require('minipass-pipeline')
const Flush = require('minipass-flush')
Expand Down Expand Up @@ -161,8 +161,14 @@ async function moveToDestination (tmp, cache, sri, opts) {
const destDir = path.dirname(destination)

await fs.mkdir(destDir, { recursive: true })
await moveFile(tmp.target, destination)
tmp.moved = true
try {
await moveFile(tmp.target, destination, { overwrite: false })
tmp.moved = true
} catch (err) {
if (!err.message.startsWith('The destination file exists')) {
throw Object.assign(err, { code: 'EEXIST' })
}
}
}

function sizeError (expected, found) {
Expand Down
56 changes: 0 additions & 56 deletions lib/util/move-file.js

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -54,7 +54,6 @@
"minipass-flush": "^1.0.5",
"minipass-pipeline": "^1.2.4",
"p-map": "^4.0.0",
"promise-inflight": "^1.0.1",
"ssri": "^10.0.0",
"tar": "^6.1.11",
"unique-filename": "^3.0.0"
Expand Down
16 changes: 16 additions & 0 deletions test/content/write.js
Expand Up @@ -233,6 +233,22 @@ t.test('cleans up tmp on successful completion', async t => {
})
})

t.test('Handles moveFile error other than EEXIST', async t => {
const write = t.mock('../../lib/content/write.js', {
'@npmcli/fs': {
moveFile: async () => {
throw new Error('Unknown error')
},
},
})
const CONTENT = 'foobarbaz'
const CACHE = t.testdir()
await t.rejects(
write.stream(CACHE).end(CONTENT).promise(),
{ message: 'Unknown error' }
)
})

t.test('cleans up tmp on streaming error', (t) => {
const CONTENT = 'foobarbaz'
const CACHE = t.testdir()
Expand Down
202 changes: 0 additions & 202 deletions test/util/move-file.js

This file was deleted.