NewSymlink creates a new symlink object
(upstream *upstream, symlinkPath, targetPath string, isDir bool, ignoreMatcher ignoreparser.IgnoreParser)
| 37 | |
| 38 | // NewSymlink creates a new symlink object |
| 39 | func NewSymlink(upstream *upstream, symlinkPath, targetPath string, isDir bool, ignoreMatcher ignoreparser.IgnoreParser) (*Symlink, error) { |
| 40 | symlink := &Symlink{ |
| 41 | SymlinkPath: symlinkPath, |
| 42 | TargetPath: targetPath, |
| 43 | IsDir: isDir, |
| 44 | events: make(chan notify.EventInfo, 1000), |
| 45 | upstream: upstream, |
| 46 | } |
| 47 | |
| 48 | symlink.watcher = notify.NewTree() |
| 49 | _ = symlink.watcher.Watch(targetPath, symlink.events, func(path string) bool { |
| 50 | if ignoreMatcher == nil || ignoreMatcher.RequireFullScan() { |
| 51 | return false |
| 52 | } |
| 53 | |
| 54 | stat, err := os.Stat(path) |
| 55 | if err != nil { |
| 56 | return false |
| 57 | } |
| 58 | |
| 59 | return ignoreMatcher.Matches(path[len(symlink.SymlinkPath):], stat.IsDir()) |
| 60 | }, notify.All) |
| 61 | |
| 62 | go func() { |
| 63 | for event := range symlink.events { |
| 64 | symlink.upstream.events <- &symlinkEvent{ |
| 65 | path: symlink.rewritePath(event.Path()), |
| 66 | event: event.Event(), |
| 67 | } |
| 68 | } |
| 69 | }() |
| 70 | |
| 71 | return symlink, nil |
| 72 | } |
| 73 | |
| 74 | func (s *Symlink) rewritePath(path string) string { |
| 75 | return s.SymlinkPath + path[len(s.TargetPath):] |
no test coverage detected