github.com/ethereum/go-ethereum@v1.16.1/internal/build/file.go (about)

     1  // Copyright 2024 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package build
    18  
    19  import (
    20  	"crypto/sha256"
    21  	"io"
    22  	"os"
    23  	"path/filepath"
    24  	"sort"
    25  	"strings"
    26  )
    27  
    28  // FileExist checks if a file exists at path.
    29  func FileExist(path string) bool {
    30  	_, err := os.Stat(path)
    31  	if err != nil && os.IsNotExist(err) {
    32  		return false
    33  	}
    34  	return true
    35  }
    36  
    37  // HashFiles iterates the provided set of files, computing the hash of each.
    38  func HashFiles(files []string) (map[string][32]byte, error) {
    39  	res := make(map[string][32]byte)
    40  	for _, filePath := range files {
    41  		f, err := os.OpenFile(filePath, os.O_RDONLY, 0666)
    42  		if err != nil {
    43  			return nil, err
    44  		}
    45  		hasher := sha256.New()
    46  		if _, err := io.Copy(hasher, f); err != nil {
    47  			return nil, err
    48  		}
    49  		res[filePath] = [32]byte(hasher.Sum(nil))
    50  	}
    51  	return res, nil
    52  }
    53  
    54  // HashFolder iterates all files under the given directory, computing the hash
    55  // of each.
    56  func HashFolder(folder string, exlude []string) (map[string][32]byte, error) {
    57  	res := make(map[string][32]byte)
    58  	err := filepath.WalkDir(folder, func(path string, d os.DirEntry, _ error) error {
    59  		// Skip anything that's exluded or not a regular file
    60  		for _, skip := range exlude {
    61  			if strings.HasPrefix(path, filepath.FromSlash(skip)) {
    62  				return filepath.SkipDir
    63  			}
    64  		}
    65  		if !d.Type().IsRegular() {
    66  			return nil
    67  		}
    68  		// Regular file found, hash it
    69  		f, err := os.OpenFile(path, os.O_RDONLY, 0666)
    70  		if err != nil {
    71  			return err
    72  		}
    73  		hasher := sha256.New()
    74  		if _, err := io.Copy(hasher, f); err != nil {
    75  			return err
    76  		}
    77  		res[path] = [32]byte(hasher.Sum(nil))
    78  		return nil
    79  	})
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	return res, nil
    84  }
    85  
    86  // DiffHashes compares two maps of file hashes and returns the changed files.
    87  func DiffHashes(a map[string][32]byte, b map[string][32]byte) []string {
    88  	var updates []string
    89  
    90  	for file := range a {
    91  		if _, ok := b[file]; !ok || a[file] != b[file] {
    92  			updates = append(updates, file)
    93  		}
    94  	}
    95  	for file := range b {
    96  		if _, ok := a[file]; !ok {
    97  			updates = append(updates, file)
    98  		}
    99  	}
   100  	sort.Strings(updates)
   101  	return updates
   102  }