[core] Fix file lock in strict mode (#791)

This commit is contained in:
LoveSy 2021-06-30 20:44:16 +08:00 committed by GitHub
parent 38ad39ef09
commit fe5dcbc3a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 3 deletions

View File

@ -101,7 +101,29 @@ public class ConfigManager {
private static final File modulesLog = new File(logPath, "modules.log"); private static final File modulesLog = new File(logPath, "modules.log");
private static final File oldModulesLog = new File(logPath, "modules.old.log"); private static final File oldModulesLog = new File(logPath, "modules.old.log");
private static final File verboseLogPath = new File(logPath, "all.log"); private static final File verboseLogPath = new File(logPath, "all.log");
private static FileLock locker = null;
static class FileLocker {
private final FileChannel lockChannel;
private final FileLock locker;
FileLocker(@NonNull FileChannel lockChannel) throws IOException {
this.lockChannel = lockChannel;
this.locker = lockChannel.tryLock();
}
boolean isValid() {
return this.locker != null && this.locker.isValid();
}
@Override
protected void finalize() throws Throwable {
this.locker.release();
this.lockChannel.close();
}
}
static FileLocker locker = null;
static { static {
try { try {
@ -187,8 +209,8 @@ public class ConfigManager {
try { try {
var lockChannel = FileChannel.open(lockPath.toPath(), openOptions, permissions); var lockChannel = FileChannel.open(lockPath.toPath(), openOptions, permissions);
locker = lockChannel.tryLock(); locker = new FileLocker(lockChannel);
return locker != null && locker.isValid(); return locker.isValid();
} catch (Throwable e) { } catch (Throwable e) {
return false; return false;
} }