seaweedfs/weed/util/fullpath.go

87 lines
1.8 KiB
Go
Raw Normal View History

2020-03-23 15:01:34 +08:00
package util
2018-05-26 14:27:06 +08:00
import (
"os"
2018-05-26 14:27:06 +08:00
"path/filepath"
"strings"
)
type FullPath string
func NewFullPath(dir, name string) FullPath {
return FullPath(dir).Child(name)
2018-05-26 14:27:06 +08:00
}
func (fp FullPath) DirAndName() (string, string) {
dir, name := filepath.Split(string(fp))
name = strings.ToValidUTF8(name, "?")
2018-05-26 14:27:06 +08:00
if dir == "/" {
return dir, name
}
if len(dir) < 1 {
return "/", ""
}
return dir[:len(dir)-1], name
}
2018-05-28 02:52:26 +08:00
func (fp FullPath) Name() string {
2018-05-26 14:27:06 +08:00
_, name := filepath.Split(string(fp))
name = strings.ToValidUTF8(name, "?")
2018-05-26 14:27:06 +08:00
return name
}
func (fp FullPath) Child(name string) FullPath {
dir := string(fp)
2021-08-15 16:53:46 +08:00
noPrefix := name
if strings.HasPrefix(name, "/") {
noPrefix = name[1:]
}
if strings.HasSuffix(dir, "/") {
2021-08-15 16:53:46 +08:00
return FullPath(dir + noPrefix)
}
2021-08-15 16:53:46 +08:00
return FullPath(dir + "/" + noPrefix)
}
2020-01-20 15:59:46 +08:00
// AsInode an in-memory only inode representation
func (fp FullPath) AsInode(fileMode os.FileMode) uint64 {
inode := uint64(HashStringToLong(string(fp)))
inode = inode - inode%16
if fileMode == 0 {
} else if fileMode&os.ModeDir > 0 {
inode += 1
} else if fileMode&os.ModeSymlink > 0 {
inode += 2
} else if fileMode&os.ModeDevice > 0 {
if fileMode&os.ModeCharDevice > 0 {
inode += 6
} else {
inode += 3
}
} else if fileMode&os.ModeNamedPipe > 0 {
inode += 4
} else if fileMode&os.ModeSocket > 0 {
inode += 5
} else if fileMode&os.ModeCharDevice > 0 {
inode += 6
} else if fileMode&os.ModeIrregular > 0 {
inode += 7
}
return inode
2020-01-20 15:59:46 +08:00
}
2020-03-26 13:19:19 +08:00
// split, but skipping the root
func (fp FullPath) Split() []string {
2020-03-26 15:09:01 +08:00
if fp == "" || fp == "/" {
2020-03-26 13:19:19 +08:00
return []string{}
}
return strings.Split(string(fp)[1:], "/")
}
2020-04-06 03:40:46 +08:00
2020-04-06 04:11:43 +08:00
func Join(names ...string) string {
return filepath.ToSlash(filepath.Join(names...))
}
func JoinPath(names ...string) FullPath {
return FullPath(Join(names...))
}