Skip to content

Instantly share code, notes, and snippets.

@verdverm
Created July 29, 2014 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verdverm/75b036439591ac0ef47e to your computer and use it in GitHub Desktop.
Save verdverm/75b036439591ac0ef47e to your computer and use it in GitHub Desktop.
symlink'n filepath.Walk --- draft
func sympathWalk(path string) {
// for now... (so i don't have to change everything below)
p := path
// is the directory / file a symlink?
f, err := os.Lstat(p)
if err == nil && f.Mode()&os.ModeSymlink == os.ModeSymlink {
realPath, err := filepath.EvalSymlinks(p)
if err != nil {
panic(err)
}
p = realPath
}
fi, err := os.Stat(p)
if err != nil {
ERROR.Println("Failed to stat watched path", p, ":", err)
continue
}
// If it is a file, watch that specific file.
if !fi.IsDir() {
err = watcher.Watch(p)
if err != nil {
ERROR.Println("Failed to watch", p, ":", err)
}
continue
}
var watcherWalker filepath.WalkFunc
watcherWalker = func(path string, info os.FileInfo, err error) error {
if err != nil {
ERROR.Println("Error walking path:", err)
return nil
}
// is it a symlinked template?
link, err := os.Lstat(path)
if err == nil && link.Mode()&os.ModeSymlink == os.ModeSymlink {
TRACE.Println("Watcher symlink: ", path)
// lookup the actual target & check for goodness
targetPath, err := filepath.EvalSymlinks(path)
if err != nil {
ERROR.Println("Failed to read symlink", err)
return err
}
targetInfo, err := os.Stat(targetPath)
if err != nil {
ERROR.Println("Failed to stat symlink target", err)
return err
}
// set the template path to the target of the symlink
path = targetPath
info = targetInfo
filepath.Walk(path, watcherWalker)
}
if info.IsDir() {
if dl, ok := listener.(DiscerningListener); ok {
if !dl.WatchDir(info) {
return filepath.SkipDir
}
}
err = watcher.Watch(path)
if err != nil {
ERROR.Println("Failed to watch", path, ":", err)
}
}
return nil
}
// Else, walk the directory tree.
filepath.Walk(p, watcherWalker)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment