Skip to content

Commit

Permalink
using rust edition 2018
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed Oct 21, 2019
1 parent 5e498e8 commit e9e67f5
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 34 deletions.
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ readme = "README.md"
keywords = ["supervisor", "daemon", "process", "restart", "logging"]
categories = ["command-line-utilities", "os", "config"]
license-file = "LICENSE"
edition = "2018"

[dependencies]
clap = "2"
getopts = "0.2"
rustc-serialize = "0.3"
time = "0.1"
nix = "0.15.0"
65 changes: 41 additions & 24 deletions src/bin/immortal.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,53 @@
extern crate immortal;
extern crate clap;

use clap::{App, Arg, SubCommand};
use immortal::daemon;
use nix::unistd::{fork, ForkResult};
use std::env;
use clap::{Arg, App, SubCommand};

fn main() {
let matches = App::new("immortal")
.version(env!("CARGO_PKG_VERSION"))
.about("Run a command forever")
.arg(Arg::with_name("q")
.short("q")
.long("quiet")
.help("Redirect standard input, output, error to /dev/null")
.takes_value(false))
.arg(Arg::with_name("f")
.short("f")
.long("follow")
.help("Follow pid")
.takes_value(false))
.arg(Arg::with_name("u")
.short("u")
.long("user")
.help("Execute command on behalf user")
.takes_value(true))
.arg(Arg::with_name("command")
.help("Command to daemonize")
.required(true)
.index(1))
.arg(
Arg::with_name("q")
.short("q")
.long("quiet")
.help("Redirect standard input, output, error to /dev/null")
.takes_value(false),
)
.arg(
Arg::with_name("f")
.short("f")
.long("follow")
.help("Follow pid")
.takes_value(false),
)
.arg(
Arg::with_name("u")
.short("u")
.long("user")
.help("Execute command on behalf user")
.takes_value(true),
)
.arg(
Arg::with_name("command")
.help("Command to daemonize")
.required(true)
.index(1),
)
.get_matches();

if matches.is_present("u") {
println!("user: {}", matches.value_of("u").unwrap());
println!("user: {}", matches.value_of("u").unwrap());
}

match fork() {
Ok(ForkResult::Parent { child, .. }) => {
println!(
"Continuing execution in parent process, new child has pid: {}",
child
);
}
Ok(ForkResult::Child) => println!("I'm a new child process"),
Err(_) => println!("Fork failed"),
}
}
24 changes: 14 additions & 10 deletions src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ pub struct Scandir {

impl Scandir {
pub fn new(dir: &str, seconds: u64) -> Result<Scandir, io::Error> {
let path = try!(fs::canonicalize(dir));
Ok(
Scandir {
path: path,
interval: seconds
}
)
let path = fs::canonicalize(dir)?;
Ok(Scandir {
path: path,
interval: seconds,
})
}

pub fn scan(&self) {
Expand All @@ -25,17 +23,23 @@ impl Scandir {
println!("{}", f);
return;
}
Ok(f) => f
Ok(f) => f,
};

for f in files {
let file = f.unwrap();
let mode = file.metadata().unwrap().permissions().mode();
let mut is_exec: bool = false;
if !file.file_type().unwrap().is_dir() {
is_exec = mode & 0o111 != 0;
is_exec = mode & 0o111 != 0;
}
println!("path: {} name: {} mode: {:o} is_exec: {}", file.path().display(), file.file_name().into_string().unwrap(), mode, is_exec);
println!(
"path: {} name: {} mode: {:o} is_exec: {}",
file.path().display(),
file.file_name().into_string().unwrap(),
mode,
is_exec
);
}
}
}

0 comments on commit e9e67f5

Please sign in to comment.