| 1 | package hostimage |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "syscall" |
| 10 | |
| 11 | "golang.org/x/sys/windows" |
| 12 | ) |
| 13 | |
| 14 | func expandWindowsCatalog(ctx context.Context, data []byte) ([]byte, error) { |
| 15 | directory, err := os.MkdirTemp("", "cage-system-catalog-") |
| 16 | if err != nil { |
| 17 | return nil, err |
| 18 | } |
| 19 | defer os.RemoveAll(directory) |
| 20 | archive := filepath.Join(directory, "products.cab") |
| 21 | if err = os.WriteFile(archive, data, 0600); err != nil { |
| 22 | return nil, err |
| 23 | } |
| 24 | system, err := windows.GetSystemDirectory() |
| 25 | if err != nil { |
| 26 | return nil, err |
| 27 | } |
| 28 | command := exec.CommandContext(ctx, filepath.Join(system, "expand.exe"), "-F:products.xml", archive, directory) |
| 29 | command.SysProcAttr = &syscall.SysProcAttr{HideWindow: true, CreationFlags: windows.CREATE_NO_WINDOW} |
| 30 | if err = command.Run(); err != nil { |
| 31 | return nil, err |
| 32 | } |
| 33 | path := filepath.Join(directory, "products.xml") |
| 34 | info, err := os.Lstat(path) |
| 35 | if err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | if !info.Mode().IsRegular() || info.Size() > 8<<20 { |
| 39 | return nil, errors.New("微软安装目录文件无效") |
| 40 | } |
| 41 | return os.ReadFile(path) |
| 42 | } |