Skip to content

Commit a5da397

Browse files
nanjjMusicDin
authored andcommitted
lxd-imagebuilder: Fixed multiple sys files not copied issue
Driver like vioinput has multiple sys files `viohidkmdf.sys` and `vioinput.sys`, without `vioinput.sys` copied, the vioinput driver not work correctly. The fix add the multiple files copied back. Signed-off-by: JUN JIE NAN <[email protected]> (cherry picked from commit 44f0a4f35072aeada023fa5bd3a0094d51fce055) Signed-off-by: Din Music <[email protected]> License: Apache-2.0
1 parent 046c6fc commit a5da397

File tree

3 files changed

+92
-33
lines changed

3 files changed

+92
-33
lines changed

lxd-imagebuilder/main_repack-windows.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -481,25 +481,29 @@ func (c *cmdRepackWindows) injectDrivers(infDir, driversDir, filerepositoryDir,
481481
}
482482

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

490-
targetName := filepath.Base(driverPath)
491-
if err = shared.Copy(driverPath, filepath.Join(targetBaseDir, targetName)); err != nil {
492-
return err
493-
}
490+
for _, sourcePath := range sourceMatches {
491+
targetName := filepath.Base(sourcePath)
492+
targetPath := filepath.Join(targetBaseDir, targetName)
493+
if err = shared.Copy(sourcePath, targetPath); err != nil {
494+
return err
495+
}
494496

495-
if ext == "cat" {
496-
continue
497-
} else if ext == "inf" {
498-
targetName = infFilename
499-
}
497+
if ext == "cat" {
498+
continue
499+
} else if ext == "inf" {
500+
targetName = infFilename
501+
}
500502

501-
if err = shared.Copy(driverPath, filepath.Join(dir, targetName)); err != nil {
502-
return err
503+
targetPath = filepath.Join(dir, targetName)
504+
if err = shared.Copy(sourcePath, targetPath); err != nil {
505+
return err
506+
}
503507
}
504508
}
505509

shared/util.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,26 @@ func CaseInsensitive(s string) (pattern string) {
6868
return
6969
}
7070

71-
// FindFirstMatch find the first file case insensitive.
71+
// FindFirstMatch find the first matched file case insensitive.
7272
func FindFirstMatch(dir string, elem ...string) (found string, err error) {
73+
matches, err := FindAllMatches(dir, elem...)
74+
if err != nil {
75+
return
76+
}
77+
78+
found = matches[0]
79+
return
80+
}
81+
82+
// FindAllMatches find all the matched files case insensitive.
83+
func FindAllMatches(dir string, elem ...string) (matches []string, err error) {
7384
names := []string{dir}
7485
for _, name := range elem {
7586
names = append(names, CaseInsensitive(name))
7687
}
7788

7889
pattern := filepath.Join(names...)
79-
matches, err := filepath.Glob(pattern)
90+
matches, err = filepath.Glob(pattern)
8091
if err != nil {
8192
return
8293
}
@@ -86,7 +97,6 @@ func FindFirstMatch(dir string, elem ...string) (found string, err error) {
8697
return
8798
}
8899

89-
found = matches[0]
90100
return
91101
}
92102

shared/util_test.go

+63-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55
"os"
66
"path/filepath"
7+
"slices"
78
"testing"
89

910
"github.com/flosch/pongo2/v4"
@@ -241,6 +242,43 @@ func TestCaseInsensitive(t *testing.T) {
241242
}
242243
}
243244

245+
func findMatchHelper(t *testing.T, filenames ...string) (dir string, actuals []string, rb func()) {
246+
t.Helper()
247+
dir, err := os.MkdirTemp("", "findmatch*")
248+
if err != nil {
249+
t.Fatal(err)
250+
}
251+
252+
actuals = make([]string, len(filenames))
253+
for i, filename := range filenames {
254+
filename = filepath.Join(dir, filename)
255+
err = os.MkdirAll(filepath.Dir(filename), 0755)
256+
if err != nil {
257+
t.Fatal(err)
258+
}
259+
260+
file, err := os.Create(filename)
261+
if err != nil {
262+
t.Fatal(err)
263+
}
264+
265+
err = file.Close()
266+
if err != nil {
267+
t.Fatal(err)
268+
}
269+
270+
actuals[i] = filename
271+
}
272+
273+
rb = func() {
274+
err = os.RemoveAll(dir)
275+
if err != nil {
276+
t.Fatal(err)
277+
}
278+
}
279+
return
280+
}
281+
244282
func TestFindFirstMatch(t *testing.T) {
245283
tcs := []struct {
246284
name string
@@ -254,35 +292,42 @@ func TestFindFirstMatch(t *testing.T) {
254292

255293
for _, tc := range tcs {
256294
t.Run(tc.name, func(t *testing.T) {
257-
dir, err := filepath.Abs("testing")
258-
if err != nil {
259-
t.Fatal(err)
260-
}
261-
262-
actual := filepath.Join(dir, tc.actual)
263-
err = os.MkdirAll(filepath.Dir(actual), 0755)
295+
dir, actuals, rb := findMatchHelper(t, tc.actual)
296+
defer rb()
297+
actual := actuals[0]
298+
want, err := FindFirstMatch(dir, tc.name)
264299
if err != nil {
265300
t.Fatal(err)
266301
}
267302

268-
f, err := os.Create(actual)
269-
if err != nil {
270-
t.Fatal(err)
303+
if want != actual {
304+
t.Fatal(want, actual)
271305
}
306+
})
307+
}
308+
}
272309

273-
err = f.Close()
274-
if err != nil {
275-
t.Fatal(err)
276-
}
310+
func TestFindAllMatches(t *testing.T) {
311+
tcs := []struct {
312+
name string
313+
filenames []string
314+
}{
315+
{"*.inf", []string{"vioinput.inf"}},
316+
{"*.sys", []string{"viohidkmdf.sys", "vioinput.sys"}},
317+
{"*.cat", []string{"vioinput.cat"}},
318+
}
277319

278-
defer os.RemoveAll(dir)
279-
want, err := FindFirstMatch(dir, tc.name)
320+
for _, tc := range tcs {
321+
t.Run(tc.name, func(t *testing.T) {
322+
dir, actuals, rb := findMatchHelper(t, tc.filenames...)
323+
defer rb()
324+
matches, err := FindAllMatches(dir, tc.name)
280325
if err != nil {
281326
t.Fatal(err)
282327
}
283328

284-
if want != actual {
285-
t.Fatal(want, actual)
329+
if !slices.Equal(matches, actuals) {
330+
t.Fatal(matches, actuals)
286331
}
287332
})
288333
}

0 commit comments

Comments
 (0)