github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/sentry/fsimpl/gofer/fstree.go (about) 1 package gofer 2 3 import ( 4 "github.com/nicocha30/gvisor-ligolo/pkg/fspath" 5 "github.com/nicocha30/gvisor-ligolo/pkg/sentry/vfs" 6 ) 7 8 // IsAncestorDentry returns true if d is an ancestor of d2; that is, d is 9 // either d2's parent or an ancestor of d2's parent. 10 func genericIsAncestorDentry(d, d2 *dentry) bool { 11 for d2 != nil { 12 if d2.parent == d { 13 return true 14 } 15 if d2.parent == d2 { 16 return false 17 } 18 d2 = d2.parent 19 } 20 return false 21 } 22 23 // ParentOrSelf returns d.parent. If d.parent is nil, ParentOrSelf returns d. 24 func genericParentOrSelf(d *dentry) *dentry { 25 if d.parent != nil { 26 return d.parent 27 } 28 return d 29 } 30 31 // PrependPath is a generic implementation of FilesystemImpl.PrependPath(). 32 func genericPrependPath(vfsroot vfs.VirtualDentry, mnt *vfs.Mount, d *dentry, b *fspath.Builder) error { 33 for { 34 if mnt == vfsroot.Mount() && &d.vfsd == vfsroot.Dentry() { 35 return vfs.PrependPathAtVFSRootError{} 36 } 37 if mnt != nil && &d.vfsd == mnt.Root() { 38 return nil 39 } 40 if d.parent == nil { 41 return vfs.PrependPathAtNonMountRootError{} 42 } 43 b.PrependComponent(d.name) 44 d = d.parent 45 } 46 } 47 48 // DebugPathname returns a pathname to d relative to its filesystem root. 49 // DebugPathname does not correspond to any Linux function; it's used to 50 // generate dentry pathnames for debugging. 51 func genericDebugPathname(d *dentry) string { 52 var b fspath.Builder 53 _ = genericPrependPath(vfs.VirtualDentry{}, nil, d, &b) 54 return b.String() 55 }