file.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <sys/types.h>
  5. #include <types/types.h>
  6. #include <types/buffer.hpp>
  7. #include <kernel/async/waitlist.hpp>
  8. #include <kernel/async/lock.hpp>
  9. #include <kernel/vfs/dentry.hpp>
  10. namespace fs {
  11. class pipe : public types::non_copyable {
  12. private:
  13. static constexpr size_t PIPE_SIZE = 4096;
  14. static constexpr uint32_t READABLE = 1;
  15. static constexpr uint32_t WRITABLE = 2;
  16. private:
  17. types::buffer buf;
  18. uint32_t flags;
  19. kernel::async::mutex mtx;
  20. kernel::async::wait_list waitlist_r;
  21. kernel::async::wait_list waitlist_w;
  22. public:
  23. pipe();
  24. void close_read();
  25. void close_write();
  26. int write(const char* buf, size_t n);
  27. int read(char* buf, size_t n);
  28. constexpr bool is_readable() const
  29. {
  30. return flags & READABLE;
  31. }
  32. constexpr bool is_writeable() const
  33. {
  34. return flags & WRITABLE;
  35. }
  36. };
  37. struct file {
  38. mode_t mode; // stores the file type in the same format as inode::mode
  39. struct file_flags {
  40. uint32_t read : 1;
  41. uint32_t write : 1;
  42. uint32_t append : 1;
  43. } flags {};
  44. file(mode_t mode, file_flags flags)
  45. : mode(mode), flags(flags) { }
  46. virtual ~file() = default;
  47. virtual ssize_t read(char* __user buf, size_t n) = 0;
  48. virtual ssize_t do_write(const char* __user buf, size_t n) = 0;
  49. virtual off_t seek(off_t n, int whence)
  50. { return (void)n, (void)whence, -ESPIPE; }
  51. ssize_t write(const char* __user buf, size_t n)
  52. {
  53. if (!flags.write)
  54. return -EBADF;
  55. if (flags.append) {
  56. seek(0, SEEK_END);
  57. }
  58. return do_write(buf, n);
  59. }
  60. // regular files should override this method
  61. virtual int getdents(char* __user buf, size_t cnt)
  62. { return (void)buf, (void)cnt, -ENOTDIR; }
  63. virtual int getdents64(char* __user buf, size_t cnt)
  64. { return (void)buf, (void)cnt, -ENOTDIR; }
  65. };
  66. struct regular_file : public virtual file {
  67. virtual ~regular_file() = default;
  68. std::size_t cursor { };
  69. inode* ind { };
  70. regular_file(file_flags flags, size_t cursor, inode* ind);
  71. virtual ssize_t read(char* __user buf, size_t n) override;
  72. virtual ssize_t do_write(const char* __user buf, size_t n) override;
  73. virtual off_t seek(off_t n, int whence) override;
  74. virtual int getdents(char* __user buf, size_t cnt) override;
  75. virtual int getdents64(char* __user buf, size_t cnt) override;
  76. };
  77. struct fifo_file : public virtual file {
  78. virtual ~fifo_file() override;
  79. std::shared_ptr<pipe> ppipe;
  80. fifo_file(file_flags flags, std::shared_ptr<fs::pipe> ppipe);
  81. virtual ssize_t read(char* __user buf, size_t n) override;
  82. virtual ssize_t do_write(const char* __user buf, size_t n) override;
  83. };
  84. } // namespace fs