tmux-backup 0.5.16

A backup & restore solution for Tmux sessions.
Documentation
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Support functions to create and read backup archive files.

use std::collections::HashSet;
use std::fmt;
use std::io::Read;
use std::path::{Path, PathBuf};

use chrono::Local;
use serde::{Deserialize, Serialize};

use crate::{Result, error::Error, 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.
pub const PANES_DIR_NAME: &str = "panes-content";

/// Name of the file storing the metadata in the backup.
///
/// This name is also used in the temporary directory when storing the catalog.
pub const METADATA_FILENAME: &str = "metadata.json";

/// Describes the Tmux sessions, windows & panes stored in a backup.
///
/// This is enough information to recreate all sessions, windows & panes.
#[derive(Debug, Serialize, Deserialize)]
pub struct Metadata {
    /// Version of the archive's format.
    pub version: String,

    /// Tmux client metadata.
    pub client: tmux::client::Client,

    /// Tmux sessions metadata.
    pub sessions: Vec<tmux::session::Session>,

    /// Tmux windows metadata.
    pub windows: Vec<tmux::window::Window>,

    /// Tmux panes metadata.
    pub panes: Vec<tmux::pane::Pane>,
}

impl Metadata {
    /// Query Tmux and return a new `Metadata`.
    pub async fn new() -> Result<Self> {
        let version = FORMAT_VERSION.to_string();
        let client = tmux::client::current().await?;
        let sessions = tmux::session::available_sessions().await?;
        let windows = tmux::window::available_windows().await?;
        let panes = tmux::pane::available_panes().await?;

        let metadata = Self {
            version,
            client,
            sessions,
            windows,
            panes,
        };

        Ok(metadata)
    }

    /// Open the archive file at `backup_filepath` and read the version string and tmux metadata.
    pub async fn read_file<P: AsRef<Path>>(backup_filepath: P) -> Result<Self> {
        let archive = std::fs::File::open(backup_filepath.as_ref())?;
        let dec = zstd::stream::read::Decoder::new(archive)?;
        let mut tar = tar::Archive::new(dec);

        // Read the version file.
        let mut version = String::new();
        version.reserve(4);

        let mut bytes = Vec::with_capacity(8 * 1024);

        for mut entry in tar.entries()?.flatten() {
            if entry.path().unwrap().to_string_lossy() == VERSION_FILENAME {
                entry.read_to_string(&mut version)?;
                if version.is_empty() {
                    return Err(Error::ArchiveVersion(
                        "could not read the format version".to_string(),
                    ));
                }
                if version != FORMAT_VERSION {
                    return Err(Error::ArchiveVersion(format!(
                        "Unsupported format version: `{version}`",
                    )));
                }
            } else if entry.path().unwrap().to_string_lossy() == METADATA_FILENAME {
                entry.read_to_end(&mut bytes)?;
            }
        }

        if bytes.is_empty() {
            return Err(Error::MissingMetadata(format!(
                "missing metadata in `{}`",
                backup_filepath.as_ref().to_string_lossy()
            )));
        }

        let metadata = serde_json::from_slice(&bytes)?;

        Ok(metadata)
    }

    /// Return an overview of the metadata.
    pub fn overview(&self) -> Overview {
        Overview {
            version: self.version.clone(),
            num_sessions: self.sessions.len() as u16,
            num_windows: self.windows.len() as u16,
            num_panes: self.panes.len() as u16,
        }
    }

    /// Return the list of windows in the provided session.
    pub fn windows_related_to(
        &self,
        session: &tmux::session::Session,
    ) -> Vec<tmux::window::Window> {
        self.windows
            .iter()
            .filter(|&w| w.sessions.contains(&session.name))
            .cloned()
            .collect()
    }

    /// Return the list of panes in the provided window.
    pub fn panes_related_to(&self, window: &tmux::window::Window) -> Vec<&tmux::pane::Pane> {
        let pane_ids: HashSet<tmux::pane_id::PaneId> = window.pane_ids().iter().cloned().collect();
        self.panes
            .iter()
            .filter(|&p| pane_ids.contains(&p.id))
            .collect()
    }
}

/// Overview of the archive's content: number of sessions, windows and panes in the archive.
///
/// These counts are displayed after the commands such as `save`, `restore`, or `catalog list
/// --details`.
#[derive(Debug)]
pub struct Overview {
    /// Format version of the archive.
    pub version: String,

    /// Number of sessions in the archive.
    pub num_sessions: u16,

    /// Number of windows in the archive.
    pub num_windows: u16,

    /// Number of panes in the archive.
    pub num_panes: u16,
}

impl fmt::Display for Overview {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_fmt(format_args!(
            "{} sessions {} windows {} panes",
            self.num_sessions, self.num_windows, self.num_panes,
        ))
    }
}

/// Print a full description of the archive, with session and window names.
pub async fn print_description<P>(_backup_filepath: P) -> Result<()>
where
    P: AsRef<Path>,
{
    unimplemented!()
    // let metadata = read_metadata(backup_filepath).await?;
    // let overview = metadata.overview();

    // println!("full details {overview}");

    // Ok(())
}

/// Return the pattern for searching the backup files.
///
/// This is called by the catalog command to list the available backups.
///
/// # Note
///
/// This pattern must match the filename generated by `new_backup_filepath()`.
pub fn backup_filepath_pattern() -> &'static str {
    r".*backup-(\d{8}T\d{6})\.\d{6}\.tar\.zst"
}

/// Return the filepath for a new backup.
///
/// This is used when the function `actions::save` needs a new filepath. The filepath is based on
/// the current timestamp and is read by the catalog using the function `backup_filepath_pattern()`.
pub fn new_backup_filepath<P>(dirpath: P) -> PathBuf
where
    P: AsRef<Path>,
{
    let timestamp_frag = Local::now().format("%Y%m%dT%H%M%S%.6f").to_string();
    let backup_filename = format!("backup-{timestamp_frag}.tar.zst");
    dirpath.as_ref().join(backup_filename)
}

/// Create a new backup file in `dest_filepath` with the contents of the metadata file and panes
/// content.
pub fn create_from_paths<P: AsRef<Path>>(
    dest_filepath: P,
    version_filepath: P,
    metadata_filepath: P,
    panes_content_dir: P,
) -> Result<()> {
    let archive = std::fs::File::create(dest_filepath.as_ref())?;
    let enc = zstd::stream::write::Encoder::new(archive, 0)?.auto_finish();
    let mut tar = tar::Builder::new(enc);

    tar.append_path_with_name(version_filepath, VERSION_FILENAME)?;
    tar.append_path_with_name(metadata_filepath.as_ref(), METADATA_FILENAME)?;
    tar.append_dir_all(PANES_DIR_NAME, panes_content_dir.as_ref())?;
    tar.finish()?;

    Ok(())
}

/// Unpack a backup at `backup_filepath` into `dest_dirpath`.
///
/// This is used to unpack the archive into `/tmp/` and access the panes-content.
pub async fn unpack<P: AsRef<Path>>(
    backup_filepath: P,
    dest_dirpath: P,
) -> std::result::Result<(), std::io::Error> {
    let archive = std::fs::File::open(backup_filepath.as_ref())?;
    let dec = zstd::stream::read::Decoder::new(archive)?;
    let mut tar = tar::Archive::new(dec);

    tar.unpack(dest_dirpath)
}

#[cfg(test)]
mod tests {
    use super::*;
    use regex::Regex;

    mod backup_filepath_pattern {
        use super::*;

        fn matches(path: &str) -> bool {
            let pattern = backup_filepath_pattern();
            Regex::new(pattern).unwrap().is_match(path)
        }

        fn extract_timestamp(path: &str) -> Option<String> {
            let pattern = backup_filepath_pattern();
            let re = Regex::new(pattern).unwrap();
            re.captures(path).map(|c| c[1].to_string())
        }

        #[test]
        fn matches_standard_backup_filename() {
            assert!(matches("backup-20220910T172024.141993.tar.zst"));
        }

        #[test]
        fn matches_with_absolute_path() {
            assert!(matches(
                "/home/user/.local/state/tmux-backup/backup-20220910T172024.141993.tar.zst"
            ));
        }

        #[test]
        fn matches_with_relative_path() {
            assert!(matches("./backups/backup-20220910T172024.141993.tar.zst"));
        }

        #[test]
        fn extracts_timestamp_without_microseconds() {
            let ts = extract_timestamp("backup-20220910T172024.141993.tar.zst");
            assert_eq!(ts, Some("20220910T172024".to_string()));
        }

        #[test]
        fn rejects_missing_extension() {
            assert!(!matches("backup-20220910T172024.141993.tar"));
            assert!(!matches("backup-20220910T172024.141993"));
        }

        #[test]
        fn rejects_wrong_prefix() {
            assert!(!matches("snapshot-20220910T172024.141993.tar.zst"));
            assert!(!matches("20220910T172024.141993.tar.zst"));
        }

        #[test]
        fn rejects_malformed_timestamp() {
            // Missing T separator
            assert!(!matches("backup-20220910172024.141993.tar.zst"));
            // Wrong date format
            assert!(!matches("backup-2022-09-10T17:20:24.141993.tar.zst"));
            // Too short
            assert!(!matches("backup-20220910T1720.141993.tar.zst"));
        }

        #[test]
        fn rejects_missing_microseconds() {
            assert!(!matches("backup-20220910T172024.tar.zst"));
        }

        #[test]
        fn accepts_various_valid_timestamps() {
            // Midnight
            assert!(matches("backup-20240101T000000.000000.tar.zst"));
            // End of day
            assert!(matches("backup-20241231T235959.999999.tar.zst"));
            // Leap year date
            assert!(matches("backup-20240229T120000.123456.tar.zst"));
        }
    }

    mod new_backup_filepath {
        use super::*;

        #[test]
        fn generates_path_in_given_directory() {
            let path = new_backup_filepath("/my/backup/dir");
            assert!(path.starts_with("/my/backup/dir"));
        }

        #[test]
        fn generated_filename_has_correct_extension() {
            let path = new_backup_filepath("/tmp");
            let filename = path.file_name().unwrap().to_string_lossy();
            assert!(filename.ends_with(".tar.zst"));
        }

        #[test]
        fn generated_filename_starts_with_backup() {
            let path = new_backup_filepath("/tmp");
            let filename = path.file_name().unwrap().to_string_lossy();
            assert!(filename.starts_with("backup-"));
        }

        #[test]
        fn generated_path_matches_pattern() {
            let path = new_backup_filepath("/tmp");
            let pattern = backup_filepath_pattern();
            let re = Regex::new(pattern).unwrap();
            assert!(re.is_match(&path.to_string_lossy()));
        }

        #[test]
        fn accepts_path_with_trailing_slash() {
            let path = new_backup_filepath("/tmp/");
            assert!(path.starts_with("/tmp"));
        }

        #[test]
        fn works_with_pathbuf() {
            let dir = PathBuf::from("/var/backups");
            let path = new_backup_filepath(dir);
            assert!(path.starts_with("/var/backups"));
        }
    }

    mod overview_display {
        use super::*;

        #[test]
        fn formats_counts_correctly() {
            let overview = Overview {
                version: "1.0".to_string(),
                num_sessions: 3,
                num_windows: 12,
                num_panes: 47,
            };

            let output = format!("{overview}");
            assert_eq!(output, "3 sessions 12 windows 47 panes");
        }

        #[test]
        fn handles_singular_counts() {
            let overview = Overview {
                version: "1.0".to_string(),
                num_sessions: 1,
                num_windows: 1,
                num_panes: 1,
            };

            // Note: The current implementation doesn't pluralize
            let output = format!("{overview}");
            assert_eq!(output, "1 sessions 1 windows 1 panes");
        }

        #[test]
        fn handles_zero_counts() {
            let overview = Overview {
                version: "1.0".to_string(),
                num_sessions: 0,
                num_windows: 0,
                num_panes: 0,
            };

            let output = format!("{overview}");
            assert_eq!(output, "0 sessions 0 windows 0 panes");
        }
    }

    mod constants {
        use super::*;

        #[test]
        fn format_version_is_semver_like() {
            // Ensure version looks like "X.Y" or similar
            assert!(FORMAT_VERSION.contains('.'));
        }

        #[test]
        fn panes_dir_name_is_reasonable() {
            assert_eq!(PANES_DIR_NAME, "panes-content");
            assert!(!PANES_DIR_NAME.contains('/'));
            assert!(!PANES_DIR_NAME.contains('\\'));
        }

        #[test]
        fn metadata_filename_is_json() {
            assert!(METADATA_FILENAME.ends_with(".json"));
        }
    }
}