file_mode_utils.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package dash
  2. // FormatFileMode converts file mode to Unix-style string representation (e.g., "drwxr-xr-x")
  3. // Handles both Go's os.ModeDir format and standard Unix file type bits
  4. func FormatFileMode(mode uint32) string {
  5. var result []byte = make([]byte, 10)
  6. // File type - handle Go's os.ModeDir first, then standard Unix file type bits
  7. if mode&0x80000000 != 0 { // Go's os.ModeDir (0x80000000 = 2147483648)
  8. result[0] = 'd'
  9. } else {
  10. switch mode & 0170000 { // S_IFMT mask
  11. case 0040000: // S_IFDIR
  12. result[0] = 'd'
  13. case 0100000: // S_IFREG
  14. result[0] = '-'
  15. case 0120000: // S_IFLNK
  16. result[0] = 'l'
  17. case 0020000: // S_IFCHR
  18. result[0] = 'c'
  19. case 0060000: // S_IFBLK
  20. result[0] = 'b'
  21. case 0010000: // S_IFIFO
  22. result[0] = 'p'
  23. case 0140000: // S_IFSOCK
  24. result[0] = 's'
  25. default:
  26. result[0] = '-' // S_IFREG is default
  27. }
  28. }
  29. // Permission bits (always use the lower 12 bits regardless of file type format)
  30. // Owner permissions
  31. if mode&0400 != 0 { // S_IRUSR
  32. result[1] = 'r'
  33. } else {
  34. result[1] = '-'
  35. }
  36. if mode&0200 != 0 { // S_IWUSR
  37. result[2] = 'w'
  38. } else {
  39. result[2] = '-'
  40. }
  41. if mode&0100 != 0 { // S_IXUSR
  42. result[3] = 'x'
  43. } else {
  44. result[3] = '-'
  45. }
  46. // Group permissions
  47. if mode&0040 != 0 { // S_IRGRP
  48. result[4] = 'r'
  49. } else {
  50. result[4] = '-'
  51. }
  52. if mode&0020 != 0 { // S_IWGRP
  53. result[5] = 'w'
  54. } else {
  55. result[5] = '-'
  56. }
  57. if mode&0010 != 0 { // S_IXGRP
  58. result[6] = 'x'
  59. } else {
  60. result[6] = '-'
  61. }
  62. // Other permissions
  63. if mode&0004 != 0 { // S_IROTH
  64. result[7] = 'r'
  65. } else {
  66. result[7] = '-'
  67. }
  68. if mode&0002 != 0 { // S_IWOTH
  69. result[8] = 'w'
  70. } else {
  71. result[8] = '-'
  72. }
  73. if mode&0001 != 0 { // S_IXOTH
  74. result[9] = 'x'
  75. } else {
  76. result[9] = '-'
  77. }
  78. return string(result)
  79. }