1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
12 // lookupnode is a caching tree node that is initially empty and calls
13 // loadOne and loadAll to load/update child nodes as needed.
15 // See (*customFileSystem)MountUsers for example usage.
16 type lookupnode struct {
18 loadOne func(parent inode, name string) (inode, error)
19 loadAll func(parent inode) ([]inode, error)
20 stale func(time.Time) bool
24 staleOne map[string]time.Time
27 // Sync flushes pending writes for loaded children and, if successful,
28 // triggers a reload on next lookup.
29 func (ln *lookupnode) Sync() error {
30 err := ln.treenode.Sync()
35 ln.staleAll = time.Time{}
41 func (ln *lookupnode) Readdir() ([]os.FileInfo, error) {
43 checkTime := time.Now()
44 if ln.stale(ln.staleAll) {
45 all, err := ln.loadAll(ln)
50 for _, child := range all {
51 _, err = ln.treenode.Child(child.FileInfo().Name(), func(inode) (inode, error) {
59 ln.staleAll = checkTime
60 // No value in ln.staleOne can make a difference to an
61 // "entry is stale?" test now, because no value is
62 // newer than ln.staleAll. Reclaim memory.
66 return ln.treenode.Readdir()
69 // Child rejects (with ErrInvalidOperation) calls to add/replace
70 // children, instead calling loadOne when a non-existing child is
72 func (ln *lookupnode) Child(name string, replace func(inode) (inode, error)) (inode, error) {
73 checkTime := time.Now()
76 if ln.stale(ln.staleAll) && ln.stale(ln.staleOne[name]) {
77 existing, err = ln.treenode.Child(name, func(inode) (inode, error) {
78 return ln.loadOne(ln, name)
80 if err == nil && existing != nil {
81 if ln.staleOne == nil {
82 ln.staleOne = map[string]time.Time{name: checkTime}
84 ln.staleOne[name] = checkTime
88 existing, err = ln.treenode.Child(name, nil)
89 if err != nil && !os.IsNotExist(err) {
94 // Let the callback try to delete or replace the
95 // existing node; if it does, return
96 // ErrInvalidOperation.
97 if tryRepl, err := replace(existing); err != nil {
98 // Propagate error from callback
100 } else if tryRepl != existing {
101 return existing, ErrInvalidOperation
104 // Return original error from ln.treenode.Child() (it might be