Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement FindFileWithPrefixRecursively #265

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/elemental/elemental.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package elemental
import (
"errors"
"fmt"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
"github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
"path/filepath"
"strings"

v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"

agentConfig "github.com/kairos-io/kairos-agent/v2/pkg/config"
cnst "github.com/kairos-io/kairos-agent/v2/pkg/constants"
"github.com/kairos-io/kairos-agent/v2/pkg/partitioner"
Expand Down Expand Up @@ -560,12 +561,12 @@ func (e Elemental) SetDefaultGrubEntry(partMountPoint string, imgMountPoint stri
func (e Elemental) FindKernelInitrd(rootDir string) (kernel string, initrd string, err error) {
kernelNames := []string{"uImage", "Image", "zImage", "vmlinuz", "image"}
initrdNames := []string{"initrd", "initramfs"}
kernel, err = utils.FindFileWithPrefix(e.config.Fs, filepath.Join(rootDir, "boot"), kernelNames...)
kernel, err = utils.FindFileWithPrefixRecursively(e.config.Fs, filepath.Join(rootDir, "boot"), kernelNames...)
if err != nil {
e.config.Logger.Errorf("No Kernel file found")
return "", "", err
}
initrd, err = utils.FindFileWithPrefix(e.config.Fs, filepath.Join(rootDir, "boot"), initrdNames...)
initrd, err = utils.FindFileWithPrefixRecursively(e.config.Fs, filepath.Join(rootDir, "boot"), initrdNames...)
if err != nil {
e.config.Logger.Errorf("No initrd file found")
return "", "", err
Expand Down
71 changes: 43 additions & 28 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/sha256"
"errors"
"fmt"
sdkTypes "github.com/kairos-io/kairos-sdk/types"
"io"
random "math/rand"
"net/url"
Expand All @@ -32,6 +31,8 @@ import (
"strings"
"time"

sdkTypes "github.com/kairos-io/kairos-sdk/types"

"github.com/kairos-io/kairos-sdk/state"

agentConfig "github.com/kairos-io/kairos-agent/v2/pkg/config"
Expand Down Expand Up @@ -437,37 +438,15 @@ func ValidTaggedContainerReference(ref string) bool {
return true
}

func FindFileWithPrefixRecursively(fs v1.FS, path string, prefixes ...string) (string, error) {
return findFileWithPrefix(true, fs, filepath.Join(path, path), prefixes...)
}

// FindFileWithPrefix looks for a file in the given path matching one of the given
// prefixes. Returns the found file path including the given path. It does not
// check subfolders recusively
func FindFileWithPrefix(fs v1.FS, path string, prefixes ...string) (string, error) {
files, err := fs.ReadDir(path)
if err != nil {
return "", err
}
for _, f := range files {
if f.IsDir() {
continue
}
for _, p := range prefixes {
if strings.HasPrefix(f.Name(), p) {
if f.Mode()&os.ModeSymlink == os.ModeSymlink {
found, err := fs.Readlink(filepath.Join(path, f.Name()))
if err == nil {
if !filepath.IsAbs(found) {
found = filepath.Join(path, found)
}
if exists, _ := fsutils.Exists(fs, found); exists {
return found, nil
}
}
} else {
return filepath.Join(path, f.Name()), nil
}
}
}
}
return "", fmt.Errorf("No file found with prefixes: %v", prefixes)
return findFileWithPrefix(false, fs, filepath.Join(path, path), prefixes...)
}

// CalcFileChecksum opens the given file and returns the sha256 checksum of it.
Expand Down Expand Up @@ -595,3 +574,39 @@ func SystemdBootConfWriter(fs v1.FS, filePath string, conf map[string]string) er

return writer.Flush()
}

func findFileWithPrefix(recursively bool, fs v1.FS, path string, prefixes ...string) (string, error) {
files, err := fs.ReadDir(path)
if err != nil {
return "", err
}
for _, f := range files {
if f.IsDir() {
if recursively {
f, err := findFileWithPrefix(recursively, fs, filepath.Join(path, f.Name()), prefixes...)
if err == nil {
return f, nil
}
}
continue
}
for _, p := range prefixes {
if strings.HasPrefix(f.Name(), p) {
if f.Mode()&os.ModeSymlink == os.ModeSymlink {
found, err := fs.Readlink(filepath.Join(path, f.Name()))
if err == nil {
if !filepath.IsAbs(found) {
found = filepath.Join(path, found)
}
if exists, _ := fsutils.Exists(fs, found); exists {
return found, nil
}
}
} else {
return filepath.Join(path, f.Name()), nil
}
}
}
}
return "", fmt.Errorf("No file found with prefixes: %v", prefixes)
}