Skip to content

Commit 90dcfe6

Browse files
committed
Lock file during patching.
Closes #1106.
1 parent 01d2a35 commit 90dcfe6

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

lib/handlers/patch.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const $rdf = require('rdflib')
1010
const crypto = require('crypto')
1111
const overQuota = require('../utils').overQuota
1212
const getContentType = require('../utils').getContentType
13+
const { lock } = require('proper-lockfile')
1314

1415
// Patch parsers by request body content type
1516
const PATCH_PARSERS = {
@@ -23,6 +24,7 @@ const DEFAULT_FOR_NEW_CONTENT_TYPE = 'text/turtle'
2324
async function patchHandler (req, res, next) {
2425
debug(`PATCH -- ${req.originalUrl}`)
2526
res.header('MS-Author-Via', 'SPARQL')
27+
let releaseLock
2628
try {
2729
// Obtain details of the target resource
2830
const ldp = req.app.locals.ldp
@@ -50,22 +52,24 @@ async function patchHandler (req, res, next) {
5052
throw error(415, `Unsupported patch content type: ${patch.contentType}`)
5153
}
5254

53-
// Parse the target graph and the patch document,
54-
// and verify permission for performing this specific patch
55-
const [graph, patchObject] = await Promise.all([
56-
readGraph(resource),
57-
parsePatch(url, patch.uri, patch.text)
58-
.then(patchObject => checkPermission(req, patchObject))
59-
])
55+
// Parse the patch document and verify permissions
56+
const patchObject = await parsePatch(url, patch.uri, patch.text)
57+
await checkPermission(req, patchObject)
6058

6159
// Patch the graph and write it back to the file
60+
releaseLock = await lock(path, { retries: 10, realpath: false })
61+
const graph = await readGraph(resource)
6262
await applyPatch(patchObject, graph, url)
6363
const result = await writeGraph(graph, resource, ldp.resourceMapper.rootPath, ldp.serverUri)
6464

6565
// Send the result to the client
6666
res.send(result)
6767
} catch (err) {
6868
return next(err)
69+
} finally {
70+
if (releaseLock) {
71+
await releaseLock()
72+
}
6973
}
7074
return next()
7175
}

package-lock.json

Lines changed: 44 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)