64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"git.misaka.ren/m1saka/dsh-web/internal/store"
|
|
"io/fs"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// Call with workspaceMu held through the write. Runtime writes remain soft-limited.
|
|
func (a *API) quotaGrowth(uid string, growth int64) error {
|
|
var u store.User
|
|
u.ID = uid
|
|
if err := a.s.DB.QueryRow("SELECT quota FROM users WHERE id=?", uid).Scan(&u.Quota); err != nil {
|
|
return err
|
|
}
|
|
used, err := a.userUsage(u)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if growth > 0 && (used > u.Quota || growth > u.Quota-used) {
|
|
return errors.New("quota exceeded")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func quotaTreeBytes(root *os.Root, name string) (int64, error) {
|
|
var total int64
|
|
entries := 0
|
|
err := fs.WalkDir(root.FS(), name, func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
entries++
|
|
if entries > 100000 || strings.Count(p, "/") > 128 {
|
|
return errors.New("tree limit")
|
|
}
|
|
info, err := d.Info()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if info.Mode().IsRegular() {
|
|
if info.Size() > upgradeSnapshotLimit-total {
|
|
return errors.New("snapshot size limit")
|
|
}
|
|
total += info.Size()
|
|
}
|
|
return nil
|
|
})
|
|
return total, err
|
|
}
|
|
|
|
func (a *API) quotaSnapshot(uid string, root *os.Root, extra int64) (int64, error) {
|
|
size, err := quotaTreeBytes(root, "home")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if err = a.quotaGrowth(uid, size+extra); err != nil {
|
|
return 0, err
|
|
}
|
|
return size, nil
|
|
}
|