common.rs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. use another_ext4::{FileAttr as Ext4FileAttr, FileType as Ext4FileType, INODE_BLOCK_SIZE};
  2. use fuser::{FileAttr, FileType, TimeOrNow};
  3. use std::time::{Duration, SystemTime, UNIX_EPOCH};
  4. pub fn translate_ftype(file_type: Ext4FileType) -> FileType {
  5. match file_type {
  6. Ext4FileType::RegularFile => FileType::RegularFile,
  7. Ext4FileType::Directory => FileType::Directory,
  8. Ext4FileType::CharacterDev => FileType::CharDevice,
  9. Ext4FileType::BlockDev => FileType::BlockDevice,
  10. Ext4FileType::Fifo => FileType::NamedPipe,
  11. Ext4FileType::Socket => FileType::Socket,
  12. Ext4FileType::SymLink => FileType::Symlink,
  13. Ext4FileType::Unknown => FileType::RegularFile,
  14. }
  15. }
  16. pub fn translate_attr(attr: Ext4FileAttr) -> FileAttr {
  17. FileAttr {
  18. ino: attr.ino as u64,
  19. size: attr.size,
  20. blocks: attr.blocks,
  21. atime: second2sys_time(attr.atime),
  22. mtime: second2sys_time(attr.mtime),
  23. ctime: second2sys_time(attr.ctime),
  24. crtime: second2sys_time(attr.crtime),
  25. kind: translate_ftype(attr.ftype),
  26. perm: attr.perm.bits(),
  27. nlink: attr.links as u32,
  28. uid: attr.uid,
  29. gid: attr.gid,
  30. rdev: 0,
  31. blksize: INODE_BLOCK_SIZE as u32,
  32. flags: 0,
  33. }
  34. }
  35. pub fn sys_time2second(time: SystemTime) -> u32 {
  36. time.duration_since(UNIX_EPOCH).unwrap().as_secs() as u32
  37. }
  38. pub fn second2sys_time(time: u32) -> SystemTime {
  39. SystemTime::UNIX_EPOCH + Duration::from_secs(time as u64)
  40. }
  41. pub fn time_or_now2second(time_or_now: TimeOrNow) -> u32 {
  42. match time_or_now {
  43. fuser::TimeOrNow::Now => sys_time2second(SystemTime::now()),
  44. fuser::TimeOrNow::SpecificTime(time) => sys_time2second(time),
  45. }
  46. }