mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
2dcb8cb93b
this is just an in memory representation. POSIX wants different inode numbers for the same named file or directory.
69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package util
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
type FullPath string
|
|
|
|
func NewFullPath(dir, name string) FullPath {
|
|
return FullPath(dir).Child(name)
|
|
}
|
|
|
|
func (fp FullPath) DirAndName() (string, string) {
|
|
dir, name := filepath.Split(string(fp))
|
|
name = strings.ToValidUTF8(name, "?")
|
|
if dir == "/" {
|
|
return dir, name
|
|
}
|
|
if len(dir) < 1 {
|
|
return "/", ""
|
|
}
|
|
return dir[:len(dir)-1], name
|
|
}
|
|
|
|
func (fp FullPath) Name() string {
|
|
_, name := filepath.Split(string(fp))
|
|
name = strings.ToValidUTF8(name, "?")
|
|
return name
|
|
}
|
|
|
|
func (fp FullPath) Child(name string) FullPath {
|
|
dir := string(fp)
|
|
noPrefix := name
|
|
if strings.HasPrefix(name, "/") {
|
|
noPrefix = name[1:]
|
|
}
|
|
if strings.HasSuffix(dir, "/") {
|
|
return FullPath(dir + noPrefix)
|
|
}
|
|
return FullPath(dir + "/" + noPrefix)
|
|
}
|
|
|
|
func (fp FullPath) AsInode(isDirectory bool) uint64 {
|
|
inode := uint64(HashStringToLong(string(fp)))
|
|
if isDirectory {
|
|
inode = inode - inode%2 // even
|
|
} else {
|
|
inode = inode - inode%2 + 1 // odd
|
|
}
|
|
return inode
|
|
}
|
|
|
|
// split, but skipping the root
|
|
func (fp FullPath) Split() []string {
|
|
if fp == "" || fp == "/" {
|
|
return []string{}
|
|
}
|
|
return strings.Split(string(fp)[1:], "/")
|
|
}
|
|
|
|
func Join(names ...string) string {
|
|
return filepath.ToSlash(filepath.Join(names...))
|
|
}
|
|
|
|
func JoinPath(names ...string) FullPath {
|
|
return FullPath(Join(names...))
|
|
}
|