Class AtomicFiles

java.lang.Object
net.fabricmc.loom.util.cache.AtomicFiles

public final class AtomicFiles extends Object
原子发布工具:保证「文件存在 ⟺ 内容完整」这一不变量成立.

各最终产物先写入「与目标同目录的唯一临时文件」,写完整后再用原子 move 落位。 这样跨 daemon 的无锁存在性检查不会在写入期间看到半成品:读方要么看到旧文件、要么看到完整的新文件。

Windows 的替换限制

POSIX 的 rename(2) 可以覆盖任何同名文件,但 Windows 不允许替换(或删除)正被其它句柄打开的文件: 只要读方还持有句柄,MoveFileEx(REPLACE_EXISTING) 就会以 ERROR_ACCESS_DENIED 失败, Java 侧表现为 AccessDeniedException。实测 InputStream、FileChannel、 ZipFile 与 FileSystem(zipfs) 的读取句柄都会触发该限制。

因此 move(java.nio.file.Path, java.nio.file.Path) 对这种情况做「有上限的退避重试」:短生命周期的读方(例如反复读取的构建)会被自动让过, 持续占用则最终抛出带诊断信息的异常,而不是把平台限制原样丢给调用方。读方的不变量不受影响—— 重试期间目标始终是旧的完整文件。

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    接收一个 Path 并向其写入内容的回调,允许抛出 IOException.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    copy(Path source, Path target)
    便捷方法:原子复制一个已存在的源文件到 target.
    static void
    move(Path source, Path target)
    原子 move:优先 ATOMIC_MOVE,个别平台不支持时退化为普通 move.
    static void
    原子发布:让 producer 把内容写入「同目录唯一临时文件」,写完后原子 move 到 target.
    static Path
    tempSibling(Path target)
    为 target 生成一个同目录、唯一且尚不存在的临时路径(不创建文件).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait