Source file src/cmd/go/internal/cache/cache.go

     1  // Copyright 2017 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 cache implements a build artifact cache.
     6  package cache
     7  
     8  import (
     9  	"bytes"
    10  	"crypto/sha256"
    11  	"encoding/hex"
    12  	"errors"
    13  	"fmt"
    14  	"internal/godebug"
    15  	"io"
    16  	"io/fs"
    17  	"os"
    18  	"path/filepath"
    19  	"strconv"
    20  	"strings"
    21  	"time"
    22  
    23  	"cmd/go/internal/base"
    24  	"cmd/go/internal/lockedfile"
    25  	"cmd/go/internal/mmap"
    26  )
    27  
    28  // An ActionID is a cache action key, the hash of a complete description of a
    29  // repeatable computation (command line, environment variables,
    30  // input file contents, executable contents).
    31  type ActionID [HashSize]byte
    32  
    33  // An OutputID is a cache output key, the hash of an output of a computation.
    34  type OutputID [HashSize]byte
    35  
    36  // Cache is the interface as used by the cmd/go.
    37  type Cache interface {
    38  	// Get returns the cache entry for the provided ActionID.
    39  	// On miss, the error type should be of type *entryNotFoundError.
    40  	//
    41  	// After a successful call to Get, OutputFile(Entry.OutputID) must
    42  	// exist on disk until Close is called (at the end of the process).
    43  	Get(ActionID) (Entry, error)
    44  
    45  	// Put adds an item to the cache.
    46  	//
    47  	// The seeker is only used to seek to the beginning. After a call to Put,
    48  	// the seek position is not guaranteed to be in any particular state.
    49  	//
    50  	// As a special case, if the ReadSeeker is of type noVerifyReadSeeker,
    51  	// the verification from GODEBUG=goverifycache=1 is skipped.
    52  	//
    53  	// After a successful call to Put, OutputFile(OutputID) must
    54  	// exist on disk until Close is called (at the end of the process).
    55  	Put(ActionID, io.ReadSeeker) (_ OutputID, size int64, _ error)
    56  
    57  	// Close is called at the end of the go process. Implementations can do
    58  	// cache cleanup work at this phase, or wait for and report any errors from
    59  	// background cleanup work started earlier. Any cache trimming in one
    60  	// process should not cause the invariants of this interface to be
    61  	// violated in another process. Namely, a cache trim from one process should
    62  	// not delete an OutputID from disk that was recently Get or Put from
    63  	// another process. As a rule of thumb, don't trim things used in the last
    64  	// day.
    65  	Close() error
    66  
    67  	// OutputFile returns the path on disk where OutputID is stored.
    68  	//
    69  	// It's only called after a successful get or put call so it doesn't need
    70  	// to return an error; it's assumed that if the previous get or put succeeded,
    71  	// it's already on disk.
    72  	OutputFile(OutputID) string
    73  
    74  	// FuzzDir returns where fuzz files are stored.
    75  	FuzzDir() string
    76  }
    77  
    78  // A Cache is a package cache, backed by a file system directory tree.
    79  type DiskCache struct {
    80  	dir string
    81  	now func() time.Time
    82  }
    83  
    84  // Open opens and returns the cache in the given directory.
    85  //
    86  // It is safe for multiple processes on a single machine to use the
    87  // same cache directory in a local file system simultaneously.
    88  // They will coordinate using operating system file locks and may
    89  // duplicate effort but will not corrupt the cache.
    90  //
    91  // However, it is NOT safe for multiple processes on different machines
    92  // to share a cache directory (for example, if the directory were stored
    93  // in a network file system). File locking is notoriously unreliable in
    94  // network file systems and may not suffice to protect the cache.
    95  func Open(dir string) (*DiskCache, error) {
    96  	info, err := os.Stat(dir)
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  	if !info.IsDir() {
   101  		return nil, &fs.PathError{Op: "open", Path: dir, Err: fmt.Errorf("not a directory")}
   102  	}
   103  	for i := 0; i < 256; i++ {
   104  		name := filepath.Join(dir, fmt.Sprintf("%02x", i))
   105  		if err := os.MkdirAll(name, 0o777); err != nil {
   106  			return nil, err
   107  		}
   108  	}
   109  	c := &DiskCache{
   110  		dir: dir,
   111  		now: time.Now,
   112  	}
   113  	return c, nil
   114  }
   115  
   116  // fileName returns the name of the file corresponding to the given id.
   117  func (c *DiskCache) fileName(id [HashSize]byte, key string) string {
   118  	return filepath.Join(c.dir, fmt.Sprintf("%02x", id[0]), fmt.Sprintf("%x", id)+"-"+key)
   119  }
   120  
   121  // An entryNotFoundError indicates that a cache entry was not found, with an
   122  // optional underlying reason.
   123  type entryNotFoundError struct {
   124  	Err error
   125  }
   126  
   127  func (e *entryNotFoundError) Error() string {
   128  	if e.Err == nil {
   129  		return "cache entry not found"
   130  	}
   131  	return fmt.Sprintf("cache entry not found: %v", e.Err)
   132  }
   133  
   134  func (e *entryNotFoundError) Unwrap() error {
   135  	return e.Err
   136  }
   137  
   138  const (
   139  	// action entry file is "v1 <hex id> <hex out> <decimal size space-padded to 20 bytes> <unixnano space-padded to 20 bytes>\n"
   140  	hexSize   = HashSize * 2
   141  	entrySize = 2 + 1 + hexSize + 1 + hexSize + 1 + 20 + 1 + 20 + 1
   142  )
   143  
   144  // verify controls whether to run the cache in verify mode.
   145  // In verify mode, the cache always returns errMissing from Get
   146  // but then double-checks in Put that the data being written
   147  // exactly matches any existing entry. This provides an easy
   148  // way to detect program behavior that would have been different
   149  // had the cache entry been returned from Get.
   150  //
   151  // verify is enabled by setting the environment variable
   152  // GODEBUG=gocacheverify=1.
   153  var verify = false
   154  
   155  var errVerifyMode = errors.New("gocacheverify=1")
   156  
   157  // DebugTest is set when GODEBUG=gocachetest=1 is in the environment.
   158  var DebugTest = false
   159  
   160  func init() { initEnv() }
   161  
   162  var (
   163  	gocacheverify = godebug.New("gocacheverify")
   164  	gocachehash   = godebug.New("gocachehash")
   165  	gocachetest   = godebug.New("gocachetest")
   166  )
   167  
   168  func initEnv() {
   169  	if gocacheverify.Value() == "1" {
   170  		gocacheverify.IncNonDefault()
   171  		verify = true
   172  	}
   173  	if gocachehash.Value() == "1" {
   174  		gocachehash.IncNonDefault()
   175  		debugHash = true
   176  	}
   177  	if gocachetest.Value() == "1" {
   178  		gocachetest.IncNonDefault()
   179  		DebugTest = true
   180  	}
   181  }
   182  
   183  // Get looks up the action ID in the cache,
   184  // returning the corresponding output ID and file size, if any.
   185  // Note that finding an output ID does not guarantee that the
   186  // saved file for that output ID is still available.
   187  func (c *DiskCache) Get(id ActionID) (Entry, error) {
   188  	if verify {
   189  		return Entry{}, &entryNotFoundError{Err: errVerifyMode}
   190  	}
   191  	return c.get(id)
   192  }
   193  
   194  type Entry struct {
   195  	OutputID OutputID
   196  	Size     int64
   197  	Time     time.Time // when added to cache
   198  }
   199  
   200  // get is Get but does not respect verify mode, so that Put can use it.
   201  func (c *DiskCache) get(id ActionID) (Entry, error) {
   202  	missing := func(reason error) (Entry, error) {
   203  		return Entry{}, &entryNotFoundError{Err: reason}
   204  	}
   205  	f, err := os.Open(c.fileName(id, "a"))
   206  	if err != nil {
   207  		return missing(err)
   208  	}
   209  	defer f.Close()
   210  	entry := make([]byte, entrySize+1) // +1 to detect whether f is too long
   211  	if n, err := io.ReadFull(f, entry); n > entrySize {
   212  		return missing(errors.New("too long"))
   213  	} else if err != io.ErrUnexpectedEOF {
   214  		if err == io.EOF {
   215  			return missing(errors.New("file is empty"))
   216  		}
   217  		return missing(err)
   218  	} else if n < entrySize {
   219  		return missing(errors.New("entry file incomplete"))
   220  	}
   221  	if entry[0] != 'v' || entry[1] != '1' || entry[2] != ' ' || entry[3+hexSize] != ' ' || entry[3+hexSize+1+hexSize] != ' ' || entry[3+hexSize+1+hexSize+1+20] != ' ' || entry[entrySize-1] != '\n' {
   222  		return missing(errors.New("invalid header"))
   223  	}
   224  	eid, entry := entry[3:3+hexSize], entry[3+hexSize:]
   225  	eout, entry := entry[1:1+hexSize], entry[1+hexSize:]
   226  	esize, entry := entry[1:1+20], entry[1+20:]
   227  	etime, entry := entry[1:1+20], entry[1+20:]
   228  	var buf [HashSize]byte
   229  	if _, err := hex.Decode(buf[:], eid); err != nil {
   230  		return missing(fmt.Errorf("decoding ID: %v", err))
   231  	} else if buf != id {
   232  		return missing(errors.New("mismatched ID"))
   233  	}
   234  	if _, err := hex.Decode(buf[:], eout); err != nil {
   235  		return missing(fmt.Errorf("decoding output ID: %v", err))
   236  	}
   237  	i := 0
   238  	for i < len(esize) && esize[i] == ' ' {
   239  		i++
   240  	}
   241  	size, err := strconv.ParseInt(string(esize[i:]), 10, 64)
   242  	if err != nil {
   243  		return missing(fmt.Errorf("parsing size: %v", err))
   244  	} else if size < 0 {
   245  		return missing(errors.New("negative size"))
   246  	}
   247  	i = 0
   248  	for i < len(etime) && etime[i] == ' ' {
   249  		i++
   250  	}
   251  	tm, err := strconv.ParseInt(string(etime[i:]), 10, 64)
   252  	if err != nil {
   253  		return missing(fmt.Errorf("parsing timestamp: %v", err))
   254  	} else if tm < 0 {
   255  		return missing(errors.New("negative timestamp"))
   256  	}
   257  
   258  	c.markUsed(c.fileName(id, "a"))
   259  
   260  	return Entry{buf, size, time.Unix(0, tm)}, nil
   261  }
   262  
   263  // GetFile looks up the action ID in the cache and returns
   264  // the name of the corresponding data file.
   265  func GetFile(c Cache, id ActionID) (file string, entry Entry, err error) {
   266  	entry, err = c.Get(id)
   267  	if err != nil {
   268  		return "", Entry{}, err
   269  	}
   270  	file = c.OutputFile(entry.OutputID)
   271  	info, err := os.Stat(file)
   272  	if err != nil {
   273  		return "", Entry{}, &entryNotFoundError{Err: err}
   274  	}
   275  	if info.Size() != entry.Size {
   276  		return "", Entry{}, &entryNotFoundError{Err: errors.New("file incomplete")}
   277  	}
   278  	return file, entry, nil
   279  }
   280  
   281  // GetBytes looks up the action ID in the cache and returns
   282  // the corresponding output bytes.
   283  // GetBytes should only be used for data that can be expected to fit in memory.
   284  func GetBytes(c Cache, id ActionID) ([]byte, Entry, error) {
   285  	entry, err := c.Get(id)
   286  	if err != nil {
   287  		return nil, entry, err
   288  	}
   289  	data, _ := os.ReadFile(c.OutputFile(entry.OutputID))
   290  	if sha256.Sum256(data) != entry.OutputID {
   291  		return nil, entry, &entryNotFoundError{Err: errors.New("bad checksum")}
   292  	}
   293  	return data, entry, nil
   294  }
   295  
   296  // GetMmap looks up the action ID in the cache and returns
   297  // the corresponding output bytes.
   298  // GetMmap should only be used for data that can be expected to fit in memory.
   299  // The boolean result indicates whether the file was opened.
   300  // If it is true, the caller should avoid attempting
   301  // to write to the file on Windows, because Windows locks
   302  // the open file, and writes to it will fail.
   303  func GetMmap(c Cache, id ActionID) ([]byte, Entry, bool, error) {
   304  	entry, err := c.Get(id)
   305  	if err != nil {
   306  		return nil, entry, false, err
   307  	}
   308  	md, opened, err := mmap.Mmap(c.OutputFile(entry.OutputID))
   309  	if err != nil {
   310  		return nil, Entry{}, opened, err
   311  	}
   312  	if int64(len(md.Data)) != entry.Size {
   313  		return nil, Entry{}, true, &entryNotFoundError{Err: errors.New("file incomplete")}
   314  	}
   315  	return md.Data, entry, true, nil
   316  }
   317  
   318  // OutputFile returns the name of the cache file storing output with the given OutputID.
   319  func (c *DiskCache) OutputFile(out OutputID) string {
   320  	file := c.fileName(out, "d")
   321  	isDir := c.markUsed(file)
   322  	if isDir { // => cached executable
   323  		entries, err := os.ReadDir(file)
   324  		if err != nil {
   325  			return fmt.Sprintf("DO NOT USE - missing binary cache entry: %v", err)
   326  		}
   327  		if len(entries) != 1 {
   328  			return "DO NOT USE - invalid binary cache entry"
   329  		}
   330  		return filepath.Join(file, entries[0].Name())
   331  	}
   332  	return file
   333  }
   334  
   335  // Time constants for cache expiration.
   336  //
   337  // We set the mtime on a cache file on each use, but at most one per mtimeInterval (1 hour),
   338  // to avoid causing many unnecessary inode updates. The mtimes therefore
   339  // roughly reflect "time of last use" but may in fact be older by at most an hour.
   340  //
   341  // We scan the cache for entries to delete at most once per trimInterval (1 day).
   342  //
   343  // When we do scan the cache, we delete entries that have not been used for
   344  // at least trimLimit (5 days). Statistics gathered from a month of usage by
   345  // Go developers found that essentially all reuse of cached entries happened
   346  // within 5 days of the previous reuse. See golang.org/issue/22990.
   347  const (
   348  	mtimeInterval = 1 * time.Hour
   349  	trimInterval  = 24 * time.Hour
   350  	trimLimit     = 5 * 24 * time.Hour
   351  )
   352  
   353  // markUsed makes a best-effort attempt to update mtime on file,
   354  // so that mtime reflects cache access time.
   355  //
   356  // Because the reflection only needs to be approximate,
   357  // and to reduce the amount of disk activity caused by using
   358  // cache entries, used only updates the mtime if the current
   359  // mtime is more than an hour old. This heuristic eliminates
   360  // nearly all of the mtime updates that would otherwise happen,
   361  // while still keeping the mtimes useful for cache trimming.
   362  //
   363  // markUsed reports whether the file is a directory (an executable cache entry).
   364  func (c *DiskCache) markUsed(file string) (isDir bool) {
   365  	info, err := os.Stat(file)
   366  	if err != nil {
   367  		return false
   368  	}
   369  	if now := c.now(); now.Sub(info.ModTime()) >= mtimeInterval {
   370  		os.Chtimes(file, now, now)
   371  	}
   372  	return info.IsDir()
   373  }
   374  
   375  func (c *DiskCache) Close() error { return c.Trim() }
   376  
   377  // Trim removes old cache entries that are likely not to be reused.
   378  func (c *DiskCache) Trim() error {
   379  	now := c.now()
   380  
   381  	// We maintain in dir/trim.txt the time of the last completed cache trim.
   382  	// If the cache has been trimmed recently enough, do nothing.
   383  	// This is the common case.
   384  	// If the trim file is corrupt, detected if the file can't be parsed, or the
   385  	// trim time is too far in the future, attempt the trim anyway. It's possible that
   386  	// the cache was full when the corruption happened. Attempting a trim on
   387  	// an empty cache is cheap, so there wouldn't be a big performance hit in that case.
   388  	if data, err := lockedfile.Read(filepath.Join(c.dir, "trim.txt")); err == nil {
   389  		if t, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64); err == nil {
   390  			lastTrim := time.Unix(t, 0)
   391  			if d := now.Sub(lastTrim); d < trimInterval && d > -mtimeInterval {
   392  				return nil
   393  			}
   394  		}
   395  	}
   396  
   397  	// Trim each of the 256 subdirectories.
   398  	// We subtract an additional mtimeInterval
   399  	// to account for the imprecision of our "last used" mtimes.
   400  	cutoff := now.Add(-trimLimit - mtimeInterval)
   401  	for i := 0; i < 256; i++ {
   402  		subdir := filepath.Join(c.dir, fmt.Sprintf("%02x", i))
   403  		c.trimSubdir(subdir, cutoff)
   404  	}
   405  
   406  	// Ignore errors from here: if we don't write the complete timestamp, the
   407  	// cache will appear older than it is, and we'll trim it again next time.
   408  	var b bytes.Buffer
   409  	fmt.Fprintf(&b, "%d", now.Unix())
   410  	if err := lockedfile.Write(filepath.Join(c.dir, "trim.txt"), &b, 0o666); err != nil {
   411  		return err
   412  	}
   413  
   414  	return nil
   415  }
   416  
   417  // trimSubdir trims a single cache subdirectory.
   418  func (c *DiskCache) trimSubdir(subdir string, cutoff time.Time) {
   419  	// Read all directory entries from subdir before removing
   420  	// any files, in case removing files invalidates the file offset
   421  	// in the directory scan. Also, ignore error from f.Readdirnames,
   422  	// because we don't care about reporting the error and we still
   423  	// want to process any entries found before the error.
   424  	f, err := os.Open(subdir)
   425  	if err != nil {
   426  		return
   427  	}
   428  	names, _ := f.Readdirnames(-1)
   429  	f.Close()
   430  
   431  	for _, name := range names {
   432  		// Remove only cache entries (xxxx-a and xxxx-d).
   433  		if !strings.HasSuffix(name, "-a") && !strings.HasSuffix(name, "-d") {
   434  			continue
   435  		}
   436  		entry := filepath.Join(subdir, name)
   437  		info, err := os.Stat(entry)
   438  		if err == nil && info.ModTime().Before(cutoff) {
   439  			if info.IsDir() { // executable cache entry
   440  				os.RemoveAll(entry)
   441  				continue
   442  			}
   443  			os.Remove(entry)
   444  		}
   445  	}
   446  }
   447  
   448  // putIndexEntry adds an entry to the cache recording that executing the action
   449  // with the given id produces an output with the given output id (hash) and size.
   450  func (c *DiskCache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify bool) error {
   451  	// Note: We expect that for one reason or another it may happen
   452  	// that repeating an action produces a different output hash
   453  	// (for example, if the output contains a time stamp or temp dir name).
   454  	// While not ideal, this is also not a correctness problem, so we
   455  	// don't make a big deal about it. In particular, we leave the action
   456  	// cache entries writable specifically so that they can be overwritten.
   457  	//
   458  	// Setting GODEBUG=gocacheverify=1 does make a big deal:
   459  	// in verify mode we are double-checking that the cache entries
   460  	// are entirely reproducible. As just noted, this may be unrealistic
   461  	// in some cases but the check is also useful for shaking out real bugs.
   462  	entry := fmt.Sprintf("v1 %x %x %20d %20d\n", id, out, size, time.Now().UnixNano())
   463  	if verify && allowVerify {
   464  		old, err := c.get(id)
   465  		if err == nil && (old.OutputID != out || old.Size != size) {
   466  			// panic to show stack trace, so we can see what code is generating this cache entry.
   467  			msg := fmt.Sprintf("go: internal cache error: cache verify failed: id=%x changed:<<<\n%s\n>>>\nold: %x %d\nnew: %x %d", id, reverseHash(id), out, size, old.OutputID, old.Size)
   468  			panic(msg)
   469  		}
   470  	}
   471  	file := c.fileName(id, "a")
   472  
   473  	// Copy file to cache directory.
   474  	mode := os.O_WRONLY | os.O_CREATE
   475  	f, err := os.OpenFile(file, mode, 0o666)
   476  	if err != nil {
   477  		return err
   478  	}
   479  	_, err = f.WriteString(entry)
   480  	if err == nil {
   481  		// Truncate the file only *after* writing it.
   482  		// (This should be a no-op, but truncate just in case of previous corruption.)
   483  		//
   484  		// This differs from os.WriteFile, which truncates to 0 *before* writing
   485  		// via os.O_TRUNC. Truncating only after writing ensures that a second write
   486  		// of the same content to the same file is idempotent, and does not — even
   487  		// temporarily! — undo the effect of the first write.
   488  		err = f.Truncate(int64(len(entry)))
   489  	}
   490  	if closeErr := f.Close(); err == nil {
   491  		err = closeErr
   492  	}
   493  	if err != nil {
   494  		// TODO(bcmills): This Remove potentially races with another go command writing to file.
   495  		// Can we eliminate it?
   496  		os.Remove(file)
   497  		return err
   498  	}
   499  	os.Chtimes(file, c.now(), c.now()) // mainly for tests
   500  
   501  	return nil
   502  }
   503  
   504  // noVerifyReadSeeker is an io.ReadSeeker wrapper sentinel type
   505  // that says that Cache.Put should skip the verify check
   506  // (from GODEBUG=goverifycache=1).
   507  type noVerifyReadSeeker struct {
   508  	io.ReadSeeker
   509  }
   510  
   511  // Put stores the given output in the cache as the output for the action ID.
   512  // It may read file twice. The content of file must not change between the two passes.
   513  func (c *DiskCache) Put(id ActionID, file io.ReadSeeker) (OutputID, int64, error) {
   514  	wrapper, isNoVerify := file.(noVerifyReadSeeker)
   515  	if isNoVerify {
   516  		file = wrapper.ReadSeeker
   517  	}
   518  	return c.put(id, "", file, !isNoVerify)
   519  }
   520  
   521  // PutExecutable is used to store the output as the output for the action ID into a
   522  // file with the given base name, with the executable mode bit set.
   523  // It may read file twice. The content of file must not change between the two passes.
   524  func (c *DiskCache) PutExecutable(id ActionID, name string, file io.ReadSeeker) (OutputID, int64, error) {
   525  	if name == "" {
   526  		panic("PutExecutable called without a name")
   527  	}
   528  	wrapper, isNoVerify := file.(noVerifyReadSeeker)
   529  	if isNoVerify {
   530  		file = wrapper.ReadSeeker
   531  	}
   532  	return c.put(id, name, file, !isNoVerify)
   533  }
   534  
   535  // PutNoVerify is like Put but disables the verify check
   536  // when GODEBUG=goverifycache=1 is set.
   537  // It is meant for data that is OK to cache but that we expect to vary slightly from run to run,
   538  // like test output containing times and the like.
   539  func PutNoVerify(c Cache, id ActionID, file io.ReadSeeker) (OutputID, int64, error) {
   540  	return c.Put(id, noVerifyReadSeeker{file})
   541  }
   542  
   543  func (c *DiskCache) put(id ActionID, executableName string, file io.ReadSeeker, allowVerify bool) (OutputID, int64, error) {
   544  	// Compute output ID.
   545  	h := sha256.New()
   546  	if _, err := file.Seek(0, 0); err != nil {
   547  		return OutputID{}, 0, err
   548  	}
   549  	size, err := io.Copy(h, file)
   550  	if err != nil {
   551  		return OutputID{}, 0, err
   552  	}
   553  	var out OutputID
   554  	h.Sum(out[:0])
   555  
   556  	// Copy to cached output file (if not already present).
   557  	fileMode := fs.FileMode(0o666)
   558  	if executableName != "" {
   559  		fileMode = 0o777
   560  	}
   561  	if err := c.copyFile(file, executableName, out, size, fileMode); err != nil {
   562  		return out, size, err
   563  	}
   564  
   565  	// Add to cache index.
   566  	return out, size, c.putIndexEntry(id, out, size, allowVerify)
   567  }
   568  
   569  // PutBytes stores the given bytes in the cache as the output for the action ID.
   570  func PutBytes(c Cache, id ActionID, data []byte) error {
   571  	_, _, err := c.Put(id, bytes.NewReader(data))
   572  	return err
   573  }
   574  
   575  // copyFile copies file into the cache, expecting it to have the given
   576  // output ID and size, if that file is not present already.
   577  func (c *DiskCache) copyFile(file io.ReadSeeker, executableName string, out OutputID, size int64, perm os.FileMode) error {
   578  	name := c.fileName(out, "d") // TODO(matloob): use a different suffix for the executable cache?
   579  	info, err := os.Stat(name)
   580  	if executableName != "" {
   581  		// This is an executable file. The file at name won't hold the output itself, but will
   582  		// be a directory that holds the output, named according to executableName. Check to see
   583  		// if the directory already exists, and if it does not, create it. Then reset name
   584  		// to the name we want the output written to.
   585  		if err != nil {
   586  			if !os.IsNotExist(err) {
   587  				return err
   588  			}
   589  			if err := os.Mkdir(name, 0o777); err != nil {
   590  				return err
   591  			}
   592  			if info, err = os.Stat(name); err != nil {
   593  				return err
   594  			}
   595  		}
   596  		if !info.IsDir() {
   597  			return errors.New("internal error: invalid binary cache entry: not a directory")
   598  		}
   599  
   600  		// directory exists. now set name to the inner file
   601  		name = filepath.Join(name, executableName)
   602  		info, err = os.Stat(name)
   603  	}
   604  	if err == nil && info.Size() == size {
   605  		// Check hash.
   606  		if f, err := os.Open(name); err == nil {
   607  			h := sha256.New()
   608  			io.Copy(h, f)
   609  			f.Close()
   610  			var out2 OutputID
   611  			h.Sum(out2[:0])
   612  			if out == out2 {
   613  				return nil
   614  			}
   615  		}
   616  		// Hash did not match. Fall through and rewrite file.
   617  	}
   618  
   619  	// Copy file to cache directory.
   620  	mode := os.O_RDWR | os.O_CREATE
   621  	if err == nil && info.Size() > size { // shouldn't happen but fix in case
   622  		mode |= os.O_TRUNC
   623  	}
   624  	f, err := os.OpenFile(name, mode, perm)
   625  	if err != nil {
   626  		if base.IsETXTBSY(err) {
   627  			// This file is being used by an executable. It must have
   628  			// already been written by another go process and then run.
   629  			// return without an error.
   630  			return nil
   631  		}
   632  		return err
   633  	}
   634  	defer f.Close()
   635  	if size == 0 {
   636  		// File now exists with correct size.
   637  		// Only one possible zero-length file, so contents are OK too.
   638  		// Early return here makes sure there's a "last byte" for code below.
   639  		return nil
   640  	}
   641  
   642  	// From here on, if any of the I/O writing the file fails,
   643  	// we make a best-effort attempt to truncate the file f
   644  	// before returning, to avoid leaving bad bytes in the file.
   645  
   646  	// Copy file to f, but also into h to double-check hash.
   647  	if _, err := file.Seek(0, 0); err != nil {
   648  		f.Truncate(0)
   649  		return err
   650  	}
   651  	h := sha256.New()
   652  	w := io.MultiWriter(f, h)
   653  	if _, err := io.CopyN(w, file, size-1); err != nil {
   654  		f.Truncate(0)
   655  		return err
   656  	}
   657  	// Check last byte before writing it; writing it will make the size match
   658  	// what other processes expect to find and might cause them to start
   659  	// using the file.
   660  	buf := make([]byte, 1)
   661  	if _, err := file.Read(buf); err != nil {
   662  		f.Truncate(0)
   663  		return err
   664  	}
   665  	h.Write(buf)
   666  	sum := h.Sum(nil)
   667  	if !bytes.Equal(sum, out[:]) {
   668  		f.Truncate(0)
   669  		return fmt.Errorf("file content changed underfoot")
   670  	}
   671  
   672  	// Commit cache file entry.
   673  	if _, err := f.Write(buf); err != nil {
   674  		f.Truncate(0)
   675  		return err
   676  	}
   677  	if err := f.Close(); err != nil {
   678  		// Data might not have been written,
   679  		// but file may look like it is the right size.
   680  		// To be extra careful, remove cached file.
   681  		os.Remove(name)
   682  		return err
   683  	}
   684  	os.Chtimes(name, c.now(), c.now()) // mainly for tests
   685  
   686  	return nil
   687  }
   688  
   689  // FuzzDir returns a subdirectory within the cache for storing fuzzing data.
   690  // The subdirectory may not exist.
   691  //
   692  // This directory is managed by the internal/fuzz package. Files in this
   693  // directory aren't removed by the 'go clean -cache' command or by Trim.
   694  // They may be removed with 'go clean -fuzzcache'.
   695  //
   696  // TODO(#48526): make Trim remove unused files from this directory.
   697  func (c *DiskCache) FuzzDir() string {
   698  	return filepath.Join(c.dir, "fuzz")
   699  }
   700  

View as plain text