move some functions out of the impl that don't need to be in there

master
rascul 5 years ago
parent c6c1e271b1
commit f9900daeef

@ -30,13 +30,13 @@ impl Files {
println!("Checking {}", &file.display()); println!("Checking {}", &file.display());
} }
Files::open(&file)?; open(&file)?;
let (libs, lib64s) = Files::libs_from_ldd(&file, &config)?; let (libs, lib64s) = libs_from_ldd(&file, &config)?;
myfiles.libs.extend(libs); myfiles.libs.extend(libs);
myfiles.lib64s.extend(lib64s); myfiles.lib64s.extend(lib64s);
if Files::check_sbin(&file) { if check_sbin(&file) {
myfiles.sbins.push(file.to_owned()); myfiles.sbins.push(file.to_owned());
} else { } else {
myfiles.bins.push(file.to_owned()); myfiles.bins.push(file.to_owned());
@ -55,174 +55,174 @@ impl Files {
Ok(myfiles) Ok(myfiles)
} }
fn open(p: &PathBuf) -> MkrootResult<()> { pub fn copy(&self, config: &Config) -> MkrootResult<()> {
if let Err(e) = File::open(&p) { let mut target = PathBuf::from(&config.root_dir);
return Err(MkrootError::from(format!( target.push("bin");
"Error opening file ({}): {}", copy_files(&self.bins, &target, 0o755, config.verbose)?;
&p.display(),
e
)));
}
Ok(())
}
fn libs_from_ldd(
file: &PathBuf,
config: &Config,
) -> MkrootResult<(Vec<PathBuf>, Vec<PathBuf>)> {
let ldd = Files::ldd(&file, &config)?;
let mut libs: Vec<PathBuf> = Vec::new();
let mut lib64s: Vec<PathBuf> = Vec::new();
if ldd.is_empty() { let mut target = PathBuf::from(&config.root_dir);
return Ok((libs, lib64s)); target.push("sbin");
} copy_files(&self.sbins, &target, 0o755, config.verbose)?;
let re = Regex::new(r"(^|.* )(?P<path>/.*) \(0x[[:xdigit:]]{16}\)$")?; let mut target = PathBuf::from(&config.root_dir);
target.push("libs");
copy_files(&self.libs, &target, 0o644, config.verbose)?;
for line in ldd.lines() { let mut target = PathBuf::from(&config.root_dir);
let line = String::from(line.trim()); target.push("lib64");
copy_files(&self.lib64s, &target, 0o644, config.verbose)?;
if let Some(caps) = re.captures(&line) { Ok(())
if let Some(rematch) = caps.name("path") { }
let match_path = PathBuf::from(rematch.as_str()); }
if config.verbose { pub fn set_linker_permissions(
println!("Adding {}", &match_path.display()); 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());
} }
if Files::check_lib64(&match_path) { set_perms(&lib, Permissions::from_mode(0o755))?;
lib64s.push(match_path);
} else {
libs.push(match_path);
}
} }
} }
} }
Ok((libs, lib64s))
} }
fn check_lib64(path: &PathBuf) -> bool { Ok(())
for c in path.components() { }
if c.as_os_str() == "lib64" {
return true;
}
}
false fn open(p: &PathBuf) -> MkrootResult<()> {
if let Err(e) = File::open(&p) {
return Err(MkrootError::from(format!(
"Error opening file ({}): {}",
&p.display(),
e
)));
} }
Ok(())
}
fn check_sbin(path: &PathBuf) -> bool { fn libs_from_ldd(
for c in path.components() { file: &PathBuf,
if c.as_os_str() == "sbin" { config: &Config,
return true; ) -> MkrootResult<(Vec<PathBuf>, Vec<PathBuf>)> {
} let ldd = ldd(&file, &config)?;
}
false let mut libs: Vec<PathBuf> = Vec::new();
let mut lib64s: Vec<PathBuf> = Vec::new();
if ldd.is_empty() {
return Ok((libs, lib64s));
} }
fn ldd(file: &PathBuf, config: &Config) -> MkrootResult<String> { let re = Regex::new(r"(^|.* )(?P<path>/.*) \(0x[[:xdigit:]]{16}\)$")?;
match Command::new(&config.ldd).arg(file).output() {
Ok(output) => { for line in ldd.lines() {
if output.status.success() { let line = String::from(line.trim());
Ok(String::from_utf8(output.stdout).unwrap_or_default())
} else if config.verbose {
let mut out = String::from("ldd failed: ");
let stdout = String::from_utf8(output.stdout).unwrap_or_default();
let stderr = String::from_utf8(output.stderr).unwrap_or_default();
if !stdout.is_empty() {
out = out + "stdout: " + &stdout + " ";
}
if !stderr.is_empty() {
out = out + "stderr: " + &stderr;
}
println!("{}", out.trim()); if let Some(caps) = re.captures(&line) {
if let Some(rematch) = caps.name("path") {
let match_path = PathBuf::from(rematch.as_str());
if config.verbose {
println!("Adding {}", &match_path.display());
}
Ok(String::new()) if check_lib64(&match_path) {
lib64s.push(match_path);
} else { } else {
Ok(String::new()) libs.push(match_path);
} }
} }
Err(e) => Err(MkrootError::from(format!(
"Error running ldd ({}): {}",
&config.ldd.display(),
e
))),
} }
} }
pub fn copy(&self, config: &Config) -> MkrootResult<()> { Ok((libs, lib64s))
let mut target = PathBuf::from(&config.root_dir); }
target.push("bin");
Files::copy_files(&self.bins, &target, 0o755, config.verbose)?;
let mut target = PathBuf::from(&config.root_dir);
target.push("sbin");
Files::copy_files(&self.sbins, &target, 0o755, config.verbose)?;
let mut target = PathBuf::from(&config.root_dir);
target.push("libs");
Files::copy_files(&self.libs, &target, 0o644, config.verbose)?;
let mut target = PathBuf::from(&config.root_dir); fn copy_files(
target.push("lib64"); files: &[PathBuf],
Files::copy_files(&self.lib64s, &target, 0o644, config.verbose)?; target: &PathBuf,
mode: u32,
verbose: bool,
) -> MkrootResult<()> {
for f in files {
let mut t = PathBuf::from(&target);
if let Some(filename) = &f.file_name() {
t.push(filename);
if verbose {
println!("Copying {} to {}", &f.display(), &t.display());
}
Ok(()) copy_file(&f, &t)?;
set_perms(&t, Permissions::from_mode(mode))?;
} else if verbose {
println!("Skipping {}", &f.display());
}
} }
pub fn set_linker_permissions( Ok(())
libs: &[PathBuf], }
dir: &PathBuf,
verbose: bool, fn ldd(file: &PathBuf, config: &Config) -> MkrootResult<String> {
) -> MkrootResult<()> { match Command::new(&config.ldd).arg(file).output() {
for lib in libs { Ok(output) => {
if let Some(fn_osstr) = &lib.file_name() { if output.status.success() {
if let Some(fn_str) = fn_osstr.to_str() { Ok(String::from_utf8(output.stdout).unwrap_or_default())
if fn_str.starts_with("ld-linux") { } else if config.verbose {
let mut lib = PathBuf::from(&dir); let mut out = String::from("ldd failed: ");
lib.push(&fn_str); let stdout = String::from_utf8(output.stdout).unwrap_or_default();
let stderr = String::from_utf8(output.stderr).unwrap_or_default();
if verbose {
println!("Setting linker {} to mode 0o755", &lib.display()); if !stdout.is_empty() {
} out = out + "stdout: " + &stdout + " ";
set_perms(&lib, Permissions::from_mode(0o755))?;
}
} }
if !stderr.is_empty() {
out = out + "stderr: " + &stderr;
}
println!("{}", out.trim());
Ok(String::new())
} else {
Ok(String::new())
} }
} }
Err(e) => Err(MkrootError::from(format!(
"Error running ldd ({}): {}",
&config.ldd.display(),
e
))),
}
}
Ok(()) fn check_lib64(path: &PathBuf) -> bool {
for c in path.components() {
if c.as_os_str() == "lib64" {
return true;
}
} }
fn copy_files( false
files: &[PathBuf], }
target: &PathBuf,
mode: u32,
verbose: bool,
) -> MkrootResult<()> {
for f in files {
let mut t = PathBuf::from(&target);
if let Some(filename) = &f.file_name() {
t.push(filename);
if verbose {
println!("Copying {} to {}", &f.display(), &t.display());
}
copy_file(&f, &t)?; fn check_sbin(path: &PathBuf) -> bool {
set_perms(&t, Permissions::from_mode(mode))?; for c in path.components() {
} else if verbose { if c.as_os_str() == "sbin" {
println!("Skipping {}", &f.display()); return true;
}
} }
Ok(())
} }
false
} }

@ -1,3 +1,3 @@
mod mkroot_files; mod mkroot_files;
pub use mkroot_files::Files; pub use mkroot_files::*;

@ -27,17 +27,17 @@ fn main() -> error::MkrootResult<()> {
} }
dirs::check(&config)?; dirs::check(&config)?;
let files = files::Files::gather(&config)?; let mkrootfiles = files::Files::gather(&config)?;
dirs::create(&config)?; dirs::create(&config)?;
files.copy(&config)?; mkrootfiles.copy(&config)?;
let mut d = PathBuf::from(&config.root_dir); let mut d = PathBuf::from(&config.root_dir);
d.push("lib"); d.push("lib");
files::Files::set_linker_permissions(&files.libs, &d, config.verbose)?; files::set_linker_permissions(&mkrootfiles.libs, &d, config.verbose)?;
let mut d = PathBuf::from(&config.root_dir); let mut d = PathBuf::from(&config.root_dir);
d.push("lib64"); d.push("lib64");
files::Files::set_linker_permissions(&files.lib64s, &d, config.verbose)?; files::set_linker_permissions(&mkrootfiles.lib64s, &d, config.verbose)?;
if let Err(e) = os_release::os_release(&config) { if let Err(e) = os_release::os_release(&config) {
return Err(error::MkrootError::from(format!( return Err(error::MkrootError::from(format!(

Loading…
Cancel
Save