1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

/*! A special repository writer that writes to a black hole. */

use {
    crate::{
        error::{DebianError, Result},
        io::ContentDigest,
        repository::{
            RepositoryPathVerification, RepositoryPathVerificationState, RepositoryWrite,
            RepositoryWriter,
        },
    },
    async_trait::async_trait,
    futures::AsyncRead,
    std::{borrow::Cow, pin::Pin, str::FromStr},
};

/// How [RepositoryWriter::verify_path()] should behave for [SinkWriter] instances.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SinkWriterVerifyBehavior {
    /// Path exists but an integrity check was not performed.
    ExistsNoIntegrityCheck,
    /// Path exists and its integrity was verified.
    ExistsIntegrityVerified,
    /// Path exists but its integrity doesn't match expectations.
    ExistsIntegrityMismatch,
    /// Path does not exist.
    Missing,
}

impl From<SinkWriterVerifyBehavior> for RepositoryPathVerificationState {
    fn from(v: SinkWriterVerifyBehavior) -> Self {
        match v {
            SinkWriterVerifyBehavior::ExistsNoIntegrityCheck => Self::ExistsNoIntegrityCheck,
            SinkWriterVerifyBehavior::ExistsIntegrityVerified => Self::ExistsIntegrityVerified,
            SinkWriterVerifyBehavior::ExistsIntegrityMismatch => Self::ExistsIntegrityMismatch,
            SinkWriterVerifyBehavior::Missing => Self::Missing,
        }
    }
}

impl FromStr for SinkWriterVerifyBehavior {
    type Err = DebianError;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s {
            "exists-no-integrity-check" => Ok(Self::ExistsNoIntegrityCheck),
            "exists-integrity-verified" => Ok(Self::ExistsIntegrityVerified),
            "exists-integrity-mismatch" => Ok(Self::ExistsIntegrityMismatch),
            "missing" => Ok(Self::Missing),
            _ => Err(DebianError::SinkWriterVerifyBehaviorUnknown(s.to_string())),
        }
    }
}

/// A [RepositoryWriter] that writes data to a black hole.
pub struct SinkWriter {
    verify_behavior: SinkWriterVerifyBehavior,
}

impl Default for SinkWriter {
    fn default() -> Self {
        Self {
            verify_behavior: SinkWriterVerifyBehavior::Missing,
        }
    }
}

impl SinkWriter {
    /// Set the behavior for [RepositoryWriter::verify_path()] on this instance.
    pub fn set_verify_behavior(&mut self, behavior: SinkWriterVerifyBehavior) {
        self.verify_behavior = behavior;
    }
}

#[async_trait]
impl RepositoryWriter for SinkWriter {
    async fn verify_path<'path>(
        &self,
        path: &'path str,
        _expected_content: Option<(u64, ContentDigest)>,
    ) -> Result<RepositoryPathVerification<'path>> {
        Ok(RepositoryPathVerification {
            path,
            state: self.verify_behavior.into(),
        })
    }

    async fn write_path<'path, 'reader>(
        &self,
        path: Cow<'path, str>,
        reader: Pin<Box<dyn AsyncRead + Send + 'reader>>,
    ) -> Result<RepositoryWrite<'path>> {
        let mut writer = futures::io::sink();
        let bytes_written = futures::io::copy(reader, &mut writer).await?;

        Ok(RepositoryWrite {
            path,
            bytes_written,
        })
    }
}