1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
use std::fs;
use std::path::Path;

use anyhow::Result;
use nix::unistd::{self, Gid, Uid};

pub fn chownr(path: &Path, uid: Option<Uid>, gid: Option<Gid>) -> Result<()> {
    if path.is_dir() {
        for entry in fs::read_dir(&path)? {
            let entry = entry?;
            chownr(entry.path().as_path(), uid, gid)?;
        }
    }
    unistd::chown(path, uid, gid)?;
    Ok(())
}