golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/gorebuild/windows.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"errors"
     9  	"io/fs"
    10  	"os"
    11  	"os/exec"
    12  	"path/filepath"
    13  	"strings"
    14  )
    15  
    16  type msiFile struct {
    17  	Name   string
    18  	Size   int64
    19  	SHA256 string
    20  }
    21  
    22  // DiffWindowsMsi diffs the content of the Windows msi and zip files provided,
    23  // logging differences. It returns true if the files were successfully parsed
    24  // and contain the same files, false otherwise.
    25  func DiffWindowsMsi(log *Log, zip, msi []byte) (ok, skip bool) {
    26  	check := func(log *Log, rebuilt *ZipFile, posted *msiFile) bool {
    27  		match := true
    28  		name := rebuilt.Name
    29  		field := func(what string, rebuilt, posted any) {
    30  			if posted != rebuilt {
    31  				log.Printf("%s: rebuilt %s = %v, posted = %v", name, what, rebuilt, posted)
    32  				match = false
    33  			}
    34  		}
    35  		r := rebuilt
    36  		p := posted
    37  		field("name", r.Name, p.Name)
    38  		field("size", int64(r.UncompressedSize64), p.Size)
    39  		field("content", r.SHA256, p.SHA256)
    40  		return match
    41  	}
    42  
    43  	ix, skip := indexMsi(log, msi, nil)
    44  	if skip {
    45  		return
    46  	}
    47  
    48  	return DiffArchive(log, IndexZip(log, zip, nil), ix, check), false
    49  }
    50  
    51  func indexMsi(log *Log, msi []byte, fix Fixer) (m map[string]*msiFile, skip bool) {
    52  	dir, err := os.MkdirTemp("", "gorebuild-")
    53  	if err != nil {
    54  		log.Printf("%v", err)
    55  		return nil, false
    56  	}
    57  	defer os.RemoveAll(dir)
    58  
    59  	tmpmsi := filepath.Join(dir, "go.msi")
    60  	if err := os.WriteFile(tmpmsi, msi, 0666); err != nil {
    61  		log.Printf("%v", err)
    62  		return nil, false
    63  	}
    64  
    65  	cmd := exec.Command("msiextract", tmpmsi)
    66  	cmd.Dir = dir
    67  	out, err := cmd.CombinedOutput()
    68  	if err != nil {
    69  		log.Printf("msiextract: %s\n%s", err, out)
    70  		return nil, errors.Is(err, exec.ErrNotFound)
    71  	}
    72  	// msiextract lists every file, so don't show the output on success.
    73  
    74  	// amd64 installer uses Go but 386 uses Program Files\Go. Try both.
    75  	root := filepath.Join(dir, "Go")
    76  	if _, err := os.Stat(root); err != nil {
    77  		root = filepath.Join(dir, `Program Files/Go`)
    78  	}
    79  
    80  	ix := make(map[string]*msiFile)
    81  	err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
    82  		if err != nil {
    83  			return err
    84  		}
    85  		if d.IsDir() {
    86  			return nil
    87  		}
    88  		data, err := os.ReadFile(path)
    89  		if err != nil {
    90  			return err
    91  		}
    92  		name := "go/" + filepath.ToSlash(strings.TrimPrefix(path, root+string(filepath.Separator)))
    93  		if fix != nil {
    94  			data = fix(log, name, data)
    95  		}
    96  		ix[name] = &msiFile{name, int64(len(data)), SHA256(data)}
    97  		return nil
    98  	})
    99  	if err != nil {
   100  		log.Printf("%v", err)
   101  		return nil, false
   102  	}
   103  	return ix, false
   104  }