http urls monitor.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright © 2014 Steve Francia <spf@spf13.com>.
  2. // Copyright 2009 The Go Authors. All rights reserved.
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package afero
  14. import (
  15. "path/filepath"
  16. "sort"
  17. "strings"
  18. )
  19. // Glob returns the names of all files matching pattern or nil
  20. // if there is no matching file. The syntax of patterns is the same
  21. // as in Match. The pattern may describe hierarchical names such as
  22. // /usr/*/bin/ed (assuming the Separator is '/').
  23. //
  24. // Glob ignores file system errors such as I/O errors reading directories.
  25. // The only possible returned error is ErrBadPattern, when pattern
  26. // is malformed.
  27. //
  28. // This was adapted from (http://golang.org/pkg/path/filepath) and uses several
  29. // built-ins from that package.
  30. func Glob(fs Fs, pattern string) (matches []string, err error) {
  31. if !hasMeta(pattern) {
  32. // Lstat not supported by a ll filesystems.
  33. if _, err = lstatIfPossible(fs, pattern); err != nil {
  34. return nil, nil
  35. }
  36. return []string{pattern}, nil
  37. }
  38. dir, file := filepath.Split(pattern)
  39. switch dir {
  40. case "":
  41. dir = "."
  42. case string(filepath.Separator):
  43. // nothing
  44. default:
  45. dir = dir[0 : len(dir)-1] // chop off trailing separator
  46. }
  47. if !hasMeta(dir) {
  48. return glob(fs, dir, file, nil)
  49. }
  50. var m []string
  51. m, err = Glob(fs, dir)
  52. if err != nil {
  53. return
  54. }
  55. for _, d := range m {
  56. matches, err = glob(fs, d, file, matches)
  57. if err != nil {
  58. return
  59. }
  60. }
  61. return
  62. }
  63. // glob searches for files matching pattern in the directory dir
  64. // and appends them to matches. If the directory cannot be
  65. // opened, it returns the existing matches. New matches are
  66. // added in lexicographical order.
  67. func glob(fs Fs, dir, pattern string, matches []string) (m []string, e error) {
  68. m = matches
  69. fi, err := fs.Stat(dir)
  70. if err != nil {
  71. return
  72. }
  73. if !fi.IsDir() {
  74. return
  75. }
  76. d, err := fs.Open(dir)
  77. if err != nil {
  78. return
  79. }
  80. defer d.Close()
  81. names, _ := d.Readdirnames(-1)
  82. sort.Strings(names)
  83. for _, n := range names {
  84. matched, err := filepath.Match(pattern, n)
  85. if err != nil {
  86. return m, err
  87. }
  88. if matched {
  89. m = append(m, filepath.Join(dir, n))
  90. }
  91. }
  92. return
  93. }
  94. // hasMeta reports whether path contains any of the magic characters
  95. // recognized by Match.
  96. func hasMeta(path string) bool {
  97. // TODO(niemeyer): Should other magic characters be added here?
  98. return strings.IndexAny(path, "*?[") >= 0
  99. }