acquireLock method
Attempts to acquire a lock for auto-updater. Returns true if lock was acquired, false if another process holds it.
Implementation
Future<bool> acquireLock() async {
final lockPath = lockFilePath;
try {
final lockFile = File(lockPath);
if (lockFile.existsSync()) {
final stats = lockFile.statSync();
final age = DateTime.now().difference(stats.modified);
if (age < _lockTimeout) {
return false;
}
// Lock is stale -- re-verify and remove
try {
final recheck = File(lockPath).statSync();
final recheckAge = DateTime.now().difference(recheck.modified);
if (recheckAge < _lockTimeout) {
return false;
}
lockFile.deleteSync();
} on FileSystemException {
return false;
}
}
} on FileSystemException catch (e) {
// File doesn't exist -- proceed to create
if (e.osError?.errorCode != 2) {
// Not ENOENT
return false;
}
}
// Create lock file
try {
final lockFile = File(lockPath);
lockFile.writeAsStringSync('$pid', mode: FileMode.writeOnly);
return true;
} on FileSystemException {
return false;
}
}