Skip to content

Commit

Permalink
[Feature] add umask option
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatson committed Aug 26, 2024
1 parent 113b048 commit 016d759
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/rqbit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ size_format = "1"
bytes = "1.5.0"
openssl = { version = "0.10", features = ["vendored"], optional = true }
upnp-serve = { path = "../upnp-serve" }
libc = "0.2.158"

[dev-dependencies]
futures = { version = "0.3" }
59 changes: 59 additions & 0 deletions crates/rqbit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,31 @@ enum LogLevel {
Error,
}

fn parse_umask(value: &str) -> anyhow::Result<libc::mode_t> {
fn parse_oct_digit(d: u8) -> Option<libc::mode_t> {
Some(match d {
b'0' => 0,
b'1' => 1,
b'2' => 2,
b'3' => 3,
b'4' => 4,
b'5' => 5,
b'6' => 6,
b'7' => 7,
_ => return None,
})
}
if value.len() != 3 {
bail!("expected 3 digits")
}
let mut output = 0;
for digit in value.as_bytes() {
let digit = parse_oct_digit(*digit).context("expected 3 digits")?;
output = output * 8 + digit;
}
Ok(output)
}

#[derive(Parser)]
#[command(version, author, about)]
struct Opts {
Expand Down Expand Up @@ -162,6 +187,15 @@ struct Opts {
/// How many torrents can be initializing (rehashing) at the same time
#[arg(long, default_value = "5", env = "RQBIT_CONCURRENT_INIT_LIMIT")]
concurrent_init_limit: usize,

/// Set the process umask to this value.
///
/// Default is inherited from your environment (usually 022).
/// This will affect the file mode of created files.
///
/// Read more at https://man7.org/linux/man-pages/man2/umask.2.html
#[arg(long, env = "RQBIT_UMASK", value_parser=parse_umask)]
umask: Option<libc::mode_t>,
}

#[derive(Parser)]
Expand Down Expand Up @@ -294,6 +328,10 @@ fn _start_deadlock_detector_thread() {
fn main() -> anyhow::Result<()> {
let opts = Opts::parse();

if let Some(umask) = opts.umask {
unsafe { libc::umask(umask) };
}

let mut rt_builder = match opts.single_thread_runtime {
true => tokio::runtime::Builder::new_current_thread(),
false => {
Expand Down Expand Up @@ -714,3 +752,24 @@ async fn async_main(opts: Opts) -> anyhow::Result<()> {
}
}
}

#[cfg(test)]
mod tests {
use crate::parse_umask;

#[test]
fn test_parse_umask() {
let range = b'0'..=b'7';
for d0 in range.clone() {
for d1 in range.clone() {
for d2 in range.clone() {
let inp = [d0, d1, d2];
let inp_str = std::str::from_utf8(&inp).unwrap();
let parsed = parse_umask(inp_str).expect(inp_str);
let expected = format!("{parsed:03o}");
assert_eq!(inp_str, expected);
}
}
}
}
}

0 comments on commit 016d759

Please sign in to comment.