~gitpocket/gitpocket

1
099b7551ea4bdf1874bee2e9873b0eb6b8a6cf8c
mainfeed-discovery
gitpocket/quota.go -rw-r--r--1.4 KiB ViewLogBlameView rawPermalink 5b7fbd8a0f feat(basin): squash and rebase merges, collaborators, disk quota, pgp verification, streamed raw files 15 hours ago
 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
64
package main

import (
	"errors"
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
	"strconv"
	"strings"
)

func readQuota(root string) int64 {
	b, err := os.ReadFile(filepath.Join(root, "quota"))
	if err != nil {
		return 0
	}
	n, _ := parseSize(strings.TrimSpace(string(b)))
	return n
}

func parseSize(s string) (int64, error) {
	s = strings.ToUpper(strings.TrimSpace(s))
	mult := int64(1)
	switch {
	case strings.HasSuffix(s, "G"):
		mult, s = 1<<30, strings.TrimSuffix(s, "G")
	case strings.HasSuffix(s, "M"):
		mult, s = 1<<20, strings.TrimSuffix(s, "M")
	case strings.HasSuffix(s, "K"):
		mult, s = 1<<10, strings.TrimSuffix(s, "K")
	}
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil || n < 0 {
		return 0, errors.New("size must be a number with an optional K, M or G suffix")
	}
	return n * mult, nil
}

func usage(dir string) int64 {
	var total int64
	filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error {
		if err == nil && !d.IsDir() {
			if info, err := d.Info(); err == nil {
				total += info.Size()
			}
		}
		return nil
	})
	return total
}

func overQuota(root string, owner string) (bool, int64, int64) {
	quota := readQuota(root)
	if quota == 0 {
		return false, 0, 0
	}
	used := usage(filepath.Join(root, "repos", owner))
	return used >= quota, used, quota
}

func quotaMessage(used, quota int64) string {
	return fmt.Sprintf("disk quota exceeded, %s used of %s", humanSize(used), humanSize(quota))
}