#[cfg(windows)]
extern crate winapi;
use std::ffi::CString;
use std::path::Path;
bitflags! {
pub struct Attributes: u32 {
const NORMAL = 128;
const HIDDEN = 2;
const READ_ONLY = 1;
}
}
const ATTRIBUTES: [Attributes; 3] = [Attributes::NORMAL, Attributes::HIDDEN, Attributes::READ_ONLY];
#[cfg(windows)]
pub fn set_attribute(file: &Path, attrib: Attributes) -> bool {
use self::winapi::um::fileapi::SetFileAttributesA;
unsafe {
let file_string: CString = CString::new(file.to_str().unwrap()).unwrap();
let success = SetFileAttributesA(file_string.as_ptr(), attrib.bits);
return success != 0;
}
}
#[cfg(windows)]
pub fn has_attribute(file: &Path, attrib: Attributes) -> bool {
use self::winapi::um::fileapi::GetFileAttributesA;
use self::winapi::um::fileapi::INVALID_FILE_ATTRIBUTES;
unsafe {
let file_string: CString = CString::new(file.to_str().unwrap()).unwrap();
let bits = GetFileAttributesA(file_string.as_ptr());
if bits == INVALID_FILE_ATTRIBUTES {
return false;
}
return bits & attrib.bits == attrib.bits;
}
}
#[cfg(windows)]
pub fn get_attributes(file: &Path) -> Result<Vec<Attributes>, String> {
use self::winapi::um::fileapi::GetFileAttributesA;
use self::winapi::um::fileapi::INVALID_FILE_ATTRIBUTES;
let mut output: Vec<Attributes> = Vec::new();
unsafe {
let file_string: CString = CString::new(file.to_str().unwrap()).unwrap();
let bits = GetFileAttributesA(file_string.as_ptr());
if bits == INVALID_FILE_ATTRIBUTES {
return Err(String::from("Windows.h Error: Invalid file attributes. Use obtain_errors() for a detailed error."));
}
for att in ATTRIBUTES.iter() {
if bits & att.bits == att.bits {
output.push(*att);
}
}
return Ok(output);
}
}
#[cfg(unix)]
pub fn set_attribute(file: &Path, attrib: Attributes) -> bool {
unimplemented!();
}
#[cfg(unix)]
pub fn has_attribute(file: &Path, attrib: Attributes) -> bool {
unimplemented!();
}
#[cfg(unix)]
pub fn get_attributes(file: &Path) -> Result<Vec<Attributes>, String> {
unimplemented!();
}