octal.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-FileCopyrightText: 2024 Christina Sørensen
  2. // SPDX-License-Identifier: EUPL-1.2
  3. //
  4. // SPDX-FileCopyrightText: 2023-2024 Christina Sørensen, eza contributors
  5. // SPDX-FileCopyrightText: 2014 Benjamin Sago
  6. // SPDX-License-Identifier: MIT
  7. use nu_ansi_term::Style;
  8. use crate::fs::fields as f;
  9. use crate::output::cell::TextCell;
  10. pub trait Render {
  11. fn render(&self, style: Style) -> TextCell;
  12. }
  13. impl Render for Option<f::OctalPermissions> {
  14. fn render(&self, style: Style) -> TextCell {
  15. match self {
  16. Some(p) => {
  17. let perm = &p.permissions;
  18. #[rustfmt::skip]
  19. let octal_sticky = f::OctalPermissions::bits_to_octal(
  20. perm.setuid,
  21. perm.setgid,
  22. perm.sticky
  23. );
  24. let octal_owner = f::OctalPermissions::bits_to_octal(
  25. perm.user_read,
  26. perm.user_write,
  27. perm.user_execute,
  28. );
  29. let octal_group = f::OctalPermissions::bits_to_octal(
  30. perm.group_read,
  31. perm.group_write,
  32. perm.group_execute,
  33. );
  34. let octal_other = f::OctalPermissions::bits_to_octal(
  35. perm.other_read,
  36. perm.other_write,
  37. perm.other_execute,
  38. );
  39. TextCell::paint(
  40. style,
  41. format!("{octal_sticky}{octal_owner}{octal_group}{octal_other}"),
  42. )
  43. }
  44. None => TextCell::paint(style, "----".into()),
  45. }
  46. }
  47. }
  48. impl f::OctalPermissions {
  49. fn bits_to_octal(r: bool, w: bool, x: bool) -> u8 {
  50. u8::from(r) * 4 + u8::from(w) * 2 + u8::from(x)
  51. }
  52. }
  53. #[cfg(test)]
  54. pub mod test {
  55. use super::Render;
  56. use crate::fs::fields as f;
  57. use crate::output::cell::TextCell;
  58. use nu_ansi_term::Color::*;
  59. #[test]
  60. fn normal_folder() {
  61. let bits = f::Permissions {
  62. user_read: true,
  63. user_write: true,
  64. user_execute: true,
  65. setuid: false,
  66. group_read: true,
  67. group_write: false,
  68. group_execute: true,
  69. setgid: false,
  70. other_read: true,
  71. other_write: false,
  72. other_execute: true,
  73. sticky: false,
  74. };
  75. let octal = Some(f::OctalPermissions { permissions: bits });
  76. let expected = TextCell::paint_str(Purple.bold(), "0755");
  77. assert_eq!(expected, octal.render(Purple.bold()));
  78. }
  79. #[test]
  80. fn normal_file() {
  81. let bits = f::Permissions {
  82. user_read: true,
  83. user_write: true,
  84. user_execute: false,
  85. setuid: false,
  86. group_read: true,
  87. group_write: false,
  88. group_execute: false,
  89. setgid: false,
  90. other_read: true,
  91. other_write: false,
  92. other_execute: false,
  93. sticky: false,
  94. };
  95. let octal = Some(f::OctalPermissions { permissions: bits });
  96. let expected = TextCell::paint_str(Purple.bold(), "0644");
  97. assert_eq!(expected, octal.render(Purple.bold()));
  98. }
  99. #[test]
  100. fn secret_file() {
  101. let bits = f::Permissions {
  102. user_read: true,
  103. user_write: true,
  104. user_execute: false,
  105. setuid: false,
  106. group_read: false,
  107. group_write: false,
  108. group_execute: false,
  109. setgid: false,
  110. other_read: false,
  111. other_write: false,
  112. other_execute: false,
  113. sticky: false,
  114. };
  115. let octal = Some(f::OctalPermissions { permissions: bits });
  116. let expected = TextCell::paint_str(Purple.bold(), "0600");
  117. assert_eq!(expected, octal.render(Purple.bold()));
  118. }
  119. #[test]
  120. fn sticky1() {
  121. let bits = f::Permissions {
  122. user_read: true,
  123. user_write: true,
  124. user_execute: true,
  125. setuid: true,
  126. group_read: true,
  127. group_write: true,
  128. group_execute: true,
  129. setgid: false,
  130. other_read: true,
  131. other_write: true,
  132. other_execute: true,
  133. sticky: false,
  134. };
  135. let octal = Some(f::OctalPermissions { permissions: bits });
  136. let expected = TextCell::paint_str(Purple.bold(), "4777");
  137. assert_eq!(expected, octal.render(Purple.bold()));
  138. }
  139. #[test]
  140. fn sticky2() {
  141. let bits = f::Permissions {
  142. user_read: true,
  143. user_write: true,
  144. user_execute: true,
  145. setuid: false,
  146. group_read: true,
  147. group_write: true,
  148. group_execute: true,
  149. setgid: true,
  150. other_read: true,
  151. other_write: true,
  152. other_execute: true,
  153. sticky: false,
  154. };
  155. let octal = Some(f::OctalPermissions { permissions: bits });
  156. let expected = TextCell::paint_str(Purple.bold(), "2777");
  157. assert_eq!(expected, octal.render(Purple.bold()));
  158. }
  159. #[test]
  160. fn sticky3() {
  161. let bits = f::Permissions {
  162. user_read: true,
  163. user_write: true,
  164. user_execute: true,
  165. setuid: false,
  166. group_read: true,
  167. group_write: true,
  168. group_execute: true,
  169. setgid: false,
  170. other_read: true,
  171. other_write: true,
  172. other_execute: true,
  173. sticky: true,
  174. };
  175. let octal = Some(f::OctalPermissions { permissions: bits });
  176. let expected = TextCell::paint_str(Purple.bold(), "1777");
  177. assert_eq!(expected, octal.render(Purple.bold()));
  178. }
  179. }