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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
//! The common options of borg commands are defined here

use std::fmt::{Display, Formatter, Write};
use std::io::BufRead;
use std::num::NonZeroU16;
use std::process::Output;

use log::{debug, error, info, trace, warn};
use serde::{Deserialize, Serialize};

use crate::errors::{CompactError, CreateError, InitError, ListError, MountError, PruneError};
use crate::output::create::Create;
use crate::output::list::ListRepository;
use crate::output::logging::{LevelName, LoggingMessage, MessageId};

/// A pattern instruction.
/// These instructions will be used for the `--pattern` command line parameter.
///
/// [PatternInstruction::Include] are useful to include paths that are contained in an excluded path.
/// The first matching pattern is used so if an [PatternInstruction::Include] matches before
/// an [PatternInstruction::Exclude], the file is backed up.
/// If an [PatternInstruction::ExcludeNoRecurse] pattern matches a directory,
/// it won't recurse into it and won't discover any potential matches for include rules
/// below that directory.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum PatternInstruction {
    /// A plain root path to use as a starting point
    Root(String),
    /// Include matched files
    Include(Pattern),
    /// Exclude matched files
    Exclude(Pattern),
    /// If an [PatternInstruction::ExcludeNoRecurse] pattern matches a directory, it won't recurse into
    /// it and won't discover any potential matches for include rules below that directory.
    ExcludeNoRecurse(Pattern),
}

impl Display for PatternInstruction {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            PatternInstruction::Root(path) => write!(f, "P {path}"),
            PatternInstruction::Include(pattern) => write!(f, "+ {pattern}"),
            PatternInstruction::Exclude(pattern) => write!(f, "- {pattern}"),
            PatternInstruction::ExcludeNoRecurse(pattern) => write!(f, "! {pattern}"),
        }
    }
}

/// The path/filenames used as input for the pattern matching start from the
/// currently active recursion root. You usually give the recursion root(s)
/// when invoking borg and these can be either relative or absolute paths.
///
/// [Pattern::Regex], [Pattern::Shell] and [Pattern::FnMatch] patterns are all implemented on top
/// of the Python SRE engine. It is very easy to formulate patterns for each of these types which
/// requires an inordinate amount of time to match paths.
///
/// If untrusted users are able to supply patterns, ensure they cannot supply [Pattern::Regex]
/// patterns. Further, ensure that [Pattern::Shell] and [Pattern::FnMatch] patterns only contain a
/// handful of wildcards at most.
///
/// Note that this enum is only for use in [PatternInstruction].
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Pattern {
    /// Fnmatch <https://docs.python.org/3/library/fnmatch.html>:
    ///
    /// These patterns use a variant of shell pattern syntax, with '\*' matching
    /// any number of characters, '?' matching any single character, '\[...]'
    /// matching any single character specified, including ranges, and '\[!...]'
    /// matching any character not specified.
    ///
    /// For the purpose of these patterns, the path separator
    /// (backslash for Windows and '/' on other systems) is not treated specially.
    /// Wrap meta-characters in brackets for a literal match
    /// (i.e. \[?] to match the literal character ?).
    /// For a path to match a pattern, the full path must match, or it must match
    /// from the start of the full path to just before a path separator. Except
    /// for the root path, paths will never end in the path separator when
    /// matching is attempted.  Thus, if a given pattern ends in a path
    /// separator, a '*' is appended before matching is attempted.
    ///
    /// A leading path separator is always removed.
    FnMatch(String),
    /// Like [Pattern::FnMatch] patterns these are similar to shell patterns.
    /// The difference is that the pattern may include **/ for matching zero or more
    /// directory levels, * for matching zero or more arbitrary characters with the exception of
    /// any path separator.
    ///
    /// A leading path separator is always removed.
    Shell(String),
    /// Regular expressions similar to those found in Perl are supported. Unlike shell patterns,
    /// regular expressions are not required to match the full path and any substring match
    /// is sufficient.
    ///
    /// It is strongly recommended to anchor patterns to the start ('^'), to the end ('$') or both.
    ///
    /// Path separators (backslash for Windows and '/' on other systems) in paths are
    /// always normalized to a forward slash ('/') before applying a pattern.
    ///
    /// The regular expression syntax is described in the Python documentation for the re module
    /// <https://docs.python.org/3/library/re.html>.
    Regex(String),
    /// This pattern style is useful to match whole sub-directories.
    ///
    /// The pattern `root/somedir` matches `root/somedir` and everything therein.
    /// A leading path separator is always removed.
    PathPrefix(String),
    /// This pattern style is (only) useful to match full paths.
    ///
    /// This is kind of a pseudo pattern as it can not have any variable or unspecified parts -
    /// the full path must be given. `root/file.ext` matches `root/file.ext` only.
    ///
    /// A leading path separator is always removed.
    ///
    /// Implementation note: this is implemented via very time-efficient O(1)
    /// hashtable lookups (this means you can have huge amounts of such patterns
    /// without impacting performance much).
    /// Due to that, this kind of pattern does not respect any context or order.
    /// If you use such a pattern to include a file, it will always be included
    /// (if the directory recursion encounters it).
    /// Other include/exclude patterns that would normally match will be ignored.
    /// Same logic applies for exclude.
    PathFullMatch(String),
}

impl Display for Pattern {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Pattern::FnMatch(x) => write!(f, "fm:{x}"),
            Pattern::Shell(x) => write!(f, "sh:{x}"),
            Pattern::Regex(x) => write!(f, "re:{x}"),
            Pattern::PathPrefix(x) => write!(f, "pp:{x}"),
            Pattern::PathFullMatch(x) => write!(f, "pf:{x}"),
        }
    }
}

/// The compression modes of an archive.
///
/// The compression modes `auto` and `obfuscate` are currently not supported by this library.
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum CompressionMode {
    /// No compression
    None,
    /// Use lz4 compression. Very high speed, very low compression.
    Lz4,
    /// Use zstd ("zstandard") compression, a modern wide-range algorithm.
    ///
    /// Allowed values ranging from 1 to 22, where 1 is the fastest and 22 the highest compressing.
    ///
    /// Archives compressed with zstd are not compatible with borg < 1.1.4.
    Zstd(u8),
    /// Use zlib ("gz") compression. Medium speed, medium compression.
    ///
    /// Allowed values ranging from 0 to 9.
    /// Giving level 0 (means "no compression", but still has zlib protocol overhead)
    /// is usually pointless, you better use [CompressionMode::None] compression.
    Zlib(u8),
    /// Use lzma ("xz") compression. Low speed, high compression.
    ///
    /// Allowed values ranging from 0 to 9.
    /// Giving levels above 6 is pointless and counterproductive because it does
    /// not compress better due to the buffer size used by borg - but it wastes
    /// lots of CPU cycles and RAM.
    Lzma(u8),
}

impl Display for CompressionMode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            CompressionMode::None => write!(f, "none"),
            CompressionMode::Lz4 => write!(f, "lz4"),
            CompressionMode::Zstd(x) => write!(f, "zstd,{x}"),
            CompressionMode::Zlib(x) => write!(f, "zlib,{x}"),
            CompressionMode::Lzma(x) => write!(f, "lzma,{x}"),
        }
    }
}

/// The encryption mode of the repository.
///
/// See <https://borgbackup.readthedocs.io/en/stable/usage/init.html#more-encryption-modes>
/// for further information about encryption modes.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum EncryptionMode {
    /// No encryption, nor hashing.
    ///
    /// This mode is not recommended.
    None,
    /// Uses no encryption, but authenticates repository contents through HMAC-SHA256 hashes
    Authenticated(String),
    /// Uses no encryption, but authenticates repository contents through keyed BLAKE2b-256 hashes
    AuthenticatedBlake2(String),
    /// Use AES-CTR-256 for encryption and HMAC-SHA256 for authentication in an
    /// encrypt-then-MAC (EtM) construction.
    ///
    /// The chunk ID hash is HMAC-SHA256 as well (with a separate key).
    ///
    /// Stores the key in the repository.
    Repokey(String),
    /// Use AES-CTR-256 for encryption and HMAC-SHA256 for authentication in an
    /// encrypt-then-MAC (EtM) construction.
    ///
    /// The chunk ID hash is HMAC-SHA256 as well (with a separate key).
    ///
    /// Stores the key locally.
    Keyfile(String),
    /// Use AES-CTR-256 for encryption and BLAKE2b-256 for authentication in an
    /// encrypt-then-MAC (EtM) construction.
    ///
    /// The chunk ID hash is a keyed BLAKE2b-256 hash.
    ///
    /// Stores the key in the repository.
    RepokeyBlake2(String),
    /// Use AES-CTR-256 for encryption and BLAKE2b-256 for authentication in an
    /// encrypt-then-MAC (EtM) construction.
    ///
    /// The chunk ID hash is a keyed BLAKE2b-256 hash.
    ///
    /// Stores the key locally.
    KeyfileBlake2(String),
}

impl EncryptionMode {
    pub(crate) fn get_passphrase(&self) -> Option<String> {
        match self {
            EncryptionMode::None => None,
            EncryptionMode::Authenticated(x) => Some(x.clone()),
            EncryptionMode::AuthenticatedBlake2(x) => Some(x.clone()),
            EncryptionMode::Repokey(x) => Some(x.clone()),
            EncryptionMode::Keyfile(x) => Some(x.clone()),
            EncryptionMode::RepokeyBlake2(x) => Some(x.clone()),
            EncryptionMode::KeyfileBlake2(x) => Some(x.clone()),
        }
    }
}

impl Display for EncryptionMode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            EncryptionMode::None => write!(f, "none"),
            EncryptionMode::Authenticated(_) => write!(f, "authenticated"),
            EncryptionMode::AuthenticatedBlake2(_) => write!(f, "authenticated-blake2"),
            EncryptionMode::Repokey(_) => write!(f, "repokey"),
            EncryptionMode::Keyfile(_) => write!(f, "keyfile"),
            EncryptionMode::RepokeyBlake2(_) => write!(f, "repokey-blake2"),
            EncryptionMode::KeyfileBlake2(_) => write!(f, "keyfile-blake2"),
        }
    }
}

/// The common options that can be used for every borg command
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct CommonOptions {
    /// The local path to the borg executable. (default = "borg")
    pub local_path: Option<String>,
    /// The remote path to the borg executable. (default = "borg")
    pub remote_path: Option<String>,
    /// set network upload rate limit in kiByte/s (0 = unlimited)
    pub upload_ratelimit: Option<u64>,
    /// Use this command to connect to the ‘borg serve’ process (default: "ssh")
    ///
    /// This can be useful to specify an alternative ssh key: "ssh -i /path/to/privkey"
    pub rsh: Option<String>,
}

impl Display for CommonOptions {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if let Some(rsh) = &self.rsh {
            write!(f, "--rsh {} ", shlex::quote(rsh))?;
        }

        if let Some(remote_path) = &self.remote_path {
            write!(f, "--remote-path {} ", shlex::quote(remote_path))?;
        }

        if let Some(upload_ratelimit) = &self.upload_ratelimit {
            write!(f, "--upload-ratelimit {upload_ratelimit} ")?;
        }

        Ok(())
    }
}

/// The quantifier for [PruneWithin]
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
pub enum PruneWithinTime {
    /// Hour quantifier
    Hour,
    /// Day quantifier
    Day,
    /// Week quantifier (7 days)
    Week,
    /// Month quantifier (31 days)
    Month,
    /// Year quantifier
    Year,
}

impl Display for PruneWithinTime {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            PruneWithinTime::Hour => write!(f, "H"),
            PruneWithinTime::Day => write!(f, "d"),
            PruneWithinTime::Week => write!(f, "w"),
            PruneWithinTime::Month => write!(f, "m"),
            PruneWithinTime::Year => write!(f, "y"),
        }
    }
}

/// The definition to specify an interval in which backups
/// should not be pruned.
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
pub struct PruneWithin {
    /// quantifier
    pub quantifier: NonZeroU16,
    /// The time quantifier
    pub time: PruneWithinTime,
}

impl Display for PruneWithin {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}{}", self.quantifier, self.time)
    }
}

/// Options for [crate::sync::prune]
///
/// A good procedure is to thin out more and more the older your backups get.
/// As an example, `keep_daily` 7 means to keep the latest backup on each day, up to 7 most
/// recent days with backups (days without backups do not count). The rules are applied from
/// secondly to yearly, and backups selected by previous rules do not count towards those of
/// later rules. The time that each backup starts is used for pruning purposes.
/// Dates and times are interpreted in the local timezone, and weeks go from Monday to Sunday.
/// Specifying a negative number of archives to keep means that there is no limit.
///
/// As of borg 1.2.0, borg will retain the oldest archive if any of the secondly, minutely,
/// hourly, daily, weekly, monthly, or yearly rules was not otherwise able to meet
/// its retention target. This enables the first chronological archive to continue aging until
/// it is replaced by a newer archive that meets the retention criteria.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PruneOptions {
    /// Path to the repository
    ///
    /// Example values:
    /// - `/tmp/foo`
    /// - `user@example.com:/opt/repo`
    /// - `ssh://user@example.com:2323:/opt/repo`
    pub repository: String,
    /// The passphrase for the repository
    ///
    /// If using a repository with [EncryptionMode::None],
    /// you can leave this option empty
    pub passphrase: Option<String>,
    /// The archives kept with this option do not count towards the totals specified
    /// by any other options.
    pub keep_within: Option<PruneWithin>,
    /// number of secondly archives to keep
    pub keep_secondly: Option<NonZeroU16>,
    /// number of minutely archives to keep
    pub keep_minutely: Option<NonZeroU16>,
    /// number of hourly archives to keep
    pub keep_hourly: Option<NonZeroU16>,
    /// number of daily archives to keep
    pub keep_daily: Option<NonZeroU16>,
    /// number of weekly archives to keep
    pub keep_weekly: Option<NonZeroU16>,
    /// number of monthly archives to keep
    pub keep_monthly: Option<NonZeroU16>,
    /// number of yearly archives to keep
    pub keep_yearly: Option<NonZeroU16>,
    /// write checkpoint every SECONDS seconds (Default: 1800)
    pub checkpoint_interval: Option<NonZeroU16>,
    /// only consider archive names matching the glob.
    ///
    /// The pattern can use [Pattern::Shell]
    pub glob_archives: Option<String>,
}

impl PruneOptions {
    /// Create an new [PruneOptions]
    pub fn new(repository: String) -> Self {
        Self {
            repository,
            passphrase: None,
            keep_within: None,
            keep_secondly: None,
            keep_minutely: None,
            keep_hourly: None,
            keep_daily: None,
            keep_weekly: None,
            keep_monthly: None,
            keep_yearly: None,
            checkpoint_interval: None,
            glob_archives: None,
        }
    }
}

/// Options for [crate::sync::mount]
///
/// Mount an archive or repository as a FUSE filesystem. This is useful for
/// browsing archives or repositories and interactive restoration.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum MountSource {
    /// Mount a repository
    Repository {
        /// Name of the repository you wish to mount
        ///
        /// Example values:
        /// - `/tmp/foo`
        /// - `user@example.com:/opt/repo`
        /// - `ssh://user@example.com:2323:/opt/repo`
        name: String,
        /// Obtain the first N archives
        first_n_archives: Option<NonZeroU16>,
        /// Obtain the last N archives
        last_n_archives: Option<NonZeroU16>,
        /// only consider archive names matching the glob.
        glob_archives: Option<String>,
    },
    /// Mount an archive (repo_name::archive_name)
    Archive {
        /// Path to the borg archive you wish to mount
        ///
        /// Example values:
        /// - `/tmp/foo::my-archive`
        /// - `user@example.com:/opt/repo::archive`
        /// - `ssh://user@example.com:2323:/opt/repo`
        archive_name: String,
    },
}

/// Options for [crate::sync::mount]
///
/// Mount an archive or repository as a FUSE filesystem. This is useful for
/// browsing archives or repositories and interactive restoration.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MountOptions {
    /// The archive or repo you wish to mount
    pub mount_source: MountSource,
    /// The path where the repo or archive will be mounted.
    ///
    /// Example values:
    /// - `/mnt/my-borg-backup`
    pub mountpoint: String,
    /// The passphrase for the repository
    ///
    /// If using a repository with [EncryptionMode::None],
    /// you can leave this option empty
    pub passphrase: Option<String>,
    /// Paths to extract. If left empty all paths will be present. Useful
    /// for whitelisting certain paths in the backup.
    ///
    /// Example values:
    /// - `/a/path/I/actually/care/about`
    /// - `**/some/intermediate/folder/*`
    pub select_paths: Vec<Pattern>,
}

impl MountOptions {
    /// Create an new [MountOptions]
    pub fn new(mount_source: MountSource, mountpoint: String) -> Self {
        Self {
            mount_source,
            mountpoint,
            passphrase: None,
            select_paths: vec![],
        }
    }
}

/// Options for [crate::sync::compact]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct CompactOptions {
    /// Path to the repository
    ///
    /// Example values:
    /// - `/tmp/foo`
    /// - `user@example.com:/opt/repo`
    /// - `ssh://user@example.com:2323:/opt/repo`
    pub repository: String,
}

/// The options for a borg create command
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CreateOptions {
    /// Path to the repository
    ///
    /// Example values:
    /// - `/tmp/foo`
    /// - `user@example.com:/opt/repo`
    /// - `ssh://user@example.com:2323:/opt/repo`
    pub repository: String,
    /// Name of archive to create (must be also a valid directory name)
    ///
    /// The archive name needs to be unique.
    /// It must not end in ‘.checkpoint’ or ‘.checkpoint.N’ (with N being a number),
    /// because these names are used for checkpoints and treated in special ways.
    ///
    /// In the archive name, you may use the following placeholders:
    /// {now}, {utcnow}, {fqdn}, {hostname}, {user} and some others.
    ///
    /// See <https://borgbackup.readthedocs.io/en/stable/usage/help.html#borg-patterns> for further
    /// information.
    pub archive: String,
    /// The passphrase for the repository
    ///
    /// If using a repository with [EncryptionMode::None],
    /// you can leave this option empty
    pub passphrase: Option<String>,
    /// Add a comment text to the archive
    pub comment: Option<String>,
    /// Specify the compression mode that should be used.
    ///
    /// Defaults to [CompressionMode::Lz4].
    pub compression: Option<CompressionMode>,
    /// The paths to archive.
    ///
    /// All given paths will be recursively traversed.
    pub paths: Vec<String>,
    /// Exclude directories that contain a CACHEDIR.TAG file
    /// (<http://www.bford.info/cachedir/spec.html>)
    pub exclude_caches: bool,
    /// The patterns to apply
    ///
    /// Using these, you may specify the backup roots (starting points)
    /// and patterns for inclusion/exclusion.
    ///
    /// See [PatternInstruction] for further information.
    ///
    /// Note that the order of the instructions is important:
    ///
    /// ```bash
    /// # backup pics, but not the ones from 2018, except the good ones:
    /// borg create --pattern=+pics/2018/good --pattern=-pics/2018 repo::arch pics
    /// ```
    pub patterns: Vec<PatternInstruction>,
    /// read include/exclude patterns from the given path, one per line
    ///
    /// Refer to <https://borgbackup.readthedocs.io/en/stable/usage/help.html#borg-help-patterns>
    /// for further information how to use pattern / exclude files.
    pub pattern_file: Option<String>,
    /// Exclude paths matching PATTERN.
    ///
    /// See [Pattern] for further information.
    pub excludes: Vec<Pattern>,
    /// read exclude patterns from the given path, one per line
    ///
    /// Refer to <https://borgbackup.readthedocs.io/en/stable/usage/help.html#borg-help-patterns>
    /// for further information how to use pattern / exclude files.
    pub exclude_file: Option<String>,
    /// Only store numeric user and group identifiers
    pub numeric_ids: bool,
    /// Detect sparse holes in input (supported only by fixed chunker)
    pub sparse: bool,
    /// Open and read block and char device files as well as FIFOs as if they were regular files.
    ///
    /// Also follows symlinks pointing to these kinds of files.
    pub read_special: bool,
    /// Do not read and store xattrs into archive
    pub no_xattrs: bool,
    /// Do not read and store ACLs into archive
    pub no_acls: bool,
    /// Do not read and store flags (e.g. NODUMP, IMMUTABLE) into archive
    pub no_flags: bool,
}

impl CreateOptions {
    /// Create an new [CreateOptions]
    pub fn new(
        repository: String,
        archive: String,
        paths: Vec<String>,
        patterns: Vec<PatternInstruction>,
    ) -> Self {
        Self {
            repository,
            archive,
            passphrase: None,
            comment: None,
            compression: None,
            paths,
            exclude_caches: false,
            patterns,
            pattern_file: None,
            excludes: vec![],
            exclude_file: None,
            numeric_ids: false,
            sparse: false,
            read_special: false,
            no_xattrs: false,
            no_acls: false,
            no_flags: false,
        }
    }
}

/// The options to provide to the [crate::sync::init] command
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct InitOptions {
    /// Path to the repository
    ///
    /// Example values:
    /// - `/tmp/foo`
    /// - `user@example.com:/opt/repo`
    /// - `ssh://user@example.com:2323:/opt/repo`
    pub repository: String,
    /// The mode to use for encryption
    pub encryption_mode: EncryptionMode,
    /// Set the repository to append_only mode.
    /// Note that this only affects the low level structure of the repository,
    /// and running delete or prune will still be allowed.
    ///
    /// See [Append-only mode (forbid compaction)](https://borgbackup.readthedocs.io/en/stable/usage/notes.html#append-only-mode)
    /// in Additional Notes for more details.
    pub append_only: bool,
    /// Create the parent directories of the repo, if they are missing.
    ///
    /// Defaults to false
    pub make_parent_dirs: bool,
    /// Set storage quota of the new repository (e.g. 5G, 1.5T)
    ///
    /// No quota is set by default
    pub storage_quota: Option<String>,
}

impl InitOptions {
    /// Create new [InitOptions].
    ///
    /// `append_only`, `make_parent_dirs` and `storage_quota` are set to their defaults.
    pub fn new(repository: String, encryption_mode: EncryptionMode) -> Self {
        Self {
            repository,
            encryption_mode,
            append_only: false,
            make_parent_dirs: false,
            storage_quota: None,
        }
    }
}

/// The options for the [crate::sync::list] command
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ListOptions {
    /// Path to the repository
    ///
    /// Example values:
    /// - `/tmp/foo`
    /// - `user@example.com:/opt/repo`
    /// - `ssh://user@example.com:2323:/opt/repo`
    pub repository: String,
    /// The passphrase for the repository
    ///
    /// If using a repository with [EncryptionMode::None],
    /// you can leave this option empty
    pub passphrase: Option<String>,
}

pub(crate) fn init_fmt_args(options: &InitOptions, common_options: &CommonOptions) -> String {
    format!(
        "--log-json {common_options}init -e {e}{append_only}{make_parent_dirs}{storage_quota} {repository}",
        e = options.encryption_mode,
        append_only = if options.append_only {
            " --append-only"
        } else {
            ""
        },
        make_parent_dirs = if options.make_parent_dirs {
            " --make-parent-dirs"
        } else {
            ""
        },
        storage_quota = options
            .storage_quota
            .as_ref()
            .map_or("".to_string(), |x| format!(
                " --storage-quota {quota}",
                quota = shlex::quote(x)
            )),
        repository = shlex::quote(&options.repository),
    )
}

fn log_message(level_name: LevelName, time: f64, name: String, message: String) {
    match level_name {
        LevelName::Debug => debug!("{time} {name}: {message}"),
        LevelName::Info => info!("{time} {name}: {message}"),
        LevelName::Warning => warn!("{time} {name}: {message}"),
        LevelName::Error => error!("{time} {name}: {message}"),
        LevelName::Critical => error!("{time} {name}: {message}"),
    }
}

pub(crate) fn init_parse_result(res: Output) -> Result<(), InitError> {
    let Some(exit_code) = res.status.code() else {
        warn!("borg process was terminated by signal");
        return Err(InitError::TerminatedBySignal);
    };

    let mut output = String::new();

    for line in res.stderr.lines() {
        let line = line.map_err(InitError::InvalidBorgOutput)?;
        writeln!(output, "{line}").unwrap();

        trace!("borg output: {line}");

        let log_msg = LoggingMessage::from_str(&line)?;

        if let LoggingMessage::LogMessage {
            name,
            message,
            level_name,
            time,
            msg_id,
        } = log_msg
        {
            log_message(level_name, time, name, message);

            if let Some(msg_id) = msg_id {
                match msg_id {
                    MessageId::RepositoryAlreadyExists => {
                        return Err(InitError::RepositoryAlreadyExists)
                    }
                    _ => {
                        if exit_code > 1 {
                            return Err(InitError::UnexpectedMessageId(msg_id));
                        }
                    }
                }
            }

            if let Some(MessageId::RepositoryAlreadyExists) = msg_id {
                return Err(InitError::RepositoryAlreadyExists);
            } else if let Some(msg_id) = msg_id {
                return Err(InitError::UnexpectedMessageId(msg_id));
            }
        }
    }

    if exit_code > 1 {
        return Err(InitError::Unknown(output));
    }

    Ok(())
}

pub(crate) fn prune_fmt_args(options: &PruneOptions, common_options: &CommonOptions) -> String {
    format!(
        "--log-json {common_options} prune{keep_within}{keep_secondly}{keep_minutely}{keep_hourly}{keep_daily}{keep_weekly}{keep_monthly}{keep_yearly} {repository}",
        keep_within = options.keep_within.as_ref().map_or("".to_string(), |x| format!(" --keep-within {x}")),
        keep_secondly = options.keep_secondly.as_ref().map_or("".to_string(), |x| format!(" --keep-secondly {x}")),
        keep_minutely = options.keep_minutely.map_or("".to_string(), |x| format!(" --keep-minutely {x}")),
        keep_hourly = options.keep_hourly.map_or("".to_string(), |x| format!(" --keep-hourly {x}")),
        keep_daily = options.keep_daily.map_or("".to_string(), |x| format!(" --keep-daily {x}")),
        keep_weekly = options.keep_weekly.map_or("".to_string(), |x| format!(" --keep-weekly {x}")),
        keep_monthly = options.keep_monthly.map_or("".to_string(), |x| format!(" --keep-monthly {x}")),
        keep_yearly = options.keep_yearly.map_or("".to_string(), |x| format!(" --keep-yearly {x}")),
        repository = shlex::quote(&options.repository)
    )
}

pub(crate) fn prune_parse_output(res: Output) -> Result<(), PruneError> {
    let Some(exit_code) = res.status.code() else {
        warn!("borg process was terminated by signal");
        return Err(PruneError::TerminatedBySignal);
    };

    let mut output = String::new();

    for line in BufRead::lines(res.stderr.as_slice()) {
        let line = line.map_err(PruneError::InvalidBorgOutput)?;
        writeln!(output, "{line}").unwrap();

        trace!("borg output: {line}");

        let log_msg = LoggingMessage::from_str(&line)?;

        if let LoggingMessage::LogMessage {
            name,
            message,
            level_name,
            time,
            msg_id,
        } = log_msg
        {
            log_message(level_name, time, name, message);

            if let Some(msg_id) = msg_id {
                if exit_code > 1 {
                    return Err(PruneError::UnexpectedMessageId(msg_id));
                }
            }
        }
    }

    if exit_code > 1 {
        return Err(PruneError::Unknown(output));
    }

    Ok(())
}

pub(crate) fn mount_fmt_args(options: &MountOptions, common_options: &CommonOptions) -> String {
    let mount_source_formatted = match &options.mount_source {
        MountSource::Repository {
            name,
            first_n_archives,
            last_n_archives,
            glob_archives,
        } => {
            format!(
                "{name}{first_n_archives}{last_n_archives}{glob_archives}",
                name = name.clone(),
                first_n_archives = first_n_archives
                    .map(|first_n| format!(" --first {}", first_n))
                    .unwrap_or_default(),
                last_n_archives = last_n_archives
                    .map(|last_n| format!(" --last {}", last_n))
                    .unwrap_or_default(),
                glob_archives = glob_archives
                    .as_ref()
                    .map(|glob| format!(" --glob-archives {}", glob))
                    .unwrap_or_default(),
            )
        }
        MountSource::Archive { archive_name } => archive_name.clone(),
    };
    format!(
        "--log-json {common_options} mount {mount_source} {mountpoint}{select_paths}",
        mount_source = mount_source_formatted,
        mountpoint = options.mountpoint,
        select_paths = options
            .select_paths
            .iter()
            .map(|x| format!(" --pattern={}", shlex::quote(&x.to_string()),))
            .collect::<Vec<String>>()
            .join(" "),
    )
}

pub(crate) fn mount_parse_output(res: Output) -> Result<(), MountError> {
    let Some(exit_code) = res.status.code() else {
        warn!("borg process was terminated by signal");
        return Err(MountError::TerminatedBySignal);
    };

    let mut output = String::new();

    for line in BufRead::lines(res.stderr.as_slice()) {
        let line = line.map_err(MountError::InvalidBorgOutput)?;
        writeln!(output, "{line}").unwrap();

        trace!("borg output: {line}");

        let log_msg = LoggingMessage::from_str(&line)?;

        if let LoggingMessage::UMountError(message) = log_msg {
            return Err(MountError::UMountError(message));
        };

        if let LoggingMessage::LogMessage {
            name,
            message,
            level_name,
            time,
            msg_id,
        } = log_msg
        {
            log_message(level_name, time, name, message);

            if let Some(msg_id) = msg_id {
                if exit_code > 1 {
                    return Err(MountError::UnexpectedMessageId(msg_id));
                }
            }
        }
    }

    if exit_code > 1 {
        return Err(MountError::Unknown(output));
    }
    Ok(())
}

pub(crate) fn list_fmt_args(options: &ListOptions, common_options: &CommonOptions) -> String {
    format!(
        "--log-json {common_options} list --json {repository}",
        repository = shlex::quote(&options.repository)
    )
}

pub(crate) fn list_parse_output(res: Output) -> Result<ListRepository, ListError> {
    let Some(exit_code) = res.status.code() else {
        warn!("borg process was terminated by signal");
        return Err(ListError::TerminatedBySignal);
    };

    let mut output = String::new();

    for line in BufRead::lines(res.stderr.as_slice()) {
        let line = line.map_err(ListError::InvalidBorgOutput)?;
        writeln!(output, "{line}").unwrap();

        trace!("borg output: {line}");

        let log_msg = LoggingMessage::from_str(&line)?;

        if let LoggingMessage::LogMessage {
            name,
            message,
            level_name,
            time,
            msg_id,
        } = log_msg
        {
            log_message(level_name, time, name, message);

            if let Some(msg_id) = msg_id {
                match msg_id {
                    MessageId::RepositoryDoesNotExist => {
                        return Err(ListError::RepositoryDoesNotExist);
                    }
                    MessageId::PassphraseWrong => {
                        return Err(ListError::PassphraseWrong);
                    }
                    _ => {
                        if exit_code > 1 {
                            return Err(ListError::UnexpectedMessageId(msg_id));
                        }
                    }
                }
            }
        }
    }

    if exit_code > 1 {
        return Err(ListError::Unknown(output));
    }

    trace!("Parsing output");
    let list_repo: ListRepository = serde_json::from_slice(&res.stdout)?;

    Ok(list_repo)
}

pub(crate) fn create_fmt_args(
    options: &CreateOptions,
    common_options: &CommonOptions,
    progress: bool,
) -> String {
    format!(
        "--log-json{p} {common_options}create --json{comment}{compression}{num_ids}{sparse}{read_special}{no_xattr}{no_acls}{no_flags}{ex_caches}{patterns}{excludes}{pattern_file}{exclude_file} {repo}::{archive} {paths}",
        p = if progress { " --progress" } else { "" },
        comment = options.comment.as_ref().map_or("".to_string(), |x| format!(
            " --comment {}",
            shlex::quote(x)
        )),
        compression = options.compression.as_ref().map_or("".to_string(), |x| format!(" --compression {x}")),
        num_ids = if options.numeric_ids { " --numeric-ids" } else { "" },
        sparse = if options.sparse { " --sparse" } else { "" },
        read_special = if options.read_special { " --read-special" } else { "" },
        no_xattr = if options.no_xattrs { " --noxattrs" } else { "" },
        no_acls = if options.no_acls { " --noacls" } else { "" },
        no_flags = if options.no_flags { " --noflags" } else { "" },
        ex_caches = if options.exclude_caches { " --exclude-caches" } else {""},
        patterns = options.patterns.iter().map(|x| format!(
            " --pattern={}",
            shlex::quote(&x.to_string()),
        )).collect::<Vec<String>>().join(" "),
        excludes = options.excludes.iter().map(|x| format!(
            " --exclude={}",
            shlex::quote(&x.to_string()),
        )).collect::<Vec<String>>().join(" "),
        pattern_file = options.pattern_file.as_ref().map_or(
            "".to_string(),
            |x| format!(" --patterns-from {}", shlex::quote(x)),
        ),
        exclude_file = options.exclude_file.as_ref().map_or(
            "".to_string(),
            |x| format!(" --exclude-from {}", shlex::quote(x)),
        ),
        repo = shlex::quote(&options.repository),
        archive = shlex::quote(&options.archive),
        paths = options.paths.join(" "),
    )
}

pub(crate) fn create_parse_output(res: Output) -> Result<Create, CreateError> {
    let Some(exit_code) = res.status.code() else {
        warn!("borg process was terminated by signal");
        return Err(CreateError::TerminatedBySignal);
    };

    let mut output = String::new();

    for line in BufRead::lines(res.stderr.as_slice()) {
        let line = line.map_err(CreateError::InvalidBorgOutput)?;
        writeln!(output, "{line}").unwrap();

        trace!("borg output: {line}");

        let log_msg = LoggingMessage::from_str(&line)?;

        if let LoggingMessage::LogMessage {
            name,
            message,
            level_name,
            time,
            msg_id,
        } = log_msg
        {
            log_message(level_name, time, name, message);

            if let Some(msg_id) = msg_id {
                match msg_id {
                    MessageId::ArchiveAlreadyExists => {
                        return Err(CreateError::ArchiveAlreadyExists)
                    }
                    MessageId::PassphraseWrong => {
                        return Err(CreateError::PassphraseWrong);
                    }
                    _ => {
                        if exit_code > 1 {
                            return Err(CreateError::UnexpectedMessageId(msg_id));
                        }
                    }
                }
            }
        }
    }

    if exit_code > 1 {
        return Err(CreateError::Unknown(output));
    }

    trace!("Parsing stats");
    let stats: Create = serde_json::from_slice(&res.stdout)?;

    Ok(stats)
}

pub(crate) fn compact_fmt_args(options: &CompactOptions, common_options: &CommonOptions) -> String {
    format!(
        "--log-json {common_options}compact {repository}",
        repository = shlex::quote(&options.repository)
    )
}

pub(crate) fn compact_parse_output(res: Output) -> Result<(), CompactError> {
    let Some(exit_code) = res.status.code() else {
        warn!("borg process was terminated by signal");
        return Err(CompactError::TerminatedBySignal);
    };

    let mut output = String::new();

    for line in BufRead::lines(res.stderr.as_slice()) {
        let line = line.map_err(CompactError::InvalidBorgOutput)?;
        writeln!(output, "{line}").unwrap();

        trace!("borg output: {line}");

        let log_msg = LoggingMessage::from_str(&line)?;

        if let LoggingMessage::LogMessage {
            name,
            message,
            level_name,
            time,
            msg_id,
        } = log_msg
        {
            log_message(level_name, time, name, message);

            if let Some(msg_id) = msg_id {
                if exit_code > 1 {
                    return Err(CompactError::UnexpectedMessageId(msg_id));
                }
            }
        }
    }

    if exit_code > 1 {
        return Err(CompactError::Unknown(output));
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::num::NonZeroU16;

    use crate::common::{
        mount_fmt_args, prune_fmt_args, CommonOptions, MountOptions, MountSource, Pattern,
        PruneOptions,
    };
    #[test]
    fn test_prune_fmt_args() {
        let mut prune_option = PruneOptions::new("prune_option_repo".to_string());
        prune_option.keep_secondly = NonZeroU16::new(1);
        prune_option.keep_minutely = NonZeroU16::new(2);
        prune_option.keep_hourly = NonZeroU16::new(3);
        prune_option.keep_daily = NonZeroU16::new(4);
        prune_option.keep_weekly = NonZeroU16::new(5);
        prune_option.keep_monthly = NonZeroU16::new(6);
        prune_option.keep_yearly = NonZeroU16::new(7);
        let args = prune_fmt_args(&prune_option, &CommonOptions::default());
        assert_eq!("--log-json  prune --keep-secondly 1 --keep-minutely 2 --keep-hourly 3 --keep-daily 4 --keep-weekly 5 --keep-monthly 6 --keep-yearly 7 prune_option_repo", args);
    }
    #[test]
    fn test_mount_fmt_args() {
        let mount_option = MountOptions::new(
            MountSource::Archive {
                archive_name: "/tmp/borg-repo::archive".to_string(),
            },
            String::from("/mnt/borg-mount"),
        );
        let args = mount_fmt_args(&mount_option, &CommonOptions::default());
        assert_eq!(
            "--log-json  mount /tmp/borg-repo::archive /mnt/borg-mount",
            args
        );
    }
    #[test]
    fn test_mount_fmt_args_patterns() {
        let mut mount_option = MountOptions::new(
            MountSource::Archive {
                archive_name: "/my-borg-repo".to_string(),
            },
            String::from("/borg-mount"),
        );
        mount_option.select_paths = vec![
            Pattern::Shell("**/test/*".to_string()),
            Pattern::Regex("^[A-Z]{3}".to_string()),
        ];
        let args = mount_fmt_args(&mount_option, &CommonOptions::default());
        assert_eq!(
            "--log-json  mount /my-borg-repo /borg-mount --pattern=\"sh:**/test/*\"  --pattern=\"re:^[A-Z]{3}\"",
            args
        );
    }
    #[test]
    fn test_mount_fmt_args_repo() {
        let mut mount_option = MountOptions::new(
            MountSource::Repository {
                name: "/my-repo".to_string(),
                first_n_archives: Some(NonZeroU16::new(10).unwrap()),
                last_n_archives: Some(NonZeroU16::new(5).unwrap()),
                glob_archives: Some("archive-name*12-2022*".to_string()),
            },
            String::from("/borg-mount"),
        );
        mount_option.select_paths = vec![Pattern::Shell("**/foobar/*".to_string())];
        let args = mount_fmt_args(&mount_option, &CommonOptions::default());
        assert_eq!(
            "--log-json  mount /my-repo --first 10 --last 5 --glob-archives archive-name*12-2022* /borg-mount --pattern=\"sh:**/foobar/*\"",
            args
        );
    }
}