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