InodeMetadata.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/Span.h>
  28. #include <Kernel/FileSystem/InodeIdentifier.h>
  29. #include <Kernel/KResult.h>
  30. #include <Kernel/UnixTypes.h>
  31. namespace Kernel {
  32. class Process;
  33. constexpr u32 encoded_device(unsigned major, unsigned minor)
  34. {
  35. return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
  36. }
  37. inline bool is_directory(mode_t mode) { return (mode & S_IFMT) == S_IFDIR; }
  38. inline bool is_character_device(mode_t mode) { return (mode & S_IFMT) == S_IFCHR; }
  39. inline bool is_block_device(mode_t mode) { return (mode & S_IFMT) == S_IFBLK; }
  40. inline bool is_regular_file(mode_t mode) { return (mode & S_IFMT) == S_IFREG; }
  41. inline bool is_fifo(mode_t mode) { return (mode & S_IFMT) == S_IFIFO; }
  42. inline bool is_symlink(mode_t mode) { return (mode & S_IFMT) == S_IFLNK; }
  43. inline bool is_socket(mode_t mode) { return (mode & S_IFMT) == S_IFSOCK; }
  44. inline bool is_sticky(mode_t mode) { return mode & S_ISVTX; }
  45. inline bool is_setuid(mode_t mode) { return mode & S_ISUID; }
  46. inline bool is_setgid(mode_t mode) { return mode & S_ISGID; }
  47. struct InodeMetadata {
  48. bool is_valid() const { return inode.is_valid(); }
  49. bool may_read(const Process&) const;
  50. bool may_write(const Process&) const;
  51. bool may_execute(const Process&) const;
  52. bool may_read(uid_t u, gid_t g, Span<const gid_t> eg) const
  53. {
  54. if (u == 0)
  55. return true;
  56. if (uid == u)
  57. return mode & S_IRUSR;
  58. if (gid == g || eg.contains_slow(gid))
  59. return mode & S_IRGRP;
  60. return mode & S_IROTH;
  61. }
  62. bool may_write(uid_t u, gid_t g, Span<const gid_t> eg) const
  63. {
  64. if (u == 0)
  65. return true;
  66. if (uid == u)
  67. return mode & S_IWUSR;
  68. if (gid == g || eg.contains_slow(gid))
  69. return mode & S_IWGRP;
  70. return mode & S_IWOTH;
  71. }
  72. bool may_execute(uid_t u, gid_t g, Span<const gid_t> eg) const
  73. {
  74. if (u == 0)
  75. return true;
  76. if (uid == u)
  77. return mode & S_IXUSR;
  78. if (gid == g || eg.contains_slow(gid))
  79. return mode & S_IXGRP;
  80. return mode & S_IXOTH;
  81. }
  82. bool is_directory() const { return Kernel::is_directory(mode); }
  83. bool is_character_device() const { return Kernel::is_character_device(mode); }
  84. bool is_block_device() const { return Kernel::is_block_device(mode); }
  85. bool is_device() const { return is_character_device() || is_block_device(); }
  86. bool is_regular_file() const { return Kernel::is_regular_file(mode); }
  87. bool is_fifo() const { return Kernel::is_fifo(mode); }
  88. bool is_symlink() const { return Kernel::is_symlink(mode); }
  89. bool is_socket() const { return Kernel::is_socket(mode); }
  90. bool is_sticky() const { return Kernel::is_sticky(mode); }
  91. bool is_setuid() const { return Kernel::is_setuid(mode); }
  92. bool is_setgid() const { return Kernel::is_setgid(mode); }
  93. KResult stat(stat& buffer) const
  94. {
  95. if (!is_valid())
  96. return KResult(-EIO);
  97. buffer.st_rdev = encoded_device(major_device, minor_device);
  98. buffer.st_ino = inode.index();
  99. buffer.st_mode = mode;
  100. buffer.st_nlink = link_count;
  101. buffer.st_uid = uid;
  102. buffer.st_gid = gid;
  103. buffer.st_dev = 0; // FIXME
  104. buffer.st_size = size;
  105. buffer.st_blksize = block_size;
  106. buffer.st_blocks = block_count;
  107. buffer.st_atime = atime;
  108. buffer.st_mtime = mtime;
  109. buffer.st_ctime = ctime;
  110. return KSuccess;
  111. }
  112. InodeIdentifier inode;
  113. off_t size { 0 };
  114. mode_t mode { 0 };
  115. uid_t uid { 0 };
  116. gid_t gid { 0 };
  117. nlink_t link_count { 0 };
  118. time_t atime { 0 };
  119. time_t ctime { 0 };
  120. time_t mtime { 0 };
  121. time_t dtime { 0 };
  122. blkcnt_t block_count { 0 };
  123. blksize_t block_size { 0 };
  124. unsigned major_device { 0 };
  125. unsigned minor_device { 0 };
  126. };
  127. }