/
dxpath.go
135 lines (117 loc) · 3.58 KB
/
dxpath.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2019 DxChain, All rights reserved.
// Use of this source code is governed by an Apache
// License 2.0 that can be found in the LICENSE file.
package storage
import (
"errors"
"fmt"
"path/filepath"
"strings"
)
var (
// ErrAlreadyRoot is the error happens when calling Parent function on
// a root directory
ErrAlreadyRoot = errors.New("cannot call the Parent function on the root directory")
// ErrEmptyDxPath is the error happens when calling NewDxPath on an empty string
ErrEmptyDxPath = errors.New("cannot create an empty DxPath")
reservedNames = []string{
".dxdir",
}
)
type (
// DxPath is the file Path or directory Path relates to the root directory of the DxFiles.
// It is used in storage client and storage client's file system
// WARN: When creating a DxPath, please use NewDxPath method to create a new DxPath.
DxPath struct {
Path string
}
// SysPath is the actual system Path of a file or a directory.
SysPath string
)
// NewDxPath create a DxPath with provided s string.
// If validation is not passed for s, an error is returned
func NewDxPath(s string) (DxPath, error) {
return newDxPath(s)
}
// newDxPath is the function to be used for NewDxPath
// It takes the input string, and clear prefix and suffix / character.
// Then it check whether the input s is valid. If not valid, return an error.
// Finally return the DxPath
func newDxPath(s string) (DxPath, error) {
dp := DxPath{clean(s)}
if err := dp.validate(); err != nil {
return DxPath{}, err
}
return dp, nil
}
// clean clean the input string for the usage of DxPath
func clean(s string) string {
s = filepath.ToSlash(s)
s = strings.TrimPrefix(s, "/")
s = strings.TrimSuffix(s, "/")
return s
}
// validate checks the validity of the dp. It is only used in newDxPath
func (dp DxPath) validate() error {
if dp.Path == "" {
return ErrEmptyDxPath
}
for _, pathElem := range strings.Split(dp.Path, "/") {
if pathElem == "." || pathElem == ".." {
return errors.New("dxpath could not contain . or .. elements")
}
if pathElem == "" || pathElem == "/" {
return errors.New("dxpath could not contain //")
}
for _, reserved := range reservedNames {
if pathElem == reserved {
return fmt.Errorf("dxpath conflict with reserved name %v", reserved)
}
}
}
return nil
}
// IsRoot checks whether a DxPath is a root directory
func (dp DxPath) IsRoot() bool {
return dp.Path == ""
}
// SysPath return the system Path of the DxPath. It concatenate the input rootDir and DxPath
func (dp DxPath) SysPath(rootDir SysPath) SysPath {
if dp.IsRoot() {
return rootDir
}
return SysPath(filepath.Join(string(rootDir), dp.Path))
}
// Parent returns the parent DxPath of the DxPath.
// If the receiver is already root, return an error of ErrAlreadyExist
func (dp DxPath) Parent() (DxPath, error) {
if dp.IsRoot() {
return DxPath{}, ErrAlreadyRoot
}
par := filepath.Dir(dp.Path)
if par == "." {
return RootDxPath(), nil
}
return newDxPath(par)
}
// RootDxPath return the special root DxPath which has Path as empty string
func RootDxPath() DxPath {
return DxPath{""}
}
// Equals check whether the two DxPath are equal
func (dp DxPath) Equals(dp2 DxPath) bool {
return dp.Path == dp2.Path
}
// Join join the DxPath with s
func (dp DxPath) Join(s string) (DxPath, error) {
return NewDxPath(filepath.Join(dp.Path, s))
}
// Join join the receiver syspath with DxPath and some extrafields.
func (sp SysPath) Join(dp DxPath, extra ...string) SysPath {
path := []string{string(sp)}
if len(dp.Path) != 0 {
path = append(path, dp.Path)
}
path = append(path, extra...)
return SysPath(filepath.Join(path...))
}