-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Always copy PHP and legacy CLI files if they have changed
- Loading branch information
1 parent
8fc58ce
commit 3bd3c31
Showing
10 changed files
with
251 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package file | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"io" | ||
"os" | ||
) | ||
|
||
// CopyIfChanged copies source data to a destination filename if it has changed. | ||
// It is considered changed if its length or contents are different. | ||
func CopyIfChanged(destFilename string, source []byte, perm os.FileMode) error { | ||
matches, err := compare(destFilename, source) | ||
if (err != nil && !os.IsNotExist(err)) || matches { | ||
return err | ||
} | ||
return os.WriteFile(destFilename, source, perm) | ||
} | ||
|
||
// compare checks if a file matches the given source. | ||
func compare(filename string, data []byte) (bool, error) { | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer f.Close() | ||
|
||
fi, err := f.Stat() | ||
if err != nil { | ||
return false, err | ||
} | ||
if fi.Size() != int64(len(data)) { | ||
return false, nil | ||
} | ||
|
||
var ( | ||
buf = make([]byte, 32*1024) | ||
offset = 0 | ||
) | ||
for { | ||
n, err := f.Read(buf) | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
return false, err | ||
} | ||
if offset+n > len(data) || !bytes.Equal(data[offset:offset+n], buf[:n]) { | ||
return false, nil | ||
} | ||
offset += n | ||
} | ||
|
||
return offset == len(data), nil | ||
} | ||
|
||
// CheckHash checks if a file has the given SHA256 hash. | ||
func CheckHash(filename, hash string) (bool, error) { | ||
fh, err := sha256File(filename) | ||
if err != nil { | ||
return false, err | ||
} | ||
return fh == hash, nil | ||
} | ||
|
||
// sha256File calculates the SHA256 hash of a file. | ||
func sha256File(filename string) (string, error) { | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer f.Close() | ||
h := sha256.New() | ||
if _, err := io.Copy(h, f); err != nil { | ||
return "", err | ||
} | ||
return hex.EncodeToString(h.Sum(nil)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package file | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCopyIfChanged(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
initialData []byte | ||
sourceData []byte | ||
expectWrite bool | ||
}{ | ||
{"File does not exist", nil, []byte("new data"), true}, | ||
{"File matches source", []byte("same data"), []byte("same data"), false}, | ||
{"File content differs", []byte("old data"), []byte("new data"), true}, | ||
{"File size differs", []byte("short"), []byte("much longer data"), true}, | ||
{"Empty source", []byte("existing data"), []byte{}, true}, | ||
} | ||
|
||
tmpDir := t.TempDir() | ||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
destFile := filepath.Join(tmpDir, "testfile") | ||
|
||
if c.initialData != nil { | ||
require.NoError(t, os.WriteFile(destFile, c.initialData, 0o600)) | ||
time.Sleep(time.Millisecond * 5) | ||
} | ||
|
||
var modTimeBeforeCopy time.Time | ||
stat, err := os.Stat(destFile) | ||
if c.initialData == nil { | ||
require.True(t, os.IsNotExist(err)) | ||
} else { | ||
require.NoError(t, err) | ||
modTimeBeforeCopy = stat.ModTime() | ||
} | ||
|
||
err = CopyIfChanged(destFile, c.sourceData, 0o600) | ||
require.NoError(t, err) | ||
|
||
statAfterCopy, err := os.Stat(destFile) | ||
require.NoError(t, err) | ||
if c.expectWrite { | ||
assert.Greater(t, statAfterCopy.ModTime().Truncate(time.Millisecond), modTimeBeforeCopy.Truncate(time.Millisecond)) | ||
} else { | ||
assert.Equal(t, modTimeBeforeCopy.Truncate(time.Millisecond), statAfterCopy.ModTime().Truncate(time.Millisecond)) | ||
} | ||
|
||
data, err := os.ReadFile(destFile) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, data, c.sourceData) | ||
}) | ||
} | ||
} |
Oops, something went wrong.