stakpak_shared/
remote_store.rs

1use crate::remote_connection::RemoteConnection;
2use std::path::PathBuf;
3use std::sync::Arc;
4
5pub struct RemoteStore {}
6
7impl RemoteStore {
8    /// Get the remote session store path (relative to remote working directory)
9    pub fn get_remote_session_store_path() -> PathBuf {
10        PathBuf::from(".stakpak").join("session")
11    }
12
13    /// Get the absolute remote session store path by canonicalizing on the remote host
14    pub async fn get_absolute_remote_session_store_path(
15        conn: &Arc<RemoteConnection>,
16    ) -> Result<String, String> {
17        let relative_path = Self::get_remote_session_store_path();
18        let relative_path_str = relative_path.to_string_lossy().to_string();
19
20        if let Err(e) = conn
21            .execute_command(&format!("mkdir -p '{}'", relative_path_str), None, None)
22            .await
23        {
24            return Err(format!("Failed to create remote session directory: {}", e));
25        }
26
27        match conn.canonicalize(&relative_path_str).await {
28            Ok(abs_path) => Ok(abs_path),
29            Err(e) => Err(format!("Failed to canonicalize remote session path: {}", e)),
30        }
31    }
32
33    /// Get the backup directory path relative to session store
34    pub fn get_backup_dir_path() -> PathBuf {
35        PathBuf::from("backups")
36    }
37
38    /// Get the full backup directory path for a given session ID
39    pub fn get_backup_session_path(session_id: &str) -> PathBuf {
40        Self::get_backup_dir_path().join(session_id)
41    }
42
43    /// Get the backup directory path as a string (for remote operations)
44    pub fn get_backup_dir_string() -> String {
45        Self::get_remote_session_store_path()
46            .join(Self::get_backup_dir_path())
47            .to_string_lossy()
48            .to_string()
49    }
50
51    /// Get the full backup directory path as a string for a given session ID (for remote operations)
52    pub fn get_backup_session_string(session_id: &str) -> String {
53        Self::get_remote_session_store_path()
54            .join(Self::get_backup_session_path(session_id))
55            .to_string_lossy()
56            .to_string()
57    }
58
59    /// Get the absolute backup session path on the remote host
60    pub async fn get_absolute_backup_session_path(
61        conn: &Arc<RemoteConnection>,
62        session_id: &str,
63    ) -> Result<String, String> {
64        let relative_backup_path = Self::get_backup_session_string(session_id);
65
66        if let Err(e) = conn
67            .execute_command(&format!("mkdir -p '{}'", relative_backup_path), None, None)
68            .await
69        {
70            return Err(format!("Failed to create remote backup directory: {}", e));
71        }
72
73        match conn.canonicalize(&relative_backup_path).await {
74            Ok(abs_path) => Ok(abs_path),
75            Err(e) => Err(format!("Failed to canonicalize remote backup path: {}", e)),
76        }
77    }
78}