Browse Source

mount: better support hard links

pull/3354/head
chrislu 2 years ago
parent
commit
4d5dc55766
  1. 57
      weed/mount/inode_to_path.go

57
weed/mount/inode_to_path.go

@ -14,18 +14,39 @@ type InodeToPath struct {
path2inode map[util.FullPath]uint64 path2inode map[util.FullPath]uint64
} }
type InodeEntry struct { type InodeEntry struct {
util.FullPath
paths []util.FullPath
nlookup uint64 nlookup uint64
isDirectory bool isDirectory bool
isChildrenCached bool isChildrenCached bool
} }
func (ie *InodeEntry) removeOnePath(p util.FullPath) bool {
if len(ie.paths) == 0 {
return false
}
idx := -1
for i, x := range ie.paths {
if x == p {
idx = i
break
}
}
if idx < 0 {
return false
}
for x := len(ie.paths) - 2; x > idx; x-- {
ie.paths[x-1] = ie.paths[x]
}
ie.paths = ie.paths[0 : len(ie.paths)-1]
return true
}
func NewInodeToPath(root util.FullPath) *InodeToPath { func NewInodeToPath(root util.FullPath) *InodeToPath {
t := &InodeToPath{ t := &InodeToPath{
inode2path: make(map[uint64]*InodeEntry), inode2path: make(map[uint64]*InodeEntry),
path2inode: make(map[util.FullPath]uint64), path2inode: make(map[util.FullPath]uint64),
} }
t.inode2path[1] = &InodeEntry{root, 1, true, false}
t.inode2path[1] = &InodeEntry{[]util.FullPath{root}, 1, true, false}
t.path2inode[root] = 1 t.path2inode[root] = 1
return t return t
} }
@ -54,9 +75,9 @@ func (i *InodeToPath) Lookup(path util.FullPath, unixTime int64, isDirectory boo
} }
} else { } else {
if !isLookup { if !isLookup {
i.inode2path[inode] = &InodeEntry{path, 0, isDirectory, false}
i.inode2path[inode] = &InodeEntry{[]util.FullPath{path}, 0, isDirectory, false}
} else { } else {
i.inode2path[inode] = &InodeEntry{path, 1, isDirectory, false}
i.inode2path[inode] = &InodeEntry{[]util.FullPath{path}, 1, isDirectory, false}
} }
} }
@ -94,10 +115,10 @@ func (i *InodeToPath) GetPath(inode uint64) (util.FullPath, fuse.Status) {
i.RLock() i.RLock()
defer i.RUnlock() defer i.RUnlock()
path, found := i.inode2path[inode] path, found := i.inode2path[inode]
if !found {
if !found || len(path.paths) == 0 {
return "", fuse.ENOENT return "", fuse.ENOENT
} }
return path.FullPath, fuse.OK
return path.paths[0], fuse.OK
} }
func (i *InodeToPath) HasPath(path util.FullPath) bool { func (i *InodeToPath) HasPath(path util.FullPath) bool {
@ -148,9 +169,17 @@ func (i *InodeToPath) RemovePath(path util.FullPath) {
inode, found := i.path2inode[path] inode, found := i.path2inode[path]
if found { if found {
delete(i.path2inode, path) delete(i.path2inode, path)
i.removePathFromInode2Path(inode, path)
}
}
func (i *InodeToPath) removePathFromInode2Path(inode uint64, path util.FullPath) {
if ie, found := i.inode2path[inode]; found {
if ie.removeOnePath(path) && len(ie.paths) == 0 {
delete(i.inode2path, inode) delete(i.inode2path, inode)
} }
} }
}
func (i *InodeToPath) MovePath(sourcePath, targetPath util.FullPath) (sourceInode, targetInode uint64) { func (i *InodeToPath) MovePath(sourcePath, targetPath util.FullPath) (sourceInode, targetInode uint64) {
i.Lock() i.Lock()
@ -158,7 +187,7 @@ func (i *InodeToPath) MovePath(sourcePath, targetPath util.FullPath) (sourceInod
sourceInode, sourceFound := i.path2inode[sourcePath] sourceInode, sourceFound := i.path2inode[sourcePath]
targetInode, targetFound := i.path2inode[targetPath] targetInode, targetFound := i.path2inode[targetPath]
if targetFound { if targetFound {
delete(i.inode2path, targetInode)
i.removePathFromInode2Path(targetInode, targetPath)
delete(i.path2inode, targetPath) delete(i.path2inode, targetPath)
} }
if sourceFound { if sourceFound {
@ -170,7 +199,11 @@ func (i *InodeToPath) MovePath(sourcePath, targetPath util.FullPath) (sourceInod
return return
} }
if entry, entryFound := i.inode2path[sourceInode]; entryFound { if entry, entryFound := i.inode2path[sourceInode]; entryFound {
entry.FullPath = targetPath
for i, p := range entry.paths {
if p == sourcePath {
entry.paths[i] = targetPath
}
}
entry.isChildrenCached = false entry.isChildrenCached = false
if !targetFound { if !targetFound {
entry.nlookup++ entry.nlookup++
@ -187,7 +220,9 @@ func (i *InodeToPath) Forget(inode, nlookup uint64, onForgetDir func(dir util.Fu
if found { if found {
path.nlookup -= nlookup path.nlookup -= nlookup
if path.nlookup <= 0 { if path.nlookup <= 0 {
delete(i.path2inode, path.FullPath)
for _, p := range path.paths {
delete(i.path2inode, p)
}
delete(i.inode2path, inode) delete(i.inode2path, inode)
} }
} }
@ -195,7 +230,9 @@ func (i *InodeToPath) Forget(inode, nlookup uint64, onForgetDir func(dir util.Fu
if found { if found {
if path.isDirectory && path.nlookup <= 0 && onForgetDir != nil { if path.isDirectory && path.nlookup <= 0 && onForgetDir != nil {
path.isChildrenCached = false path.isChildrenCached = false
onForgetDir(path.FullPath)
for _, p := range path.paths {
onForgetDir(p)
}
} }
} }
} }
Loading…
Cancel
Save