File: storage.go

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (45 lines) | stat: -rw-r--r-- 1,020 bytes parent folder | download | duplicates (3)
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
package api

import (
	"fmt"
	"syscall"

	"github.com/gin-gonic/gin"
)

type diskFree struct {
	// Storage size [MiB]
	Total uint64
	// Available Storage [MiB]
	Free uint64
	// Percentage Full
	PercentFull float32
}

// @Summary Get Storage Utilization
// @Description **Get disk free information of aptly storage**
// @Description
// @Description Units in MiB.
// @Tags Status
// @Produce json
// @Success 200 {object} diskFree "Storage information"
// @Failure 400 {object} Error "Internal Error"
// @Router /api/storage [get]
func apiDiskFree(c *gin.Context) {
	var df diskFree

	fs := context.Config().GetRootDir()

	var stat syscall.Statfs_t
	err := syscall.Statfs(fs, &stat)
	if err != nil {
		AbortWithJSONError(c, 400, fmt.Errorf("Error getting storage info on %s: %s", fs, err))
		return
	}

	df.Total = uint64(stat.Blocks) * uint64(stat.Bsize) / 1048576
	df.Free = uint64(stat.Bavail) * uint64(stat.Bsize) / 1048576
	df.PercentFull = 100.0 - float32(stat.Bavail)/float32(stat.Blocks)*100.0

	c.JSON(200, df)
}