aboutsummaryrefslogtreecommitdiff
path: root/kernel/include/kernel/filesystem/open_file_description.hpp
blob: 738afd4b523da9d75c61d0659462b1755c9e8c4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef TEACH_OS_KERNEL_FILESYSTEM_OPEN_FILE_DESCRIPTION_HPP
#define TEACH_OS_KERNEL_FILESYSTEM_OPEN_FILE_DESCRIPTION_HPP

#include "kernel/filesystem/inode.hpp"

#include <kstd/memory>

#include <cstddef>

namespace kernel::filesystem
{
  /**
  @brief Represents an open file description in the filesystem. This class encapsulates the state of an open file,
  including a reference to the associated inode and the current file offset.
  */
  struct open_file_description
  {
    /**
    @brief Constructs an open file description for the given @p inode.
    @param inode The inode to associate with the open file description.
    */
    explicit open_file_description(kstd::shared_ptr<inode> const & inode);

    /**
    @brief Destructor for the open file description.
    */
    ~open_file_description() = default;

    /**
    @brief Reads data from the open file description into a @p buffer, starting at the current file offset and for a
    given
    @p size. The file offset is advanced by the number of bytes read.
    @param buffer The buffer to read data into.
    @param size The number of bytes to read.
    @return The number of bytes read.
    */
    auto read(void * buffer, size_t size) -> size_t;

    /**
    @brief Writes data to the open file description from a @p buffer, starting at the current file offset and for a
    given
    @p size. The file offset is advanced by the number of bytes written.
    @param buffer The buffer to write data from.
    @param size The number of bytes to write.
    @return The number of bytes written.
    */
    auto write(void const * buffer, size_t size) -> size_t;

    /**
    @brief Returns the current file offset for this open file description.
    @return The current file offset in bytes.
     */
    [[nodiscard]] auto offset() const -> size_t;

  private:
    kstd::shared_ptr<inode> m_inode;
    size_t m_offset;
  };

}  // namespace kernel::filesystem

#endif