Commit b655ee0c authored by Nathaniel Barragan's avatar Nathaniel Barragan

Added file permission error

parent 24040462
......@@ -20,6 +20,7 @@ use std::{
cell::{Cell, RefCell},
fs::{self, OpenOptions},
io::{self, Write},
os::unix::fs::PermissionsExt,
os::unix::io::{AsRawFd, IntoRawFd},
path::Path,
rc::Rc,
......@@ -41,7 +42,6 @@ FLAGS:
-n, --no-execute Do not execute any commands, perform only syntax checking
-x Print commands before execution
-v, --version Print the version, platform and revision of Ion then exit
OPTIONS:
-c <command> Evaluate given commands instead of reading from the commandline
-o <key-bindings> Shortcut layout. Valid options: "vi", "emacs"
......@@ -313,7 +313,17 @@ impl<'a> InteractiveShell<'a> {
.ok()
.map_or(false, |res| res.is_failure())
{
eprintln!("ion: {}", err);
match err.kind() {
io::ErrorKind::PermissionDenied => {
// Check the permissions on the command
let cmd_permissions = fs::metadata(command[0].as_str()).unwrap().permissions();
// Let's get the unix permissions for this file
let unix_perms = UnixPermissions::from(cmd_permissions.mode() as u16);
// Now we get the mode string
eprintln!("Could not execute command {}\nThe current permissions of this command are: {}.\n\nTry running chmod +x [FILE]", command[0], unix_perms.to_string());
},
_ => eprintln!("ion: Error executing command: {}", err),
}
shell.reset_flow();
}
}
......@@ -375,6 +385,82 @@ impl<'a> InteractiveShell<'a> {
}
}
pub struct UnixMode {
pub write: bool,
pub read: bool,
pub execute: bool,
}
impl UnixMode {
pub fn from_mask(mode: u16, read_mask: u16, write_mask: u16, execute_mask: u16) -> Self {
let read = match mode & read_mask {
0 => false,
_ => true,
};
let write = match mode & write_mask {
0 => false,
_ => true,
};
let execute = match mode &execute_mask {
0 => false,
_ => true,
};
Self {
write, read, execute,
}
}
}
impl ToString for UnixMode {
fn to_string(&self) -> String {
match (self.read, self.write, self.execute) {
(false, false, false) => "---",
(true, false, false) => "r--",
(false, true, false) => "-w-",
(false, false, true) => "--x",
(true, true, false) => "rw-",
(true, false, true) => "r-x",
(false, true, true) => "-wx",
(true, true, true) => "rwx",
}.to_string()
}
}
// A structure which stores Unix Permissions.
pub struct UnixPermissions {
user_mode: UnixMode,
group_mode: UnixMode,
other_mode: UnixMode,
}
impl ToString for UnixPermissions {
fn to_string(&self) -> String {
[self.user_mode.to_string(), self.group_mode.to_string(), self.other_mode.to_string()].join("")
}
}
impl From<u16> for UnixPermissions {
fn from(mode: u16) -> Self {
Self {
user_mode: UnixMode::from_mask(mode, UnixPermissions::S_IRUSR, UnixPermissions::S_IWUSR, UnixPermissions::S_IXUSR),
group_mode: UnixMode::from_mask(mode, UnixPermissions::S_IRGRP, UnixPermissions::S_IWGRP, UnixPermissions::S_IXGRP),
other_mode: UnixMode::from_mask(mode, UnixPermissions::S_IROTH, UnixPermissions::S_IWOTH, UnixPermissions::S_IXOTH),
}
}
}
impl UnixPermissions {
const S_IXUSR: u16 = 64;
const S_IWUSR: u16 = 128;
const S_IRUSR: u16 = 256;
const S_IXGRP: u16 = 8;
const S_IWGRP: u16 = 16;
const S_IRGRP: u16 = 32;
const S_IXOTH: u16 = 1;
const S_IWOTH: u16 = 2;
const S_IROTH: u16 = 4;
}
#[derive(Debug)]
struct WordDivide<I>
where
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment