util.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. package notify
  5. import (
  6. "errors"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. )
  11. const all = ^Event(0)
  12. const sep = string(os.PathSeparator)
  13. var errDepth = errors.New("exceeded allowed iteration count (circular symlink?)")
  14. func min(i, j int) int {
  15. if i > j {
  16. return j
  17. }
  18. return i
  19. }
  20. func max(i, j int) int {
  21. if i < j {
  22. return j
  23. }
  24. return i
  25. }
  26. // must panics if err is non-nil.
  27. func must(err error) {
  28. if err != nil {
  29. panic(err)
  30. }
  31. }
  32. // nonil gives first non-nil error from the given arguments.
  33. func nonil(err ...error) error {
  34. for _, err := range err {
  35. if err != nil {
  36. return err
  37. }
  38. }
  39. return nil
  40. }
  41. func cleanpath(path string) (realpath string, isrec bool, err error) {
  42. if strings.HasSuffix(path, "...") {
  43. isrec = true
  44. path = path[:len(path)-3]
  45. }
  46. if path, err = filepath.Abs(path); err != nil {
  47. return "", false, err
  48. }
  49. if path, err = canonical(path); err != nil {
  50. return "", false, err
  51. }
  52. return path, isrec, nil
  53. }
  54. // canonical resolves any symlink in the given path and returns it in a clean form.
  55. // It expects the path to be absolute. It fails to resolve circular symlinks by
  56. // maintaining a simple iteration limit.
  57. func canonical(p string) (string, error) {
  58. p, err := filepath.Abs(p)
  59. if err != nil {
  60. return "", err
  61. }
  62. for i, j, depth := 1, 0, 1; i < len(p); i, depth = i+1, depth+1 {
  63. if depth > 128 {
  64. return "", &os.PathError{Op: "canonical", Path: p, Err: errDepth}
  65. }
  66. if j = strings.IndexRune(p[i:], '/'); j == -1 {
  67. j, i = i, len(p)
  68. } else {
  69. j, i = i, i+j
  70. }
  71. fi, err := os.Lstat(p[:i])
  72. if err != nil {
  73. return "", err
  74. }
  75. if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
  76. s, err := os.Readlink(p[:i])
  77. if err != nil {
  78. return "", err
  79. }
  80. if filepath.IsAbs(s) {
  81. p = "/" + s + p[i:]
  82. } else {
  83. p = p[:j] + s + p[i:]
  84. }
  85. i = 1 // no guarantee s is canonical, start all over
  86. }
  87. }
  88. return filepath.Clean(p), nil
  89. }
  90. func joinevents(events []Event) (e Event) {
  91. if len(events) == 0 {
  92. e = All
  93. } else {
  94. for _, event := range events {
  95. e |= event
  96. }
  97. }
  98. return
  99. }
  100. func split(s string) (string, string) {
  101. if i := lastIndexSep(s); i != -1 {
  102. return s[:i], s[i+1:]
  103. }
  104. return "", s
  105. }
  106. func base(s string) string {
  107. if i := lastIndexSep(s); i != -1 {
  108. return s[i+1:]
  109. }
  110. return s
  111. }
  112. func indexbase(root, name string) int {
  113. if n, m := len(root), len(name); m >= n && name[:n] == root &&
  114. (n == m || name[n] == os.PathSeparator) {
  115. return min(n+1, m)
  116. }
  117. return -1
  118. }
  119. func indexSep(s string) int {
  120. for i := 0; i < len(s); i++ {
  121. if s[i] == os.PathSeparator {
  122. return i
  123. }
  124. }
  125. return -1
  126. }
  127. func lastIndexSep(s string) int {
  128. for i := len(s) - 1; i >= 0; i-- {
  129. if s[i] == os.PathSeparator {
  130. return i
  131. }
  132. }
  133. return -1
  134. }