Skip to content

Commit

Permalink
OVA: Change the scanning for ova and ovf files inside the NFS directory
Browse files Browse the repository at this point in the history
Signed-off-by: Bella Khizgiyaev <bkhizgiy@redhat.com>
  • Loading branch information
bkhizgiy committed Sep 6, 2023
1 parent a1671ed commit f660427
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions cmd/ova-provider-server/ova-provider-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
const (
invalidRequestMethodMsg = "Invalid request method"
errorProcessingOvfMsg = "Error processing OVF file"
OvaExt = ".ova"
OvfExt = ".ovf"
)

// xml struct
Expand Down Expand Up @@ -293,34 +295,52 @@ func scanOVAsOnNFS() (envelopes []Envelope, ovaPaths []string) {
}

func findOVAFiles(directory string) ([]string, []string, error) {
childs, err := os.ReadDir(directory)
if err != nil {
return nil, nil, err
}

var ovaFiles, ovfFiles []string
for _, child := range childs {
if !child.IsDir() {
continue
}
newDir := directory + "/" + child.Name()
files, err := os.ReadDir(newDir)
var ovaMaxDepth = 2

err := filepath.WalkDir(directory, func(path string, info os.DirEntry, err error) error {
if err != nil {
return nil, nil, err
return err
}
for _, file := range files {
path := filepath.Join(directory, child.Name(), file.Name())
switch {
case strings.HasSuffix(strings.ToLower(file.Name()), ".ova"):
ovaFiles = append(ovaFiles, path)
case strings.HasSuffix(strings.ToLower(file.Name()), ".ovf"):
ovfFiles = append(ovfFiles, path)
}

if info.IsDir() {
return nil
}

relativePath, _ := filepath.Rel(directory, path)
depth := len(strings.Split(relativePath, string(filepath.Separator)))

switch {
case (depth <= ovaMaxDepth) && isOva(info.Name()):
ovaFiles = append(ovaFiles, path)

case (depth >= ovaMaxDepth && depth <= ovaMaxDepth+1) && isOvf(info.Name()):
ovfFiles = append(ovfFiles, path)
}

return nil
})

if err != nil {
fmt.Println("Error scanning OVA and OVF files: ", err)
return nil, nil, err
}
return ovaFiles, ovfFiles, nil
}

func isOva(filename string) bool {
return hasSuffixIgnoreCase(filename, OvaExt)
}

func isOvf(filename string) bool {
return hasSuffixIgnoreCase(filename, OvfExt)
}

// Checks if the given file has the desired extension
func hasSuffixIgnoreCase(fileName, suffix string) bool {
return strings.HasSuffix(strings.ToLower(fileName), strings.ToLower(suffix))
}

func readOVFFromOVA(ovaFile string) (*Envelope, error) {
var envelope Envelope
file, err := os.Open(ovaFile)
Expand Down

0 comments on commit f660427

Please sign in to comment.