2020-03-23 07:01:34 +00:00
|
|
|
package util
|
2018-05-26 06:27:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FullPath string
|
|
|
|
|
|
|
|
func NewFullPath(dir, name string) FullPath {
|
2019-03-31 06:08:29 +00:00
|
|
|
return FullPath(dir).Child(name)
|
2018-05-26 06:27:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fp FullPath) DirAndName() (string, string) {
|
|
|
|
dir, name := filepath.Split(string(fp))
|
2020-09-26 07:13:39 +00:00
|
|
|
name = strings.ToValidUTF8(name, "?")
|
2018-05-26 06:27:06 +00:00
|
|
|
if dir == "/" {
|
|
|
|
return dir, name
|
|
|
|
}
|
|
|
|
if len(dir) < 1 {
|
|
|
|
return "/", ""
|
|
|
|
}
|
|
|
|
return dir[:len(dir)-1], name
|
|
|
|
}
|
|
|
|
|
2018-05-27 18:52:26 +00:00
|
|
|
func (fp FullPath) Name() string {
|
2018-05-26 06:27:06 +00:00
|
|
|
_, name := filepath.Split(string(fp))
|
2020-09-26 07:13:39 +00:00
|
|
|
name = strings.ToValidUTF8(name, "?")
|
2018-05-26 06:27:06 +00:00
|
|
|
return name
|
|
|
|
}
|
2019-03-31 06:08:29 +00:00
|
|
|
|
2023-10-12 21:29:55 +00:00
|
|
|
func (fp FullPath) IsLongerFileName(maxFilenameLength uint32) bool {
|
|
|
|
if maxFilenameLength == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return uint32(len([]byte(fp.Name()))) > maxFilenameLength
|
|
|
|
}
|
|
|
|
|
2019-03-31 06:08:29 +00:00
|
|
|
func (fp FullPath) Child(name string) FullPath {
|
|
|
|
dir := string(fp)
|
2021-08-15 08:53:46 +00:00
|
|
|
noPrefix := name
|
|
|
|
if strings.HasPrefix(name, "/") {
|
|
|
|
noPrefix = name[1:]
|
|
|
|
}
|
2019-03-31 06:08:29 +00:00
|
|
|
if strings.HasSuffix(dir, "/") {
|
2021-08-15 08:53:46 +00:00
|
|
|
return FullPath(dir + noPrefix)
|
2019-03-31 06:08:29 +00:00
|
|
|
}
|
2021-08-15 08:53:46 +00:00
|
|
|
return FullPath(dir + "/" + noPrefix)
|
2019-03-31 06:08:29 +00:00
|
|
|
}
|
2020-01-20 07:59:46 +00:00
|
|
|
|
2022-01-12 19:51:13 +00:00
|
|
|
// AsInode an in-memory only inode representation
|
2022-03-14 07:03:29 +00:00
|
|
|
func (fp FullPath) AsInode(unixTime int64) uint64 {
|
2022-01-12 07:44:48 +00:00
|
|
|
inode := uint64(HashStringToLong(string(fp)))
|
2022-03-14 07:03:29 +00:00
|
|
|
inode = inode + uint64(unixTime)*37
|
2022-01-12 07:44:48 +00:00
|
|
|
return inode
|
2020-01-20 07:59:46 +00:00
|
|
|
}
|
2020-03-26 05:19:19 +00:00
|
|
|
|
|
|
|
// split, but skipping the root
|
|
|
|
func (fp FullPath) Split() []string {
|
2020-03-26 07:09:01 +00:00
|
|
|
if fp == "" || fp == "/" {
|
2020-03-26 05:19:19 +00:00
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return strings.Split(string(fp)[1:], "/")
|
|
|
|
}
|
2020-04-05 19:40:46 +00:00
|
|
|
|
2020-04-05 20:11:43 +00:00
|
|
|
func Join(names ...string) string {
|
|
|
|
return filepath.ToSlash(filepath.Join(names...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func JoinPath(names ...string) FullPath {
|
|
|
|
return FullPath(Join(names...))
|
|
|
|
}
|
2022-08-07 07:56:15 +00:00
|
|
|
|
|
|
|
func (fp FullPath) IsUnder(other FullPath) bool {
|
|
|
|
if other == "/" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return strings.HasPrefix(string(fp), string(other)+"/")
|
|
|
|
}
|
|
|
|
|
2022-08-07 08:34:32 +00:00
|
|
|
func StringSplit(separatedValues string, sep string) []string {
|
2022-08-07 07:55:34 +00:00
|
|
|
if separatedValues == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return strings.Split(separatedValues, sep)
|
|
|
|
}
|