File.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/NonnullRefPtr.h>
  28. #include <AK/RefCounted.h>
  29. #include <AK/String.h>
  30. #include <AK/Types.h>
  31. #include <Kernel/Forward.h>
  32. #include <Kernel/KResult.h>
  33. #include <Kernel/UnixTypes.h>
  34. #include <LibBareMetal/Memory/VirtualAddress.h>
  35. namespace Kernel {
  36. // File is the base class for anything that can be referenced by a FileDescription.
  37. //
  38. // The most important functions in File are:
  39. //
  40. // read() and write()
  41. // - Implement reading and writing.
  42. // - Return the number of bytes read/written, OR a negative error code.
  43. //
  44. // can_read() and can_write()
  45. //
  46. // - Used to implement blocking I/O, and the select() and poll() syscalls.
  47. // - Return true if read() or write() would succeed, respectively.
  48. // - Note that can_read() should return true in EOF conditions,
  49. // and a subsequent call to read() should return 0.
  50. //
  51. // ioctl()
  52. //
  53. // - Optional. If unimplemented, ioctl() on this File will fail with -ENOTTY.
  54. // - Can be overridden in subclasses to implement arbitrary functionality.
  55. // - Subclasses should take care to validate incoming addresses before dereferencing.
  56. //
  57. // mmap()
  58. //
  59. // - Optional. If unimplemented, mmap() on this File will fail with -ENODEV.
  60. // - Called by mmap() when userspace wants to memory-map this File somewhere.
  61. // - Should create a Region in the Process and return it if successful.
  62. class File : public RefCounted<File> {
  63. public:
  64. virtual ~File();
  65. virtual KResultOr<NonnullRefPtr<FileDescription>> open(int options);
  66. virtual void close();
  67. virtual bool can_read(const FileDescription&) const = 0;
  68. virtual bool can_write(const FileDescription&) const = 0;
  69. virtual ssize_t read(FileDescription&, u8*, ssize_t) = 0;
  70. virtual ssize_t write(FileDescription&, const u8*, ssize_t) = 0;
  71. virtual int ioctl(FileDescription&, unsigned request, unsigned arg);
  72. virtual KResultOr<Region*> mmap(Process&, FileDescription&, VirtualAddress preferred_vaddr, size_t offset, size_t size, int prot, bool shared);
  73. virtual String absolute_path(const FileDescription&) const = 0;
  74. virtual KResult truncate(u64) { return KResult(-EINVAL); }
  75. virtual KResult chown(uid_t, gid_t) { return KResult(-EBADF); }
  76. virtual KResult chmod(mode_t) { return KResult(-EBADF); }
  77. virtual const char* class_name() const = 0;
  78. virtual bool is_seekable() const { return false; }
  79. virtual bool is_inode() const { return false; }
  80. virtual bool is_fifo() const { return false; }
  81. virtual bool is_device() const { return false; }
  82. virtual bool is_tty() const { return false; }
  83. virtual bool is_master_pty() const { return false; }
  84. virtual bool is_block_device() const { return false; }
  85. virtual bool is_character_device() const { return false; }
  86. virtual bool is_socket() const { return false; }
  87. protected:
  88. File();
  89. };
  90. }