-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathfree_space.go
41 lines (35 loc) · 1.19 KB
/
free_space.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
package transmissionrpc
import (
"context"
"fmt"
"github.com/hekmon/cunits/v2"
)
/*
Free Space
https://github.com/transmission/transmission/blob/4.0.3/docs/rpc-spec.md#47-free-space
*/
// FreeSpace allow to see how much free space is available in a client-specified folder.
func (c *Client) FreeSpace(ctx context.Context, path string) (freeSpace, totalSize cunits.Bits, err error) {
payload := &transmissionFreeSpacePayload{Path: path}
var space TransmissionFreeSpace
if err = c.rpcCall(ctx, "free-space", payload, &space); err == nil {
if space.Path == path {
freeSpace = cunits.ImportInByte(float64(space.Size))
totalSize = cunits.ImportInByte(float64(space.TotalSize))
} else {
err = fmt.Errorf("returned path '%s' does not match with requested path '%s'", space.Path, path)
}
} else {
err = fmt.Errorf("'free-space' rpc method failed: %w", err)
}
return
}
type transmissionFreeSpacePayload struct {
Path string `json:"path"`
}
// TransmissionFreeSpace represents the freespace available in bytes for a specific path.
type TransmissionFreeSpace struct {
Path string `json:"path"`
Size int64 `json:"size-bytes"`
TotalSize int64 `json:"total_size"` // RPC v17
}