umask.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //go:build !windows && !plan9 && !js
  2. package unix
  3. import (
  4. "fmt"
  5. "math"
  6. "strconv"
  7. "sync"
  8. "golang.org/x/sys/unix"
  9. "src.elv.sh/pkg/eval/errs"
  10. "src.elv.sh/pkg/eval/vals"
  11. "src.elv.sh/pkg/eval/vars"
  12. )
  13. const (
  14. validUmaskMsg = "integer in the range [0..0o777]"
  15. )
  16. //elvdoc:var umask
  17. //
  18. // The file mode creation mask. Its value is a string in Elvish octal
  19. // representation; e.g. 0o027. This makes it possible to use it in any context
  20. // that expects a `$number`.
  21. //
  22. // When assigning a new value a string is implicitly treated as an
  23. // octal number. If that fails the usual rules for interpreting
  24. // [numbers](./language.html#number) are used. The following are equivalent:
  25. // `set unix:umask = 027` and `set unix:umask = 0o27`. You can also assign to it
  26. // a `float64` data type that has no fractional component. The assigned value
  27. // must be within the range [0 ... 0o777], otherwise the assignment will throw
  28. // an exception.
  29. //
  30. // You can do a temporary assignment to affect a single command; e.g. `umask=077
  31. // touch a_file`. After the command completes the old umask will be restored.
  32. // **Warning**: Since the umask applies to the entire process, not individual
  33. // threads, changing it temporarily in this manner is dangerous if you are doing
  34. // anything in parallel, such as via the [`peach`](builtin.html#peach) command.
  35. // UmaskVariable is a variable whose value always reflects the current file
  36. // creation permission mask. Setting it changes the current file creation
  37. // permission mask for the process (not an individual thread).
  38. type UmaskVariable struct{}
  39. var _ vars.Var = UmaskVariable{}
  40. // Guard against concurrent fetch and assignment of $unix:umask. This assumes
  41. // no other part of the elvish code base will call unix.Umask() as it only
  42. // protects against races involving the aforementioned Elvish var.
  43. var umaskMutex sync.Mutex
  44. // Get returns the current file creation umask as a string.
  45. func (UmaskVariable) Get() interface{} {
  46. // Note: The seemingly redundant syscall is because the unix.Umask() API
  47. // doesn't allow querying the current value without changing it. So ensure
  48. // we reinstate the current value.
  49. umaskMutex.Lock()
  50. defer umaskMutex.Unlock()
  51. umask := unix.Umask(0)
  52. unix.Umask(umask)
  53. return fmt.Sprintf("0o%03o", umask)
  54. }
  55. // Set changes the current file creation umask. It can be called with a string
  56. // (the usual case) or a float64.
  57. func (UmaskVariable) Set(v interface{}) error {
  58. var umask int
  59. switch v := v.(type) {
  60. case string:
  61. i, err := strconv.ParseInt(v, 8, 0)
  62. if err != nil {
  63. i, err = strconv.ParseInt(v, 0, 0)
  64. if err != nil {
  65. return errs.BadValue{
  66. What: "umask", Valid: validUmaskMsg, Actual: vals.ToString(v)}
  67. }
  68. }
  69. umask = int(i)
  70. case int:
  71. // We don't bother supporting big.Int or bit.Rat because no valid umask value would be
  72. // represented by those types.
  73. umask = v
  74. case float64:
  75. intPart, fracPart := math.Modf(v)
  76. if fracPart != 0 {
  77. return errs.BadValue{
  78. What: "umask", Valid: validUmaskMsg, Actual: vals.ToString(v)}
  79. }
  80. umask = int(intPart)
  81. default:
  82. return errs.BadValue{
  83. What: "umask", Valid: validUmaskMsg, Actual: vals.Kind(v)}
  84. }
  85. if umask < 0 || umask > 0o777 {
  86. return errs.OutOfRange{
  87. What: "umask", ValidLow: "0", ValidHigh: "0o777",
  88. Actual: fmt.Sprintf("%O", umask)}
  89. }
  90. umaskMutex.Lock()
  91. defer umaskMutex.Unlock()
  92. unix.Umask(umask)
  93. return nil
  94. }