file.rs 2.1 KB

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