Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a maxdepth parameter to limit tree traversal #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions livemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type state int

var host = flag.String("host", "", "Host IP to listen on (default: \"\" == 127.0.0.1)")
var port = flag.String("port", "8080", "Port to listen on (default: 8080)")
var maxdepth = flag.Int("maxdepth", 0, "max tree depth to traverse, 0 == infinite")

const (
None state = iota
Expand Down Expand Up @@ -71,26 +72,6 @@ func HasMarkdownSuffix(s string) bool {
return false
}

func AddWatch(w *fsnotify.Watcher) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
w.Add(path)
} else {
if HasMarkdownSuffix(path) {
tocMutex.Lock()
toc = append(toc, path)
tocMutex.Unlock()
log.Println("Found", path)
w.Add(path)
}
}
return nil
}
}

func WatcherEventLoop(w *fsnotify.Watcher, updates chan Update, done chan bool) {
for {
select {
Expand Down Expand Up @@ -196,6 +177,40 @@ func HandleListener(listeners chan Listener) func(ws *websocket.Conn) {
listeners <- Listener{subpath, ws, Close}
}
}
func GetPathDepth(path string) int {
return strings.Count(filepath.Clean(path), string(os.PathSeparator))
}

func AddWatch(w *fsnotify.Watcher, rootpath string) filepath.WalkFunc {
rootPathDepth := GetPathDepth(rootpath)

return func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// a maxdepth of 0 means inifinite traversal
if *maxdepth > 0 {
currentDepth := GetPathDepth(path)
if (currentDepth - rootPathDepth) > *maxdepth {
return filepath.SkipDir
}
}

if info.IsDir() {
w.Add(path)
} else {
if HasMarkdownSuffix(path) {
tocMutex.Lock()
toc = append(toc, path)
tocMutex.Unlock()
log.Println("Found", path)
w.Add(path)
}
}
return nil
}
}

func main() {
flag.Parse()
Expand All @@ -215,8 +230,8 @@ func main() {
go WatcherEventLoop(watcher, updates, done)

log.Println("Watching directory", path)
err = filepath.Walk(path, AddWatch(watcher))
if err != nil {
err = filepath.Walk(path, AddWatch(watcher, path))
if err != nil && err != filepath.SkipDir {
log.Fatal(err)
}

Expand Down