Skip to content

Commit 9e45888

Browse files
committed
delete file.acl when deleting file - atomic delete
1 parent 32e139a commit 9e45888

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

lib/ldp.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,21 @@ class LDP {
422422
}
423423
}
424424

425+
// delete resource with acl and meta links
425426
async deleteResource (path) {
427+
const linkPath = this.resourceMapper._removeDollarExtension(path)
426428
try {
427-
return await withLock(path, { mustExist: false }, () => promisify(fs.unlink)(path))
429+
// first delete file, then links with write permission only (atomic delete)
430+
await withLock(path, { mustExist: false }, () => promisify(fs.unlink)(path))
431+
432+
const aclPath = `${linkPath}${this.suffixAcl}`
433+
if (await promisify(fs.exists)(aclPath)) {
434+
await withLock(aclPath, { mustExist: false }, () => promisify(fs.unlink)(aclPath))
435+
}
436+
const metaPath = `${linkPath}${this.suffixMeta}`
437+
if (await promisify(fs.exists)(metaPath)) {
438+
await withLock(metaPath, { mustExist: false }, () => promisify(fs.unlink)(metaPath))
439+
}
428440
} catch (err) {
429441
debug.container('DELETE -- unlink() error: ' + err)
430442
throw error(err, 'Failed to delete resource')

test/integration/http-test.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,12 @@ describe('HTTP APIs', function () {
506506
// Ensure all these are finished before running tests
507507
return Promise.all([
508508
rm('/false-file-48484848'),
509-
// createTestContainer('delete-test-empty-container'),
510-
createTestResource('/delete-test-empty-container/test.txt.acl'),
509+
createTestResource('/delete-test-empty-container/.meta.acl'),
511510
createTestResource('/put-resource-1.ttl'),
511+
createTestResource('/put-resource-with-acl.ttl'),
512+
createTestResource('/put-resource-with-acl.ttl.acl'),
513+
createTestResource('/put-resource-with-acl.txt'),
514+
createTestResource('/put-resource-with-acl.txt.acl'),
512515
createTestResource('/delete-test-non-empty/test.ttl')
513516
])
514517
})
@@ -524,12 +527,32 @@ describe('HTTP APIs', function () {
524527
.expect(200, done)
525528
})
526529

530+
it('should delete previously PUT file with ACL', function (done) {
531+
server.delete('/put-resource-with-acl.ttl')
532+
.expect(200, done)
533+
})
534+
535+
it('should return 404 on deleting .acl of previously deleted PUT file with ACL', function (done) {
536+
server.delete('/put-resource-with-acl.ttl.acl')
537+
.expect(404, done)
538+
})
539+
540+
it('should delete previously PUT file with bad extension and with ACL', function (done) {
541+
server.delete('/put-resource-with-acl.txt')
542+
.expect(200, done)
543+
})
544+
545+
it('should return 404 on deleting .acl of previously deleted PUT file with bad extension and with ACL', function (done) {
546+
server.delete('/put-resource-with-acl.txt.acl')
547+
.expect(404, done)
548+
})
549+
527550
it('should fail to delete non-empty containers', function (done) {
528551
server.delete('/delete-test-non-empty/')
529552
.expect(409, done)
530553
})
531554

532-
it('should delete a new and empty container - with file.acl', function (done) {
555+
it('should delete a new and empty container - with .meta.acl', function (done) {
533556
server.delete('/delete-test-empty-container/')
534557
.end(() => {
535558
server.get('/delete-test-empty-container/')

test/integration/ldp-test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,22 @@ describe('LDP', function () {
170170
return assert.isRejected(ldp.delete('/resources/testPut.txt'))
171171
})
172172

173-
it.skip('should delete a file in an existing dir', async () => {
173+
it.skip('should delete a file with ACL in an existing dir', async () => {
174174
// First create a dummy file
175175
var stream = stringToStream('hello world')
176176
await ldp.put('/resources/testPut.txt', stream, 'text/plain')
177+
await ldp.put('/resources/testPut.txt.acl', stream, 'text/turtle')
177178
// Make sure it exists
178179
fs.stat(ldp.resourceMapper._rootPath + '/resources/testPut.txt', function (err) {
179180
if (err) {
180181
throw err
181182
}
182183
})
184+
fs.stat(ldp.resourceMapper._rootPath + '/resources/testPut.txt.acl', function (err) {
185+
if (err) {
186+
throw err
187+
}
188+
})
183189

184190
// Now delete the dummy file
185191
await ldp.delete('/resources/testPut.txt')
@@ -189,6 +195,11 @@ describe('LDP', function () {
189195
throw new Error('file still exists')
190196
}
191197
})
198+
fs.stat(ldp.resourceMapper._rootPath + '/resources/testPut.txt.acl', function (err, s) {
199+
if (!err) {
200+
throw new Error('file still exists')
201+
}
202+
})
192203
})
193204

194205
it.skip('should fail to delete a non-empty folder', async () => {

0 commit comments

Comments
 (0)