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
15 changes: 15 additions & 0 deletions src/errors/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
'use strict'

/**
* Error raised when there is lock already in place when repo is being opened.
*/
class LockExistsError extends Error {
constructor (message) {
super(message)
this.name = 'LockExistsError'
this.code = 'ERR_LOCK_EXISTS'
this.message = message
}
}

LockExistsError.code = 'ERR_LOCK_EXISTS'
exports.LockExistsError = LockExistsError

/**
* Error raised when requested item is not found.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/lock-memory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const errors = require('./errors')
const debug = require('debug')

const log = debug('repo:lock')
Expand All @@ -17,6 +18,11 @@ const LOCKS = {}
exports.lock = async (dir) => { // eslint-disable-line require-await
const file = dir + '/' + lockFile
log('locking %s', file)

if (LOCKS[file] === true) {
throw new errors.LockExistsError(`Lock already being held for file: ${file}`)
}

LOCKS[file] = true
const closer = {
async close () { // eslint-disable-line require-await
Expand Down
1 change: 1 addition & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ describe('IPFS Repo Tests on the Browser', () => {
require('./keystore-test')(repo)
require('./config-test')(repo)
require('./api-addr-test')(repo)
require('./lock-test')(repo)
})
8 changes: 1 addition & 7 deletions test/lock-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,10 @@ module.exports = (repo) => {
})

describe('lock-memory', () => {
it('should lock a dir', async () => {
it('should lock and unlock dir', async () => {
const dir = '/foo/bar'
expect(await lockMemory.locked(dir)).to.be.false()

await lockMemory.lock(dir)
expect(await lockMemory.locked(dir)).to.be.true()
})

it('should unlock a dir', async () => {
const dir = '/foo/bar'
const closer = await lockMemory.lock(dir)
expect(await lockMemory.locked(dir)).to.be.true()

Expand Down