Skip to content

Commit

Permalink
Addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nviennot committed Apr 7, 2021
1 parent d984ab7 commit 3fe921b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
container,
};

/// Install FastFreeze, mostly to setup virtualization
/// Install FastFreeze, required when namespaces are not available (e.g., Docker).
#[derive(StructOpt, PartialEq, Debug, Serialize)]
pub struct Install {
/// Verbosity. Can be repeated
Expand All @@ -37,7 +37,7 @@ pub struct Install {

pub fn is_ff_installed() -> Result<bool> {
Ok(match LD_SYSTEM_PATH.read_link() {
Ok(path) => path.to_string_lossy().contains("virtcpuid"),
Ok(path) => path.eq(&*LD_VIRTCPUID_PATH),
// EINVAL means the file is not a symlink.
Err(e) if e.kind() == ErrorKind::InvalidInput => false,
Err(e) => Err(e).with_context(||
Expand Down
2 changes: 1 addition & 1 deletion src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use super::{
// subcommand version is not useful, disable it.
global_setting(AppSettings::VersionlessSubcommands),
)]
#[structopt(after_help(" restore-only is achived by using \
#[structopt(after_help(" restore-only is achieved by using \
the `run` subcommand without passing the application command-line arguments"
))]
pub struct Opts {
Expand Down
8 changes: 4 additions & 4 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
// limitations under the License.

pub mod run;
pub mod checkpoint;
pub mod extract;
pub mod wait;
mod checkpoint;
mod extract;
mod wait;
pub mod install;
pub mod main;
mod main;

use crate::consts::*;

Expand Down
27 changes: 14 additions & 13 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ pub struct Run {

/// Application command, used when running the app from scratch.
/// When absent, FastFreeze runs in restore-only mode.
// Note: Type should be OsString, but structopt doesn't like it
// Also, we wish to pass min_values=1, but it's not working.
#[structopt()]
app_args: Vec<String>,
app_args: Vec<OsString>,

/// Shell command to run once the application is running.
// Note: Type should be OsString, but structopt doesn't like it
Expand Down Expand Up @@ -444,7 +442,7 @@ fn ensure_non_conflicting_pid() -> Result<()> {

fn do_run(
image_url: ImageUrl,
app_args: Option<Vec<String>>,
app_args: Option<Vec<OsString>>,
preserved_paths: HashSet<PathBuf>,
passphrase_file: Option<PathBuf>,
no_restore: bool,
Expand Down Expand Up @@ -507,28 +505,31 @@ fn do_run(
Ok(())
}

fn default_image_name(app_args: &[String]) -> String {
fn default_image_name(app_args: &[OsString]) -> Result<String> {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

fn program_name(cmd: &str) -> String {
// unwrap() is fine, we'll error earlier if cmd is empty.
cmd.split('/').last().unwrap().to_string()
fn program_name(cmd: &OsString) -> Result<String> {
Ok(Path::new(cmd)
.file_name().ok_or_else(||
anyhow!("Failed to determine program name from command name. \
Please use `--image-url` to specify the image URL"))?
.to_string_lossy().into_owned())
}

match app_args {
Ok(match app_args {
[] => unreachable!(),
[app_name] => program_name(app_name),
[app_name] => program_name(app_name)?,
_ => {
let hash = {
let mut hasher = DefaultHasher::new();
app_args.hash(&mut hasher);
hasher.finish()
};

format!("{}-{:04x}", program_name(&app_args[0]), hash & 0xFFFF)
format!("{}-{:04x}", program_name(&app_args[0])?, hash & 0xFFFF)
}
}
})
}

impl super::CLI for Run {
Expand All @@ -554,7 +555,7 @@ impl super::CLI for Run {
(None, None) =>
bail!("--image-url is necessary when running in restore-only mode"),
(None, Some(app_args)) => {
let image_path = DEFAULT_IMAGE_DIR.join(default_image_name(app_args));
let image_path = DEFAULT_IMAGE_DIR.join(default_image_name(app_args)?);
let image_url = format!("file:{}", image_path.display());
info!("image-url is {}", image_url);
image_url
Expand Down

0 comments on commit 3fe921b

Please sign in to comment.