fsx

    Durable file operations for MoonBit: atomic write (fsync+rename), mkdir_p, append, chmod, rotation and flock

    fs
    file
    atomic
    fsync
    rotate
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    7 hours ago
    Downloads
    3

    #chensuiyi/fsx

    Durable file operations · 持久化文件操作

    崩溃安全的文件写入与目录操作:原子写(fsync + rename + 父目录 fsync)、mkdir_p、追加、chmod、日志轮转(rename 与 copytruncate 双模式)、flock、/proc 友好的小文件读取。

    • 每一步写都由 poll 限时(5s),满盘/卡死目标不会挂住调用方
    • mkdir_p 幂等;错误统一 FileError::Failed(op~, errno~)
    • 运行时零依赖;Linux native

    Crash-safe file writing and directory operations: atomic write (fsync + rename + parent-dir fsync), mkdir_p, append, chmod, log rotation (rename and copytruncate modes), flock, /proc-friendly small reads.

    • Every write step is bounded by a poll deadline (5s) — a full disk or a stalled target never hangs the caller
    • mkdir_p is idempotent; errors are FileError::Failed(op~, errno~)
    • Zero runtime dependencies; Linux native

    #API

    接口 Interface说明 Description
    write_atomic(path, data)写 .tmp → fsync → 原子改名;Atomic write via tmp + fsync + rename
    mkdir_p(path, mode?)幂等建目录;Idempotent recursive mkdir
    append_file / chmod / unlink / file_size追加 / 改权限 / 删除 / 大小;Append / chmod / remove / size
    rotate / copytruncate轮转(保留 10 代);Rotation, ten generations kept
    lock_exclusive(path)非阻塞 flock(CLOEXEC);Non-blocking flock, CLOEXEC
    read_small(path, cap?)循环读到 EOF 或 cap;Read to EOF, capped

    License: MIT

    FileError

    pub(all) suberror FileError {
    Failed(op~ : String, errno~ : Int)
    }

    impl Show for FileError

    FileError::output

    fn FileError::output(self : FileError, logger : &Logger) -> Unit

    FileError::to_string

    fn FileError::to_string(self : FileError) -> String

    append_file

    fn append_file(path : String, data : Bytes) -> Unit raise FileError

    chmod

    fn chmod(path : String, mode : Int) -> Unit raise FileError

    Set the mode of an existing path. mkdir_p only applies its mode to directories it creates, so a state dir left behind by an older version (or created by hand) needs an explicit chmod to stop being group or world readable — it holds app logs.

    copytruncate

    fn copytruncate(path : String) -> Unit raise FileError

    Copy path into path.1 (shifting the chain) and truncate path to zero. For logs held open by app processes, which keep writing to the old inode; a few bytes written during the copy are lost.

    file_size

    fn file_size(path : String) -> Int64?

    File size in bytes; None when the file does not exist or is unreadable.

    lock_exclusive

    fn lock_exclusive(path : String) -> Int raise FileError

    Take a non-blocking exclusive flock on path; the returned fd must be kept open for the lock to be held (released on close or process exit).

    mkdir_p

    fn mkdir_p(path : String, mode? : Int) -> Unit raise FileError

    read_small

    fn read_small(path : String, cap? : Int) -> Bytes raise FileError

    Read a small file into a buffer; works on /proc virtual files.

    rotate

    fn rotate(path : String) -> Unit raise FileError

    Shift the rotation chain and rename path to path.1 (drops path.10). For logs written by this daemon, where no fd survives the rename.
    fn unlink(path : String) -> Unit raise FileError

    write_atomic

    fn write_atomic(path : String, data : Bytes) -> Unit raise FileError

    Write ".tmp", fsync, then atomically rename over path.