Skip to content

Commit

Permalink
fix: rpm files section misses empty folders
Browse files Browse the repository at this point in the history
  • Loading branch information
M0Rf30 committed Nov 10, 2023
1 parent f018fdf commit 4b9c0c2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
38 changes: 28 additions & 10 deletions pkg/rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package rpm

import (
"fmt"
"os"
"io/fs"
"path/filepath"
"strings"

Expand Down Expand Up @@ -161,12 +161,22 @@ func (r *RPM) Update() error {
return nil
}

// getFiles retrieves the files from the RPM package directory and populates the PKGBUILD.Files field.
// getFiles retrieves a list of files from the RPM struct.
//
// It iterates over the files in the package directory and adds them to the PKGBUILD.Files slice.
// It also handles the backup paths specified in the PKGBUILD.Backup field.
// This function iterates over the list of backup paths in the PKGBUILD struct
// and adds them to the "backup" set. It then uses filepath.WalkDir to walk
// through the files in the PKGBUILD.PackageDir directory. For each file, it
// checks if it is a directory or a regular file. If it is a directory, it
// checks if it is an empty directory and adds it to the "files" list if it is.
// If it is a regular file, it adds it to the "files" list. After collecting all
// the file paths, it removes the parent directories of each file path from the
// "paths" set and adds the remaining file paths to the "PKGBUILD.Files" list in
// the RPM struct. If a file path is found in the "backup" set, it is marked as
// a config file and its path is prefixed with "%config". Finally, it returns
// nil, indicating that no error occurred.
//
// Returns an error if there is any issue while walking the directory or retrieving the files.
// No parameters.
// Returns an error if there was an issue retrieving the files.
func (r *RPM) getFiles() error {
backup := set.NewSet()
paths := set.NewSet()
Expand All @@ -181,14 +191,22 @@ func (r *RPM) getFiles() error {

var files []string

err := filepath.Walk(r.PKGBUILD.PackageDir,
func(path string,
info os.FileInfo, err error) error {
if !info.IsDir() {
err := filepath.WalkDir(r.PKGBUILD.PackageDir,
func(path string, dirEntry fs.DirEntry, err error) error {
if err != nil {
return err
}

if dirEntry.IsDir() {
isEmptyDir := utils.IsEmptyDir(path, dirEntry)
if isEmptyDir {
files = append(files, path)
}
} else {
files = append(files, path)
}

return err
return nil
})

if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions pkg/utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,26 @@ func GetDirSize(path string) (int64, error) {
return size, err
}

// IsEmptyDir checks if a directory is empty.
//
// It takes in two parameters: path, a string representing the directory path,
// and dirEntry, an os.DirEntry representing the directory entry. It returns a
// boolean value indicating whether the directory is empty or not.
func IsEmptyDir(path string, dirEntry os.DirEntry) bool {
cleanFilePath := filepath.Clean(path)

if !dirEntry.IsDir() {
return false
}

entries, err := os.ReadDir(cleanFilePath)
if err != nil {
return false
}

return len(entries) == 0
}

// MkdirAll creates a directory and all its parent directories.
//
// It takes a string parameter `path` which represents the path of the directory to be created.
Expand Down

0 comments on commit 4b9c0c2

Please sign in to comment.