Class AtomicConfigWriter.StagedWrite
java.lang.Object
com.ultikits.ultitools.config.document.AtomicConfigWriter.StagedWrite
- Enclosing class:
AtomicConfigWriter
A temporary file written beside its target and not yet moved over it.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final byte[]private final IOExceptionprivate final AtomicConfigWriter.FileOperationsprivate final Pathprivate final Pathprivate final Path -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateStagedWrite(Path target, Path temporary, Path identity, AtomicConfigWriter.FileOperations files, byte[] data, IOException deferredCause) -
Method Summary
Modifier and TypeMethodDescriptionvoidcommit()Atomically replaces the target, or uses the backed in-place path on an eligible refusal.(package private) voidPublishes the staged text as a new file, for a target that did not exist when it was read: never replaces a file that exists by then.booleancommitIfUnchanged(byte[] expected) Commits only while the target still holds exactlyexpected, re-read under the lock every config load and write holds, immediately before the replacement; otherwise the staged file is released and nothing is replaced.booleandiscard()Removes the temporary file without touching the target.private voidPuts the target back after its in-place write failed once it was open (#622): writesoriginal- the bytes it held immediately before, which the forcedbackupholds - back in place, then removes the backup as no longer needed.private voidrefreshBackup(Path backup, byte[] original, FileAttribute<?>[] attributes) Refreshes a retained backup without exposing a partial replacement.private voidreplaceInPlace(byte[] replacement, IOException cause) target()The file this write replaces (a symbolic link already resolved).The temporary file holding the new content on the normal path.
-
Field Details
-
target
-
temporary
-
identity
-
files
-
data
private final byte[] data -
deferredCause
-
-
Constructor Details
-
StagedWrite
private StagedWrite(Path target, Path temporary, Path identity, AtomicConfigWriter.FileOperations files, byte[] data, IOException deferredCause)
-
-
Method Details
-
target
The file this write replaces (a symbolic link already resolved).- Returns:
- the target
-
temporary
The temporary file holding the new content on the normal path. After an eligible temporary-create refusal, this is a reserved same-directory path that may not exist; the bytes remain in memory.- Returns:
- the temporary file
-
commit
Atomically replaces the target, or uses the backed in-place path on an eligible refusal.- Throws:
IOException- if replacement fails; a failed in-place attempt puts the target back from its backup first (aAtomicConfigWriter.RestoreFailedExceptionwhen that fails too, the backup then kept)
-
commitIfUnchanged
Commits only while the target still holds exactlyexpected, re-read under the lock every config load and write holds, immediately before the replacement; otherwise the staged file is released and nothing is replaced. This is the last-moment check for a caller that verified its edit against bytes it read earlier: an edit saved meanwhile is kept, never overwritten.- Parameters:
expected- the bytes the caller read and verified its edit against- Returns:
- whether the target was replaced
- Throws:
IOException- if reading the target or the replacement fails (seecommit())
-
replaceInPlace
- Throws:
IOException
-
putBack
private void putBack(byte[] original, Path backup, Exception failure) throws AtomicConfigWriter.RestoreFailedException Puts the target back after its in-place write failed once it was open (#622): writesoriginal- the bytes it held immediately before, which the forcedbackupholds - back in place, then removes the backup as no longer needed. When that fails, the backup is kept (no load deletes it any more), one SEVERE names the target and the backup and no content, and aAtomicConfigWriter.RestoreFailedExceptionreplaces the failure. -
refreshBackup
private void refreshBackup(Path backup, byte[] original, FileAttribute<?>[] attributes) throws IOException Refreshes a retained backup without exposing a partial replacement.- Throws:
IOException
-
commitAsNewFile
Publishes the staged text as a new file, for a target that did not exist when it was read: never replaces a file that exists by then. The temporary file is hard-linked onto the target, which fails if the target exists; where the file system refuses links, the target is checked again immediately before a move that does not replace. The temporary name is removed either way.- Throws:
FileAlreadyExistsException- if a file appeared at the target; it is left as it isIOException- if publishing fails otherwise; no file was replaced
-
discard
public boolean discard()Removes the temporary file without touching the target.- Returns:
- whether the temporary file is gone
-