Skip to content

Commit

Permalink
Merge pull request #858 from nanjj/main
Browse files Browse the repository at this point in the history
Fixed multiple sys files not copied issue
  • Loading branch information
stgraber authored Jun 21, 2024
2 parents 9dc8a21 + 44f0a4f commit d59bfec
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 33 deletions.
28 changes: 16 additions & 12 deletions distrobuilder/main_repack-windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,25 +481,29 @@ func (c *cmdRepackWindows) injectDrivers(infDir, driversDir, filerepositoryDir,
}

for ext, dir := range map[string]string{"inf": infDir, "cat": driversDir, "dll": driversDir, "sys": driversDir} {
driverPath, err := shared.FindFirstMatch(sourceDir, fmt.Sprintf("*.%s", ext))
sourceMatches, err := shared.FindAllMatches(sourceDir, fmt.Sprintf("*.%s", ext))
if err != nil {
logger.Debugf("failed to find first match %q %q", driverName, ext)
continue
}

targetName := filepath.Base(driverPath)
if err = shared.Copy(driverPath, filepath.Join(targetBaseDir, targetName)); err != nil {
return err
}
for _, sourcePath := range sourceMatches {
targetName := filepath.Base(sourcePath)
targetPath := filepath.Join(targetBaseDir, targetName)
if err = shared.Copy(sourcePath, targetPath); err != nil {
return err
}

if ext == "cat" {
continue
} else if ext == "inf" {
targetName = infFilename
}
if ext == "cat" {
continue
} else if ext == "inf" {
targetName = infFilename
}

if err = shared.Copy(driverPath, filepath.Join(dir, targetName)); err != nil {
return err
targetPath = filepath.Join(dir, targetName)
if err = shared.Copy(sourcePath, targetPath); err != nil {
return err
}
}
}

Expand Down
16 changes: 13 additions & 3 deletions shared/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,26 @@ func CaseInsensitive(s string) (pattern string) {
return
}

// FindFirstMatch find the first file case insensitive.
// FindFirstMatch find the first matched file case insensitive.
func FindFirstMatch(dir string, elem ...string) (found string, err error) {
matches, err := FindAllMatches(dir, elem...)
if err != nil {
return
}

found = matches[0]
return
}

// FindAllMatches find all the matched files case insensitive.
func FindAllMatches(dir string, elem ...string) (matches []string, err error) {
names := []string{dir}
for _, name := range elem {
names = append(names, CaseInsensitive(name))
}

pattern := filepath.Join(names...)
matches, err := filepath.Glob(pattern)
matches, err = filepath.Glob(pattern)
if err != nil {
return
}
Expand All @@ -81,7 +92,6 @@ func FindFirstMatch(dir string, elem ...string) (found string, err error) {
return
}

found = matches[0]
return
}

Expand Down
81 changes: 63 additions & 18 deletions shared/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"os"
"path/filepath"
"slices"
"testing"

"github.com/flosch/pongo2/v4"
Expand Down Expand Up @@ -241,6 +242,43 @@ func TestCaseInsensitive(t *testing.T) {
}
}

func findMatchHelper(t *testing.T, filenames ...string) (dir string, actuals []string, rb func()) {
t.Helper()
dir, err := os.MkdirTemp("", "findmatch*")
if err != nil {
t.Fatal(err)
}

actuals = make([]string, len(filenames))
for i, filename := range filenames {
filename = filepath.Join(dir, filename)
err = os.MkdirAll(filepath.Dir(filename), 0755)
if err != nil {
t.Fatal(err)
}

file, err := os.Create(filename)
if err != nil {
t.Fatal(err)
}

err = file.Close()
if err != nil {
t.Fatal(err)
}

actuals[i] = filename
}

rb = func() {
err = os.RemoveAll(dir)
if err != nil {
t.Fatal(err)
}
}
return
}

func TestFindFirstMatch(t *testing.T) {
tcs := []struct {
name string
Expand All @@ -254,35 +292,42 @@ func TestFindFirstMatch(t *testing.T) {

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
dir, err := filepath.Abs("testing")
if err != nil {
t.Fatal(err)
}

actual := filepath.Join(dir, tc.actual)
err = os.MkdirAll(filepath.Dir(actual), 0755)
dir, actuals, rb := findMatchHelper(t, tc.actual)
defer rb()
actual := actuals[0]
want, err := FindFirstMatch(dir, tc.name)
if err != nil {
t.Fatal(err)
}

f, err := os.Create(actual)
if err != nil {
t.Fatal(err)
if want != actual {
t.Fatal(want, actual)
}
})
}
}

err = f.Close()
if err != nil {
t.Fatal(err)
}
func TestFindAllMatches(t *testing.T) {
tcs := []struct {
name string
filenames []string
}{
{"*.inf", []string{"vioinput.inf"}},
{"*.sys", []string{"viohidkmdf.sys", "vioinput.sys"}},
{"*.cat", []string{"vioinput.cat"}},
}

defer os.RemoveAll(dir)
want, err := FindFirstMatch(dir, tc.name)
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
dir, actuals, rb := findMatchHelper(t, tc.filenames...)
defer rb()
matches, err := FindAllMatches(dir, tc.name)
if err != nil {
t.Fatal(err)
}

if want != actual {
t.Fatal(want, actual)
if !slices.Equal(matches, actuals) {
t.Fatal(matches, actuals)
}
})
}
Expand Down

0 comments on commit d59bfec

Please sign in to comment.