file.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. use super::Ext4MountPoint;
  2. use crate::prelude::*;
  3. /// 文件描述符
  4. pub struct Ext4File {
  5. /// 挂载点句柄
  6. pub mp: *mut Ext4MountPoint,
  7. /// 文件 inode id
  8. pub inode: u32,
  9. /// 打开标志
  10. pub flags: u32,
  11. /// 文件大小
  12. pub fsize: u64,
  13. /// 实际文件位置
  14. pub fpos: usize,
  15. }
  16. impl Default for Ext4File {
  17. fn default() -> Self {
  18. Self {
  19. mp: ptr::null_mut(),
  20. inode: 0,
  21. flags: 0,
  22. fsize: 0,
  23. fpos: 0,
  24. }
  25. }
  26. }
  27. bitflags! {
  28. pub struct OpenFlags: u32 {
  29. const O_ACCMODE = 0o0003;
  30. const O_RDONLY = 0o00;
  31. const O_WRONLY = 0o01;
  32. const O_RDWR = 0o02;
  33. const O_CREAT = 0o0100;
  34. const O_EXCL = 0o0200;
  35. const O_NOCTTY = 0o0400;
  36. const O_TRUNC = 0o01000;
  37. const O_APPEND = 0o02000;
  38. const O_NONBLOCK = 0o04000;
  39. const O_NDELAY = Self::O_NONBLOCK.bits();
  40. const O_SYNC = 0o4010000;
  41. const O_FSYNC = Self::O_SYNC.bits();
  42. const O_ASYNC = 0o020000;
  43. const O_LARGEFILE = 0o0100000;
  44. const O_DIRECTORY = 0o0200000;
  45. const O_NOFOLLOW = 0o0400000;
  46. const O_CLOEXEC = 0o2000000;
  47. const O_DIRECT = 0o040000;
  48. const O_NOATIME = 0o1000000;
  49. const O_PATH = 0o10000000;
  50. const O_DSYNC = 0o010000;
  51. const O_TMPFILE = 0o20000000 | Self::O_DIRECTORY.bits();
  52. }
  53. }
  54. impl OpenFlags {
  55. pub fn from_str(flags: &str) -> Result<Self> {
  56. match flags {
  57. "r" | "rb" => Ok(Self::O_RDONLY),
  58. "w" | "wb" => Ok(Self::O_WRONLY | Self::O_CREAT | Self::O_TRUNC),
  59. "a" | "ab" => Ok(Self::O_WRONLY | Self::O_CREAT | Self::O_APPEND),
  60. "r+" | "rb+" | "r+b" => Ok(Self::O_RDWR),
  61. "w+" | "wb+" | "w+b" => Ok(Self::O_RDWR | Self::O_CREAT | Self::O_TRUNC),
  62. "a+" | "ab+" | "a+b" => Ok(Self::O_RDWR | Self::O_CREAT | Self::O_APPEND),
  63. _ => Err(Ext4Error::new(ErrCode::EINVAL)),
  64. }
  65. }
  66. }
  67. #[derive(Copy, PartialEq, Eq, Clone, Debug)]
  68. #[allow(unused)]
  69. pub enum SeekFrom {
  70. Start(usize),
  71. End(isize),
  72. Current(isize),
  73. }