feat: add config option for if the DOC should lock the file channel (#2526)

- related to #2222
- effectively just a workaround as most people would not need to worry about locking the clipboard file
This commit is contained in:
Jordan 2023-12-18 15:04:34 +00:00 committed by GitHub
parent f44b1b48c7
commit 6caf4640ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 15 deletions

View File

@ -721,6 +721,13 @@ public class Settings extends Config {
" - Requires clipboard.use-disk to be enabled" " - Requires clipboard.use-disk to be enabled"
}) })
public boolean SAVE_CLIPBOARD_NBT_TO_DISK = true; public boolean SAVE_CLIPBOARD_NBT_TO_DISK = true;
@Comment({
"Apply a file lock on the clipboard file (only relevant if clipboad.on-disk is enabled)",
" - Prevents other processes using the file whilst in use by FAWE",
" - This extends to other servers, useful if you have multiple servers using a unified clipboard folder",
" - May run into issues where a file lock is not correctly lifted"
})
public boolean LOCK_CLIPBOARD_FILE = false;
} }

View File

@ -305,22 +305,24 @@ public class DiskOptimizedClipboard extends LinearClipboard {
private void init() throws IOException { private void init() throws IOException {
if (this.fileChannel == null) { if (this.fileChannel == null) {
this.fileChannel = braf.getChannel(); this.fileChannel = braf.getChannel();
try { if (Settings.settings().CLIPBOARD.LOCK_CLIPBOARD_FILE) {
FileLock lock = this.fileChannel.lock(); try {
LOCK_HOLDER_CACHE.put(file.getName(), new LockHolder(lock)); FileLock lock = this.fileChannel.lock();
} catch (OverlappingFileLockException e) { LOCK_HOLDER_CACHE.put(file.getName(), new LockHolder(lock));
LockHolder existing = LOCK_HOLDER_CACHE.get(file.getName()); } catch (OverlappingFileLockException e) {
if (existing != null) { LockHolder existing = LOCK_HOLDER_CACHE.get(file.getName());
long ms = System.currentTimeMillis() - existing.lockHeldSince; if (existing != null) {
LOGGER.error( long ms = System.currentTimeMillis() - existing.lockHeldSince;
"Cannot lock clipboard file {} acquired by thread {}, {}ms ago", LOGGER.error(
file.getName(), "Cannot lock clipboard file {} acquired by thread {}, {}ms ago",
existing.thread, file.getName(),
ms existing.thread,
); ms
);
}
// Rethrow to prevent clipboard access
throw e;
} }
// Rethrow to prevent clipboard access
throw e;
} }
this.byteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, braf.length()); this.byteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, braf.length());
} }