Skip to main content

AtomicWriter

Struct AtomicWriter 

Source
pub struct AtomicWriter { /* private fields */ }
Expand description

流式原子写入器。

适用于加密大文件——载荷逐块产生,不必先在内存里拼出完整文件。 未调用 AtomicWriter::commit 就 drop 时,临时文件会被自动删除。

§示例

use omy_core::fsatomic::AtomicWriter;
let mut w = AtomicWriter::create(std::path::Path::new("out.omy"))?;
w.write_all(b"header")?;
w.write_all(b"chunk-0")?;
w.commit()?;   // 不调用 commit 则临时文件被丢弃

Implementations§

Source§

impl AtomicWriter

Source

pub fn create(target: &Path) -> Result<Self>

创建临时文件并准备写入。

临时文件名为 <target>.<pid>.<nonce><TMP_SUFFIX>,与目标同目录—— 这是 rename 原子性的前提(跨文件系统的 rename 不是原子操作,且多数平台 直接失败)。

名字必须每个实例唯一,不能固定为 <target>.tmp:两个并发写入者(例如 两个 Tauri 命令线程同时保存同一配置)会共用同一个 tmp 文件,先提交者 的 rename 会把 tmp 移走,后提交者的 rename 报 ENOENT——并发保存因此 偶发失败。唯一的 tmp 让各写入者互不干扰,最终语义仍是「最后一次提交 胜出」。

§Errors

创建目录或临时文件失败时返回 Error::Io。

Source

pub fn write_all(&mut self, buf: &[u8]) -> Result<()>

追加写入一段数据。

§Errors

写入失败或写入器已关闭时返回 Error::Io。

Source

pub const fn written(&self) -> u64

已写入的字节数。

Source

pub fn target(&self) -> &Path

目标路径。

Source

pub fn commit(self) -> Result<()>

提交:fsync → fsync 父目录 → rename → fsync 父目录。

§Errors

任一步失败时返回 Error::Io;此时会尽力删除临时文件,目标保持原样。

Source

pub fn abort(self)

主动放弃写入并删除临时文件。

Trait Implementations§

Source§

impl Debug for AtomicWriter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for AtomicWriter

Source§

fn drop(&mut self)

未提交即析构 → 丢弃临时文件,避免留下垃圾。

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Seek for AtomicWriter

标准 Seek:流式加密写完载荷后要回到开头填写头部。

注意 written 统计的是写入调用累计的字节数,seek 回去覆写头部会让它 大于文件长度;需要文件长度时以 stream_position / 元数据为准。

Source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

Seek to an offset, in bytes, in a stream. Read more
1.55.0 · Source§

fn rewind(&mut self) -> Result<(), Error>

Rewind to the beginning of a stream. Read more
Source§

fn stream_len(&mut self) -> Result<u64, Error>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · Source§

fn stream_position(&mut self) -> Result<u64, Error>

Returns the current seek position from the start of the stream. Read more
1.80.0 · Source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

Seeks relative to the current position. Read more
Source§

impl Write for AtomicWriter

标准 Write:让流式加密直接写进临时文件,提交前目标位置不受影响。

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.