file.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. constexpr bool is_free() const
  37. {
  38. return !(flags & (READABLE | WRITABLE));
  39. }
  40. };
  41. struct file {
  42. mode_t mode; // stores the file type in the same format as inode::mode
  43. dentry* parent {};
  44. struct file_flags {
  45. uint32_t read : 1;
  46. uint32_t write : 1;
  47. uint32_t append : 1;
  48. } flags {};
  49. file(mode_t mode, dentry* parent, file_flags flags)
  50. : mode(mode) , parent(parent), flags(flags) { }
  51. virtual ~file() = default;
  52. virtual ssize_t read(char* __user buf, size_t n) = 0;
  53. virtual ssize_t do_write(const char* __user buf, size_t n) = 0;
  54. virtual off_t seek(off_t n, int whence)
  55. { return (void)n, (void)whence, -ESPIPE; }
  56. ssize_t write(const char* __user buf, size_t n)
  57. {
  58. if (!flags.write)
  59. return -EBADF;
  60. if (flags.append) {
  61. seek(0, SEEK_END);
  62. }
  63. return do_write(buf, n);
  64. }
  65. // regular files should override this method
  66. virtual int getdents(char* __user buf, size_t cnt)
  67. { return (void)buf, (void)cnt, -ENOTDIR; }
  68. virtual int getdents64(char* __user buf, size_t cnt)
  69. { return (void)buf, (void)cnt, -ENOTDIR; }
  70. };
  71. struct regular_file : public virtual file {
  72. virtual ~regular_file() = default;
  73. std::size_t cursor { };
  74. inode* ind { };
  75. regular_file(dentry* parent, file_flags flags, size_t cursor, inode* ind);
  76. virtual ssize_t read(char* __user buf, size_t n) override;
  77. virtual ssize_t do_write(const char* __user buf, size_t n) override;
  78. virtual off_t seek(off_t n, int whence) override;
  79. virtual int getdents(char* __user buf, size_t cnt) override;
  80. virtual int getdents64(char* __user buf, size_t cnt) override;
  81. };
  82. struct fifo_file : public virtual file {
  83. virtual ~fifo_file() override;
  84. std::shared_ptr<pipe> ppipe;
  85. fifo_file(dentry* parent, file_flags flags, std::shared_ptr<fs::pipe> ppipe);
  86. virtual ssize_t read(char* __user buf, size_t n) override;
  87. virtual ssize_t do_write(const char* __user buf, size_t n) override;
  88. };
  89. } // namespace fs