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.
15 const sep = string(os.PathSeparator)
17 var errDepth = errors.New("exceeded allowed iteration count (circular symlink?)")
19 func min(i, j int) int {
26 func max(i, j int) int {
33 // must panics if err is non-nil.
34 func must(err error) {
40 // nonil gives first non-nil error from the given arguments.
41 func nonil(err ...error) error {
42 for _, err := range err {
50 func cleanpath(path string) (realpath string, isrec bool, err error) {
51 if strings.HasSuffix(path, "...") {
53 path = path[:len(path)-3]
55 if path, err = filepath.Abs(path); err != nil {
58 if path, err = canonical(path); err != nil {
61 return path, isrec, nil
64 // canonical resolves any symlink in the given path and returns it in a clean form.
65 // It expects the path to be absolute. It fails to resolve circular symlinks by
66 // maintaining a simple iteration limit.
67 func canonical(p string) (string, error) {
68 p, err := filepath.Abs(p)
72 for i, j, depth := 1, 0, 1; i < len(p); i, depth = i+1, depth+1 {
74 return "", &os.PathError{Op: "canonical", Path: p, Err: errDepth}
76 if j = strings.IndexRune(p[i:], '/'); j == -1 {
81 fi, err := os.Lstat(p[:i])
85 if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
86 s, err := os.Readlink(p[:i])
90 if filepath.IsAbs(s) {
95 i = 1 // no guarantee s is canonical, start all over
98 return filepath.Clean(p), nil
101 func joinevents(events []Event) (e Event) {
102 if len(events) == 0 {
105 for _, event := range events {
112 func split(s string) (string, string) {
113 if i := lastIndexSep(s); i != -1 {
114 return s[:i], s[i+1:]
119 func base(s string) string {
120 if i := lastIndexSep(s); i != -1 {
126 func indexbase(root, name string) int {
127 if n, m := len(root), len(name); m >= n && name[:n] == root &&
128 (n == m || name[n] == os.PathSeparator) {
134 func indexSep(s string) int {
135 for i := 0; i < len(s); i++ {
136 if s[i] == os.PathSeparator {
143 func lastIndexSep(s string) int {
144 for i := len(s) - 1; i >= 0; i-- {
145 if s[i] == os.PathSeparator {