MCPcopy Create free account
hub / github.com/bytecodealliance/system-interface / append

Method append

src/fs/file_io_ext.rs:527–558  ·  view source on GitHub ↗
(&self, buf: &[u8])

Source from the content-addressed store, hash-verified

525 }
526
527 fn append(&self, buf: &[u8]) -> io::Result<usize> {
528 use rustix::fs::{fcntl_getfl, fcntl_setfl, seek, OFlags, SeekFrom};
529 use rustix::io::write;
530
531 // On Linux, use `pwritev2`.
532 #[cfg(any(target_os = "android", target_os = "linux"))]
533 {
534 use rustix::io::{pwritev2, Errno, ReadWriteFlags};
535
536 let iovs = [IoSlice::new(buf)];
537 match pwritev2(self, &iovs, 0, ReadWriteFlags::APPEND) {
538 Err(Errno::NOSYS) | Err(Errno::NOTSUP) => {}
539 otherwise => return Ok(otherwise?),
540 }
541 }
542
543 // Otherwise use `F_SETFL` to switch the file description to append
544 // mode, do the write, and switch back. This is not atomic with
545 // respect to other users of the file description, but this is
546 // possibility is documented in the trait.
547 //
548 // Optimization idea: some users don't care about the current position,
549 // changing, so perhaps we could add a `append_relaxed` etc. API which
550 // doesn't preserve positions.
551 let old_flags = fcntl_getfl(self)?;
552 let old_pos = tell(self)?;
553 fcntl_setfl(self, old_flags | OFlags::APPEND)?;
554 let result = write(self, buf);
555 fcntl_setfl(self, old_flags).unwrap();
556 seek(self, SeekFrom::Start(old_pos)).unwrap();
557 Ok(result?)
558 }
559
560 fn append_vectored(&self, bufs: &[IoSlice]) -> io::Result<usize> {
561 use rustix::fs::{fcntl_getfl, fcntl_setfl, seek, OFlags, SeekFrom};

Callers 3

append_allMethod · 0.80
append_vectoredMethod · 0.80
_reopen_appendFunction · 0.80

Calls 3

writeFunction · 0.85
reopen_appendFunction · 0.85
writeMethod · 0.80

Tested by

no test coverage detected