cli icon indicating copy to clipboard operation
cli copied to clipboard

Check file hashes and permissions on non-0 Legacy CLI exit code

Open akalipetis opened this issue 2 years ago • 1 comments

When running the Legacy CLI and there is a non-zero exit code, we should check file hashes and permissions and if needed cleanup the cache directory.

### Tasks

akalipetis avatar Jul 31 '23 13:07 akalipetis

Or something like this - we already call os.Stat() to check for existence - so this adds the size check and then the comparison, currently byte-by-byte. It would be more memory-efficient (not necessarily faster) if we compare hashes - dynamically calculated, or perhaps statically embedded in the Go binary and statically written in the cache directory. Given the files add up to ~20MB we'd probably want hashes.

// fileChanged checks if a file's content differs from the provided bytes.
func fileChanged(filename string, content []byte) (bool, error) {
	stat, err := os.Stat(filename)
	if err != nil {
		if os.IsNotExist(err) {
			return true, nil
		}
		return false, fmt.Errorf("could not stat file: %w", err)
	}
	if int(stat.Size()) != len(content) {
		return true, nil
	}
	current, err := os.ReadFile(filename)
	if err != nil {
		return false, err
	}
	return !bytes.Equal(current, content), nil
}

pjcdawkins avatar Aug 03 '23 16:08 pjcdawkins