github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/host/machine_info.go (about)

     1  // Copyright 2020 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package host
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/google/syzkaller/pkg/flatrpc"
    12  )
    13  
    14  func ReadFiles(files []string) []*flatrpc.FileInfo {
    15  	var res []*flatrpc.FileInfo
    16  	for _, glob := range files {
    17  		glob = filepath.FromSlash(glob)
    18  		if !strings.Contains(glob, "*") {
    19  			res = append(res, readFile(glob))
    20  			continue
    21  		}
    22  		matches, err := filepath.Glob(glob)
    23  		if err != nil {
    24  			res = append(res, &flatrpc.FileInfo{
    25  				Name:  glob,
    26  				Error: err.Error(),
    27  			})
    28  			continue
    29  		}
    30  		for _, file := range matches {
    31  			res = append(res, readFile(file))
    32  		}
    33  	}
    34  	return res
    35  }
    36  
    37  func readFile(file string) *flatrpc.FileInfo {
    38  	data, err := os.ReadFile(file)
    39  	exists, errStr := true, ""
    40  	if err != nil {
    41  		exists, errStr = !os.IsNotExist(err), err.Error()
    42  	}
    43  	return &flatrpc.FileInfo{
    44  		Name:   file,
    45  		Exists: exists,
    46  		Error:  errStr,
    47  		Data:   data,
    48  	}
    49  }