Repository: firecracker-microvm/firecracker
File: src/jailer/src/env.rs (Rust)

Code before:
            let jailer_path = jailer_cache_dir.join(&index_folder);
            fs::create_dir_all(&jailer_path)
                .map_err(|err| JailerError::CreateDir(jailer_path.to_owned(), err))?;

            // We now read the contents of the current directory and copy the files we are
            // interested in to the destination path.
            for entry in FOLDER_HIERARCHY.iter() {
                let host_cache_file = host_path.join(entry);
                let jailer_cache_file = jailer_path.join(entry);

                if let Ok(line) = readln_special(&host_cache_file) {
                    writeln_special(&jailer_cache_file, line)?;

                    // We now change the permissions.
                    let dest_path_cstr = to_cstring(&jailer_cache_file)?;
                    // SAFETY: Safe because dest_path_cstr is null-terminated.
                    SyscallReturnCode(unsafe {
                        libc::chown(dest_path_cstr.as_ptr(), self.uid(), self.gid())
                    })
                    .into_empty_result()
                    .map_err(|err| {
                        JailerError::ChangeFileOwner(jailer_cache_file.to_owned(), err)
                    })?;
                } else {
                    err_str += entry;
                    err_str += ", ";
                }
            }

            // Pop the last 2 chars if a comma and space are present.
            // The unwrap is safe since we check that the string actually
            // ends with those 2 chars.
            if err_str.ends_with(", ") {
                err_str.pop().unwrap();
                err_str.pop().unwrap();
            }

            if !err_str.is_empty() {
                println!("Could not read the {err_str} for cache level {index}.");
            }
        }
        Ok(())
    }

    #[cfg(target_arch = "aarch64")]
    fn copy_midr_el1_info(&self) -> Result<(), JailerError> {
        use crate::{readln_special, to_cstring, writeln_special};

        const HOST_MIDR_EL1_INFO: &str = "/sys/devices/system/cpu/cpu0/regs/identification";

        let jailer_midr_el1_directory =

Review comment (performance):
Instead of building an error string you can just print the error inline:
```rust
eprintln!("Cache level {index}: {entry} entry not found, skipping");
```
This is a simpler approach, event thought it will produce more output.

Code after:

            // We now read the contents of the current directory and copy the files we are
            // interested in to the destination path.
            for entry in FOLDER_HIERARCHY.iter() {
                let host_cache_file = host_path.join(entry);
                let jailer_cache_file = jailer_path.join(entry);

                if let Ok(line) = readln_special(&host_cache_file) {
                    writeln_special(&jailer_cache_file, line)?;

                    // We now change the permissions.
                    let dest_path_cstr = to_cstring(&jailer_cache_file)?;
                    // SAFETY: Safe because dest_path_cstr is null-terminated.
                    SyscallReturnCode(unsafe {
                        libc::chown(dest_path_cstr.as_ptr(), self.uid(), self.gid())
                    })
                    .into_empty_result()
                    .map_err(|err| {
                        JailerError::ChangeFileOwner(jailer_cache_file.to_owned(), err)
                    })?;
                }
            }
        }
        Ok(())
    }

    #[cfg(target_arch = "aarch64")]
    fn copy_midr_el1_info(&self) -> Result<(), JailerError> {
        use crate::{readln_special, to_cstring, writeln_special};

        const HOST_MIDR_EL1_INFO: &str = "/sys/devices/system/cpu/cpu0/regs/identification";

        let jailer_midr_el1_directory =
            Path::new(self.chroot_dir()).join("sys/devices/system/cpu/cpu0/regs/identification/");
        fs::create_dir_all(&jailer_midr_el1_directory)
            .map_err(|err| JailerError::CreateDir(jailer_midr_el1_directory.to_owned(), err))?;

        let host_midr_el1_file = PathBuf::from(format!("{}/midr_el1", HOST_MIDR_EL1_INFO));
        let jailer_midr_el1_file = jailer_midr_el1_directory.join("midr_el1");

        // Read and copy the MIDR_EL1 file to Jailer
        let line = readln_special(&host_midr_el1_file)?;
        writeln_special(&jailer_midr_el1_file, line)?;

        // Change the permissions.
        let dest_path_cstr = to_cstring(&jailer_midr_el1_file)?;
        // SAFETY: Safe because `dest_path_cstr` is null-terminated.
        SyscallReturnCode(unsafe { libc::chown(dest_path_cstr.as_ptr(), self.uid(), self.gid()) })
            .into_empty_result()
            .map_err(|err| JailerError::ChangeFileOwner(jailer_midr_el1_file.to_owned(), err))?;