FilePermissions.hx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package asys;
  2. /**
  3. File permissions in specify whether a file can be read, written, or executed
  4. by its owner, its owning group, and everyone else. Instances of this type
  5. can be constructed by combining individual file permissions with the `|`
  6. operator:
  7. ```haxe
  8. ReadOwner | WriteOwner | ReadGroup | ReadOthers
  9. ```
  10. Alternatively, file permissions may be specified as a string with exactly 9
  11. characters, in the format `rwxrwxrwx`, where each letter may instead be a
  12. `-` character. The first three characters represent the permissions of the
  13. owner, the second three characters represent the permissions of the owning
  14. group, and the last three characters represent the permissions of everyone
  15. else.
  16. ```haxe
  17. "rw-r--r--"
  18. ```
  19. Finally, file permissions may be constructed from an octal representation
  20. using the `fromOctal` function.
  21. ```haxe
  22. FilePermissions.fromOctal("644")
  23. ```
  24. **/
  25. enum abstract FilePermissions(Int) {
  26. @:from public static function fromString(s:String):FilePermissions {
  27. inline function bit(cc:Int, expect:Int):Int {
  28. return (if (cc == expect)
  29. 1;
  30. else if (cc == "-".code)
  31. 0;
  32. else
  33. throw "invalid file permissions string");
  34. }
  35. switch (s.length) {
  36. case 9: // rwxrwxrwx
  37. return new FilePermissions(bit(s.charCodeAt(0), "r".code) << 8
  38. | bit(s.charCodeAt(1), "w".code) << 7
  39. | bit(s.charCodeAt(2), "x".code) << 6
  40. | bit(s.charCodeAt(3), "r".code) << 5
  41. | bit(s.charCodeAt(4), "w".code) << 4
  42. | bit(s.charCodeAt(5), "x".code) << 3
  43. | bit(s.charCodeAt(6), "r".code) << 2
  44. | bit(s.charCodeAt(7), "w".code) << 1
  45. | bit(s.charCodeAt(8), "x".code));
  46. case _:
  47. throw "invalid file permissions string";
  48. }
  49. }
  50. public static function fromOctal(s:String):FilePermissions {
  51. inline function digit(n:Int):Int {
  52. if (n >= "0".code && n <= "7".code) return n - "0".code;
  53. throw "invalid octal file permissions";
  54. }
  55. switch (s.length) {
  56. case 3: // 777
  57. return new FilePermissions(digit(s.charCodeAt(0)) << 6
  58. | digit(s.charCodeAt(1)) << 3
  59. | digit(s.charCodeAt(2)));
  60. case _:
  61. throw "invalid octal file permissions";
  62. }
  63. }
  64. var None = 0;
  65. var ExecuteOthers = 1 << 0;
  66. var WriteOthers = 1 << 1;
  67. var ReadOthers = 1 << 2;
  68. var ExecuteGroup = 1 << 3;
  69. var WriteGroup = 1 << 4;
  70. var ReadGroup = 1 << 5;
  71. var ExecuteOwner = 1 << 6;
  72. var WriteOwner = 1 << 7;
  73. var ReadOwner = 1 << 8;
  74. inline function new(value:Int)
  75. this = value;
  76. inline function get_raw():Int return this;
  77. @:op(A | B)
  78. inline function join(other:FilePermissions) return new FilePermissions(this | other.get_raw());
  79. }