From 694bbf09769c7ac6df3c8d16922d2bfcf3fe3457 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 15 Aug 2019 19:19:33 -0700 Subject: [PATCH] walk: add a walk function This commit was moved from ipfs/go-ipfs-files@e1ba4f66e7ebec9f8744703808e4adc43afe0faf --- files/walk.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 files/walk.go diff --git a/files/walk.go b/files/walk.go new file mode 100644 index 000000000..f23e7e47f --- /dev/null +++ b/files/walk.go @@ -0,0 +1,27 @@ +package files + +import ( + "path/filepath" +) + +// Walk walks a file tree, like `os.Walk`. +func Walk(nd Node, cb func(fpath string, nd Node) error) error { + var helper func(string, Node) error + helper = func(path string, nd Node) error { + if err := cb(path, nd); err != nil { + return err + } + dir, ok := nd.(Directory) + if !ok { + return nil + } + iter := dir.Entries() + for iter.Next() { + if err := helper(filepath.Join(path, iter.Name()), iter.Node()); err != nil { + return err + } + } + return iter.Err() + } + return helper("", nd) +}