drone-fatfs 0.2.3

Bindings to ChaN's FatFs.
use core::mem::uninitialized;
use drone_core::ffi::CStr;
use drone_fatfs_raw::FILINFO;
use fat_attr::FatAttr;
use fat_time::FatTime;

/// Holds information about a file.
pub struct FileInfo {
  filinfo: Box<FILINFO>,
}

unsafe impl Send for FileInfo {}

impl FileInfo {
  pub(crate) unsafe fn new_uninitialized() -> Self {
    let filinfo = Box::new(uninitialized());
    Self { filinfo }
  }

  pub(crate) fn as_raw_mut(&mut self) -> *mut FILINFO {
    self.filinfo.as_mut()
  }

  /// Returns the file size.
  pub fn size(&self) -> u32 {
    self.filinfo.fsize
  }

  /// Returns the last modified time.
  pub fn time(&self) -> FatTime {
    FatTime::from(
      u32::from(self.filinfo.fdate) << 16 & u32::from(self.filinfo.ftime),
    )
  }

  /// Returns the file attributes.
  pub fn attr(&self) -> FatAttr {
    FatAttr::from(self.filinfo.fattrib)
  }

  /// Returns the primary file name.
  pub fn name(&self) -> &CStr {
    unsafe { CStr::from_bytes_with_nul_unchecked(&self.filinfo.fname) }
  }

  /// Returns the alternative file name.
  #[cfg(feature = "lfn")]
  pub fn alt_name(&self) -> &CStr {
    unsafe { CStr::from_bytes_with_nul_unchecked(&self.filinfo.altname) }
  }
}