mirror of
https://github.com/ivabus/privod
synced 2024-11-22 00:15:08 +03:00
Reformat code, add optional range, rename C file
Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
parent
2910c3f314
commit
105372a965
4 changed files with 28 additions and 19 deletions
10
README.md
10
README.md
|
@ -1,20 +1,16 @@
|
||||||
# Privod (оптический привод)
|
# Privod (оптический привод)
|
||||||
|
|
||||||
Eject CD/DVD/BD drive at random time (from 5 secs to 5 mins)
|
Eject CD/DVD/BD drive at random time.
|
||||||
|
|
||||||
Linux only.
|
Linux only.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
cargo run <PATH_TO_DRIVE>
|
cargo run <PATH_TO_DRIVE> [RANGE_START] [RANGE_END]
|
||||||
```
|
```
|
||||||
|
|
||||||
`PATH_TO_DRIVE` is usually `/dev/sr0`
|
`PATH_TO_DRIVE` is usually `/dev/sr0`, `RANGE_START` (in secs) and `RANGE_END` (in secs) are optional (default is 5 and 300).
|
||||||
|
|
||||||
## TODO
|
|
||||||
|
|
||||||
- [ ] Delay setup
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|
6
build.rs
6
build.rs
|
@ -1,6 +1,4 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
cc::Build::new()
|
cc::Build::new().file("src/privod.c").compile("privod");
|
||||||
.file("src/get_status.c")
|
println!("cargo:rerun-if-changed=src/privod.c");
|
||||||
.compile("get_status");
|
|
||||||
println!("cargo:rerun-if-changed=src/get_status.c");
|
|
||||||
}
|
}
|
||||||
|
|
26
src/main.rs
26
src/main.rs
|
@ -1,31 +1,43 @@
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use std::ffi::{c_char, CString};
|
use std::ffi::{c_char, CString};
|
||||||
|
|
||||||
#[link(name = "get_status")]
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
pub fn get_status(drive_path: *const c_char) -> std::ffi::c_int;
|
pub fn get_status(drive_path: *const c_char) -> std::ffi::c_int;
|
||||||
pub fn eject(drive_path: *const c_char);
|
pub fn eject(drive_path: *const c_char);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sleep_rnd() {
|
fn sleep_rnd(start: u64, end: u64) {
|
||||||
let sleeptime = rand::thread_rng().gen_range(5..300);
|
let sleeptime = rand::thread_rng().gen_range(start..=end);
|
||||||
eprintln!("Sleeping for {} secs", &sleeptime);
|
eprintln!("Sleeping for {} secs", &sleeptime);
|
||||||
std::thread::sleep(std::time::Duration::from_secs(sleeptime));
|
std::thread::sleep(std::time::Duration::from_secs(sleeptime));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let args = std::env::args().collect::<Vec<String>>();
|
||||||
|
if args.len() != 2 && args.len() != 4 {
|
||||||
|
eprintln!(
|
||||||
|
"Usage: {} <PATH_TO_DRIVE> [RANGE_START] [RANGE_END]",
|
||||||
|
args[0]
|
||||||
|
);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
let (start, end) = if args.len() == 4 {
|
||||||
|
(args[2].parse().unwrap(), args[3].parse().unwrap())
|
||||||
|
} else {
|
||||||
|
(5, 300)
|
||||||
|
};
|
||||||
let dr_name = CString::new(std::env::args().collect::<Vec<String>>()[1].as_bytes()).unwrap();
|
let dr_name = CString::new(std::env::args().collect::<Vec<String>>()[1].as_bytes()).unwrap();
|
||||||
unsafe {
|
unsafe {
|
||||||
loop {
|
loop {
|
||||||
if get_status(dr_name.as_ptr()) != 0x1 {
|
if get_status(dr_name.as_ptr()) != 0x1 {
|
||||||
while get_status(dr_name.as_ptr()) != 0x1_i32 {
|
while get_status(dr_name.as_ptr()) != 0x1_i32 {
|
||||||
eprintln!("Status != 0x1, sleeping 100ms");
|
eprintln!("Status != closed, sleeping 250ms");
|
||||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
std::thread::sleep(std::time::Duration::from_millis(250));
|
||||||
}
|
}
|
||||||
sleep_rnd()
|
sleep_rnd(start, end)
|
||||||
}
|
}
|
||||||
eject(dr_name.as_ptr());
|
eject(dr_name.as_ptr());
|
||||||
sleep_rnd();
|
sleep_rnd(start, end);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,9 @@
|
||||||
#include <linux/cdrom.h>
|
#include <linux/cdrom.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
|
// Status: 0 - opened, 1 - closed, 2 - failed
|
||||||
|
|
||||||
int get_status(char *drive_path) {
|
int get_status(char *drive_path) {
|
||||||
int cdrom;
|
int cdrom;
|
||||||
int status=1;
|
int status=1;
|
||||||
|
@ -20,7 +23,7 @@ int get_status(char *drive_path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void eject(char *drive_path) {
|
void eject(char *drive_path) {
|
||||||
// Ignoring everything
|
// Ignore everything
|
||||||
int cdrom;
|
int cdrom;
|
||||||
cdrom = open(drive_path, O_RDONLY | O_NONBLOCK);
|
cdrom = open(drive_path, O_RDONLY | O_NONBLOCK);
|
||||||
ioctl (cdrom, CDROMEJECT);
|
ioctl (cdrom, CDROMEJECT);
|
Loading…
Reference in a new issue