atomicReplaceWithHooks method

  1. @override
Future<void> atomicReplaceWithHooks(
  1. Future<void> buildTemp(
    1. StorageBackend temp
    ), {
  2. void onBeforeSwap()?,
  3. void onAfterSwap()?,
})
override

Like atomicReplace, invoking onBeforeSwap right before the atomic swap executes and onAfterSwap immediately after the swap succeeds.

Implementation

@override
Future<void> atomicReplaceWithHooks(
  Future<void> Function(StorageBackend temp) buildTemp, {
  void Function()? onBeforeSwap,
  void Function()? onAfterSwap,
}) async {
  if (readOnly) {
    throw const WalPackReadOnlyException();
  }
  if (_closed) {
    throw StateError('IoStorageBackend ($path) is closed');
  }

  // Replace the real file behind a symlink instead of the link itself.
  final targetPath = resolveReplaceTarget(path);
  _sweepStaleCompactionTemps(targetPath);
  final originalMode = readPermissionBits(targetPath);

  final tempPath = '$targetPath.compact.$pid.${_tempCounter++}.tmp';
  final tempFile = File(tempPath);
  // Create the temp file born with 0600 (and exact original mode while still
  // empty) so restricted containers are never exposed with 0644 permissions.
  PosixFs.createPrivateTempFile(tempPath, mode: originalMode);

  final tempBackend = await IoStorageBackend.open(
    tempPath,
    createIfMissing: true,
    readOnly: false,
    ioMode: ioMode,
    keepHandleOpen: true,
  );
  // The temp name never needs to be durable: only the rename below does.
  tempBackend._needsDirectorySync = false;

  var swapped = false;
  try {
    await buildTemp(tempBackend);
    await tempBackend.flush();
    await tempBackend.close();
    if (originalMode != null) {
      PosixFs.chmod(tempPath, originalMode);
    }

    await _ioMutex.synchronized(() async {
      final oldRaf = _raf;
      _knownPos = -1;
      _closeDarwinSyncFd();
      onBeforeSwap?.call();
      if (Platform.isWindows || !keepHandleOpen) {
        _raf = null;
        if (oldRaf != null) {
          await oldRaf.close();
        }
        await tempFile.rename(targetPath);
        swapped = true;
        onAfterSwap?.call();
        if (keepHandleOpen) {
          _raf = await File(path).open(mode: FileMode.append);
          _darwinSyncFd = PosixFs.openDarwinSyncFd(targetPath);
        }
      } else {
        await tempFile.rename(targetPath);
        swapped = true;
        onAfterSwap?.call();
        _raf = null;
        if (oldRaf != null) {
          try {
            await oldRaf.close();
          } on Object {
            // Closing the replaced descriptor must not report swap failure.
          }
        }
        _raf = await File(path).open(mode: FileMode.append);
        _darwinSyncFd = PosixFs.openDarwinSyncFd(targetPath);
      }
      // Make the rename itself durable before reporting success.
      final dir = File(targetPath).absolute.parent.path;
      if (ioMode == .sync) {
        PosixFs.syncDirectory(dir);
      } else {
        await PosixFs.syncDirectoryAsync(dir);
      }
      _needsDirectorySync = false;
    });
  } on Object {
    _knownPos = -1;
    try {
      if (!tempBackend._closed) {
        await tempBackend.close();
      }
    } on Object {
      // Ignore secondary cleanup error.
    }
    if (!swapped) {
      try {
        if (tempFile.existsSync()) {
          tempFile.deleteSync();
        }
      } on Object {
        // Ignore secondary cleanup error.
      }
    }
    rethrow;
  }
}