diff options
author | Slack Coder <slackcoder@server.ky> | 2022-03-11 13:08:24 -0500 |
---|---|---|
committer | Slack Coder <slackcoder@server.ky> | 2022-03-30 11:34:46 -0500 |
commit | 29589a24b13fb223b113e94eca2c4fff0e56a4d9 (patch) | |
tree | e1754d195463439ae2834cd502b170648e47cdb8 /filesystem.go | |
download | pkgtools-go-29589a24b13fb223b113e94eca2c4fff0e56a4d9.tar.xz |
Initial commit
Diffstat (limited to 'filesystem.go')
-rw-r--r-- | filesystem.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/filesystem.go b/filesystem.go new file mode 100644 index 0000000..c33df15 --- /dev/null +++ b/filesystem.go @@ -0,0 +1,63 @@ +package pkgtools + +import ( + "io/fs" + "math" + "os" + "strconv" + "time" +) + +const maxLockWait = 10 * time.Second + +func CopyFile(dst string, src string) error { + return ErrNotImplemented +} + +func HumanReadableSize(size int64) string { + suffixes := []string{ + "B", + "K", + "M", + "G", + "T", + } + + base := math.Log(float64(size)) / math.Log(1024) + newSize := math.Ceil(math.Pow(1024, base-math.Floor(base))*10) / 10 + newSuffix := suffixes[int(math.Floor(base))] + ret := strconv.FormatFloat(newSize, 'f', -1, 64) + newSuffix + return ret +} + +func IsFile(path string) (bool, error) { + info, err := os.Stat(path) + if err != nil { + return false, err + } + return info.Mode().IsRegular(), nil +} + +func IsDir(path string) (bool, error) { + info, err := os.Stat(path) + if err != nil { + return false, err + } + return info.IsDir(), nil +} + +func IsSymlink(path string) (bool, error) { + info, err := os.Stat(path) + if err != nil { + return false, err + } + return info.Mode() == fs.ModeSymlink, nil +} + +func FileSize(fp string) (int64, error) { + info, err := os.Stat(fp) + if err != nil { + return 0, err + } + return info.Size(), nil +} |