fs: Add Find method to DirTree
This commit is contained in:
parent
d5ff7104e5
commit
71fe046937
1 changed files with 15 additions and 7 deletions
22
fs/walk.go
22
fs/walk.go
|
@ -203,19 +203,27 @@ func (dt DirTree) addDir(entry DirEntry) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Find returns the DirEntry for filePath or nil if not found
|
||||||
|
func (dt DirTree) Find(filePath string) (parentPath string, entry DirEntry) {
|
||||||
|
parentPath = parentDir(filePath)
|
||||||
|
for _, entry := range dt[parentPath] {
|
||||||
|
if entry.Remote() == filePath {
|
||||||
|
return parentPath, entry
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return parentPath, nil
|
||||||
|
}
|
||||||
|
|
||||||
// check that dirPath has a *Dir in its parent
|
// check that dirPath has a *Dir in its parent
|
||||||
func (dt DirTree) checkParent(root, dirPath string) {
|
func (dt DirTree) checkParent(root, dirPath string) {
|
||||||
if dirPath == root {
|
if dirPath == root {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
parentPath := parentDir(dirPath)
|
parentPath, entry := dt.Find(dirPath)
|
||||||
entries := dt[parentPath]
|
if entry != nil {
|
||||||
for _, entry := range entries {
|
return
|
||||||
if entry.Remote() == dirPath {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
dt[parentPath] = append(entries, NewDir(dirPath, time.Now()))
|
dt[parentPath] = append(dt[parentPath], NewDir(dirPath, time.Now()))
|
||||||
dt.checkParent(root, parentPath)
|
dt.checkParent(root, parentPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue