diff options
Diffstat (limited to 'src/sys')
-rw-r--r-- | src/sys/mod.rs | 10 | ||||
-rw-r--r-- | src/sys/unix.rs | 34 |
2 files changed, 44 insertions, 0 deletions
diff --git a/src/sys/mod.rs b/src/sys/mod.rs new file mode 100644 index 0000000..a63f171 --- /dev/null +++ b/src/sys/mod.rs @@ -0,0 +1,10 @@ +#[cfg(unix)] +mod unix; +#[cfg(unix)] +pub use self::unix::*; + +pub enum PathWritability { + Writable, + NotWritable, + NotExist, +} diff --git a/src/sys/unix.rs b/src/sys/unix.rs new file mode 100644 index 0000000..0f433b3 --- /dev/null +++ b/src/sys/unix.rs @@ -0,0 +1,34 @@ +use users; +use std; + +use std::os::unix::fs::MetadataExt; +use std::os::unix::fs::PermissionsExt; + +pub fn path_writable(path: &std::path::Path) -> super::PathWritability { + std::fs::metadata(path) + .map(|stat| { + // XXX there really has to be a better option here + let euid = users::get_effective_uid(); + let egid = users::get_effective_gid(); + let file_uid = stat.uid(); + let file_gid = stat.gid(); + let file_mode = stat.permissions().mode(); + + if euid == 0 { + super::PathWritability::Writable + } + else if (file_uid == euid) && (file_mode & 0o200 != 0) { + super::PathWritability::Writable + } + else if (file_gid == egid) && (file_mode & 0o020 != 0) { + super::PathWritability::Writable + } + else if file_mode & 0o002 != 0 { + super::PathWritability::Writable + } + else { + super::PathWritability::NotWritable + } + }) + .unwrap_or(super::PathWritability::NotExist) +} |