Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restore: Add caching for uid/gid names #33

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/backend/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use aho_corasick::AhoCorasick;
use bytes::Bytes;
use cached::proc_macro::cached;

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Build docs (stable, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (beta, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (beta, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (beta, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (beta, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (beta, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Cross checking x86_64-pc-windows-msvc

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (stable, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (stable, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (stable, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (stable, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (stable, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Cross checking x86_64-pc-windows-msvc

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Cross checking x86_64-pc-windows-gnu

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Test (stable, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Cross checking x86_64-pc-windows-gnu

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (nightly, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (nightly, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (nightly, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (nightly, windows-latest)

unused import: `cached::proc_macro::cached`

Check warning on line 13 in src/backend/local.rs

View workflow job for this annotation

GitHub Actions / Check Powerset of Features (nightly, windows-latest)

unused import: `cached::proc_macro::cached`
use filetime::{set_symlink_file_times, FileTime};
use log::{debug, trace, warn};
#[cfg(not(windows))]
Expand Down Expand Up @@ -469,6 +470,20 @@
is_file: bool,
}

// Helper function to cache mapping user name -> uid
#[cfg(not(windows))]
#[cached]
fn uid_from_name(name: String) -> Option<Uid> {
User::from_name(&name).unwrap().map(|u| u.uid)
}

// Helper function to cache mapping group name -> gid
#[cfg(not(windows))]
#[cached]
fn gid_from_name(name: String) -> Option<Gid> {
Group::from_name(&name).unwrap().map(|g| g.gid)
}

impl LocalDestination {
/// Create a new [`LocalDestination`]
///
Expand Down Expand Up @@ -648,20 +663,14 @@
pub fn set_user_group(&self, item: impl AsRef<Path>, meta: &Metadata) -> RusticResult<()> {
let filename = self.path(item);

let user = meta
.user
.as_ref()
.and_then(|name| User::from_name(name).unwrap());

let user = meta.user.clone().and_then(uid_from_name);
// use uid from user if valid, else from saved uid (if saved)
let uid = user.map(|u| u.uid).or_else(|| meta.uid.map(Uid::from_raw));
let uid = user.or_else(|| meta.uid.map(Uid::from_raw));

let group = meta
.group
.as_ref()
.and_then(|name| Group::from_name(name).unwrap());
let group = meta.group.clone().and_then(gid_from_name);
// use gid from group if valid, else from saved gid (if saved)
let gid = group.map(|g| g.gid).or_else(|| meta.gid.map(Gid::from_raw));
let gid = group.or_else(|| meta.gid.map(Gid::from_raw));

fchownat(None, &filename, uid, gid, FchownatFlags::NoFollowSymlink)
.map_err(LocalErrorKind::FromErrnoError)?;
Ok(())
Expand Down
Loading