DirectoryEntry.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "DirectoryEntry.h"
  7. #include <sys/stat.h>
  8. namespace Core {
  9. static DirectoryEntry::Type directory_entry_type_from_stat(mode_t st_mode)
  10. {
  11. switch (st_mode) {
  12. case S_IFIFO:
  13. return DirectoryEntry::Type::NamedPipe;
  14. case S_IFCHR:
  15. return DirectoryEntry::Type::CharacterDevice;
  16. case S_IFDIR:
  17. return DirectoryEntry::Type::Directory;
  18. case S_IFBLK:
  19. return DirectoryEntry::Type::BlockDevice;
  20. case S_IFREG:
  21. return DirectoryEntry::Type::File;
  22. case S_IFLNK:
  23. return DirectoryEntry::Type::SymbolicLink;
  24. case S_IFSOCK:
  25. return DirectoryEntry::Type::Socket;
  26. default:
  27. return DirectoryEntry::Type::Unknown;
  28. }
  29. VERIFY_NOT_REACHED();
  30. }
  31. #ifndef AK_OS_SOLARIS
  32. static DirectoryEntry::Type directory_entry_type_from_posix(unsigned char dt_constant)
  33. {
  34. switch (dt_constant) {
  35. case DT_UNKNOWN:
  36. return DirectoryEntry::Type::Unknown;
  37. case DT_FIFO:
  38. return DirectoryEntry::Type::NamedPipe;
  39. case DT_CHR:
  40. return DirectoryEntry::Type::CharacterDevice;
  41. case DT_DIR:
  42. return DirectoryEntry::Type::Directory;
  43. case DT_BLK:
  44. return DirectoryEntry::Type::BlockDevice;
  45. case DT_REG:
  46. return DirectoryEntry::Type::File;
  47. case DT_LNK:
  48. return DirectoryEntry::Type::SymbolicLink;
  49. case DT_SOCK:
  50. return DirectoryEntry::Type::Socket;
  51. # ifndef AK_OS_OPENBSD
  52. case DT_WHT:
  53. return DirectoryEntry::Type::Whiteout;
  54. # endif
  55. }
  56. VERIFY_NOT_REACHED();
  57. }
  58. #endif
  59. DirectoryEntry DirectoryEntry::from_stat(DIR* d, dirent const& de)
  60. {
  61. struct stat statbuf;
  62. fstat(dirfd(d), &statbuf);
  63. return DirectoryEntry {
  64. .type = directory_entry_type_from_stat(statbuf.st_mode),
  65. .name = de.d_name,
  66. };
  67. }
  68. #ifndef AK_OS_SOLARIS
  69. DirectoryEntry DirectoryEntry::from_dirent(dirent const& de)
  70. {
  71. return DirectoryEntry {
  72. .type = directory_entry_type_from_posix(de.d_type),
  73. .name = de.d_name,
  74. };
  75. }
  76. #endif
  77. }