GitCorner

Code

ossartcraftfilmcraft🌐
f67d38b35cc5
use super::format::ExportSettings;
use super::{Arc, ExportError, Mutex, PoisonError, Result, Write, pcm};

/// Receives in-memory export output: `(path, bytes)` per finished file.
#[derive(Clone)]
pub struct OutputSink(pub Arc<dyn Fn(&str, Vec<u8>) -> std::io::Result<()> + Send + Sync>);

impl std::fmt::Debug for OutputSink {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("OutputSink")
    }
}

/// How an audio-only export codes its samples.
pub(crate) enum AudioWriter {
    Pcm(pcm::PcmContainer),
    Flac(Box<craft_audio::flac::Encoder>),
}

/// An export's output file: on disk, or in memory for an [`OutputSink`].
pub(crate) enum Out {
    File(std::io::BufWriter<std::fs::File>),
    Mem(std::io::Cursor<Vec<u8>>),
}

impl Out {
    pub(crate) fn create(settings: &ExportSettings) -> Result<Out> {
        if let Some(file) = &settings.output_file {
            let file = file.try_clone().map_err(|e| ExportError::Io(e.to_string()))?;
            return Ok(Out::File(std::io::BufWriter::new(file)));
        }
        Out::create_path(settings, &settings.path)
    }

    /// An output file at `path` (OP-Atom exports write several).
    pub(crate) fn create_path(settings: &ExportSettings, path: &str) -> Result<Out> {
        if settings.sink.is_some() {
            return Ok(Out::Mem(std::io::Cursor::new(Vec::new())));
        }
        if path == settings.path
            && let Some(file) = &settings.output_file
        {
            let file = file.try_clone().map_err(|e| ExportError::Io(e.to_string()))?;
            return Ok(Out::File(std::io::BufWriter::new(file)));
        }
        let f = std::fs::OpenOptions::new()
            .read(settings.format.is_mp4_with(settings.multiplexer))
            .write(true)
            .create(true)
            .truncate(true)
            .open(path)
            .map_err(|e| ExportError::Io(format!("{path}: {e}")))?;
        Ok(Out::File(std::io::BufWriter::new(f)))
    }

    /// Flush (and hand in-memory output to the sink); returns the file size.
    pub(crate) fn finish(self, settings: &ExportSettings) -> Result<u64> {
        self.finish_path(settings, &settings.path)
    }

    pub(crate) fn finish_path(self, settings: &ExportSettings, path: &str) -> Result<u64> {
        match self {
            Out::File(mut w) => {
                w.flush().map_err(|e| ExportError::Io(e.to_string()))?;
                Ok(std::fs::metadata(path).map(|m| m.len()).unwrap_or(0))
            }
            Out::Mem(c) => write_output(settings, path, c.into_inner()),
        }
    }
}

/// Whether `path` still names the open `file` (the same device and inode). Another process can
/// replace a reserved output's directory entry while the handle stays open; a write through the
/// handle then lands in a file nobody can reach. Where the identity cannot be read (non-Unix
/// targets) this says yes, as an unchecked write did before.
pub fn path_is_file(path: &str, file: &std::fs::File) -> bool {
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        match (std::fs::metadata(path), file.metadata()) {
            (Ok(a), Ok(b)) => a.dev() == b.dev() && a.ino() == b.ino(),
            _ => false,
        }
    }
    #[cfg(not(unix))]
    {
        let _ = (path, file);
        true
    }
}

/// Write one finished output file (to the sink when there is one); returns its size.
pub(crate) fn write_output(settings: &ExportSettings, path: &str, data: Vec<u8>) -> Result<u64> {
    let n = data.len() as u64;
    match (&settings.sink, &settings.output_file) {
        (Some(sink), _) => (sink.0)(path, data),
        // the primary file was reserved and opened by the caller: write through that handle, as
        // reopening the path would follow whatever replaced the entry in the meantime
        (None, Some(file)) if path == settings.path => (|| {
            let mut f = file.try_clone()?;
            f.set_len(0)?;
            std::io::Seek::rewind(&mut f)?;
            f.write_all(&data)?;
            f.flush()?;
            if path_is_file(path, &f) {
                Ok(())
            } else {
                Err(std::io::Error::other(format!("{path} was replaced while the export was written; the export was not saved there")))
            }
        })(),
        (None, _) => std::fs::write(path, &data),
    }
    .map_err(|e| ExportError::Io(e.to_string()))?;
    Ok(n)
}

impl Write for Out {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        match self {
            Out::File(w) => w.write(buf),
            Out::Mem(w) => w.write(buf),
        }
    }
    fn flush(&mut self) -> std::io::Result<()> {
        match self {
            Out::File(w) => w.flush(),
            Out::Mem(w) => w.flush(),
        }
    }
}

impl std::io::Seek for Out {
    fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
        match self {
            Out::File(w) => w.seek(pos),
            Out::Mem(w) => w.seek(pos),
        }
    }
}

impl std::io::Read for Out {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        match self {
            Out::File(w) => {
                // Fast-start muxing reads media back to shift it in place. Commit buffered
                // writes before reading through the same file handle and cursor.
                w.flush()?;
                std::io::Read::read(w.get_mut(), buf)
            }
            Out::Mem(w) => std::io::Read::read(w, buf),
        }
    }
}

/// An in-memory writer that stays reachable after the encoder that owns a clone is dropped.
#[derive(Clone, Default)]
pub(crate) struct SharedBuf(pub(crate) Arc<Mutex<Vec<u8>>>);

impl Write for SharedBuf {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.0.lock().unwrap_or_else(PoisonError::into_inner).extend_from_slice(buf);
        Ok(buf.len())
    }
    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

#[cfg(all(test, unix))]
mod tests {
    use super::*;

    #[test]
    fn a_displaced_reservation_is_an_error_and_the_replacement_is_kept() {
        let dir = std::env::temp_dir().join(format!("fc-displaced-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("out.gif");
        let path_str = path.to_string_lossy().into_owned();
        let file = std::fs::OpenOptions::new().read(true).write(true).create_new(true).open(&path).unwrap();
        assert!(path_is_file(&path_str, &file));
        let settings = ExportSettings { path: path_str.clone(), output_file: Some(Arc::new(file)), ..Default::default() };
        assert_eq!(write_output(&settings, &path_str, b"gif".to_vec()).unwrap(), 3);
        assert_eq!(std::fs::read(&path).unwrap(), b"gif");
        // another process replaces the entry
        std::fs::remove_file(&path).unwrap();
        std::fs::write(&path, b"theirs").unwrap();
        assert!(write_output(&settings, &path_str, b"gif".to_vec()).is_err());
        assert_eq!(std::fs::read(&path).unwrap(), b"theirs");
        let _ = std::fs::remove_dir_all(&dir);
    }
}