mirror of
https://github.com/talent-plan/tinykv.git
synced 2024-12-27 13:20:24 +08:00
5e089a2cd1
Signed-off-by: Connor <zbk602423539@gmail.com> Co-authored-by: Nick Cameron <nrc@ncameron.org> Co-authored-by: linning <linningde25@gmail.com> Co-authored-by: YangKeao <keao.yang@yahoo.com> Co-authored-by: andylokandy <andylokandy@hotmail.com> Co-authored-by: Iosmanthus Teng <myosmanthustree@gmail.com>
58 lines
1012 B
Go
58 lines
1012 B
Go
package util
|
|
|
|
import (
|
|
"hash/crc32"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/pingcap/errors"
|
|
)
|
|
|
|
func GetFileSize(path string) (uint64, error) {
|
|
fi, err := os.Stat(path)
|
|
if err != nil {
|
|
return 0, errors.WithStack(err)
|
|
}
|
|
return uint64(fi.Size()), nil
|
|
}
|
|
|
|
func FileExists(path string) bool {
|
|
fi, err := os.Stat(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return !fi.IsDir()
|
|
}
|
|
|
|
func DirExists(path string) bool {
|
|
fi, err := os.Stat(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return fi.IsDir()
|
|
}
|
|
|
|
func DeleteFileIfExists(path string) (bool, error) {
|
|
err := os.Remove(path)
|
|
if os.IsNotExist(err) {
|
|
return false, nil
|
|
} else if err != nil {
|
|
return false, errors.WithStack(err)
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// CalcCRC32 Calculates the given file's CRC32 checksum.
|
|
func CalcCRC32(path string) (uint32, error) {
|
|
digest := crc32.NewIEEE()
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return 0, errors.WithStack(err)
|
|
}
|
|
_, err = io.Copy(digest, f)
|
|
if err != nil {
|
|
return 0, errors.WithStack(err)
|
|
}
|
|
return digest.Sum32(), nil
|
|
}
|