Skip to content

Commit

Permalink
storage.go: refactor validate()
Browse files Browse the repository at this point in the history
  • Loading branch information
prestist committed Sep 29, 2022
1 parent 440d26f commit c6025cd
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 5 deletions.
18 changes: 17 additions & 1 deletion config/v3_0/types/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,34 @@ func (s Storage) MergedKeys() map[string]string {
}

func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
s.validateDirectories(c, &r)
s.validateFiles(c, &r)
s.validateLinks(c, &r)
s.validateFilesystems(c, &r)
return
}

func (s Storage) validateDirectories(c vpath.ContextPath, r *report.Report) {
for i, d := range s.Directories {
for _, l := range s.Links {
if strings.HasPrefix(d.Path, l.Path+"/") {
r.AddOnError(c.Append("directories", i), errors.ErrDirectoryUsedSymlink)
}
}
}
}

func (s Storage) validateFiles(c vpath.ContextPath, r *report.Report) {
for i, f := range s.Files {
for _, l := range s.Links {
if strings.HasPrefix(f.Path, l.Path+"/") {
r.AddOnError(c.Append("files", i), errors.ErrFileUsedSymlink)
}
}
}
}

func (s Storage) validateLinks(c vpath.ContextPath, r *report.Report) {
for i, l1 := range s.Links {
for _, l2 := range s.Links {
if strings.HasPrefix(l1.Path, l2.Path+"/") {
Expand All @@ -67,6 +81,9 @@ func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
}
}
}
}

func (s Storage) validateFilesystems(c vpath.ContextPath, r *report.Report) {
disks := make(map[string]Disk)
for _, d := range s.Disks {
disks[d.Device] = d
Expand All @@ -82,5 +99,4 @@ func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
}
}
}
return
}
18 changes: 17 additions & 1 deletion config/v3_1/types/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,34 @@ func (s Storage) MergedKeys() map[string]string {
}

func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
s.validateDirectories(c, &r)
s.validateFiles(c, &r)
s.validateLinks(c, &r)
s.validateFilesystems(c, &r)
return
}

func (s Storage) validateDirectories(c vpath.ContextPath, r *report.Report) {
for i, d := range s.Directories {
for _, l := range s.Links {
if strings.HasPrefix(d.Path, l.Path+"/") {
r.AddOnError(c.Append("directories", i), errors.ErrDirectoryUsedSymlink)
}
}
}
}

func (s Storage) validateFiles(c vpath.ContextPath, r *report.Report) {
for i, f := range s.Files {
for _, l := range s.Links {
if strings.HasPrefix(f.Path, l.Path+"/") {
r.AddOnError(c.Append("files", i), errors.ErrFileUsedSymlink)
}
}
}
}

func (s Storage) validateLinks(c vpath.ContextPath, r *report.Report) {
for i, l1 := range s.Links {
for _, l2 := range s.Links {
if strings.HasPrefix(l1.Path, l2.Path+"/") {
Expand All @@ -67,6 +81,9 @@ func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
}
}
}
}

func (s Storage) validateFilesystems(c vpath.ContextPath, r *report.Report) {
disks := make(map[string]Disk)
for _, d := range s.Disks {
disks[d.Device] = d
Expand All @@ -82,5 +99,4 @@ func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
}
}
}
return
}
18 changes: 17 additions & 1 deletion config/v3_2/types/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,34 @@ func (s Storage) MergedKeys() map[string]string {
}

func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
s.validateDirectories(c, &r)
s.validateFiles(c, &r)
s.validateLinks(c, &r)
s.validateFilesystems(c, &r)
return
}

func (s Storage) validateDirectories(c vpath.ContextPath, r *report.Report) {
for i, d := range s.Directories {
for _, l := range s.Links {
if strings.HasPrefix(d.Path, l.Path+"/") {
r.AddOnError(c.Append("directories", i), errors.ErrDirectoryUsedSymlink)
}
}
}
}

func (s Storage) validateFiles(c vpath.ContextPath, r *report.Report) {
for i, f := range s.Files {
for _, l := range s.Links {
if strings.HasPrefix(f.Path, l.Path+"/") {
r.AddOnError(c.Append("files", i), errors.ErrFileUsedSymlink)
}
}
}
}

func (s Storage) validateLinks(c vpath.ContextPath, r *report.Report) {
for i, l1 := range s.Links {
for _, l2 := range s.Links {
if strings.HasPrefix(l1.Path, l2.Path+"/") {
Expand All @@ -67,6 +81,9 @@ func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
}
}
}
}

func (s Storage) validateFilesystems(c vpath.ContextPath, r *report.Report) {
disks := make(map[string]Disk)
for _, d := range s.Disks {
disks[d.Device] = d
Expand All @@ -82,5 +99,4 @@ func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
}
}
}
return
}
18 changes: 17 additions & 1 deletion config/v3_3/types/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,34 @@ func (s Storage) MergedKeys() map[string]string {
}

func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
s.validateDirectories(c, &r)
s.validateFiles(c, &r)
s.validateLinks(c, &r)
s.validateFilesystems(c, &r)
return
}

func (s Storage) validateDirectories(c vpath.ContextPath, r *report.Report) {
for i, d := range s.Directories {
for _, l := range s.Links {
if strings.HasPrefix(d.Path, l.Path+"/") {
r.AddOnError(c.Append("directories", i), errors.ErrDirectoryUsedSymlink)
}
}
}
}

func (s Storage) validateFiles(c vpath.ContextPath, r *report.Report) {
for i, f := range s.Files {
for _, l := range s.Links {
if strings.HasPrefix(f.Path, l.Path+"/") {
r.AddOnError(c.Append("files", i), errors.ErrFileUsedSymlink)
}
}
}
}

func (s Storage) validateLinks(c vpath.ContextPath, r *report.Report) {
for i, l1 := range s.Links {
for _, l2 := range s.Links {
if strings.HasPrefix(l1.Path, l2.Path+"/") {
Expand All @@ -71,6 +85,9 @@ func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
}
}
}
}

func (s Storage) validateFilesystems(c vpath.ContextPath, r *report.Report) {
disks := make(map[string]Disk)
for _, d := range s.Disks {
disks[d.Device] = d
Expand All @@ -86,5 +103,4 @@ func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
}
}
}
return
}
18 changes: 17 additions & 1 deletion config/v3_4_experimental/types/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,34 @@ func (s Storage) MergedKeys() map[string]string {
}

func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
s.validateDirectories(c, &r)
s.validateFiles(c, &r)
s.validateLinks(c, &r)
s.validateFilesystems(c, &r)
return
}

func (s Storage) validateDirectories(c vpath.ContextPath, r *report.Report) {
for i, d := range s.Directories {
for _, l := range s.Links {
if strings.HasPrefix(d.Path, l.Path+"/") {
r.AddOnError(c.Append("directories", i), errors.ErrDirectoryUsedSymlink)
}
}
}
}

func (s Storage) validateFiles(c vpath.ContextPath, r *report.Report) {
for i, f := range s.Files {
for _, l := range s.Links {
if strings.HasPrefix(f.Path, l.Path+"/") {
r.AddOnError(c.Append("files", i), errors.ErrFileUsedSymlink)
}
}
}
}

func (s Storage) validateLinks(c vpath.ContextPath, r *report.Report) {
for i, l1 := range s.Links {
for _, l2 := range s.Links {
if strings.HasPrefix(l1.Path, l2.Path+"/") {
Expand All @@ -71,6 +85,9 @@ func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
}
}
}
}

func (s Storage) validateFilesystems(c vpath.ContextPath, r *report.Report) {
disks := make(map[string]Disk)
for _, d := range s.Disks {
disks[d.Device] = d
Expand All @@ -86,5 +103,4 @@ func (s Storage) Validate(c vpath.ContextPath) (r report.Report) {
}
}
}
return
}

0 comments on commit c6025cd

Please sign in to comment.