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
// Conserve backup system.
// Copyright 2022 Stephanie Aelmore.
// Copyright 2015-2023 Martin Pool.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//! Stores the user and group as Strings in the archive.
//! There is potentially a more efficient way to do this, but this approach works
//! better than just saving the uid and gid, so that backups may potentially
//! be restored on a different system.
use std::fmt::Display;
use std::io;
use std::path::Path;
use serde::{Deserialize, Serialize};
#[cfg(unix)]
mod unix;
#[cfg(unix)]
use unix::set_owner;
#[cfg(windows)]
mod windows;
#[cfg(windows)]
use windows::set_owner;
#[derive(Default, Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Owner {
// TODO: Maybe the strings can be 'static references to the cache?
pub user: Option<String>,
pub group: Option<String>,
}
impl Owner {
pub fn is_none(&self) -> bool {
self.user.is_none() && self.group.is_none()
}
pub fn set_owner<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
set_owner(self, path.as_ref())
}
pub fn clear(&mut self) {
self.user = None;
self.group = None;
}
}
impl Display for Owner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let none = "none".to_string();
write!(
f,
"{:<10} {:<10}",
if let Some(user) = &self.user {
user
} else {
&none
},
if let Some(group) = &self.group {
group
} else {
&none
}
)
}
}