3535 }
3636)
3737
38+ var paths = map [string ]struct {}{}
39+
3840func (cfg Config ) Validate (c path.ContextPath ) (r report.Report ) {
3941 systemdPath := "/etc/systemd/system/"
4042 unitPaths := map [string ]struct {}{}
@@ -76,43 +78,21 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
7678 Path string
7779 Field string
7880 }
79- paths := map [string ]struct {}{}
8081 r := report.Report {}
8182
8283 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 )
9286 }
9387
9488 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 )
10491 }
10592
10693 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 )
11696 }
11797
11898 sort .Slice (entries , func (i , j int ) bool {
@@ -122,7 +102,7 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
122102 for i , entry := range entries {
123103 if i > 0 && isWithin (entry .Path , entries [i - 1 ].Path ) {
124104 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 )
126106 return r
127107 }
128108 }
@@ -131,16 +111,37 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
131111 return r
132112}
133113
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+
135128func depth (path string ) uint {
136129 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 ++
139141 }
140142 return count
141143}
142144
143- // isWithin checks if newPath is within prevPath.
144145func isWithin (newPath , prevPath string ) bool {
145146 return strings .HasPrefix (newPath , prevPath ) && newPath != prevPath
146147}
0 commit comments