|
| 1 | +// |
| 2 | +// Copyright (c) 2024 Hemi Labs, Inc. |
| 3 | +// |
| 4 | +// This file is part of the posixutils-rs project covered under |
| 5 | +// the MIT License. For the full license text, please see the LICENSE |
| 6 | +// file in the root directory of this project. |
| 7 | +// SPDX-License-Identifier: MIT |
| 8 | +// |
| 9 | + |
| 10 | +use clap::Parser; |
| 11 | + |
| 12 | +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; |
| 13 | +use std::env; |
| 14 | +use std::fs; |
| 15 | +use std::fs::File; |
| 16 | +use std::io::{ErrorKind, Read, Result, Write}; |
| 17 | +use std::process::{exit, ExitStatus}; |
| 18 | + |
| 19 | +#[derive(Parser)] |
| 20 | +struct CronArgs { |
| 21 | + #[arg(short, long, help = gettext("edit user's crontab"))] |
| 22 | + edit: bool, |
| 23 | + #[arg(short, long, help = gettext("list user's crontab"))] |
| 24 | + list: bool, |
| 25 | + #[arg(short, long, help = gettext("delete user's crontab"))] |
| 26 | + remove: bool, |
| 27 | + #[arg(name = "FILE", help = gettext("file to replace user's current crontab with"))] |
| 28 | + file: Option<String>, |
| 29 | +} |
| 30 | + |
| 31 | +fn print_usage(err: &str) { |
| 32 | + let name = env::args().next().unwrap(); |
| 33 | + eprintln!("{name}: usage error: {err}"); |
| 34 | + eprintln!("usage:\t{name} [ FILE ]"); |
| 35 | + eprintln!("\t{name} [ -e | -l | -r ]"); |
| 36 | + eprintln!("\t-e\t- edit user's crontab"); |
| 37 | + eprintln!("\t-l\t- list user's crontab"); |
| 38 | + eprintln!("\t-r\t- delete user's crontab"); |
| 39 | +} |
| 40 | + |
| 41 | +fn list_crontab(path: &str) -> Result<String> { |
| 42 | + fs::read_to_string(path) |
| 43 | +} |
| 44 | + |
| 45 | +fn remove_crontab(path: &str) -> Result<()> { |
| 46 | + fs::remove_file(path) |
| 47 | +} |
| 48 | + |
| 49 | +fn edit_crontab(path: &str) -> Result<ExitStatus> { |
| 50 | + File::create(path)?; |
| 51 | + let editor = env::var("EDITOR").unwrap_or("vi".to_string()); |
| 52 | + let shell = env::var("SHELL").unwrap_or("sh".to_string()); |
| 53 | + let args = ["-c".to_string(), format!("{editor} {path}")]; |
| 54 | + std::process::Command::new(shell).args(args).status() |
| 55 | +} |
| 56 | + |
| 57 | +fn replace_crontab(from: &str, to: &str) -> Result<()> { |
| 58 | + let mut source = File::open(from)?; |
| 59 | + let mut target = File::create(to)?; |
| 60 | + let mut buffer = Vec::new(); |
| 61 | + |
| 62 | + source.read_to_end(&mut buffer)?; |
| 63 | + target.write_all(&buffer)?; |
| 64 | + |
| 65 | + Ok(()) |
| 66 | +} |
| 67 | + |
| 68 | +fn main() -> std::result::Result<(), Box<dyn std::error::Error>> { |
| 69 | + setlocale(LocaleCategory::LcAll, ""); |
| 70 | + textdomain("posixutils-rs")?; |
| 71 | + bind_textdomain_codeset("posixutils-rs", "UTF-8")?; |
| 72 | + |
| 73 | + let args = CronArgs::parse(); |
| 74 | + let Ok(logname) = env::var("LOGNAME") else { |
| 75 | + println!("Could not obtain the user's logname."); |
| 76 | + exit(1); |
| 77 | + }; |
| 78 | + #[cfg(target_os = "linux")] |
| 79 | + let path = format!("/var/spool/cron/{logname}"); |
| 80 | + #[cfg(target_os = "macos")] |
| 81 | + let path = format!("/var/spool/cron/{logname}"); |
| 82 | + |
| 83 | + let opt_count = [args.edit, args.list, args.remove, args.file.is_some()] |
| 84 | + .into_iter() |
| 85 | + .map(|x| x as i32) |
| 86 | + .sum::<i32>(); |
| 87 | + if opt_count > 1 { |
| 88 | + print_usage("Too many options specified."); |
| 89 | + exit(1); |
| 90 | + } |
| 91 | + |
| 92 | + if opt_count < 1 { |
| 93 | + print_usage("Not enough options specified."); |
| 94 | + exit(1); |
| 95 | + } |
| 96 | + |
| 97 | + if args.edit { |
| 98 | + match edit_crontab(&path) { |
| 99 | + Ok(status) => exit(status.code().unwrap_or(0)), |
| 100 | + Err(err) => { |
| 101 | + match err.kind() { |
| 102 | + ErrorKind::NotFound => println!("No crontab file has been found."), |
| 103 | + ErrorKind::PermissionDenied => { |
| 104 | + println!("Permission to access user's crontab file denied.") |
| 105 | + } |
| 106 | + ErrorKind::Interrupted => println!("crontab was interrupted."), |
| 107 | + ErrorKind::OutOfMemory => println!("crontab exceeded available memory."), |
| 108 | + _ => println!("Unknown error: {}", err), |
| 109 | + } |
| 110 | + exit(1); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if args.list { |
| 116 | + match list_crontab(&path) { |
| 117 | + Ok(content) => println!("{}", content), |
| 118 | + Err(err) => { |
| 119 | + match err.kind() { |
| 120 | + ErrorKind::NotFound => println!("No crontab file has been found."), |
| 121 | + ErrorKind::PermissionDenied => { |
| 122 | + println!("Permission to access user's crontab file denied.") |
| 123 | + } |
| 124 | + ErrorKind::Interrupted => println!("crontab was interrupted."), |
| 125 | + ErrorKind::OutOfMemory => println!("crontab exceeded available memory."), |
| 126 | + _ => println!("Unknown error: {}", err), |
| 127 | + } |
| 128 | + exit(1); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if args.remove { |
| 134 | + match remove_crontab(&path) { |
| 135 | + Ok(()) => println!("Removed crontab file"), |
| 136 | + Err(err) => { |
| 137 | + match err.kind() { |
| 138 | + ErrorKind::NotFound => println!("No crontab file has been found."), |
| 139 | + ErrorKind::PermissionDenied => { |
| 140 | + println!("Permission to access user's crontab file denied.") |
| 141 | + } |
| 142 | + ErrorKind::Interrupted => println!("crontab was interrupted."), |
| 143 | + ErrorKind::OutOfMemory => println!("crontab exceeded available memory."), |
| 144 | + _ => println!("Unknown error: {}", err), |
| 145 | + } |
| 146 | + exit(1); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if let Some(file) = args.file { |
| 152 | + match replace_crontab(&file, &path) { |
| 153 | + Ok(()) => println!("Replaced crontab file with {file}"), |
| 154 | + Err(err) => { |
| 155 | + match err.kind() { |
| 156 | + ErrorKind::NotFound => { |
| 157 | + println!("Crontab file or user-specified file has not been found.") |
| 158 | + } |
| 159 | + ErrorKind::PermissionDenied => println!( |
| 160 | + "Permission to access user's crontab file or user-specified file denied." |
| 161 | + ), |
| 162 | + ErrorKind::Interrupted => println!("crontab was interrupted."), |
| 163 | + ErrorKind::OutOfMemory => println!("crontab exceeded available memory."), |
| 164 | + _ => println!("Unknown error: {}", err), |
| 165 | + } |
| 166 | + exit(1); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + Ok(()) |
| 172 | +} |
0 commit comments