github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libkbfs/node.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libkbfs
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"os"
    11  	"runtime"
    12  	"time"
    13  
    14  	"github.com/keybase/client/go/kbfs/data"
    15  	"github.com/keybase/client/go/kbfs/kbfsblock"
    16  	billy "gopkg.in/src-d/go-billy.v4"
    17  )
    18  
    19  // nodeCore holds info shared among one or more nodeStandard objects.
    20  type nodeCore struct {
    21  	pathNode  *data.PathNode
    22  	parent    Node
    23  	cache     *nodeCacheStandard
    24  	entryType data.EntryType
    25  	// used only when parent is nil (the object has been unlinked)
    26  	cachedPath data.Path
    27  	cachedDe   data.DirEntry
    28  	obfuscator data.Obfuscator
    29  }
    30  
    31  func newNodeCore(
    32  	ptr data.BlockPointer, name data.PathPartString, parent Node,
    33  	cache *nodeCacheStandard, et data.EntryType) *nodeCore {
    34  	return &nodeCore{
    35  		pathNode: &data.PathNode{
    36  			BlockPointer: ptr,
    37  			Name:         name,
    38  		},
    39  		parent:    parent,
    40  		cache:     cache,
    41  		entryType: et,
    42  	}
    43  }
    44  
    45  func newNodeCoreForDir(
    46  	ptr data.BlockPointer, name data.PathPartString, parent Node,
    47  	cache *nodeCacheStandard, obfuscator data.Obfuscator) *nodeCore {
    48  	nc := newNodeCore(ptr, name, parent, cache, data.Dir)
    49  	nc.obfuscator = obfuscator
    50  	return nc
    51  }
    52  
    53  func (c *nodeCore) ParentID() NodeID {
    54  	if c.parent == nil {
    55  		return nil
    56  	}
    57  	return c.parent.GetID()
    58  }
    59  
    60  // String is to support printing *nodeCore as a NodeID. If we want to
    61  // print nodeCores as nodeCores (e.g., for debugging) we might have to
    62  // implement Formatter.
    63  func (c *nodeCore) String() string {
    64  	return fmt.Sprintf("%p", c)
    65  }
    66  
    67  type nodeStandard struct {
    68  	core *nodeCore
    69  }
    70  
    71  var _ Node = (*nodeStandard)(nil)
    72  
    73  func nodeStandardFinalizer(n *nodeStandard) {
    74  	n.core.cache.forget(n.core)
    75  }
    76  
    77  func makeNodeStandard(core *nodeCore) *nodeStandard {
    78  	n := &nodeStandard{core}
    79  	runtime.SetFinalizer(n, nodeStandardFinalizer)
    80  	return n
    81  }
    82  
    83  func (n *nodeStandard) GetBlockID() (blockID kbfsblock.ID) {
    84  	return n.core.pathNode.BlockPointer.ID
    85  }
    86  
    87  func (n *nodeStandard) GetCanonicalPath() string {
    88  	return n.core.cache.PathFromNode(n).CanonicalPathString()
    89  }
    90  
    91  func (n *nodeStandard) GetPathPlaintextSansTlf() (string, bool) {
    92  	return n.core.cache.PathFromNode(n).PlaintextSansTlf()
    93  }
    94  
    95  func (n *nodeStandard) GetID() NodeID {
    96  	return n.core
    97  }
    98  
    99  func (n *nodeStandard) GetFolderBranch() data.FolderBranch {
   100  	return n.core.cache.folderBranch
   101  }
   102  
   103  func (n *nodeStandard) GetBasename() data.PathPartString {
   104  	if len(n.core.cachedPath.Path) > 0 {
   105  		// Must be unlinked.
   106  		return data.PathPartString{}
   107  	}
   108  	return n.core.pathNode.Name
   109  }
   110  
   111  func (n *nodeStandard) Readonly(_ context.Context) bool {
   112  	return false
   113  }
   114  
   115  func (n *nodeStandard) ShouldCreateMissedLookup(
   116  	ctx context.Context, _ data.PathPartString) (
   117  	bool, context.Context, data.EntryType, os.FileInfo, data.PathPartString,
   118  	data.BlockPointer) {
   119  	return false, ctx, data.File, nil, data.PathPartString{}, data.ZeroPtr
   120  }
   121  
   122  func (n *nodeStandard) ShouldRetryOnDirRead(ctx context.Context) bool {
   123  	return false
   124  }
   125  
   126  func (n *nodeStandard) RemoveDir(_ context.Context, _ data.PathPartString) (
   127  	removeHandled bool, err error) {
   128  	return false, nil
   129  }
   130  
   131  func (n *nodeStandard) WrapChild(child Node) Node {
   132  	return child
   133  }
   134  
   135  func (n *nodeStandard) Unwrap() Node {
   136  	return n
   137  }
   138  
   139  func (n *nodeStandard) GetFS(_ context.Context) NodeFSReadOnly {
   140  	return nil
   141  }
   142  
   143  func (n *nodeStandard) GetFile(_ context.Context) billy.File {
   144  	return nil
   145  }
   146  
   147  func (n *nodeStandard) EntryType() data.EntryType {
   148  	return n.core.entryType
   149  }
   150  
   151  func (n *nodeStandard) FillCacheDuration(d *time.Duration) {}
   152  
   153  func (n *nodeStandard) Obfuscator() data.Obfuscator {
   154  	return n.core.obfuscator
   155  }
   156  
   157  func (n *nodeStandard) ChildName(name string) data.PathPartString {
   158  	if n.core.entryType != data.Dir {
   159  		panic("Only dirs can have child names")
   160  	}
   161  	return data.NewPathPartString(name, n.core.obfuscator)
   162  }