fs_types.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/fs.h>
  3. #include <linux/export.h>
  4. /*
  5. * fs on-disk file type to dirent file type conversion
  6. */
  7. static const unsigned char fs_dtype_by_ftype[FT_MAX] = {
  8. [FT_UNKNOWN] = DT_UNKNOWN,
  9. [FT_REG_FILE] = DT_REG,
  10. [FT_DIR] = DT_DIR,
  11. [FT_CHRDEV] = DT_CHR,
  12. [FT_BLKDEV] = DT_BLK,
  13. [FT_FIFO] = DT_FIFO,
  14. [FT_SOCK] = DT_SOCK,
  15. [FT_SYMLINK] = DT_LNK
  16. };
  17. /**
  18. * fs_ftype_to_dtype() - fs on-disk file type to dirent type.
  19. * @filetype: The on-disk file type to convert.
  20. *
  21. * This function converts the on-disk file type value (FT_*) to the directory
  22. * entry type (DT_*).
  23. *
  24. * Context: Any context.
  25. * Return:
  26. * * DT_UNKNOWN - Unknown type
  27. * * DT_FIFO - FIFO
  28. * * DT_CHR - Character device
  29. * * DT_DIR - Directory
  30. * * DT_BLK - Block device
  31. * * DT_REG - Regular file
  32. * * DT_LNK - Symbolic link
  33. * * DT_SOCK - Local-domain socket
  34. */
  35. unsigned char fs_ftype_to_dtype(unsigned int filetype)
  36. {
  37. if (filetype >= FT_MAX)
  38. return DT_UNKNOWN;
  39. return fs_dtype_by_ftype[filetype];
  40. }
  41. EXPORT_SYMBOL_NS_GPL(fs_ftype_to_dtype, ANDROID_GKI_VFS_EXPORT_ONLY);
  42. /*
  43. * dirent file type to fs on-disk file type conversion
  44. * Values not initialized explicitly are FT_UNKNOWN (0).
  45. */
  46. static const unsigned char fs_ftype_by_dtype[DT_MAX] = {
  47. [DT_REG] = FT_REG_FILE,
  48. [DT_DIR] = FT_DIR,
  49. [DT_LNK] = FT_SYMLINK,
  50. [DT_CHR] = FT_CHRDEV,
  51. [DT_BLK] = FT_BLKDEV,
  52. [DT_FIFO] = FT_FIFO,
  53. [DT_SOCK] = FT_SOCK,
  54. };
  55. /**
  56. * fs_umode_to_ftype() - file mode to on-disk file type.
  57. * @mode: The file mode to convert.
  58. *
  59. * This function converts the file mode value to the on-disk file type (FT_*).
  60. *
  61. * Context: Any context.
  62. * Return:
  63. * * FT_UNKNOWN - Unknown type
  64. * * FT_REG_FILE - Regular file
  65. * * FT_DIR - Directory
  66. * * FT_CHRDEV - Character device
  67. * * FT_BLKDEV - Block device
  68. * * FT_FIFO - FIFO
  69. * * FT_SOCK - Local-domain socket
  70. * * FT_SYMLINK - Symbolic link
  71. */
  72. unsigned char fs_umode_to_ftype(umode_t mode)
  73. {
  74. return fs_ftype_by_dtype[S_DT(mode)];
  75. }
  76. EXPORT_SYMBOL_GPL(fs_umode_to_ftype);
  77. /**
  78. * fs_umode_to_dtype() - file mode to dirent file type.
  79. * @mode: The file mode to convert.
  80. *
  81. * This function converts the file mode value to the directory
  82. * entry type (DT_*).
  83. *
  84. * Context: Any context.
  85. * Return:
  86. * * DT_UNKNOWN - Unknown type
  87. * * DT_FIFO - FIFO
  88. * * DT_CHR - Character device
  89. * * DT_DIR - Directory
  90. * * DT_BLK - Block device
  91. * * DT_REG - Regular file
  92. * * DT_LNK - Symbolic link
  93. * * DT_SOCK - Local-domain socket
  94. */
  95. unsigned char fs_umode_to_dtype(umode_t mode)
  96. {
  97. return fs_ftype_to_dtype(fs_umode_to_ftype(mode));
  98. }
  99. EXPORT_SYMBOL_GPL(fs_umode_to_dtype);