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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//!
//! Cache postgresql files, access to executables, clean up files
//!

use std::path::PathBuf;

use crate::pg_errors::PgEmbedError;
use futures::TryFutureExt;
use tokio::io::AsyncWriteExt;
use crate::postgres::PgSettings;
use crate::pg_fetch::PgFetchSettings;
use std::io::{Error, ErrorKind};
use crate::pg_errors::PgEmbedError::PgPurgeFailure;
use std::cell::Cell;
use tokio::process::Command;
use crate::pg_enums::{PgAuthMethod, OperationSystem};

///
/// Access to pg_ctl, initdb, database directory and cache directory
///
pub struct PgAccess {
    /// Cache directory path
    pub cache_dir: PathBuf,
    /// Database directory path
    pub database_dir: PathBuf,
    /// Postgresql pg_ctl executable path
    pub pg_ctl_exe: PathBuf,
    /// Postgresql initdb executable path
    pub init_db_exe: PathBuf,
    /// Password file path
    pub pw_file_path: PathBuf,
    /// Postgresql binaries zip file path
    pub zip_file_path: PathBuf,
}

impl PgAccess {
    ///
    /// Create a new instance
    ///
    /// Directory structure for cached postgresql binaries will be created
    ///
    pub async fn new(fetch_settings: &PgFetchSettings, database_dir: &PathBuf) -> Result<Self, PgEmbedError> {
        let cache_dir = Self::create_cache_dir_structure(&fetch_settings).await?;
        let mut pg_ctl = cache_dir.clone();
        pg_ctl.push("bin/pg_ctl");
        let mut init_db = cache_dir.clone();
        init_db.push("bin/initdb");
        let mut pw_file = cache_dir.clone();
        pw_file.push("pwfile");
        let mut zip_file_path = cache_dir.clone();
        let platform =
            fetch_settings.platform();
        let file_name = format!(
            "{}-{}.zip",
            platform,
            &fetch_settings.version.0
        );
        zip_file_path.push(file_name);

        Ok(
            PgAccess {
                cache_dir,
                database_dir: database_dir.clone(),
                pg_ctl_exe: pg_ctl,
                init_db_exe: init_db,
                pw_file_path: pw_file,
                zip_file_path,
            }
        )
    }

    ///
    /// Create directory structure for cached postgresql executables
    ///
    /// Returns PathBuf(cache_directory) on success, an error otherwise
    ///
    async fn create_cache_dir_structure(fetch_settings: &PgFetchSettings) -> Result<PathBuf, PgEmbedError> {
        let cache_dir =
            dirs::cache_dir().ok_or_else(
                || PgEmbedError::DirCreationError(Error::new(ErrorKind::Other, "cache dir error"))
            )?;
        let os_string = match fetch_settings.operating_system {
            OperationSystem::Darwin | OperationSystem::Windows | OperationSystem::Linux => fetch_settings.operating_system.to_string(),
            OperationSystem::AlpineLinux => format!("arch_{}", fetch_settings.operating_system.to_string())
        };
        let pg_path = format!("pg-embed/{}/{}/{}", os_string, fetch_settings.architecture.to_string(), fetch_settings.version.0);
        let mut cache_pg_embed = cache_dir.clone();
        cache_pg_embed.push(pg_path);
        tokio::fs::create_dir_all(
            &cache_pg_embed,
        ).map_err(|e| PgEmbedError::DirCreationError(e))
            .await?;
        Ok(cache_pg_embed)
    }

    ///
    /// Check if postgresql executables are already cached
    ///
    pub async fn pg_executables_cached(&self) -> Result<bool, PgEmbedError> {
        if let Ok(_) = tokio::fs::File::open(self.init_db_exe.as_path()).await {
            Ok(true)
        } else {
            Ok(false)
        }
    }

    ///
    /// Check if database directory exists
    ///
    pub async fn database_dir_exists(&self) -> Result<bool, PgEmbedError> {
        if let Ok(_) = tokio::fs::File::open(self.database_dir.as_path()).await {
            Ok(true)
        } else {
            Ok(false)
        }
    }

    ///
    /// Write pg binaries zip to postgresql cache directory
    ///
    pub async fn write_pg_zip(&self, bytes: &[u8]) -> Result<(), PgEmbedError> {
        let mut file: tokio::fs::File =
            tokio::fs::File::create(&self.zip_file_path.as_path()).map_err(|e| PgEmbedError::WriteFileError(e)).await?;
        file.write_all(&bytes).map_err(|e| PgEmbedError::WriteFileError(e))
            .await?;
        Ok(())
    }

    ///
    /// Clean up created files and directories.
    ///
    /// Remove created directories containing the database and the password file.
    ///
    pub fn clean(&self) -> Result<(), PgEmbedError> {
        // not using tokio::fs async methods because clean() is called on drop
        std::fs::remove_dir_all(self.database_dir.as_path()).map_err(|e| PgEmbedError::PgCleanUpFailure(e))?;
        std::fs::remove_file(self.pw_file_path.as_path()).map_err(|e| PgEmbedError::PgCleanUpFailure(e))?;
        Ok(())
    }

    ///
    /// Purge postgresql executables
    ///
    /// Remove cached postgresql executables
    ///
    async fn purge(&self) -> Result<(), PgEmbedError> {
        tokio::fs::remove_dir_all(self.cache_dir.as_path()).map_err(|e| PgEmbedError::PgPurgeFailure(e)).await
    }

    ///
    /// Create a database password file
    ///
    /// Returns `Ok(())` on success, otherwise returns an error.
    ///
    pub async fn create_password_file(&self, password: &[u8]) -> Result<(), PgEmbedError> {
        let mut file: tokio::fs::File = tokio::fs::File::create(self.zip_file_path.as_path()).map_err(|e| PgEmbedError::WriteFileError(e)).await?;
        let _ = file
            .write(password).map_err(|e| PgEmbedError::WriteFileError(e))
            .await?;
        Ok(())
    }

    ///
    /// Create initdb command
    ///
    pub fn init_db_command(&self, database_dir: &PathBuf, user: &str, auth_method: &PgAuthMethod) -> Box<Cell<Command>> {
        let init_db_executable = self.init_db_exe.to_str().unwrap();
        let password_file_arg = format!("--pwfile={}", self.zip_file_path.to_str().unwrap());
        let auth_host =
            match auth_method {
                PgAuthMethod::Plain => {
                    "password"
                }
                PgAuthMethod::MD5 => {
                    "md5"
                }
                PgAuthMethod::ScramSha256 => {
                    "scram-sha-256"
                }
            };

        let mut command =
            Box::new(
                Cell::new(
                    tokio::process::Command::new(init_db_executable)
                )
            );
        command.get_mut()
            .args(&[
                "-A",
                auth_host,
                "-U",
                user,
                "-D",
                database_dir.to_str().unwrap(),
                &password_file_arg,
            ]);
        command
    }

    ///
    /// Create pg_ctl start command
    ///
    pub fn start_db_command(&self, database_dir: &PathBuf, port: i16) -> Box<Cell<Command>> {
        let pg_ctl_executable = self.pg_ctl_exe.to_str().unwrap();
        let port_arg = format!("-F -p {}", port.to_string());
        let mut command =
            Box::new(
                Cell::new(
                    tokio::process::Command::new(pg_ctl_executable)
                )
            );
        command.get_mut()
            .args(&[
                "-o", &port_arg, "start", "-w", "-D", database_dir.to_str().unwrap()
            ]);
        command
    }

    ///
    /// Create pg_ctl stop command
    ///
    pub fn stop_db_command(&self, database_dir: &PathBuf) -> Box<Cell<Command>> {
        let pg_ctl_executable = self.pg_ctl_exe.to_str().unwrap();
        let mut command =
            Box::new(
                Cell::new(
                    tokio::process::Command::new(pg_ctl_executable)
                )
            );
        command.get_mut()
            .args(&[
                "stop", "-w", "-D", database_dir.to_str().unwrap(),
            ]);
        command
    }
}