From 671f13b29235abbfc7137571d596f5f784aa6b0b Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 3 Mar 2021 07:32:10 +0200 Subject: feat: store user and group who own each file Actually, these aren't yet actually stored in the backup. That will happen when there's a way to verify file metadata stored in the backup. But this adds the code to look up the data during a backup so that at least that part gets some exercise. --- Cargo.toml | 1 + src/fsentry.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index d36c450..e9bd95e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ structopt = "0.3" tempfile = "3.1" thiserror = "1" tokio = { version = "0.2", features = ["macros"] } +users = "0.11" uuid = { version = "0.8", features = ["v4"] } walkdir = "2" warp = { version = "0.2", features = ["tls"] } diff --git a/src/fsentry.rs b/src/fsentry.rs index 570877a..28f7c61 100644 --- a/src/fsentry.rs +++ b/src/fsentry.rs @@ -37,6 +37,14 @@ pub struct FilesystemEntry { // The target of a symbolic link, if any. symlink_target: Option, + + // User and group owning the file. We store them as both the + // numeric id and the textual name corresponding to the numeric id + // at the time of the backup. + uid: u32, + gid: u32, + user: String, + group: String, } #[derive(Debug, thiserror::Error)] @@ -67,6 +75,10 @@ impl FilesystemEntry { } else { None }; + + let uid = meta.st_uid(); + let gid = meta.st_gid(); + Ok(Self { path: path.to_path_buf().into_os_string().into_vec(), kind: FilesystemKind::from_file_type(meta.file_type()), @@ -77,6 +89,10 @@ impl FilesystemEntry { atime: meta.st_atime(), atime_ns: meta.st_atime_nsec(), symlink_target, + uid, + gid, + user: get_username(uid), + group: get_groupname(gid), }) } @@ -122,6 +138,20 @@ impl FilesystemEntry { } } +fn get_username(uid: u32) -> String { + match users::get_user_by_uid(uid) { + None => "".to_string(), + Some(user) => user.name().to_os_string().to_string_lossy().into_owned(), + } +} + +fn get_groupname(gid: u32) -> String { + match users::get_group_by_gid(gid) { + None => "".to_string(), + Some(group) => group.name().to_os_string().to_string_lossy().into_owned(), + } +} + /// Different types of file system entries. #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] pub enum FilesystemKind { -- cgit v1.2.1