Skip to content

Commit

Permalink
SPVBlockStore: lock the store file as early as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
schildbach committed Jun 29, 2024
1 parent de8afc6 commit 085ed1d
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions core/src/main/java/org/bitcoinj/store/SPVBlockStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public SPVBlockStore(NetworkParameters params, File file, int capacity, boolean
boolean exists = file.exists();
// Set up the backing file.
randomAccessFile = new RandomAccessFile(file, "rw");
FileChannel channel = randomAccessFile.getChannel();
// Lock the file.
fileLock = channel.tryLock();
if (fileLock == null)
throw new ChainFileLockedException("Store file is already locked by another process");

fileLength = getFileSize(capacity);
if (!exists) {
log.info("Creating new SPV block chain file " + file);
Expand All @@ -140,11 +146,6 @@ else if (fileLength < randomAccessFile.length())
}
}

FileChannel channel = randomAccessFile.getChannel();
fileLock = channel.tryLock();
if (fileLock == null)
throw new ChainFileLockedException("Store file is already locked by another process");

// Map it into memory read/write. The kernel will take care of flushing writes to disk at the most
// efficient times, which may mean that until the map is deallocated the data on disk is randomly
// inconsistent. However the only process accessing it is us, via this mapping, so our own view will
Expand Down

0 comments on commit 085ed1d

Please sign in to comment.