This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmission-verify.scm
349 lines (297 loc) · 12.2 KB
/
transmission-verify.scm
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
(import
chicken.process-context
chicken.string
chicken.sort
chicken.port)
(import
cling
srfi-1
typed-records)
(import
daemon
transmission
transmission.utils)
(include "connection-options.scm")
(defstruct options
sort-by
torrent
daemon
logfile
rest)
(define *status-order*
(make-parameter
`(
; NOTE: `check` & `check-wait` first to try to avoid rechecking -- only
; useful in case another client is being used concurrently.
,status/check
,status/check-wait
; NOTE: If we're asking to verify torrents, it's probably because we
; suspect they might be corrupt. This status is the one in which a
; peer can suffer most from corrupt files, because if corrupted
; pieces are being sent to other peers, they'll eventually stop
; asking us for anything. The faster we can get seeding torrents
; checked, the faster they can resume seeding.
,status/seed
,status/download
,status/seed-wait
,status/download-wait
; NOTE: Least priority, because we clearly don't care about a stopped
; torrent's files' integrity.
,status/stopped
)))
(define ((sort-by/<predicate key default <?) t1 t2)
(let ((atr1 (alist-ref key t1 eq? default))
(atr2 (alist-ref key t2 eq? default)))
(<? atr1 atr2)))
(define *sort-by/<predicates*
`(
("id" . ,(sort-by/<predicate 'id 0 <))
("latest-activity" . ,(sort-by/<predicate 'activityDate 0 >))
("priority" . ,(sort-by/<predicate 'bandwidthPriority priority/low >))
("progress" . ,(sort-by/<predicate 'percentDone 0 >))
("queue-position" . ,(sort-by/<predicate 'queuePosition 0 <))
; NOTE: Sorted in an ascending manner, i.e., lesser ratio means higher
; priority, because it needs to be seeded more than a torrent with
; greater ratio.
("ratio" . ,(sort-by/<predicate 'uploadRatio 0 <))
("status" . ,(sort-by/<predicate 'status 0 (lambda (s1 s2) (not (not (member s2 (cdr (member s1 (*status-order*)))))))))
("total-size" . ,(sort-by/<predicate 'totalSize 0 <))
; NOTE: Sorted in an ascending manner, i.e., smaller size means higher
; priority, because it's the quickest way to get the greatest number
; of torrents "out of the door".
("progress*total-size"
. ,(lambda (t1 t2)
(let ((p1 (alist-ref 'progress t1 eq? 0))
(ts1 (alist-ref 'totalSize t1 eq? 0))
(p2 (alist-ref 'progress t2 eq? 0))
(ts2 (alist-ref 'totalSize t2 eq? 0)))
(< (* p1 ts1) (* p2 ts2)))))
))
(define (status-str->status-int str)
(let ((rel `(("stopped" . ,status/stopped)
("check-wait" . ,status/check-wait)
("check" . ,status/check)
("download-wait" . ,status/download-wait)
("download" . ,status/download)
("seed-wait" . ,status/seed-wait)
("seed" . ,status/seed))))
(alist-ref str rel string=? #f)))
(define (status-str-list->status-int-list l)
(map status-str->status-int l))
(define *sort-by/alternatives* (map car *sort-by/<predicates*))
(define (sort-by-str->sort-by-pred s)
(let ((pred (alist-ref s *sort-by/<predicates* string=? #f)))
(unless pred
(error 'sort-by-str->sort-by-pred "`sort-by` predicate wasn't found -- this shouldn't have happened!"))
pred))
(define (sort-by-str-list->sort-by-pred-list l)
(map sort-by-str->sort-by-pred l))
(define ((sort/by sort-by) l)
(define (<*? t1 t2)
(define (kons <? ret)
(or ret (<? t1 t2)))
(fold kons #f sort-by))
(sort l <*?))
(define (status/verifying? status)
(or (= status status/check-wait)
(= status status/check)))
(define *OPTS*
(cling
(lambda (ret . rest)
(update-options ret #:rest rest))
(arg '((--sort-by) . sort-by)
#:help "The order in which the torrents should be verified."
#:kons (lambda (ret _ sort-by)
(let* ((sort-by (string-split sort-by "," #t))
(invalid (filter (complement (cute member <> *sort-by/alternatives* string=?)) sort-by)))
(cond
((null? sort-by)
(error '--sort-by "`sort-key` must not be empty."))
((null? invalid)
(update-options ret #:sort-by (sort-by-str-list->sort-by-pred-list sort-by)))
(else
(error '--sort-by "The following `sort-by` keys are not valid: " invalid ". `sort-by` must be in " *sort-by/alternatives*))))))
; TODO: Parse the given string.
(arg '((--status-order) . status-order)
#:help "The order to sort statuses by. Only useful if `status` is specified with `--sort-by`."
#:kons (lambda (ret _ status-order)
(eprint "`--status-order` isn't implemented yet -- ignoring and using the default order of " (*status-order*))
(update-options ret #:status-order (*status-order*))))
; TODO: Parse the given string.
(arg '((--torrent -t) . torrent)
#:help "The torrents to verify (all by default)."
#:kons (lambda (ret _ torrent)
(eprint "`--torrent` isn't implemented yet -- ignoring and acting on all torrents.")
(update-options ret #:torrent #f)))
(arg '((--daemon))
#:help "Run in the background."
#:kons (lambda (ret _ _)
(update-options ret #:daemon #t)))
(arg '((--logfile) . logfile)
#:help "Combined with --daemon, save program output to LOGFILE"
#:kons (lambda (ret _ logfile)
(update-options ret #:logfile logfile)))
))
(define (help*)
(help *connection-opts*)
(help *OPTS*))
(define (process-arguments* args)
(define sort-by
'(; NOTE: `status`, `priority`, `latest-activity`, and `ratio` basically
; all give order based on a torrent's priority, while
; `progress*total-size` gives order based on how quickly a torrent
; can be verified, i.e., smaller torrents can be verified in less
; time than larger torrents.
"status"
"priority"
"latest-activity"
"ratio"
"progress*total-size"
))
(receive (args help?) (update-connection-options! args)
(values (process-arguments *OPTS*
(make-options
#:sort-by (sort-by-str-list->sort-by-pred-list sort-by)
#:logfile #t
)
args)
help?)))
(define (try-n tries proc #!optional (succ? identity))
(assert (positive? tries))
(let loop ((n 0))
(if (< n tries)
(let ((res (proc)))
(if (succ? res)
res
(loop (add1 n))))
#f)))
(define (torrent-ref torrent key #!optional default)
(if torrent
(alist-ref key torrent eq? default)
default))
(define (torrent-hash torrent)
(if (string? torrent)
torrent
(torrent-ref torrent 'hashString #f)))
(define (get-torrents* fields #!optional ids)
(with-transmission-result (torrent-get fields #:ids ids)
(lambda (r . _) (vector->list (alist-ref 'torrents r eq? #())))
(constantly #f)))
(define (get-torrents #!optional ids)
(let ((fields
'(
"activityDate"
"bandwidthPriority"
"hashString"
"id"
"percentDone"
"queuePosition"
"status"
"totalSize"
)))
(get-torrents* fields ids)))
(define (get-torrents/hashes #!optional ids)
(let ((ret (get-torrents* '("hashString") ids)))
(if ret
(let ((ret (map torrent-hash ret)))
(assert (every identity ret) "Some torrents didn't have the `hashString` field -- this shouldn't have happened!")
ret)
'())))
(define (torrent-status torrent #!optional (default status/seed-wait))
(if torrent
(torrent-ref torrent 'status default)
default))
(define (torrent-status! torrent)
(let ((r (get-torrents* '("status") torrent)))
(and r
(torrent-status (car r)))))
; NOTE: This procedure doesn't actually check that a torrent has been verified,
; but that it's not being checked nor queued to be checked. Therefore, it
; assumes the torrent was put on queue to be verified, i.e.,
; `torrent-verify` was called for it.
(define (verified? hash)
; NOTE: If the call succeeds but we get no torrent or hash, then maybe the
; torrent doesn't exist (anymore?). Let's assume that.
; FIXME: If, for example, Transmission goes down, this results in an
; infinite loop until the process is killed or Transmission is
; started up again.
; TODO: Try to distinguish failure reasons.
(and-let* ((torrents (get-torrents* '("status") hash)))
(every (lambda (torrent)
(alist-let/nor torrent (status)
(not (status/verifying? status))))
torrents)))
(define (start-verifying torrent)
(define ((verify* hash))
(define true (constantly #t))
(define false (constantly #f))
(with-transmission-result (torrent-verify #:ids hash) true false))
(and-let* ((hash (torrent-hash torrent)))
(or (status/verifying? (torrent-status! hash))
(try-n 2 (verify* hash)))))
(define (wait-until-verified torrent)
(and-let* ((hash (torrent-hash torrent)))
; TODO: Smarten up this loop (e.g., smarter sleep times).
(let loop ((sleep-time 10)) ; seconds
(unless (verified? hash)
(sleep sleep-time)
(loop sleep-time)))))
(define ((verify/next sort) verifying verified to-verify include-new?)
(define (diff to-verify verified)
(lset-difference
(lambda (a b) (string=? (torrent-hash a) b))
to-verify verified))
(let ((verified (if verifying (cons (torrent-hash verifying) verified) verified))
(ids (if include-new? #f to-verify)))
(let ((to-verify (get-torrents ids)))
(unless to-verify
(error 'verify/next "Failed to get torrents: " ids))
(let ((to-verify (filter identity (map torrent-hash (sort (diff to-verify verified))))))
(values verified to-verify)))))
(define (verify/first sort ids include-new?)
((verify/next sort) #f '() (get-torrents/hashes ids) include-new?))
(define (verify/single torrent)
(let ((hash (torrent-hash torrent)))
(when (start-verifying hash)
(wait-until-verified hash))))
(define (verify sort ids include-new?)
(let ((verify/next (verify/next sort)))
(receive (verified to-verify) (verify/first sort ids include-new?)
(let loop ((verified verified)
(to-verify to-verify))
(unless (null? to-verify)
(let ((verifying (car to-verify))
(to-verify (cdr to-verify)))
(verify/single verifying)
(receive (verified to-verify) (verify/next verifying verified to-verify include-new?)
(loop verified to-verify))))))))
(define ((verify/action torrent sort))
(cond
((null? torrent) #f)
((not torrent) (verify sort #f #t))
((null? (cdr torrent))
(and-let* ((torrent (get-torrents (car torrent))))
(verify/single torrent)))
(else (verify sort torrent #f))))
(define (main args)
(receive (options help?) (process-arguments* args)
(let ((torrent (options-torrent options))
(sort (sort/by (options-sort-by options)))
(daemon? (options-daemon options))
(logfile (options-logfile options)))
(let ((action (verify/action torrent sort))
(torrent (and torrent (get-torrents/hashes torrent))))
(cond
(help? (help*))
((null? torrent) #f) ; noop
(daemon?
(let ((pid (daemon action
#:cwd #f ; Don't change CWD
#:killothers? #t
#:stderr logfile
#:stdout logfile
#:want-pid? #t)))
(unless pid (error 'main "Failed to start daemon"))
(print pid)))
(else (action)))))))
(main (command-line-arguments))