15
15
package types
16
16
17
17
import (
18
+ "fmt"
18
19
"path/filepath"
19
20
"sort"
20
21
"strings"
35
36
}
36
37
)
37
38
39
+ var paths = map [string ]struct {}{}
40
+
38
41
func (cfg Config ) Validate (c path.ContextPath ) (r report.Report ) {
39
42
systemdPath := "/etc/systemd/system/"
40
43
unitPaths := map [string ]struct {}{}
@@ -76,43 +79,21 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
76
79
Path string
77
80
Field string
78
81
}
79
- paths := map [string ]struct {}{}
80
82
r := report.Report {}
81
83
82
84
for i , f := range cfg .Storage .Files {
83
- if _ , exists := paths [f .Path ]; exists {
84
- r .AddOnError (c .Append ("storage" , "files" , i , "path" ), errors .ErrPathConflictsParentDir ) //TODO: should add different error?
85
- return r
86
- }
87
- paths [f .Path ] = struct {}{}
88
- entries = append (entries , struct {
89
- Path string
90
- Field string
91
- }{Path : f .Path , Field : "files" })
85
+ r = handlePathConflict (f .Path , "files" , i , c , r , errors .ErrPathAlreadyExists )
86
+ addPathAndEntry (f .Path , "files" , & entries )
92
87
}
93
88
94
89
for i , d := range cfg .Storage .Directories {
95
- if _ , exists := paths [d .Path ]; exists {
96
- r .AddOnError (c .Append ("storage" , "directories" , i , "path" ), errors .ErrPathConflictsParentDir ) //TODO: should add different error?
97
- return r
98
- }
99
- paths [d .Path ] = struct {}{}
100
- entries = append (entries , struct {
101
- Path string
102
- Field string
103
- }{Path : d .Path , Field : "directories" })
90
+ r = handlePathConflict (d .Path , "directories" , i , c , r , errors .ErrPathAlreadyExists )
91
+ addPathAndEntry (d .Path , "directories" , & entries )
104
92
}
105
93
106
94
for i , l := range cfg .Storage .Links {
107
- if _ , exists := paths [l .Path ]; exists {
108
- r .AddOnError (c .Append ("storage" , "links" , i , "path" ), errors .ErrPathConflictsParentDir ) //TODO: error to already exist path
109
- return r
110
- }
111
- paths [l .Path ] = struct {}{}
112
- entries = append (entries , struct {
113
- Path string
114
- Field string
115
- }{Path : l .Path , Field : "links" })
95
+ r = handlePathConflict (l .Path , "links" , i , c , r , errors .ErrPathAlreadyExists )
96
+ addPathAndEntry (l .Path , "links" , & entries )
116
97
}
117
98
118
99
sort .Slice (entries , func (i , j int ) bool {
@@ -122,7 +103,8 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
122
103
for i , entry := range entries {
123
104
if i > 0 && isWithin (entry .Path , entries [i - 1 ].Path ) {
124
105
if entries [i - 1 ].Field != "directories" {
125
- r .AddOnError (c .Append ("storage" , entry .Field , i , "path" ), errors .ErrPathConflictsParentDir ) //TODO: conflict parent directories error
106
+ errorMsg := fmt .Errorf ("invalid entry at path %s: %v" , entry .Path , errors .ErrMissLabeledDir )
107
+ r .AddOnError (c .Append ("storage" , entry .Field , i , "path" ), errorMsg )
126
108
return r
127
109
}
128
110
}
@@ -131,16 +113,37 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
131
113
return r
132
114
}
133
115
134
- // check the depth
116
+ func handlePathConflict (path , fieldName string , index int , c path.ContextPath , r report.Report , err error ) report.Report {
117
+ if _ , exists := paths [path ]; exists {
118
+ r .AddOnError (c .Append ("storage" , fieldName , index , "path" ), err )
119
+ }
120
+ return r
121
+ }
122
+
123
+ func addPathAndEntry (path , fieldName string , entries * []struct { Path , Field string }) {
124
+ * entries = append (* entries , struct {
125
+ Path string
126
+ Field string
127
+ }{Path : path , Field : fieldName })
128
+ }
129
+
135
130
func depth (path string ) uint {
136
131
var count uint
137
- for p := filepath .Clean (path ); p != "/" && p != "." ; count ++ {
138
- p = filepath .Dir (p )
132
+ cleanedPath := filepath .FromSlash (filepath .Clean (path ))
133
+ sep := string (filepath .Separator )
134
+
135
+ volume := filepath .VolumeName (cleanedPath )
136
+ if volume != "" {
137
+ cleanedPath = cleanedPath [len (volume ):]
138
+ }
139
+
140
+ for cleanedPath != sep && cleanedPath != "." {
141
+ cleanedPath = filepath .Dir (cleanedPath )
142
+ count ++
139
143
}
140
144
return count
141
145
}
142
146
143
- // isWithin checks if newPath is within prevPath.
144
147
func isWithin (newPath , prevPath string ) bool {
145
148
return strings .HasPrefix (newPath , prevPath ) && newPath != prevPath
146
149
}
0 commit comments