aboutsummaryrefslogtreecommitdiff
path: root/weed/util/fullpath.go
blob: 6c4f5c6ae5808641dd2ee5c76eb8af2a7956916e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package util

import (
	"os"
	"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)
}

// 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
}

// 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...))
}