2020-03-23 15:01:34 +08:00
|
|
|
package util
|
2018-05-26 14:27:06 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FullPath string
|
|
|
|
|
|
|
|
func NewFullPath(dir, name string) FullPath {
|
2019-03-31 14:08:29 +08:00
|
|
|
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))
|
|
|
|
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))
|
|
|
|
return name
|
|
|
|
}
|
2019-03-31 14:08:29 +08:00
|
|
|
|
|
|
|
func (fp FullPath) Child(name string) FullPath {
|
|
|
|
dir := string(fp)
|
|
|
|
if strings.HasSuffix(dir, "/") {
|
|
|
|
return FullPath(dir + name)
|
|
|
|
}
|
|
|
|
return FullPath(dir + "/" + name)
|
|
|
|
}
|
2020-01-20 15:59:46 +08:00
|
|
|
|
|
|
|
func (fp FullPath) AsInode() uint64 {
|
2020-03-23 15:01:34 +08:00
|
|
|
return uint64(HashStringToLong(string(fp)))
|
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:], "/")
|
|
|
|
}
|