parent
5c238db1da
commit
17afd3f8c7
@ -0,0 +1,47 @@
|
|||||||
|
package xmpp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseSizeGarbage(t *testing.T) {
|
||||||
|
_, err := parseSize("abc")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("abc should not be accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSizeAsphalt(t *testing.T) {
|
||||||
|
size, err := parseSize("2B")
|
||||||
|
if size != 2 {
|
||||||
|
t.Errorf("Error parsing two bytes: %v %v", size, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSize9K(t *testing.T) {
|
||||||
|
size, err := parseSize("9 KB")
|
||||||
|
if size != 9216 {
|
||||||
|
t.Errorf("Error parsing 9K: %v %v", size, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSizeBits(t *testing.T) {
|
||||||
|
size, err := parseSize("9 Kb")
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Error parsing kilobits: %v %v", size, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSizeEB(t *testing.T) {
|
||||||
|
size, err := parseSize("3EB")
|
||||||
|
if size != 3458764513820540928 {
|
||||||
|
t.Errorf("Error parsing exabytes: %v %v", size, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSizeOverflow(t *testing.T) {
|
||||||
|
size, err := parseSize("314EB")
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Overflow is not overflowing: %v %v", size, err)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,89 @@
|
|||||||
|
package gateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StorageQuota is a value from config parsed to bytes number
|
||||||
|
var StorageQuota uint64
|
||||||
|
// CachedStorageSize estimates the storage size between full rescans
|
||||||
|
var CachedStorageSize uint64
|
||||||
|
var StorageLock = sync.Mutex{}
|
||||||
|
|
||||||
|
// MeasureStorageSize replaces the estimated storage size with relevant data from the filesystem
|
||||||
|
func MeasureStorageSize(path string) {
|
||||||
|
dents, err := ioutil.ReadDir(path)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var total uint64
|
||||||
|
for _, fi := range dents {
|
||||||
|
if !fi.IsDir() {
|
||||||
|
total += uint64(fi.Size())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if total != CachedStorageSize {
|
||||||
|
if CachedStorageSize > 0 {
|
||||||
|
log.Warnf("Correcting cached storage size: was %v, actually %v", CachedStorageSize, total)
|
||||||
|
}
|
||||||
|
CachedStorageSize = total
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CleanOldFiles purges the oldest files in a directory that exceed the limit
|
||||||
|
func CleanOldFiles(path string, limit uint64) {
|
||||||
|
dents, err := ioutil.ReadDir(path)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var total uint64
|
||||||
|
for _, fi := range dents {
|
||||||
|
if !fi.IsDir() {
|
||||||
|
total += uint64(fi.Size())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort by time
|
||||||
|
sort.Slice(dents, func(i int, j int) bool {
|
||||||
|
return dents[i].ModTime().Before(dents[j].ModTime())
|
||||||
|
})
|
||||||
|
|
||||||
|
// purge
|
||||||
|
if total > limit {
|
||||||
|
toPurge := total - limit
|
||||||
|
|
||||||
|
var purgedAmount uint64
|
||||||
|
var purgedCount uint64
|
||||||
|
|
||||||
|
for _, fi := range dents {
|
||||||
|
if !fi.IsDir() {
|
||||||
|
err = os.Remove(path + string(os.PathSeparator) + fi.Name())
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Couldn't remove %v: %v", fi.Name(), err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
purgedAmount += uint64(fi.Size())
|
||||||
|
purgedCount += 1
|
||||||
|
if purgedAmount >= toPurge {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("Cleaned %v bytes of %v old files", purgedAmount, purgedCount)
|
||||||
|
if CachedStorageSize > purgedAmount {
|
||||||
|
CachedStorageSize -= purgedAmount
|
||||||
|
} else {
|
||||||
|
CachedStorageSize = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue