set permissions

master
rascul 5 years ago
parent 6d6bc2b91b
commit 13811011f9

@ -1,4 +1,5 @@
use std::fs::{copy as fscopy, File}; use std::fs::{copy as fscopy, metadata, set_permissions, File};
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
@ -157,24 +158,75 @@ impl Files {
pub fn copy(&self, config: &Config) -> MkrootResult<()> { pub fn copy(&self, config: &Config) -> MkrootResult<()> {
let mut target = PathBuf::from(&config.root_dir); let mut target = PathBuf::from(&config.root_dir);
target.push("bin"); target.push("bin");
Files::copy_files(&self.bins, &target, config.verbose)?; Files::copy_files(&self.bins, &target, 0o755, config.verbose)?;
let mut target = PathBuf::from(&config.root_dir); let mut target = PathBuf::from(&config.root_dir);
target.push("sbin"); target.push("sbin");
Files::copy_files(&self.sbins, &target, config.verbose)?; Files::copy_files(&self.sbins, &target, 0o755, config.verbose)?;
let mut target = PathBuf::from(&config.root_dir); let mut target = PathBuf::from(&config.root_dir);
target.push("libs"); target.push("libs");
Files::copy_files(&self.libs, &target, config.verbose)?; Files::copy_files(&self.libs, &target, 0o644, config.verbose)?;
let mut target = PathBuf::from(&config.root_dir); let mut target = PathBuf::from(&config.root_dir);
target.push("lib64"); target.push("lib64");
Files::copy_files(&self.lib64s, &target, config.verbose)?; Files::copy_files(&self.lib64s, &target, 0o644, config.verbose)?;
Ok(()) Ok(())
} }
fn copy_files(files: &[PathBuf], target: &PathBuf, verbose: bool) -> MkrootResult<()> { pub fn set_linker_permissions(
libs: &[PathBuf],
dir: &PathBuf,
verbose: bool,
) -> MkrootResult<()> {
for lib in libs {
if let Some(fn_osstr) = &lib.file_name() {
if let Some(fn_str) = fn_osstr.to_str() {
if fn_str.starts_with("ld-linux") {
let mut lib = PathBuf::from(&dir);
lib.push(&fn_str);
if verbose {
println!("Setting linker {} to mode 0o755", &lib.display());
}
match metadata(&lib) {
Ok(meta) => {
let mut perms = meta.permissions();
perms.set_mode(0o755);
if let Err(e) = set_permissions(&lib, perms) {
return Err(MkrootError::from(format!(
"Error setting permissions {}: {}",
&lib.display(),
e
)));
} else {
return Ok(());
}
}
Err(e) => {
return Err(MkrootError::from(format!(
"Error reading metadata {}: {}",
&lib.display(),
e
)))
}
}
}
}
}
}
Ok(())
}
fn copy_files(
files: &[PathBuf],
target: &PathBuf,
mode: u32,
verbose: bool,
) -> MkrootResult<()> {
for f in files { for f in files {
let mut t = PathBuf::from(&target); let mut t = PathBuf::from(&target);
if let Some(filename) = &f.file_name() { if let Some(filename) = &f.file_name() {
@ -191,6 +243,10 @@ impl Files {
&t.display(), &t.display(),
e e
))); )));
} else {
let mut perms = metadata(&t)?.permissions();
perms.set_mode(mode);
set_permissions(&t, perms)?;
} }
} else if verbose { } else if verbose {
println!("Skipping {}", &f.display()); println!("Skipping {}", &f.display());

@ -1,5 +1,6 @@
use std::fs::File; use std::fs::{metadata, set_permissions, File};
use std::io::{BufWriter, Write}; use std::io::{BufWriter, Write};
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf; use std::path::PathBuf;
use crate::config::Config; use crate::config::Config;
@ -13,13 +14,18 @@ pub fn os_release(config: &Config) -> MkrootResult<()> {
let mut path = PathBuf::from(&config.root_dir); let mut path = PathBuf::from(&config.root_dir);
path.push("etc/os-release"); path.push("etc/os-release");
let f = File::create(&path)?; {
let mut writer = BufWriter::new(f); let f = File::create(&path)?;
let mut writer = BufWriter::new(f);
writer.write_fmt(format_args!(
"NAME=\"{}\"\nVERSION=\"{}\"\n",
&config.osname, &config.osversion
))?;
}
writer.write_fmt(format_args!( let mut perms = metadata(&path)?.permissions();
"NAME=\"{}\"\nVERSION=\"{}\"\n", perms.set_mode(0o644);
&config.osname, &config.osversion set_permissions(&path, perms)?;
))?;
Ok(()) Ok(())
} }

@ -6,6 +6,8 @@ mod dirs;
mod error; mod error;
mod files; mod files;
use std::path::PathBuf;
use structopt::StructOpt; use structopt::StructOpt;
use config::Config; use config::Config;
@ -26,6 +28,14 @@ fn main() -> error::MkrootResult<()> {
dirs::create(&config)?; dirs::create(&config)?;
files.copy(&config)?; files.copy(&config)?;
let mut d = PathBuf::from(&config.root_dir);
d.push("lib");
files::Files::set_linker_permissions(&files.libs, &d, config.verbose)?;
let mut d = PathBuf::from(&config.root_dir);
d.push("lib64");
files::Files::set_linker_permissions(&files.lib64s, &d, config.verbose)?;
if let Err(e) = files::os_release(&config) { if let Err(e) = files::os_release(&config) {
return Err(error::MkrootError::from(format!( return Err(error::MkrootError::from(format!(
"Error creating etc/os-release: {}", "Error creating etc/os-release: {}",
@ -34,8 +44,6 @@ fn main() -> error::MkrootResult<()> {
} }
// cp skel // cp skel
// run ldconfig
// set permissions
Ok(()) Ok(())
} }

Loading…
Cancel
Save