summaryrefslogtreecommitdiff
path: root/rola-bucket/src/protocol/local_fs.rs
blob: ec8999b3ed066b2d47987daa4debd71058731362 (plain) (blame)
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
use shared_functions::copy_with_temp_rename;
use std::path::PathBuf;

use crate::{
    BucketTransferProtocol, BucketTransferProtocolError,
    LocalFileSystemProtocol::{BucketSide, ClientSide},
};

/// Represents the two sides of a local filesystem transfer protocol.
///
/// This enum distinguishes between the **client side** (where the bucket root is local)
/// and the **bucket side** (where the client root is local). Each variant stores the
/// path to the corresponding root directory on the local machine.
///
/// - `ClientSide { bucket_root }`: Used when this instance is running on the client side.
///   The bucket root is the directory on the local filesystem that acts as the bucket.
/// - `BucketSide { client_root }`: Used when this instance is running on the bucket side.
///   The client root is the directory on the local filesystem where client data is stored.
pub enum LocalFileSystemProtocol {
    /// The client side of the transfer; `bucket_root` is the local path to the bucket.
    ClientSide { bucket_root: PathBuf },
    /// The bucket side of the transfer; `client_root` is the local path to the client's data.
    BucketSide { client_root: PathBuf },
}

impl BucketTransferProtocol for LocalFileSystemProtocol {
    type ExtraInfo = ();

    fn upload_to_bucket(
        &self,
        req: &super::UploadToBucketRequest<Self::ExtraInfo>,
    ) -> Result<(), BucketTransferProtocolError> {
        let ClientSide { bucket_root } = self else {
            // NOTE: As a local filesystem transfer protocol,
            //   if this function is not executed on the client side, it does nothing by default.
            return Ok(());
        };
        let abs_send_path = req.absolute_file_path_to_upload;
        let abs_receive_path = bucket_root.join(req.relative_file_path_to_receive);
        copy_with_temp_rename(abs_send_path, abs_receive_path)
            .map_err(BucketTransferProtocolError::IoError)?;
        Ok(())
    }

    fn download_from_bucket(
        &self,
        req: &super::DownloadFromBucketRequest<Self::ExtraInfo>,
    ) -> Result<(), BucketTransferProtocolError> {
        let ClientSide { bucket_root } = self else {
            // NOTE: As a local filesystem transfer protocol,
            //   if this function is not executed on the client side, it does nothing by default.
            return Ok(());
        };
        let abs_send_path = bucket_root.join(req.relative_file_path_to_transfer);
        let abs_receive_path = req.absolute_file_path_to_download;
        copy_with_temp_rename(abs_send_path, abs_receive_path)
            .map_err(BucketTransferProtocolError::IoError)?;
        Ok(())
    }

    fn transfer_to_client(
        &self,
        req: &super::TransferToClientRequest<Self::ExtraInfo>,
    ) -> Result<(), BucketTransferProtocolError> {
        let BucketSide { client_root } = self else {
            // NOTE: As a local filesystem transfer protocol,
            //   if this function is not executed on the bucket side, it does nothing by default.
            return Ok(());
        };
        let abs_send_path = req.absolute_file_path_to_transfer;
        let abs_receive_path = client_root.join(req.relative_file_path_to_download);
        copy_with_temp_rename(abs_send_path, abs_receive_path)
            .map_err(BucketTransferProtocolError::IoError)?;
        Ok(())
    }

    fn receive_from_client(
        &self,
        req: &super::ReceiveFromClientRequest<Self::ExtraInfo>,
    ) -> Result<(), BucketTransferProtocolError> {
        let BucketSide { client_root } = self else {
            // NOTE: As a local filesystem transfer protocol,
            //   if this function is not executed on the bucket side, it does nothing by default.
            return Ok(());
        };
        let abs_send_path = req.absolute_file_path_to_receive;
        let abs_receive_path = client_root.join(req.relative_file_path_to_upload);
        copy_with_temp_rename(abs_send_path, abs_receive_path)
            .map_err(BucketTransferProtocolError::IoError)?;
        Ok(())
    }
}