Update readme
This commit is contained in:
commit
1470419177
7 changed files with 748 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
/target
|
||||||
|
/out
|
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"editor.formatOnSave": true
|
||||||
|
}
|
319
Cargo.lock
generated
Normal file
319
Cargo.lock
generated
Normal file
|
@ -0,0 +1,319 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstream"
|
||||||
|
version = "0.6.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526"
|
||||||
|
dependencies = [
|
||||||
|
"anstyle",
|
||||||
|
"anstyle-parse",
|
||||||
|
"anstyle-query",
|
||||||
|
"anstyle-wincon",
|
||||||
|
"colorchoice",
|
||||||
|
"is_terminal_polyfill",
|
||||||
|
"utf8parse",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle"
|
||||||
|
version = "1.0.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-parse"
|
||||||
|
version = "0.2.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb"
|
||||||
|
dependencies = [
|
||||||
|
"utf8parse",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-query"
|
||||||
|
version = "1.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a"
|
||||||
|
dependencies = [
|
||||||
|
"windows-sys 0.52.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-wincon"
|
||||||
|
version = "3.0.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8"
|
||||||
|
dependencies = [
|
||||||
|
"anstyle",
|
||||||
|
"windows-sys 0.52.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "2.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap"
|
||||||
|
version = "4.5.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8"
|
||||||
|
dependencies = [
|
||||||
|
"clap_builder",
|
||||||
|
"clap_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_builder"
|
||||||
|
version = "4.5.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54"
|
||||||
|
dependencies = [
|
||||||
|
"anstream",
|
||||||
|
"anstyle",
|
||||||
|
"clap_lex",
|
||||||
|
"strsim",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_derive"
|
||||||
|
version = "4.5.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab"
|
||||||
|
dependencies = [
|
||||||
|
"heck",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_lex"
|
||||||
|
version = "0.7.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "colorchoice"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "errno"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"windows-sys 0.52.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fastrand"
|
||||||
|
version = "2.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "heck"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "is_terminal_polyfill"
|
||||||
|
version = "1.70.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.159"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "linux-raw-sys"
|
||||||
|
version = "0.4.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell"
|
||||||
|
version = "1.20.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.87"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b3e4daa0dcf6feba26f985457cdf104d4b4256fc5a09547140f3631bb076b19a"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.37"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustix"
|
||||||
|
version = "0.38.37"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"errno",
|
||||||
|
"libc",
|
||||||
|
"linux-raw-sys",
|
||||||
|
"windows-sys 0.52.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strsim"
|
||||||
|
version = "0.11.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "2.0.79"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tempfile"
|
||||||
|
version = "3.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"fastrand",
|
||||||
|
"once_cell",
|
||||||
|
"rustix",
|
||||||
|
"windows-sys 0.59.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "utf8parse"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-sys"
|
||||||
|
version = "0.52.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
||||||
|
dependencies = [
|
||||||
|
"windows-targets",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-sys"
|
||||||
|
version = "0.59.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
|
||||||
|
dependencies = [
|
||||||
|
"windows-targets",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-targets"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||||
|
dependencies = [
|
||||||
|
"windows_aarch64_gnullvm",
|
||||||
|
"windows_aarch64_msvc",
|
||||||
|
"windows_i686_gnu",
|
||||||
|
"windows_i686_gnullvm",
|
||||||
|
"windows_i686_msvc",
|
||||||
|
"windows_x86_64_gnu",
|
||||||
|
"windows_x86_64_gnullvm",
|
||||||
|
"windows_x86_64_msvc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_gnullvm"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_msvc"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnu"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnullvm"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_msvc"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnu"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnullvm"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_msvc"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zap"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"clap",
|
||||||
|
"tempfile",
|
||||||
|
]
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "zap"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
clap = { version = "4.5.20", features = ["derive"] }
|
||||||
|
tempfile = "3.13.0"
|
74
README.md
Normal file
74
README.md
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
# Zap - Lightning Fast CLI Image Flasher
|
||||||
|
|
||||||
|
Zap is a command-line interface (CLI) tool designed for quick and efficient ISO image flashing. It offers both interactive and command-line modes, making it versatile for various use cases.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Interactive mode for guided operation
|
||||||
|
- Support for predefined operating systems
|
||||||
|
- Custom ISO file flashing
|
||||||
|
- Dry run option for testing without disk writes
|
||||||
|
- Quiet mode for integration with GUI applications
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Zap can be used in several ways:
|
||||||
|
|
||||||
|
### Basic Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
zap [OPTIONS]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Interactive Mode
|
||||||
|
|
||||||
|
```
|
||||||
|
zap -i
|
||||||
|
```
|
||||||
|
|
||||||
|
### Flashing a Custom ISO
|
||||||
|
|
||||||
|
```
|
||||||
|
zap -f path/to/your/file.iso -t /dev/sdX
|
||||||
|
```
|
||||||
|
|
||||||
|
### Using a Predefined OS
|
||||||
|
|
||||||
|
```
|
||||||
|
zap -o Oreon -t /dev/sdX
|
||||||
|
```
|
||||||
|
|
||||||
|
## Options
|
||||||
|
|
||||||
|
- `-i, --interactive`: Enable interactive mode (ignores all other arguments)
|
||||||
|
- `-d, --dry-run`: Perform a dry run without modifying anything on disk
|
||||||
|
- `-q, --quiet`: Don't log to stdout (useful for GUI applications)
|
||||||
|
- `-o, --os <OS>`: Use one of the predefined operating systems
|
||||||
|
- `-f, --file <FILE>`: Path to the ISO file
|
||||||
|
- `-t, --target <TARGET>`: Specify the drive/partition to write the ISO to
|
||||||
|
|
||||||
|
## Supported Operating Systems
|
||||||
|
|
||||||
|
Currently, Zap supports the following predefined operating systems:
|
||||||
|
|
||||||
|
- Oreon
|
||||||
|
|
||||||
|
More operating systems will be added in future updates.
|
||||||
|
|
||||||
|
## Error Handling
|
||||||
|
|
||||||
|
Zap includes robust error handling to prevent common mistakes:
|
||||||
|
|
||||||
|
- OS and File arguments cannot be used simultaneously
|
||||||
|
- Drive argument is required when a file is chosen
|
||||||
|
- Only ISO files are accepted
|
||||||
|
- The specified ISO file must exist
|
||||||
|
- Drive argument is required when no OS or file is chosen
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
We welcome contributions! Feel free to submit pull requests or open issues on our project repository.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
This project is licensed under the GNU Affero General Public License (AGPL). See the LICENSE file for details.
|
227
src/flasher.rs
Normal file
227
src/flasher.rs
Normal file
|
@ -0,0 +1,227 @@
|
||||||
|
use std::fs::{File, OpenOptions};
|
||||||
|
use std::io::{self, BufReader, Error, ErrorKind, Read, Write};
|
||||||
|
use std::path::Path;
|
||||||
|
use std::process::{exit, Command};
|
||||||
|
|
||||||
|
fn validate_input(iso_path: &str, destination: &str) -> Option<Result<(), Error>> {
|
||||||
|
// Check if the ISO file exists
|
||||||
|
if !Path::new(iso_path).exists() {
|
||||||
|
return Some(Err(Error::new(
|
||||||
|
ErrorKind::NotFound,
|
||||||
|
format!("The ISO file {} does not exist!", iso_path),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the destination is the root directory
|
||||||
|
if destination == "/" {
|
||||||
|
return Some(Err(Error::new(
|
||||||
|
ErrorKind::InvalidInput,
|
||||||
|
"Flashing to the root directory is prohibited!",
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip checks for devices like /dev/sda
|
||||||
|
if destination.starts_with("/dev/") {
|
||||||
|
return None; // Allow flashing to block devices
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the destination exists
|
||||||
|
if !Path::new(destination).exists() {
|
||||||
|
return Some(Err(Error::new(
|
||||||
|
ErrorKind::NotFound,
|
||||||
|
format!("The destination {} does not exist!", destination),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the destination is not a regular file
|
||||||
|
if Path::new(destination).is_file() {
|
||||||
|
return Some(Err(Error::new(
|
||||||
|
ErrorKind::InvalidInput,
|
||||||
|
format!(
|
||||||
|
"The destination {} is a file, not a directory!",
|
||||||
|
destination
|
||||||
|
),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for permission to write to the destination
|
||||||
|
if let Err(err) = File::create(Path::new(destination).join("test_permission")) {
|
||||||
|
return Some(Err(Error::new(
|
||||||
|
ErrorKind::PermissionDenied,
|
||||||
|
format!(
|
||||||
|
"The process does not have permission to modify the destination {}: {}",
|
||||||
|
destination, err
|
||||||
|
),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
if Path::new(destination).join("test_permission").exists() {
|
||||||
|
let _ = std::fs::remove_file(Path::new(destination).join("test_permission"));
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_metadata(iso_file: File) -> Result<(), Error> {
|
||||||
|
let metadata = iso_file.metadata()?;
|
||||||
|
println!("ISO file size: {} bytes", metadata.len());
|
||||||
|
println!("ISO file last modified: {:?}", metadata.modified()?);
|
||||||
|
println!("ISO file permissions: {:?}", metadata.permissions());
|
||||||
|
println!(
|
||||||
|
"ISO file is read-only: {}",
|
||||||
|
metadata.permissions().readonly()
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn flash_iso(iso_path: &str, destination: &str) -> Result<(), io::Error> {
|
||||||
|
// Add a confirmation step if the destination is /dev/sda
|
||||||
|
if destination == "/dev/sda" {
|
||||||
|
println!(
|
||||||
|
"Warning: You are about to flash to /dev/sda. This could overwrite important data."
|
||||||
|
);
|
||||||
|
println!("Are you sure you want to continue? (y/n): ");
|
||||||
|
|
||||||
|
let mut input = String::new();
|
||||||
|
io::stdin().read_line(&mut input)?;
|
||||||
|
|
||||||
|
if input.trim().to_lowercase() != "y" {
|
||||||
|
println!("Operation cancelled.");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate input, returns if invalid
|
||||||
|
if let Some(value) = validate_input(iso_path, destination) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print metadata of the ISO file
|
||||||
|
let iso_file = File::open(iso_path)?;
|
||||||
|
let metadata = iso_file.metadata()?;
|
||||||
|
let total_size = metadata.len();
|
||||||
|
println!("ISO file size: {} bytes", total_size);
|
||||||
|
print_metadata(iso_file)?;
|
||||||
|
|
||||||
|
// Open the ISO file for reading
|
||||||
|
let iso_file = File::open(iso_path)?;
|
||||||
|
let mut reader = BufReader::new(iso_file);
|
||||||
|
|
||||||
|
// Open the destination block device for writing
|
||||||
|
let mut dest_file = OpenOptions::new().write(true).open(destination)?;
|
||||||
|
|
||||||
|
// Buffer to hold chunks of data while reading/writing
|
||||||
|
let mut buffer = [0u8; 4096];
|
||||||
|
let mut bytes_written: u64 = 0;
|
||||||
|
|
||||||
|
println!("Flashing {} to {}", iso_path, destination);
|
||||||
|
|
||||||
|
// Copy data from ISO to the destination in chunks
|
||||||
|
loop {
|
||||||
|
let bytes_read = reader.read(&mut buffer)?;
|
||||||
|
if bytes_read == 0 {
|
||||||
|
break; // EOF reached
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log the number of bytes read
|
||||||
|
// println!("Read {} bytes from ISO", bytes_read);
|
||||||
|
|
||||||
|
dest_file.write_all(&buffer[..bytes_read])?;
|
||||||
|
bytes_written += bytes_read as u64;
|
||||||
|
|
||||||
|
// Calculate and display progress every 1 MB (1_048_576 bytes)
|
||||||
|
if bytes_written % (1 << 20) == 0 || bytes_written == total_size {
|
||||||
|
let progress = (bytes_written * 100) / total_size;
|
||||||
|
println!("Progress: {}%", progress);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure all data is flushed to the device
|
||||||
|
dest_file.flush()?;
|
||||||
|
|
||||||
|
// Now that flushing is complete, print success message
|
||||||
|
println!(
|
||||||
|
"ISO file {} flashed successfully to {}",
|
||||||
|
iso_path, destination
|
||||||
|
);
|
||||||
|
|
||||||
|
// Explicitly exit if desired (generally unnecessary)
|
||||||
|
// std::process::exit(0);
|
||||||
|
|
||||||
|
// Log that it's going to sync data to disk
|
||||||
|
println!("Syncing data to disk...");
|
||||||
|
Command::new("sync")
|
||||||
|
.status()
|
||||||
|
.expect("Failed to flush data to disk");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use tempfile::tempdir;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_flash_iso_nonexistent_iso() {
|
||||||
|
let result = flash_iso("nonexistent.iso", "/tmp");
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(result.unwrap_err().kind(), ErrorKind::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_flash_iso_nonexistent_destination() {
|
||||||
|
let temp_file = tempfile::NamedTempFile::new().unwrap();
|
||||||
|
let result = flash_iso(temp_file.path().to_str().unwrap(), "/nonexistent");
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(result.unwrap_err().kind(), ErrorKind::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_flash_iso_destination_is_file() {
|
||||||
|
let temp_file = tempfile::NamedTempFile::new().unwrap();
|
||||||
|
let result = flash_iso(
|
||||||
|
temp_file.path().to_str().unwrap(),
|
||||||
|
temp_file.path().to_str().unwrap(),
|
||||||
|
);
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(result.unwrap_err().kind(), ErrorKind::InvalidInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_flash_iso_fail() {
|
||||||
|
let temp_file = tempfile::NamedTempFile::new().unwrap();
|
||||||
|
let temp_dir = tempdir().unwrap();
|
||||||
|
let result = flash_iso(
|
||||||
|
temp_file.path().to_str().unwrap(),
|
||||||
|
temp_dir.path().to_str().unwrap(),
|
||||||
|
);
|
||||||
|
assert!(!result.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_flash_iso_to_root() {
|
||||||
|
let temp_file = tempfile::NamedTempFile::new().unwrap();
|
||||||
|
let result = flash_iso(temp_file.path().to_str().unwrap(), "/");
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(result.unwrap_err().kind(), ErrorKind::InvalidInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_flash_iso_no_permission() {
|
||||||
|
let temp_file = tempfile::NamedTempFile::new().unwrap();
|
||||||
|
let temp_dir = tempdir().unwrap();
|
||||||
|
let no_permission_dir = temp_dir.path().join("no_permission");
|
||||||
|
std::fs::create_dir(&no_permission_dir).unwrap();
|
||||||
|
let _ = std::fs::set_permissions(
|
||||||
|
&no_permission_dir,
|
||||||
|
<std::fs::Permissions as std::os::unix::fs::PermissionsExt>::from_mode(0o000),
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = flash_iso(
|
||||||
|
temp_file.path().to_str().unwrap(),
|
||||||
|
no_permission_dir.to_str().unwrap(),
|
||||||
|
);
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(result.unwrap_err().kind(), ErrorKind::PermissionDenied);
|
||||||
|
}
|
||||||
|
}
|
115
src/main.rs
Normal file
115
src/main.rs
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
use clap::{CommandFactory, Parser};
|
||||||
|
mod flasher;
|
||||||
|
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
#[command(version, about, long_about = None)]
|
||||||
|
/// Zap - Lightning fast CLI image flasher
|
||||||
|
struct Args {
|
||||||
|
/// Enable interactive mode (Will ignore all other arguments)
|
||||||
|
#[arg(short, long)]
|
||||||
|
interactive: bool,
|
||||||
|
|
||||||
|
/// Dry run, will not modify/write anything on disk
|
||||||
|
#[arg(short, long)]
|
||||||
|
dry_run: bool,
|
||||||
|
|
||||||
|
/// Don't log to stdout, useful for GUI applications utilizing Zap
|
||||||
|
#[arg(short, long)]
|
||||||
|
quiet: bool,
|
||||||
|
|
||||||
|
/// Use one of the predefined Operating Systems
|
||||||
|
#[arg(short, long)]
|
||||||
|
os: Option<String>,
|
||||||
|
|
||||||
|
/// Path to the ISO file
|
||||||
|
#[arg(short, long)]
|
||||||
|
file: Option<String>,
|
||||||
|
|
||||||
|
/// Specify the drive/partition to write the ISO to
|
||||||
|
#[arg(short, long)]
|
||||||
|
target: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
const VALID_OS_NAMES: &[&str] = &["Oreon"];
|
||||||
|
|
||||||
|
fn is_valid_os(os: &str) -> bool {
|
||||||
|
VALID_OS_NAMES.contains(&os)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_args(args: &Args) -> Result<(), String> {
|
||||||
|
if args.os.is_some() && args.file.is_some() {
|
||||||
|
return Err("OS and File arguments cannot be passed at the same time".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.file.is_some() && args.target.is_none() {
|
||||||
|
return Err("Drive argument is required when a file is chosen".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ref file) = args.file {
|
||||||
|
if !file.ends_with(".iso") {
|
||||||
|
return Err("Invalid file! Please use an ISO file".to_string());
|
||||||
|
}
|
||||||
|
if !std::path::Path::new(file).exists() {
|
||||||
|
return Err("The file does not exist!".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.os.is_none() && args.file.is_none() && args.target.is_none() {
|
||||||
|
return Err("Drive argument is required when no OS or file is chosen".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_interactive_mode() {
|
||||||
|
println!("Interactive mode is enabled.");
|
||||||
|
// TODO: Implement interactive mode
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_os_selection(os: &str) {
|
||||||
|
if is_valid_os(os) {
|
||||||
|
println!("You chose a valid OS! {}", os);
|
||||||
|
} else {
|
||||||
|
println!("You chose an invalid OS! Please use an ISO file instead or one of the following OS choices:");
|
||||||
|
println!("Valid OS choices: {:?}", VALID_OS_NAMES);
|
||||||
|
println!("More OS choices coming soon!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args = Args::parse();
|
||||||
|
|
||||||
|
if args.interactive {
|
||||||
|
handle_interactive_mode();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if std::env::args().len() <= 1 {
|
||||||
|
Args::command().print_help().unwrap();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(error) = validate_args(&args) {
|
||||||
|
println!("{}", error);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let (Some(ref file), Some(ref drive)) = (&args.file, &args.target) {
|
||||||
|
println!("File: {}", file);
|
||||||
|
println!("Drive: {}", drive);
|
||||||
|
|
||||||
|
if !args.dry_run {
|
||||||
|
match flasher::flash_iso(file, drive) {
|
||||||
|
Ok(_) => println!("ISO flashed successfully!"),
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error flashing ISO: {}", e);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Dry run: Would flash {} to {}", file, drive);
|
||||||
|
}
|
||||||
|
} else if let Some(ref os) = args.os {
|
||||||
|
handle_os_selection(os);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue