Skip to content

Commit

Permalink
feat(archive): introduce format version
Browse files Browse the repository at this point in the history
  • Loading branch information
graelo committed Aug 18, 2022
1 parent 69c3286 commit 8894127
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
31 changes: 19 additions & 12 deletions src/actions/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use async_std::{fs, task};
use futures::future::join_all;

use crate::{
management::{
archive::{self, Metadata, METADATA_FILENAME, PANES_DIR_NAME},
catalog::BackupOverview,
},
management::{archive::v1, catalog::BackupOverview},
tmux,
};

Expand All @@ -28,33 +25,41 @@ pub async fn save<P: AsRef<Path>>(backup_dirpath: P) -> Result<(PathBuf, BackupO
fs::create_dir_all(&temp_dirpath).await?;

// Save sessions & windows into `metadata.yaml` in the temp folder.
let metadata_task: task::JoinHandle<Result<(PathBuf, u16, u16)>> = {
let metadata_task: task::JoinHandle<Result<(PathBuf, PathBuf, u16, u16)>> = {
let temp_dirpath = temp_dirpath.clone();

task::spawn(async move {
let temp_version_filepath = temp_dirpath.join(v1::VERSION_FILENAME);
fs::write(&temp_version_filepath, v1::FORMAT_VERSION).await?;

let sessions = tmux::session::available_sessions().await?;
let windows = tmux::window::available_windows().await?;
let panes = tmux::pane::available_panes().await?;
let num_sessions = sessions.len() as u16;
let num_windows = windows.len() as u16;

let metadata = Metadata {
let metadata = v1::Metadata {
sessions,
windows,
panes,
};
let yaml = serde_yaml::to_string(&metadata)?;

let temp_metadata_filepath = temp_dirpath.join(METADATA_FILENAME);
let temp_metadata_filepath = temp_dirpath.join(v1::METADATA_FILENAME);
fs::write(temp_metadata_filepath.as_path(), yaml).await?;

Ok((temp_metadata_filepath, num_sessions, num_windows))
Ok((
temp_version_filepath,
temp_metadata_filepath,
num_sessions,
num_windows,
))
})
};

// Save pane contents in the temp folder.
let (temp_panes_content_dir, num_panes) = {
let temp_panes_content_dir = temp_dirpath.join(PANES_DIR_NAME);
let temp_panes_content_dir = temp_dirpath.join(v1::PANES_DIR_NAME);
fs::create_dir_all(&temp_panes_content_dir).await?;

let panes = tmux::pane::available_panes().await?;
Expand All @@ -63,13 +68,15 @@ pub async fn save<P: AsRef<Path>>(backup_dirpath: P) -> Result<(PathBuf, BackupO

(temp_panes_content_dir, num_panes)
};
let (temp_metadata_filepath, num_sessions, num_windows) = metadata_task.await?;
let (temp_version_filepath, temp_metadata_filepath, num_sessions, num_windows) =
metadata_task.await?;

// Tar-compress content of temp folder into a new backup file in `backup_dirpath`.
let new_backup_filepath = archive::new_backup_filepath(backup_dirpath.as_ref());
let new_backup_filepath = v1::new_backup_filepath(backup_dirpath.as_ref());

archive::create(
v1::create(
&new_backup_filepath,
&temp_version_filepath,
&temp_metadata_filepath,
&temp_panes_content_dir,
)?;
Expand Down
1 change: 1 addition & 0 deletions src/management/archive/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod v1;
10 changes: 8 additions & 2 deletions src/management/archive.rs → src/management/archive/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ use serde::{Deserialize, Serialize};
use crate::management::catalog::BackupOverview;
use crate::tmux;

/// Version of the archive format.
pub const FORMAT_VERSION: &str = "1.0";

/// Name of the file storing the version of the archive format.
pub const VERSION_FILENAME: &str = "version";

/// Name of the directory storing the panes content in the backup.
///
/// This name is also used in the temporary directory when retrieving the panes content from Tmux.
Expand Down Expand Up @@ -91,16 +97,16 @@ pub fn read_metadata<P: AsRef<Path>>(backup_filepath: P) -> Result<Metadata> {
/// content.
pub fn create<P: AsRef<Path>>(
backup_filepath: P,
version_filepath: P,
metadata_filepath: P,
panes_content_dir: P,
) -> Result<()> {
let archive = std::fs::File::create(backup_filepath.as_ref())?;
let enc = zstd::stream::write::Encoder::new(archive, 0)?.auto_finish();
let mut tar = tar::Builder::new(enc);

// println!("appending {:?}", metadata_filepath);
tar.append_path_with_name(version_filepath, "VERSION_FILENAME")?;
tar.append_path_with_name(metadata_filepath.as_ref(), METADATA_FILENAME)?;
// println!("appending {:?}", panes_content_dir);
tar.append_dir_all(PANES_DIR_NAME, panes_content_dir.as_ref())?;
tar.finish()?;

Expand Down
4 changes: 2 additions & 2 deletions src/management/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use chrono::{Duration, Local, NaiveDateTime};
use regex::Regex;

use crate::config::SubList;
use crate::management::archive::Metadata;
use crate::management::archive::v1;

use super::compaction::{Plan, Strategy};

Expand Down Expand Up @@ -219,7 +219,7 @@ impl Catalog {
where
P: AsRef<Path>,
{
match Metadata::read(backup_filepath) {
match v1::Metadata::read(backup_filepath) {
Ok(metadata) => {
println!("{}", metadata.get_overview());
}
Expand Down

0 comments on commit 8894127

Please sign in to comment.