forked from sneak/util
Compare commits
1 Commits
proposal-s
...
proposal-f
| Author | SHA1 | Date | |
|---|---|---|---|
| 9d3d35b3d7 |
31
exists.go
Normal file
31
exists.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
// FileExists reports whether there is a regular file at path. A directory, a
|
||||||
|
// device node or a socket gives false, and so does anything the process is not
|
||||||
|
// allowed to look at, since from the caller's point of view there is no usable
|
||||||
|
// file there either way.
|
||||||
|
//
|
||||||
|
// Symbolic links are followed, so a link pointing at a regular file gives true
|
||||||
|
// and a link pointing at nothing gives false.
|
||||||
|
func FileExists(path string) bool {
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return info.Mode().IsRegular()
|
||||||
|
}
|
||||||
|
|
||||||
|
// DirExists reports whether there is a directory at path. As with FileExists,
|
||||||
|
// anything that cannot be looked at gives false, and symbolic links are
|
||||||
|
// followed.
|
||||||
|
func DirExists(path string) bool {
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return info.IsDir()
|
||||||
|
}
|
||||||
57
exists_test.go
Normal file
57
exists_test.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFileExistsAndDirExists(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
|
||||||
|
file := filepath.Join(root, "a-file")
|
||||||
|
if err := os.WriteFile(file, []byte("contents"), 0644); err != nil {
|
||||||
|
t.Fatalf("could not write the test file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
directory := filepath.Join(root, "a-directory")
|
||||||
|
if err := os.Mkdir(directory, 0755); err != nil {
|
||||||
|
t.Fatalf("could not make the test directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
linkToFile := filepath.Join(root, "link-to-file")
|
||||||
|
if err := os.Symlink(file, linkToFile); err != nil {
|
||||||
|
t.Fatalf("could not make the test link: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
brokenLink := filepath.Join(root, "broken-link")
|
||||||
|
if err := os.Symlink(filepath.Join(root, "nothing-here"), brokenLink); err != nil {
|
||||||
|
t.Fatalf("could not make the broken test link: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
path string
|
||||||
|
expectedFile bool
|
||||||
|
expectedIsDir bool
|
||||||
|
}{
|
||||||
|
{"a regular file", file, true, false},
|
||||||
|
{"a directory", directory, false, true},
|
||||||
|
{"a link to a file", linkToFile, true, false},
|
||||||
|
{"a link to nothing", brokenLink, false, false},
|
||||||
|
{"a path that is not there", filepath.Join(root, "missing"), false, false},
|
||||||
|
{"a path whose parent is not there", filepath.Join(root, "missing", "deeper"), false, false},
|
||||||
|
{"the empty path", "", false, false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
if got := FileExists(test.path); got != test.expectedFile {
|
||||||
|
t.Errorf("FileExists: expected %v got %v", test.expectedFile, got)
|
||||||
|
}
|
||||||
|
if got := DirExists(test.path); got != test.expectedIsDir {
|
||||||
|
t.Errorf("DirExists: expected %v got %v", test.expectedIsDir, got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
package util
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/hex"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
// SHA256File returns the SHA-256 digest of the contents of the file at path,
|
|
||||||
// written as lowercase hexadecimal, in the same form as the sha256sum command
|
|
||||||
// prints. The file is copied through the hash in chunks rather than read into
|
|
||||||
// memory, so its size does not matter.
|
|
||||||
//
|
|
||||||
// The error is whatever went wrong opening or reading the file, passed along
|
|
||||||
// unchanged so a caller can test it with os.IsNotExist and the like.
|
|
||||||
func SHA256File(path string) (string, error) {
|
|
||||||
file, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
digest := sha256.New()
|
|
||||||
if _, err := io.Copy(digest, file); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return hex.EncodeToString(digest.Sum(nil)), nil
|
|
||||||
}
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
package util
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSHA256File(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
contents []byte
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
"an empty file",
|
|
||||||
[]byte{},
|
|
||||||
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"a short file",
|
|
||||||
[]byte("hello world\n"),
|
|
||||||
"a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"binary contents",
|
|
||||||
[]byte{0x00, 0x01, 0x02, 0x03},
|
|
||||||
"054edec1d0211f624fed0cbca9d4f9400b0e491c43742af2c5b0abebf0c990d8",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"larger than one read buffer",
|
|
||||||
bytes.Repeat([]byte("a"), 100000),
|
|
||||||
"6d1cf22d7cc09b085dfc25ee1a1f3ae0265804c607bc2074ad253bcc82fd81ee",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
path := filepath.Join(t.TempDir(), "file")
|
|
||||||
if err := os.WriteFile(path, test.contents, 0644); err != nil {
|
|
||||||
t.Fatalf("could not write the test file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
got, err := SHA256File(path)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("did not expect an error, got %v", err)
|
|
||||||
}
|
|
||||||
if got != test.expected {
|
|
||||||
t.Errorf("expected %q got %q", test.expected, got)
|
|
||||||
}
|
|
||||||
if got != strings.ToLower(got) {
|
|
||||||
t.Errorf("expected lowercase hexadecimal, got %q", got)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSHA256FileReportsAMissingFile(t *testing.T) {
|
|
||||||
path := filepath.Join(t.TempDir(), "not-there")
|
|
||||||
|
|
||||||
got, err := SHA256File(path)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("expected an error for a file that does not exist, got %q", got)
|
|
||||||
}
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
t.Errorf("expected an error that os.IsNotExist recognises, got %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSHA256FileReportsADirectory(t *testing.T) {
|
|
||||||
if _, err := SHA256File(t.TempDir()); err == nil {
|
|
||||||
t.Errorf("expected an error for a directory")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user