/
fetch_commons.go
91 lines (81 loc) 路 2.49 KB
/
fetch_commons.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package fetch
import (
"crawshaw.io/sqlite"
"github.com/go-xorm/builder"
"github.com/itchio/butler/butlerd"
"github.com/itchio/butler/database/models"
"github.com/itchio/butler/endpoints/system"
"github.com/itchio/hades"
"github.com/itchio/wharf/state"
)
func FetchCommons(rc *butlerd.RequestContext, params butlerd.FetchCommonsParams) (*butlerd.FetchCommonsResult, error) {
conn := rc.GetConn()
defer rc.PutConn(conn)
var caves []*butlerd.CaveSummary
var downloadKeys []*butlerd.DownloadKeySummary
var installLocations []*models.InstallLocation
var flocs []*butlerd.InstallLocationSummary
models.MustExec(
conn,
builder.Select("id", "game_id", "last_touched_at", "seconds_run", "installed_size").From("caves"),
func(stmt *sqlite.Stmt) error {
caves = append(caves, &butlerd.CaveSummary{
ID: stmt.ColumnText(0),
GameID: stmt.ColumnInt64(1),
LastTouchedAt: models.ColumnTime(2, stmt),
SecondsRun: stmt.ColumnInt64(3),
InstalledSize: stmt.ColumnInt64(4),
})
return nil
},
)
models.MustExec(
conn,
builder.Select("id", "game_id", "created_at").From("download_keys"),
func(stmt *sqlite.Stmt) error {
downloadKeys = append(downloadKeys, &butlerd.DownloadKeySummary{
ID: stmt.ColumnInt64(0),
GameID: stmt.ColumnInt64(1),
CreatedAt: models.ColumnTime(2, stmt),
})
return nil
},
)
models.MustSelect(conn, &installLocations, builder.NewCond(), hades.Search{})
for _, il := range installLocations {
flocs = append(flocs, FormatInstallLocation(conn, rc.Consumer, il))
}
res := &butlerd.FetchCommonsResult{
Caves: caves,
DownloadKeys: downloadKeys,
InstallLocations: flocs,
}
return res, nil
}
func FormatInstallLocation(conn *sqlite.Conn, consumer *state.Consumer, il *models.InstallLocation) *butlerd.InstallLocationSummary {
sum := &butlerd.InstallLocationSummary{
ID: il.ID,
Path: il.Path,
SizeInfo: &butlerd.InstallLocationSizeInfo{
InstalledSize: -1,
FreeSize: -1,
TotalSize: -1,
},
}
models.MustExecRaw(conn, `
SELECT coalesce(sum(coalesce(installed_size, 0)), 0) AS installed_size
FROM caves
WHERE install_location_id = ?
`, func(stmt *sqlite.Stmt) error {
sum.SizeInfo.InstalledSize = stmt.ColumnInt64(0)
return nil
}, il.ID)
stats, err := system.StatFS(il.Path)
if err != nil {
consumer.Warnf("Could not statFS (%s): %s", il.Path, err.Error())
} else {
sum.SizeInfo.FreeSize = stats.FreeSize
sum.SizeInfo.TotalSize = stats.TotalSize
}
return sum
}