-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtorrents.go
More file actions
227 lines (209 loc) · 6.28 KB
/
Copy pathtorrents.go
File metadata and controls
227 lines (209 loc) · 6.28 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/pyed/rtapi"
)
func formatBytes(bytes uint64) string {
const unit = uint64(1024)
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
div, exponent := unit, 0
for quotient := bytes / unit; quotient >= unit && exponent < 5; quotient /= unit {
div *= unit
exponent++
}
return fmt.Sprintf("%.1f %ciB", float64(bytes)/float64(div), "KMGTPE"[exponent])
}
func hashPrefixes(torrents rtapi.Torrents) map[string]string {
result := make(map[string]string, len(torrents))
for _, torrent := range torrents {
hash := strings.ToLower(strings.TrimSpace(torrent.Hash))
if hash == "" {
continue
}
length := min(7, len(hash))
for length < len(hash) {
unique := true
for _, other := range torrents {
otherHash := strings.ToLower(strings.TrimSpace(other.Hash))
if other != torrent && strings.HasPrefix(otherHash, hash[:length]) {
unique = false
break
}
}
if unique {
break
}
length++
}
result[hash] = hash[:length]
}
return result
}
func torrentRef(torrent *rtapi.Torrent, prefixes map[string]string) string {
if torrent == nil {
return "unknown"
}
hash := strings.ToLower(strings.TrimSpace(torrent.Hash))
if prefix := prefixes[hash]; prefix != "" {
return prefix
}
return hash
}
func resolveTorrent(torrents rtapi.Torrents, reference string) (*rtapi.Torrent, error) {
reference = strings.ToLower(strings.TrimSpace(reference))
if len(reference) < 7 {
return nil, errors.New("torrent hash prefix must contain at least 7 characters")
}
var match *rtapi.Torrent
for _, torrent := range torrents {
if strings.HasPrefix(strings.ToLower(torrent.Hash), reference) {
if match != nil {
return nil, fmt.Errorf("torrent hash prefix %q is ambiguous", reference)
}
match = torrent
}
}
if match == nil {
return nil, fmt.Errorf("no torrent matches hash prefix %q", reference)
}
return match, nil
}
func selectTorrents(torrents rtapi.Torrents, references []string, allowAll bool) (rtapi.Torrents, error) {
if len(references) == 0 {
return nil, errors.New("at least one torrent hash is required")
}
if allowAll && len(references) == 1 && strings.EqualFold(references[0], "all") {
if len(torrents) == 0 {
return nil, errors.New("no torrents are loaded")
}
return torrents, nil
}
selected := make(rtapi.Torrents, 0, len(references))
seen := make(map[string]struct{}, len(references))
for _, reference := range references {
torrent, err := resolveTorrent(torrents, reference)
if err != nil {
return nil, err
}
key := strings.ToLower(torrent.Hash)
if _, duplicate := seen[key]; duplicate {
continue
}
seen[key] = struct{}{}
selected = append(selected, torrent)
}
return selected, nil
}
func trackerHost(tracker *url.URL) string {
if tracker == nil || tracker.Hostname() == "" {
return "unknown"
}
return tracker.Hostname()
}
func formatTorrent(torrent *rtapi.Torrent, reference string) string {
return fmt.Sprintf("<%s> %s\n%s %s (%s) ↓ %s ↑ %s R: %.2f",
reference, torrent.Name, torrent.State, formatBytes(torrent.Completed), torrent.Percent,
formatBytes(torrent.DownRate), formatBytes(torrent.UpRate), torrent.Ratio)
}
func formatTorrentInfo(torrent *rtapi.Torrent) string {
return fmt.Sprintf("%s\n%s %s (%s) ↓ %s ↑ %s R: %.2f UP: %s\nAdded: %s, ETA: %s\nTracker: %s",
torrent.Name, torrent.State, formatBytes(torrent.Completed), torrent.Percent,
formatBytes(torrent.DownRate), formatBytes(torrent.UpRate), torrent.Ratio,
formatBytes(torrent.UpTotal), timeFromUnix(torrent.Age), formatETA(torrent.ETA), trackerHost(torrent.Tracker))
}
func timeFromUnix(seconds uint64) string {
return time.Unix(int64(seconds), 0).Format(time.Stamp)
}
func formatETA(seconds uint64) string {
if seconds == 0 {
return "unknown"
}
return (time.Duration(seconds) * time.Second).String()
}
func deletionRelative(root, target string) (string, error) {
if root == "" {
return "", errors.New("deldata is disabled; configure an absolute -data-root")
}
if !filepath.IsAbs(root) || !filepath.IsAbs(target) {
return "", errors.New("data root and torrent path must be absolute")
}
relative, err := filepath.Rel(filepath.Clean(root), filepath.Clean(target))
if err != nil {
return "", fmt.Errorf("compare data path with configured root: %w", err)
}
if relative == "." || relative == "" || filepath.IsAbs(relative) || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
return "", errors.New("torrent data path is not strictly inside the configured data root")
}
return relative, nil
}
func pathsOverlap(left, right string) bool {
if left == "" || right == "" || !filepath.IsAbs(left) || !filepath.IsAbs(right) {
return false
}
left = canonicalPath(left)
right = canonicalPath(right)
for _, pair := range [][2]string{{left, right}, {right, left}} {
relative, err := filepath.Rel(pair[0], pair[1])
if err == nil && (relative == "." || (relative != ".." && !strings.HasPrefix(relative, ".."+string(filepath.Separator)))) {
return true
}
}
return false
}
func canonicalPath(path string) string {
resolved, err := filepath.EvalSymlinks(path)
if err == nil {
return filepath.Clean(resolved)
}
return filepath.Clean(path)
}
func validateTorrentData(rootPath, targetPath string) (*os.Root, string, error) {
relative, err := deletionRelative(rootPath, targetPath)
if err != nil {
return nil, "", err
}
root, err := os.OpenRoot(rootPath)
if err != nil {
return nil, "", fmt.Errorf("open data root: %w", err)
}
info, err := root.Lstat(relative)
if err != nil {
root.Close()
return nil, "", fmt.Errorf("validate torrent data path: %w", err)
}
if info.Mode()&os.ModeSymlink != 0 {
root.Close()
return nil, "", errors.New("torrent data path must not be a symbolic link")
}
return root, relative, nil
}
func pluralNames(torrents rtapi.Torrents) string {
names := make([]string, len(torrents))
for i, torrent := range torrents {
names[i] = torrent.Name
}
return strings.Join(names, ", ")
}
func parseCount(tokens []string, fallback, total int) (int, error) {
n := fallback
if len(tokens) > 0 {
var err error
n, err = strconv.Atoi(tokens[0])
if err != nil {
return 0, errors.New("argument must be a number")
}
}
if n <= 0 || n > total {
n = total
}
return n, nil
}