aboutsummaryrefslogtreecommitdiffsponsor
path: root/filesystem.go
blob: c33df1511e2dcc2044f5b7a60569ff26f9c9ba6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
}