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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
|
;;; zfs.el --- ZFS management UI -*- lexical-binding: t; -*-
(require 'vui)
(require 'vui-components)
(require 'async)
(require 'zfs-cli)
(declare-function evil-define-key* "evil-core" (state keymap key def &rest bindings))
(declare-function evil-set-initial-state "evil-core" (mode state))
(defcustom zfs-buffer-name "*zfs*"
"Name of the ZFS management buffer."
:type 'string
:group 'zfs)
(defcustom zfs-targets-file (locate-user-emacs-file "zfs-targets.eld")
"File where backup targets are stored as a plain elisp list."
:type 'file
:group 'zfs)
(defcustom zfs-backup-targets nil
"Seed backup targets, used when `zfs-targets-file' does not exist yet.
Same plist shape as documented there."
:type '(repeat sexp)
:group 'zfs)
(defvar-local zfs-ui--row-actions nil)
(defvar-local zfs-ui--refresh-fn nil)
(defvar-local zfs-ui--back-action nil)
(defun zfs-ui--targets-load ()
(if (file-exists-p zfs-targets-file)
(with-temp-buffer
(insert-file-contents zfs-targets-file)
(goto-char (point-min))
(read (current-buffer)))
zfs-backup-targets))
(defun zfs-ui--targets-save (targets)
(with-temp-file zfs-targets-file
(pp targets (current-buffer))))
(defun zfs--format-bytes (bytes)
(cond
((null bytes) "-")
((not (numberp bytes)) (format "%s" bytes))
(t (let ((units '((1099511627776 . "T") (1073741824 . "G") (1048576 . "M") (1024 . "K")))
(result nil))
(dolist (unit units)
(when (and (not result) (>= bytes (car unit)))
(let ((value (/ bytes (float (car unit)))))
(setq result (if (< value 10) (format "%.1f%s" value (cdr unit))
(format "%.0f%s" value (cdr unit)))))))
(or result (format "%dB" bytes))))))
(defun zfs--format-ago (epoch)
(if (not (numberp epoch))
"-"
(let ((delta (- (float-time) epoch)))
(cond
((< delta 90) "just now")
((< delta 3600) (format "%dm ago" (truncate (/ delta 60))))
((< delta 86400) (format "%dh ago" (truncate (/ delta 3600))))
((< delta (* 30 86400)) (format "%dd ago" (truncate (/ delta 86400))))
((< delta (* 365 86400)) (format "%dmo ago" (truncate (/ delta (* 30 86400)))))
(t (format "%dy ago" (truncate (/ delta (* 365 86400)))))))))
(defun zfs--gauge (percent &optional width)
(let* ((width (or width 20))
(done (max 0 (min width (round (* width (/ (or percent 0) 100.0)))))))
(concat (make-string done ?█) (make-string (- width done) ?░))))
(defun zfs--health-face (health)
(pcase health
("ONLINE" 'success)
("DEGRADED" 'warning)
(_ 'error)))
(defun zfs--capacity-face (percent)
(cond
((>= (or percent 0) 90) 'error)
((>= (or percent 0) 75) 'warning)
(t 'success)))
(defun zfs--direct-children (datasets parent)
(let ((prefix (concat parent "/")))
(seq-filter (lambda (entry)
(and (string-prefix-p prefix (car entry))
(not (string-match-p "/" (substring (car entry) (length prefix))))))
datasets)))
(defun zfs--snapshots-of (snaps dataset)
(seq-filter (lambda (entry)
(string-prefix-p (concat dataset "@") (car entry)))
snaps))
(defun zfs--snapshot-leaf (full-name)
(car (last (split-string full-name "@" t))))
(defun zfs-ui--fetch (host include-disks callback)
(let ((cli-dir (file-name-directory (locate-library "zfs-cli.el"))))
(async-start
`(lambda ()
(add-to-list 'load-path ,cli-dir)
(require 'zfs-cli)
(list :pools (condition-case err (zfs-cli-pools ',host)
(error (cons :error (error-message-string err))))
:datasets (condition-case err (zfs-cli-datasets ',host)
(error (cons :error (error-message-string err))))
:snaps (condition-case err (zfs-cli-snapshots ',host)
(error (cons :error (error-message-string err))))
:locked ,(if include-disks
'(condition-case err
(mapcar (lambda (device)
(append device
(list :pool (and (zfs-cli-disk-unlocked-p device)
(zfs-cli-pool-on-device
(concat "/dev/mapper/" (zfs-cli-disk-mapper device)))))))
(zfs-cli-locked-disks))
(error (cons :error (error-message-string err))))
nil)))
callback)))
(defun zfs-ui--fetch-done (result)
(let ((pools (plist-get result :pools))
(datasets (plist-get result :datasets))
(snaps (plist-get result :snaps))
(locked (plist-get result :locked)))
(vui-batch
(vui-set-state :loading nil)
(vui-set-state :refreshed (format-time-string "%H:%M"))
(if (and (eq (car-safe pools) :error) (eq (car-safe datasets) :error))
(vui-set-state :message (cons (format "Could not reach host: %s" (cdr pools)) 'error))
(vui-set-state :pools (and (not (eq (car-safe pools) :error)) pools))
(vui-set-state :datasets (and (not (eq (car-safe datasets) :error)) datasets))
(vui-set-state :snaps (and (not (eq (car-safe snaps) :error)) snaps))
(vui-set-state :locked (and (listp locked) (not (eq (car-safe locked) :error)) locked))))))
(defun zfs-ui--refresh (host)
(vui-set-state :loading t)
(zfs-ui--fetch host (null host) (vui-async-callback (result) (zfs-ui--fetch-done result))))
(defun zfs-ui--go (page &optional arg)
(vui-batch
(vui-set-state :page page)
(vui-set-state :page-arg arg)
(vui-set-state :backup-target nil)
(vui-set-state :backup-preview nil)
(vui-set-state :backup-progress nil)
(vui-set-state :backup-done nil)
(vui-set-state :create-status nil)
(vui-set-state :copy-status nil)
(vui-set-state :risk-status nil)
(vui-set-state :format-status nil)))
(defun zfs-ui--fail (err)
(vui-set-state :message (cons (format "Error: %s" (error-message-string err)) 'error)))
(defun zfs-ui--do-snapshot (host dataset)
(let ((snap-name (read-string (format "Snapshot name for %s: " dataset)
(format-time-string "manual-%Y-%m-%d_%H-%M"))))
(condition-case err
(progn
(zfs-cli-snapshot host dataset snap-name)
(vui-set-state :message (cons (format "Snapshot %s@%s created" dataset snap-name) 'success))
(zfs-ui--refresh host))
(error (zfs-ui--fail err)))))
(defun zfs-ui--do-mount (host dataset)
(condition-case err
(progn
(zfs-cli-mount host dataset)
(vui-set-state :message (cons (format "Mounted %s" dataset) 'success))
(zfs-ui--refresh host))
(error (zfs-ui--fail err))))
(defun zfs-ui--do-diff (host snapshot)
(let ((command (if host
(format "ssh -o BatchMode=yes %s %s"
(shell-quote-argument (plist-get host :ssh))
(shell-quote-argument (concat "doas -n zfs diff -H " snapshot)))
(format "doas -n zfs diff -H %s" snapshot))))
(if (fboundp 'ghostel-compile)
(ghostel-compile command)
(compile command))))
(defun zfs-ui--do-scrub (host pool stop)
(condition-case err
(progn
(zfs-cli-scrub host pool stop)
(vui-set-state :message (cons (if stop "Scrub stopped" "Scrub started — reads all data, verifies checksums")
'success))
(zfs-ui--refresh host))
(error (zfs-ui--fail err))))
(defun zfs-ui--do-rollback (host snapshot)
(condition-case err
(progn
(zfs-cli-rollback host snapshot)
(vui-set-state :message (cons (format "Rolled back to %s" snapshot) 'success))
(zfs-ui--refresh host)
(zfs-ui--go 'overview))
(error (vui-set-state :risk-status (cons (error-message-string err) 'error)))))
(defun zfs-ui--do-export (host pool)
(condition-case err
(progn
(zfs-cli-export host pool)
(vui-set-state :message (cons (format "Exported %s" pool) 'success))
(zfs-ui--refresh host)
(zfs-ui--go 'overview))
(error (vui-set-state :risk-status (cons (error-message-string err) 'error)))))
(defun zfs-ui--do-format (device pool-name encrypted passphrase)
(let ((mapper (concat "luks-" (plist-get device :name))))
(vui-set-state :format-status (cons "Formatting… this takes a moment" 'shadow))
(zfs-cli-format-disk-async
(plist-get device :path) pool-name mapper (and encrypted passphrase)
(vui-async-callback (status err)
(if (zerop status)
(vui-batch
(vui-set-state :message (cons (format "Pool %s created on %s" pool-name (plist-get device :name))
'success))
(zfs-ui--refresh nil)
(zfs-ui--go 'overview))
(vui-set-state :format-status (cons (format "Format failed: %s" err) 'error)))))))
(defun zfs-ui--do-unlock (device)
(let ((passphrase (read-passwd (format "LUKS passphrase for %s: " (plist-get device :name)))))
(condition-case err
(let ((mapper (concat "luks-" (plist-get device :name))))
(zfs-cli-luks-open (plist-get device :path) mapper passphrase)
(let ((pool (zfs-cli-pool-on-device (concat "/dev/mapper/" mapper))))
(if pool
(progn
(zfs-cli-import nil pool t)
(vui-set-state :message (cons (format "Unlocked — pool %s imported" pool) 'success)))
(vui-set-state :message (cons "Unlocked, but no ZFS pool found on the device" 'warning))))
(zfs-ui--refresh nil))
(error (zfs-ui--fail err)))))
(defun zfs-ui--do-import (device)
(condition-case err
(let ((pool (zfs-cli-pool-on-device (concat "/dev/mapper/" (zfs-cli-disk-mapper device)))))
(if pool
(progn
(zfs-cli-import nil pool t)
(vui-set-state :message (cons (format "Imported %s" pool) 'success))
(zfs-ui--refresh nil))
(vui-set-state :message (cons "No ZFS pool found on the device" 'warning))))
(error (zfs-ui--fail err))))
(defun zfs-ui--common-base (snaps target-snap-names dataset)
(let* ((own (zfs--snapshots-of snaps dataset))
(common (seq-filter (lambda (entry)
(member (zfs--snapshot-leaf (car entry)) target-snap-names))
own))
(sorted (sort (copy-sequence common)
(lambda (a b) (< (or (zfs-cli-prop a 'creation) 0)
(or (zfs-cli-prop b 'creation) 0))))))
(when sorted
(car (car (last sorted))))))
(defun zfs-ui--newest-snapshot (snaps dataset)
(let ((sorted (sort (copy-sequence (zfs--snapshots-of snaps dataset))
(lambda (a b) (< (or (zfs-cli-prop a 'creation) 0)
(or (zfs-cli-prop b 'creation) 0))))))
(car (last sorted))))
(defun zfs-ui--backup-preview (host dataset snapshot snaps entry target)
(let* ((leaf (file-name-nondirectory dataset))
(target-dataset (concat (plist-get target :dataset) "/" leaf))
(target-snap-names (condition-case nil
(mapcar (lambda (snap-entry) (zfs--snapshot-leaf (car snap-entry)))
(zfs-cli-snapshots target target-dataset))
(error nil)))
(base (zfs-ui--common-base snaps target-snap-names dataset))
(raw (and entry (not (member (zfs-cli-prop entry 'encryption) '(nil "off")))))
(preview (zfs-cli-send-preview host snapshot base)))
(list :target-dataset target-dataset
:base base
:raw raw
:kind (plist-get preview :kind)
:bytes (plist-get preview :bytes))))
(defun zfs-ui--backup-event (event)
(pcase (car event)
(:progress
(vui-set-state :backup-progress (nth 1 event)))
(:done
(if (zerop (nth 1 event))
(vui-batch
(vui-set-state :backup-done 'success)
(vui-set-state :backup-progress nil))
(vui-batch
(vui-set-state :backup-done (format "%s" (nth 2 event)))
(vui-set-state :backup-progress nil))))))
(defun zfs-ui--start-backup (host snapshot target preview)
(vui-batch
(vui-set-state :backup-progress "starting…")
(vui-set-state :backup-done nil))
(zfs-cli-send-async host snapshot target (plist-get preview :target-dataset)
(plist-get preview :base) (plist-get preview :raw)
(vui-async-callback (event)
(zfs-ui--backup-event event))))
(defun zfs-ui--field-string (key)
(or (vui-field-value key) ""))
(defun zfs-ui--ack-p (key)
(equal (zfs-ui--field-string key) "ack"))
(defun zfs-ui--start-move (host dataset mountpoint move-dir)
(vui-set-state :create-status (cons "Copying… 0%" 'shadow))
(zfs-cli-rsync-async
move-dir mountpoint 'contents nil
(vui-async-callback (event)
(pcase (car event)
(:progress
(when (string-match "\\([0-9]+%\\)" (nth 1 event))
(vui-set-state :create-status
(cons (format "Copying… %s" (match-string 1 (nth 1 event))) 'shadow))))
(:done
(if (zerop (nth 1 event))
(vui-set-state :message
(cons (format "%s created, data copied — original kept in %s" dataset move-dir)
'success))
(vui-set-state :message
(cons (format "%s created, but copying failed: %s" dataset (nth 2 event))
'error)))
(zfs-ui--go 'dataset dataset)
(zfs-ui--refresh host))))))
(defconst zfs-ui--copy-modes
'((contents "Copy folder contents" "Put a folder's contents into the target. Source stays untouched.")
(item "Copy selected item" "Place the selected file or folder inside the target.")
(move "Move files safely" "Copy first, then remove copied files; folders stay for review.")))
(defun zfs-ui--copy-mode-title (mode)
(cadr (assoc mode zfs-ui--copy-modes)))
(defun zfs-ui--copy-preview-line (line)
(truncate-string-to-width line (max 24 (min 76 (- (window-width) 4))) nil nil "…"))
(defun zfs-ui--copy-preview (output)
(let* ((lines (mapcar #'string-trim (split-string (or output "") "\n")))
(useful (seq-filter (lambda (line)
(and (not (string-empty-p line))
(not (string-prefix-p "sending incremental file list" line))
(not (string-prefix-p "sent " line))
(not (string-prefix-p "total size" line))))
lines))
(shown (seq-take useful 14))
(more (- (length useful) (length shown))))
(if useful
(concat "\n\nPreview:\n"
(string-join (mapcar (lambda (line)
(concat " " (zfs-ui--copy-preview-line line)))
shown)
"\n")
(if (> more 0) (format "\n … %d more" more) ""))
"\n\nPreview: rsync found no file changes.")))
(defun zfs-ui--copy-event (source target dry-run event)
(pcase (car event)
(:progress
(if (string-match "\\([0-9]+%\\)" (nth 1 event))
(vui-set-state :copy-status
(cons (format "%s… %s"
(if dry-run "Checking" "Copying")
(match-string 1 (nth 1 event)))
'shadow))
(when dry-run
(vui-set-state :copy-status (cons "Checking… building preview" 'shadow)))))
(:done
(if (zerop (nth 1 event))
(vui-set-state :copy-status
(cons (if dry-run
(concat (format "Dry run complete: %s → %s, no files changed" source target)
(zfs-ui--copy-preview (nth 3 event)))
(format "Transfer complete: %s → %s" source target))
'success))
(vui-set-state :copy-status (cons (format "Transfer failed: %s" (nth 2 event)) 'error))))))
(defun zfs-ui--do-copy (&optional mode dry-run source target)
(if mode
nil
(setq mode 'contents))
(let* ((field-source (vui-field-value :copy-source))
(field-target (vui-field-value :copy-target))
(source (if (or (null source) (string-blank-p source))
(or field-source (zfs-ui--field-string 'copy-source))
source))
(target (if (or (null target) (string-blank-p target))
(or field-target (zfs-ui--field-string 'copy-target))
target)))
(cond
((string-blank-p source)
(vui-set-state :copy-status (cons "Source is empty" 'error)))
((string-blank-p target)
(vui-set-state :copy-status (cons "Target is empty" 'error)))
(t
(vui-set-state :copy-status (cons (if dry-run "Checking… reading file list" "Copying… 0%") 'shadow))
(zfs-cli-rsync-async source target mode dry-run
(vui-async-callback (event)
(zfs-ui--copy-event source target dry-run event)))))))
(defun zfs-ui--do-create-dataset (host parent)
(let* ((leaf (zfs-ui--field-string 'create-name))
(mountpoint (zfs-ui--field-string 'create-mountpoint))
(quota (zfs-ui--field-string 'create-quota))
(passphrase (zfs-ui--field-string 'create-passphrase))
(encrypted (not (string-blank-p passphrase)))
(compression (zfs-ui--field-string 'create-compression))
(move-dir (zfs-ui--field-string 'create-move))
(dataset (concat parent "/" leaf)))
(cond
((string-blank-p leaf)
(vui-set-state :create-status (cons "Name is empty" 'error)))
((string-match-p "/" leaf)
(vui-set-state :create-status (cons "Name must be a single segment, no slashes" 'error)))
((and (not (string-blank-p move-dir)) (string-blank-p mountpoint))
(vui-set-state :create-status (cons "Moving a folder in needs a mountpoint" 'error)))
(t
(let ((properties nil))
(when (and compression (not (equal compression "inherit")))
(push (cons "compression" compression) properties))
(when (and mountpoint (not (string-blank-p mountpoint)) (not (equal mountpoint "none")))
(push (cons "mountpoint" mountpoint) properties))
(when (and quota (not (string-blank-p quota)))
(push (cons "quota" quota) properties))
(condition-case err
(progn
(if encrypted
(zfs-cli-create-encrypted host dataset passphrase properties)
(zfs-cli-create host dataset properties))
(when (and (not (string-blank-p mountpoint)) (not (equal mountpoint "none")))
(ignore-errors (zfs-cli-mount host dataset)))
(if (not (string-blank-p move-dir))
(zfs-ui--start-move host dataset mountpoint move-dir)
(vui-set-state :message (cons (format "Created %s" dataset) 'success))
(zfs-ui--go 'dataset dataset)
(zfs-ui--refresh host)))
(error (vui-set-state :create-status (cons (error-message-string err) 'error)))))))))
(defconst zfs-ui--property-glossary
'(("used" . "live data + snapshots + children together")
("referenced" . "live data right now, snapshots excluded")
("usedbysnapshots" . "only reachable through snapshots; freed only by snapshot removal outside this UI")
("usedbychildren" . "used by datasets below this one")
("available" . "room this dataset can still grow into")
("compressratio" . "how much smaller data is stored; 1.31x ≈ 24% saved")
("encryption" . "dataset-level encryption, independent of disk LUKS")
("keystatus" . "available = key loaded, data readable")
("mountpoint" . "where it shows up; legacy = mounted by the system, not zfs")
("recordsize" . "max block size; matters for databases and VM images")
("quota" . "hard size limit, if set")
("reservation" . "space guaranteed to this dataset, if set")
("creation" . "when the dataset was created")
("mounted" . "whether it is currently mounted")))
(defun zfs-ui--dataset-row (entry depth snap-count)
(let* ((name (car entry))
(short (file-name-nondirectory name))
(label (concat (make-string (* 2 depth) ?\s)
(if (> snap-count 0) "▸ " " ")
short
(when (> snap-count 0) (format " (%d)" snap-count))))
(encryption (zfs-cli-prop entry 'encryption))
(keystatus (zfs-cli-prop entry 'keystatus)))
(vui-hstack
(vui-box (vui-button (truncate-string-to-width label 32) :no-decoration t
:key (concat "ds:" name)
:on-click (lambda () (zfs-ui--go 'dataset name)))
:width 34)
(vui-box (vui-text (zfs--format-bytes (zfs-cli-prop entry 'used))) :width 9)
(vui-box (vui-text (zfs--format-bytes (zfs-cli-prop entry 'available)) :face 'shadow) :width 9)
(vui-box (vui-text (format "%s" (or (zfs-cli-prop entry 'compressratio) "-")) :face 'shadow) :width 7)
(vui-box (cond
((or (null encryption) (equal encryption "off")) (vui-text "·" :face 'shadow))
((equal keystatus "available") (vui-text "🔓" :face 'success))
(t (vui-text "🔒" :face 'warning)))
:width 4))))
(defun zfs-ui--dataset-rows (entry depth datasets snaps)
(let* ((name (car entry))
(children (zfs--direct-children datasets name))
(own-snaps (zfs--snapshots-of snaps name)))
(push (cons (concat "ds:" name)
(list :open (vui-with-async-context (zfs-ui--go 'dataset name))
:backup (vui-with-async-context (zfs-ui--go 'backup name))
:create-child (vui-with-async-context (zfs-ui--go 'create name))))
zfs-ui--row-actions)
(cons (zfs-ui--dataset-row entry depth (length own-snaps))
(mapcan (lambda (child) (zfs-ui--dataset-rows child (1+ depth) datasets snaps))
children))))
(defun zfs-ui--pool-row (pool)
(let* ((name (car pool))
(health (zfs-cli-prop pool 'health))
(capacity (zfs-cli-prop pool 'capacity))
(size (zfs-cli-prop pool 'size))
(free (zfs-cli-prop pool 'free)))
(push (cons (concat "pool:" name)
(list :open (vui-with-async-context (zfs-ui--go 'pool name))
:create-child (vui-with-async-context (zfs-ui--go 'create name))))
zfs-ui--row-actions)
(vui-hstack
:spacing 1
(vui-box (vui-button name :no-decoration t
:key (concat "pool:" name)
:on-click (lambda () (zfs-ui--go 'pool name)))
:width 10)
(vui-box (vui-text (or health "?") :face (zfs--health-face health)) :width 9)
(vui-box (vui-text (zfs--gauge capacity)
:face (zfs--capacity-face capacity))
:width 22)
(vui-text (format "%d%% · %s free of %s"
(or capacity 0)
(zfs--format-bytes free)
(zfs--format-bytes size))
:face 'shadow))))
(defun zfs-ui--locked-disk-row (device pools)
(let* ((name (plist-get device :name))
(unlocked (zfs-cli-disk-unlocked-p device))
(mapper (zfs-cli-disk-mapper device))
(pool (plist-get device :pool))
(imported (and pool (assoc pool pools))))
(push (cons (concat "lock:" name)
(if unlocked
(unless imported
(list :import (vui-with-async-context (zfs-ui--do-import device))))
(list :unlock (vui-with-async-context (zfs-ui--do-unlock device)))))
zfs-ui--row-actions)
(vui-hstack
:spacing 1
(vui-box (vui-text (if unlocked "🔓" "🔒") :face (if unlocked 'warning 'shadow)) :width 3)
(vui-box (vui-button name :no-decoration t
:key (concat "lock:" name)
:on-click (lambda () nil))
:width 10)
(vui-box (vui-text (or (plist-get device :model) "?") :face 'shadow) :width 24)
(vui-box (vui-text (zfs--format-bytes (plist-get device :size)) :face 'shadow) :width 8)
(vui-text (cond
(imported (format "pool %s imported" pool))
(unlocked (format "unlocked as %s · i imports pool %s" mapper (or pool "?")))
(t "locked · u unlock"))
:face 'shadow))))
(defun zfs-ui--page-overview (pools datasets snaps locked)
(push (cons 'page
(list :copy (vui-with-async-context (zfs-ui--go 'copy))
:admin (vui-with-async-context (zfs-ui--go 'admin))
:targets (vui-with-async-context (zfs-ui--go 'targets))))
zfs-ui--row-actions)
(let ((roots (seq-filter (lambda (entry) (not (string-match-p "/" (car entry)))) datasets)))
(apply
#'vui-vstack
:spacing 1
(append
(when locked
(list (vui-vstack
:spacing 0
(vui-heading-3 "Disks")
(vui-muted "LUKS disks ZFS can use once unlocked")
(apply #'vui-vstack :spacing 0
(mapcar (lambda (device) (zfs-ui--locked-disk-row device pools)) locked)))))
(list
(vui-vstack
:spacing 0
(vui-heading-3 "Utilities")
(vui-hstack
:spacing 2
(vui-box (vui-text "C" :face 'success) :width 3)
(vui-button "Copy files" :no-decoration t :face 'success
:on-click (lambda () (zfs-ui--go 'copy)))
(vui-text "dry-run, copy, or move paths with rsync" :face 'shadow))
(vui-hstack
:spacing 2
(vui-box (vui-text "A" :face 'warning) :width 3)
(vui-button "Admin actions" :no-decoration t :face 'warning
:on-click (lambda () (zfs-ui--go 'admin)))
(vui-text "ack-gated export and format tools" :face 'shadow)))
(vui-vstack
:spacing 0
(vui-heading-3 "Pools")
(vui-muted "teams of disks; datasets live inside them · RET details")
(if pools
(apply #'vui-vstack :spacing 0 (mapcar #'zfs-ui--pool-row pools))
(vui-muted "No pools found. Kernel module loaded, pools imported?")))
(vui-vstack
:spacing 0
(vui-heading-3 "Datasets")
(vui-muted "named drawers · RET details · b backup · c new dataset inside")
(vui-hstack
(vui-box (vui-text "NAME" :face 'shadow) :width 34)
(vui-box (vui-text "USED" :face 'shadow) :width 9)
(vui-box (vui-text "AVAIL" :face 'shadow) :width 9)
(vui-box (vui-text "RATIO" :face 'shadow) :width 7)
(vui-box (vui-text "ENC" :face 'shadow) :width 4))
(apply #'vui-vstack :spacing 0
(mapcan (lambda (root) (zfs-ui--dataset-rows root 0 datasets snaps)) roots))))))))
(defun zfs-ui--property-row (entry property)
(let* ((raw (zfs-cli-prop entry (intern property)))
(value (cond
((equal property "creation") (format "%s (%s)"
(format-time-string "%Y-%m-%d" raw)
(zfs--format-ago raw)))
((and (member property '("quota" "reservation")) (or (null raw) (equal raw 0))) "none")
((numberp raw) (zfs--format-bytes raw))
(t (format "%s" (or raw "-")))))
(gloss (cdr (assoc property zfs-ui--property-glossary))))
(vui-hstack
(vui-box (vui-text property :face 'shadow) :width 18)
(vui-box (vui-text value) :width 22)
(vui-text (or gloss "") :face 'shadow))))
(defun zfs-ui--snapshot-row (snap dataset host)
(let* ((full-name (car snap))
(leaf (zfs--snapshot-leaf full-name)))
(push (cons (concat "snap:" full-name)
(list :diff (vui-with-async-context (zfs-ui--do-diff host full-name))
:rollback (vui-with-async-context (zfs-ui--go 'rollback full-name))
:backup (vui-with-async-context (zfs-ui--go 'backup (cons dataset full-name)))))
zfs-ui--row-actions)
(vui-hstack
(vui-box (vui-button (concat "@" leaf) :no-decoration t :face 'shadow
:key (concat "snap:" full-name)
:on-click (lambda () nil))
:width 38)
(vui-box (vui-text (zfs--format-bytes (zfs-cli-prop snap 'used)) :face 'shadow) :width 9)
(vui-text (zfs--format-ago (zfs-cli-prop snap 'creation)) :face 'shadow))))
(defun zfs-ui--page-dataset (name host datasets snaps)
(let* ((entry (assoc name datasets))
(own-snaps (zfs--snapshots-of snaps name)))
(when entry
(push (cons 'page
(list :snapshot (vui-with-async-context (zfs-ui--do-snapshot host name))
:backup (vui-with-async-context (zfs-ui--go 'backup name))
:create-child (vui-with-async-context (zfs-ui--go 'create name))
:mount (vui-with-async-context (zfs-ui--do-mount host name))))
zfs-ui--row-actions))
(if (null entry)
(vui-text (format "%s not found — g to refresh" name) :face 'error)
(apply
#'vui-vstack
:spacing 1
(append
(list
(vui-hstack :spacing 2
(vui-heading-3 name)
(vui-text (downcase (or (alist-get 'type (cdr entry)) "")) :face 'shadow))
(vui-muted (format "Everything in this dataset and its snapshots uses %s."
(zfs--format-bytes (zfs-cli-prop entry 'used))))
(vui-text "s snapshot · b backup · c new dataset inside · m mount" :face 'shadow)
(apply #'vui-vstack :spacing 0
(mapcar (lambda (property) (zfs-ui--property-row entry property))
'("used" "referenced" "usedbysnapshots" "usedbychildren" "available"
"compressratio" "encryption" "keystatus" "mountpoint" "recordsize"
"quota" "creation" "mounted"))))
(list
(apply
#'vui-vstack
:spacing 0
(append
(list (vui-heading-3 (format "Snapshots (%d)" (length own-snaps)))
(vui-muted "frozen moments · d diff · b backup · R rollback opens an ack page"))
(if own-snaps
(mapcar (lambda (snap) (zfs-ui--snapshot-row snap name host)) own-snaps)
(list (vui-muted "none yet — press s to freeze this moment")))))))))))
(defun zfs-ui--vdev-row (vdev depth)
(vui-hstack
(vui-box (vui-text (concat (make-string (* 2 depth) ?\s) (plist-get vdev :name))) :width 40)
(vui-box (vui-text (or (plist-get vdev :state) "?")
:face (zfs--health-face (plist-get vdev :state)))
:width 9)
(vui-text (format "%s/%s/%s errors"
(or (plist-get vdev :read-errors) 0)
(or (plist-get vdev :write-errors) 0)
(or (plist-get vdev :checksum-errors) 0))
:face 'shadow)))
(defun zfs-ui--page-pool (name host)
(let ((status (ignore-errors (zfs-cli-pool-status host name))))
(when status
(push (cons 'page
(list :scrub-start (vui-with-async-context (zfs-ui--do-scrub host name nil))
:scrub-stop (vui-with-async-context (zfs-ui--do-scrub host name t))
:export (vui-with-async-context (zfs-ui--go 'export name))))
zfs-ui--row-actions))
(if (null status)
(vui-text (format "Could not read status of %s" name) :face 'error)
(let ((state (alist-get 'state status))
(error-count (alist-get 'error_count status))
(scan (alist-get 'scan status))
(vdevs (zfs-cli-pool-vdevs status)))
(vui-vstack
:spacing 1
(vui-hstack :spacing 2
(vui-heading-3 name)
(vui-text state :face (zfs--health-face state)))
(vui-muted (if (> (or error-count 0) 0)
(format "%d errors — check the devices below" error-count)
"No known data errors."))
(vui-text (if scan
(format "Scan: %s" (or (alist-get 'state scan) "recorded"))
"Scrub: never run here — it reads everything and verifies checksums")
:face 'shadow)
(vui-text "1 scrub start · 2 scrub stop · x export opens an ack page" :face 'shadow)
(vui-vstack
:spacing 0
(vui-heading-3 "Devices")
(apply #'vui-vstack :spacing 0
(mapcar (lambda (vdev) (zfs-ui--vdev-row vdev 0)) vdevs))))))))
(defun zfs-ui--form-row (label content &optional gloss)
(vui-hstack
(vui-box (vui-text label :face 'shadow) :width 14 :align :right)
content
(when gloss (vui-hstack (vui-space 2) (vui-text gloss :face 'shadow)))))
(defun zfs-ui--page-create (parent host create-status)
(push (cons 'page (list :submit (vui-with-async-context (zfs-ui--do-create-dataset host parent))))
zfs-ui--row-actions)
(vui-vstack
:spacing 1
(vui-heading-3 (format "New dataset inside %s" parent))
(vui-muted "A dataset is a drawer: its own compression, quota and snapshots.")
(zfs-ui--form-row "Name"
(vui-field :size 24 :key 'create-name :placeholder "music")
(format "→ %s/<name>" parent))
(zfs-ui--form-row "Mountpoint"
(vui-field :size 24 :key 'create-mountpoint
:placeholder (expand-file-name "~/music"))
"where it appears; empty = not mounted")
(zfs-ui--form-row "Compression"
(vui-field :size 24 :key 'create-compression :value "zstd"
:placeholder "zstd")
"zstd squeezes better, lz4 is faster, off disables")
(zfs-ui--form-row "Passphrase"
(vui-field :size 24 :key 'create-passphrase :secret t
:placeholder "empty = not encrypted")
"dataset-level, on top of disk LUKS")
(zfs-ui--form-row "Quota"
(vui-field :size 24 :key 'create-quota :placeholder "empty = no limit")
"e.g. 50G")
(zfs-ui--form-row "Move folder in"
(vui-field :size 24 :key 'create-move
:placeholder "empty = start empty")
"rsync copy with progress; original is kept")
(vui-hstack
(vui-box (vui-text "") :width 14)
(vui-button "Create dataset" :on-click (lambda () (zfs-ui--do-create-dataset host parent))))
(when create-status
(vui-text (car create-status) :face (cdr create-status)))))
(defun zfs-ui--copy-field-width ()
(max 24 (min 58 (- (window-width) 8))))
(defun zfs-ui--copy-path-field (label key value placeholder)
(vui-vstack
:spacing 0
(vui-text label :face 'shadow)
(vui-field :size (zfs-ui--copy-field-width)
:key key
:value value
:placeholder placeholder
:on-change (lambda (next)
(vui-batch
(vui-set-state key next)
(vui-set-state :copy-status nil))))))
(defun zfs-ui--copy-mode-card (mode selected)
(let ((entry (assoc mode zfs-ui--copy-modes)))
(vui-vstack
:spacing 0
(vui-button (format "%s %s"
(if (eq mode selected) "●" "○")
(cadr entry))
:no-decoration t
:face (when (eq mode selected) 'success)
:on-click (lambda ()
(vui-batch
(vui-set-state :copy-source (zfs-ui--field-string :copy-source))
(vui-set-state :copy-target (zfs-ui--field-string :copy-target))
(vui-set-state :copy-mode mode)
(vui-set-state :copy-status nil))))
(vui-box (vui-text (caddr entry) :face 'shadow)
:width (max 24 (min 48 (- (window-width) 8)))))))
(defun zfs-ui--page-copy (copy-status &optional copy-mode copy-dry-run copy-source copy-target)
(if copy-mode
nil
(setq copy-mode 'contents))
(push (cons 'page (list :submit (vui-with-async-context
(zfs-ui--do-copy copy-mode copy-dry-run copy-source copy-target))))
zfs-ui--row-actions)
(vui-vstack
:spacing 1
(vui-heading-3 "Copy files")
(vui-muted "Preview or transfer files with rsync. Long paths stay on their own lines.")
(zfs-ui--copy-path-field "Source" :copy-source copy-source "~/Downloads/media")
(zfs-ui--copy-path-field "Target" :copy-target copy-target "/mnt/tank/media")
(vui-vstack
:spacing 1
(vui-text "Mode" :face 'shadow)
(apply #'vui-vstack
:spacing 1
(mapcar (lambda (entry) (zfs-ui--copy-mode-card (car entry) copy-mode))
zfs-ui--copy-modes)))
(vui-checkbox :checked copy-dry-run
:label "Dry run — preview the plan without changing files"
:on-change (lambda (value)
(vui-batch
(vui-set-state :copy-source (zfs-ui--field-string :copy-source))
(vui-set-state :copy-target (zfs-ui--field-string :copy-target))
(vui-set-state :copy-dry-run value)
(vui-set-state :copy-status nil))))
(vui-hstack
:spacing 2
(vui-button (if copy-dry-run "Preview transfer" "Start transfer")
:on-click (lambda () (zfs-ui--do-copy copy-mode copy-dry-run copy-source copy-target)))
(vui-text (or (zfs-ui--copy-mode-title copy-mode) "") :face 'shadow))
(when copy-status
(vui-text (car copy-status) :face (cdr copy-status)))))
(defun zfs-ui--page-admin ()
(push (cons 'page (list :format (vui-with-async-context (zfs-ui--go 'format))))
zfs-ui--row-actions)
(vui-vstack
:spacing 1
(vui-heading-3 "Admin actions")
(vui-text "These actions live one level away and require typing ack before they run." :face 'warning)
(vui-hstack
:spacing 2
(vui-button "Format unused disk" :face 'warning :on-click (lambda () (zfs-ui--go 'format))))
(vui-muted "Pool export is on a pool page. Snapshot rollback is on a snapshot row.")))
(defun zfs-ui--page-export (pool host risk-status)
(push (cons 'page
(list :confirm (vui-with-async-context
(if (zfs-ui--ack-p 'export-ack)
(zfs-ui--do-export host pool)
(vui-set-state :risk-status (cons "Type ack to export this pool" 'error))))))
zfs-ui--row-actions)
(vui-vstack
:spacing 1
(vui-heading-3 (format "Export %s" pool))
(vui-text "Export takes the pool offline for this system until it is imported again." :face 'warning)
(zfs-ui--form-row "Confirm" (vui-field :size 10 :key 'export-ack :placeholder "ack")
"type ack")
(vui-hstack
(vui-box (vui-text "") :width 14)
(vui-button "Export pool"
:face 'warning
:on-click (lambda ()
(if (zfs-ui--ack-p 'export-ack)
(zfs-ui--do-export host pool)
(vui-set-state :risk-status (cons "Type ack to export this pool" 'error))))))
(when risk-status
(vui-text (car risk-status) :face (cdr risk-status)))))
(defun zfs-ui--page-rollback (snapshot host risk-status)
(push (cons 'page
(list :confirm (vui-with-async-context
(if (zfs-ui--ack-p 'rollback-ack)
(zfs-ui--do-rollback host snapshot)
(vui-set-state :risk-status (cons "Type ack to roll back" 'error))))))
zfs-ui--row-actions)
(vui-vstack
:spacing 1
(vui-heading-3 (format "Rollback to @%s" (zfs--snapshot-leaf snapshot)))
(vui-text "This discards live changes made after the snapshot." :face 'warning)
(vui-muted "The UI calls plain zfs rollback without -r: if newer snapshots exist, ZFS refuses instead of deleting them.")
(zfs-ui--form-row "Confirm" (vui-field :size 10 :key 'rollback-ack :placeholder "ack")
"type ack")
(vui-hstack
(vui-box (vui-text "") :width 14)
(vui-button "Rollback"
:face 'warning
:on-click (lambda ()
(if (zfs-ui--ack-p 'rollback-ack)
(zfs-ui--do-rollback host snapshot)
(vui-set-state :risk-status (cons "Type ack to roll back" 'error))))))
(when risk-status
(vui-text (car risk-status) :face (cdr risk-status)))))
(defun zfs-ui--page-format (format-status)
(let ((candidates (zfs-cli-disk-candidates)))
(apply
#'vui-vstack
:spacing 1
(append
(list
(vui-heading-3 "Format unused disk")
(vui-text "Only unused disks are listed here. The next page still requires ack." :face 'warning))
(if candidates
(mapcar (lambda (device)
(vui-button (format "%s — %s — %s"
(plist-get device :name)
(or (plist-get device :model) "?")
(zfs--format-bytes (plist-get device :size)))
:no-decoration t
:on-click (lambda () (zfs-ui--go 'format-confirm device))))
candidates)
(list (vui-muted "No unused disks found. Plug one in and press g.")))
(when format-status
(list (vui-text (car format-status) :face (cdr format-status))))))))
(defun zfs-ui--page-format-confirm (device format-status)
(vui-vstack
:spacing 1
(vui-heading-3 (format "Format %s" (plist-get device :name)))
(vui-text (format "%s · %s — all data on it is erased"
(or (plist-get device :model) "?")
(zfs--format-bytes (plist-get device :size)))
:face 'error)
(zfs-ui--form-row "Pool name" (vui-field :size 24 :key 'format-pool :placeholder "tank"))
(zfs-ui--form-row "Passphrase" (vui-field :size 24 :key 'format-pass :secret t
:placeholder "empty = no encryption")
"LUKS passphrase; empty leaves the disk unencrypted")
(zfs-ui--form-row "Disk name" (vui-field :size 24 :key 'format-confirm
:placeholder (plist-get device :name))
"type the disk name")
(zfs-ui--form-row "Confirm" (vui-field :size 10 :key 'format-ack :placeholder "ack")
"type ack")
(vui-hstack
(vui-box (vui-text "") :width 14)
(vui-button "Format"
:face 'error
:on-click
(lambda ()
(let ((pool-name (zfs-ui--field-string 'format-pool))
(passphrase (zfs-ui--field-string 'format-pass)))
(cond
((string-blank-p pool-name)
(vui-set-state :format-status (cons "Pool name is empty" 'error)))
((not (equal (zfs-ui--field-string 'format-confirm) (plist-get device :name)))
(vui-set-state :format-status (cons "Type the disk name to confirm" 'error)))
((not (zfs-ui--ack-p 'format-ack))
(vui-set-state :format-status (cons "Type ack to format this disk" 'error)))
(t
(zfs-ui--do-format device
pool-name
(not (string-blank-p passphrase))
passphrase)))))))
(when format-status
(vui-text (car format-status) :face (cdr format-status)))))
(defun zfs-ui--page-backup (arg host datasets snaps targets backup-target backup-preview backup-progress backup-done)
(let* ((dataset (if (consp arg) (car arg) arg))
(snapshot (if (consp arg) (cdr arg)
(car (zfs-ui--newest-snapshot snaps dataset))))
(entry (assoc dataset datasets)))
(if (null snapshot)
(vui-vstack
:spacing 1
(vui-heading-3 (format "Backup %s" dataset))
(vui-text "No snapshots yet — a backup sends a snapshot." :face 'warning)
(vui-button "Create one now"
:on-click (lambda () (zfs-ui--do-snapshot host dataset))))
(vui-vstack
:spacing 1
(vui-heading-3 (format "Backup %s" dataset))
(vui-muted "Sends a snapshot to another machine or pool. After the first full copy only the changes travel.")
(vui-hstack
(vui-box (vui-text "Snapshot" :face 'shadow) :width 14 :align :right)
(vui-text (concat "@" (zfs--snapshot-leaf snapshot)))
(vui-space 2)
(vui-text (format "(%s)" (zfs--format-ago (zfs-cli-prop (assoc snapshot snaps) 'creation)))
:face 'shadow))
(vui-vstack
:spacing 0
(vui-hstack (vui-box (vui-text "Target" :face 'shadow) :width 14 :align :right)
(vui-text ""))
(apply
#'vui-vstack
:spacing 0
(mapcar (lambda (target)
(vui-hstack
(vui-box (vui-text "") :width 14)
(vui-button (format "%s — %s %s"
(plist-get target :name)
(or (plist-get target :ssh) "this machine")
(plist-get target :dataset))
:no-decoration (not (equal target backup-target))
:face (when (equal target backup-target) 'success)
:on-click
(lambda ()
(vui-set-state :backup-target target)
(condition-case err
(vui-set-state :backup-preview
(zfs-ui--backup-preview host dataset snapshot snaps entry target))
(error (vui-set-state :message
(cons (format "Preview failed: %s" (error-message-string err)) 'error))))))))
targets)))
(vui-hstack
(vui-box (vui-text "One-off" :face 'shadow) :width 14 :align :right)
(vui-field :size 18 :key 'oneoff-ssh :placeholder "user@host — empty = local")
(vui-space 1)
(vui-field :size 18 :key 'oneoff-prefix :placeholder "pool/prefix")
(vui-space 1)
(vui-button "Use once"
:on-click
(lambda ()
(let ((prefix (zfs-ui--field-string 'oneoff-prefix)))
(if (string-blank-p prefix)
(vui-set-state :message (cons "One-off target needs a dataset prefix" 'error))
(let ((target (list :name "one-off" :dataset prefix))
(ssh (zfs-ui--field-string 'oneoff-ssh)))
(unless (string-blank-p ssh)
(setq target (append target (list :ssh ssh))))
(vui-set-state :backup-target target)
(condition-case err
(vui-set-state :backup-preview
(zfs-ui--backup-preview host dataset snapshot snaps entry target))
(error (vui-set-state :message
(cons (format "Preview failed: %s" (error-message-string err)) 'error))))))))))
(when backup-target
(vui-hstack
(vui-box (vui-text "Will send" :face 'shadow) :width 14 :align :right)
(if backup-preview
(vui-text (format "%s · %s%s"
(if (equal (plist-get backup-preview :kind) "incremental")
(format "only changes since @%s" (zfs--snapshot-leaf (plist-get backup-preview :base)))
"everything (first time)")
(zfs--format-bytes (plist-get backup-preview :bytes))
(if (plist-get backup-preview :raw) " · stays encrypted" "")))
(vui-text "…" :face 'shadow))))
(when (and backup-target backup-preview (not backup-progress) (not backup-done))
(vui-hstack
(vui-box (vui-text "") :width 14)
(vui-button "Start backup"
:on-click (lambda () (zfs-ui--start-backup host snapshot backup-target backup-preview)))))
(when backup-progress
(vui-hstack
(vui-box (vui-text "Sending" :face 'shadow) :width 14 :align :right)
(vui-text backup-progress :face 'shadow)))
(when backup-done
(vui-hstack
(vui-box (vui-text "") :width 14)
(if (eq backup-done 'success)
(vui-text "Backup complete." :face 'success)
(vui-text (format "Backup failed: %s" backup-done) :face 'error))))))))
(defun zfs-ui--page-targets (targets)
(push (cons 'page
(list :add (vui-with-async-context
(let ((name (zfs-ui--field-string 'target-name))
(ssh (zfs-ui--field-string 'target-ssh))
(prefix (zfs-ui--field-string 'target-prefix)))
(if (or (string-blank-p name) (string-blank-p prefix))
(vui-set-state :message (cons "Name and dataset prefix are required" 'error))
(let ((new (list :name name :dataset prefix)))
(unless (string-blank-p ssh)
(setq new (append new (list :ssh ssh))))
(let ((updated (append targets (list new))))
(zfs-ui--targets-save updated)
(vui-set-state :targets updated)
(vui-set-state :message (cons (format "Target %s saved to %s" name zfs-targets-file)
'success)))))))))
zfs-ui--row-actions)
(apply
#'vui-vstack
:spacing 1
(append
(list
(vui-heading-3 "Backup targets")
(vui-muted (format "Stored as plain elisp data in %s — edit by hand or here." zfs-targets-file)))
(if targets
(list (apply
#'vui-vstack
:spacing 0
(mapcar (lambda (target)
(vui-hstack
:spacing 2
(vui-box (vui-text (plist-get target :name)) :width 16)
(vui-box (vui-text (or (plist-get target :ssh) "this machine") :face 'shadow) :width 24)
(vui-text (plist-get target :dataset) :face 'shadow)))
targets)))
(list (vui-muted "No targets yet — add one below.")))
(list
(vui-vstack
:spacing 1
(vui-heading-3 "Add target")
(zfs-ui--form-row "Name" (vui-field :size 24 :key 'target-name :placeholder "interserver"))
(zfs-ui--form-row "SSH" (vui-field :size 24 :key 'target-ssh :placeholder "user@host — empty = this machine"))
(zfs-ui--form-row "Dataset prefix" (vui-field :size 24 :key 'target-prefix :placeholder "backuppool/mainframe")
"must exist on the target; the dataset's leaf name is appended")
(vui-hstack
(vui-box (vui-text "") :width 14)
(vui-button "Save target"
:on-click (lambda ()
(when-let* ((actions (cdr (assoc 'page zfs-ui--row-actions)))
(fn (plist-get actions :add)))
(funcall fn))))))))))
(vui-defcomponent zfs-app ()
:state ((page 'overview)
(page-arg nil)
(host nil)
(pools nil)
(datasets nil)
(snaps nil)
(locked nil)
(targets (zfs-ui--targets-load))
(loading t)
(message nil)
(refreshed nil)
(backup-target nil)
(backup-preview nil)
(backup-progress nil)
(backup-done nil)
(create-status nil)
(copy-status nil)
(copy-mode 'contents)
(copy-dry-run t)
(copy-source "")
(copy-target "")
(risk-status nil)
(format-status nil))
:render
(progn
(vui-use-effect ()
(zfs-ui--refresh host)
nil)
(setq zfs-ui--row-actions nil)
(setq zfs-ui--refresh-fn (vui-with-async-context (zfs-ui--refresh host)))
(setq zfs-ui--back-action
(vui-with-async-context
(if (eq page 'overview)
(bury-buffer)
(zfs-ui--go 'overview))))
(let ((content
(cond
(loading (vui-text "Loading…" :face 'shadow))
((eq page 'overview)
(zfs-ui--page-overview (or pools nil) (or datasets nil) (or snaps nil) locked))
((eq page 'dataset) (zfs-ui--page-dataset page-arg host datasets snaps))
((eq page 'pool) (zfs-ui--page-pool page-arg host))
((eq page 'create) (zfs-ui--page-create page-arg host create-status))
((eq page 'copy) (zfs-ui--page-copy copy-status copy-mode copy-dry-run copy-source copy-target))
((eq page 'admin) (zfs-ui--page-admin))
((eq page 'export) (zfs-ui--page-export page-arg host risk-status))
((eq page 'rollback) (zfs-ui--page-rollback page-arg host risk-status))
((eq page 'format) (zfs-ui--page-format format-status))
((eq page 'format-confirm) (zfs-ui--page-format-confirm page-arg format-status))
((eq page 'backup) (zfs-ui--page-backup page-arg host datasets snaps targets
backup-target backup-preview
backup-progress backup-done))
((eq page 'targets) (zfs-ui--page-targets targets))
(t (vui-text "unknown page" :face 'error)))))
(vui-vstack
:spacing 1
(vui-flex :width 'window :justify :space-between
(vui-hstack :spacing 2
(vui-heading-2 "ZFS")
(vui-text (cond
((eq page 'overview) (if host (format "on %s" (plist-get host :name)) "on this machine"))
(t (format "› %s" (if (symbolp page) (symbol-name page) page))))
:face 'shadow))
(vui-text (format "%s pools · refreshed %s" (length pools) (or refreshed "—")) :face 'shadow))
(if message
(vui-text (car message) :face (cdr message))
(vui-text " "))
content
(vui-text (pcase page
('overview "RET open · C copy · A admin · u unlock · T targets · r refresh · q quit")
('dataset "q back · s snapshot · b backup · c new dataset · d diff · R rollback · r refresh")
('pool "q back · 1 scrub start · 2 scrub stop · x export · r refresh")
('backup "q back · click a target · r refresh")
(_ "q back · r refresh"))
:face 'shadow)))))
(defun zfs-ui--key-on-line ()
(save-excursion
(beginning-of-line)
(let ((end (line-end-position))
(found nil))
(while (and (<= (point) end) (not found))
(setq found (vui-key-at))
(forward-char 1))
found)))
(defun zfs-ui--invoke (action)
(let* ((key (zfs-ui--key-on-line))
(row (and key (cdr (assoc key zfs-ui--row-actions))))
(page (cdr (assoc 'page zfs-ui--row-actions))))
(cond
((plist-get row action) (funcall (plist-get row action)))
((and (eq action :open) (plist-get row :open)) (funcall (plist-get row :open)))
((plist-get page action) (funcall (plist-get page action)))
(t (message "Nothing here for that key")))))
(define-derived-mode zfs-mode vui-mode "ZFS"
(hl-line-mode 1))
(defun zfs-ui-open ()
(interactive)
(or (vui-activate)
(zfs-ui--invoke :open)))
(defun zfs-ui-back ()
(interactive)
(if zfs-ui--back-action
(funcall zfs-ui--back-action)
(zfs-ui--invoke :back)))
(defun zfs-ui-refresh ()
(interactive)
(when zfs-ui--refresh-fn
(funcall zfs-ui--refresh-fn)))
(defun zfs-ui-snapshot ()
(interactive)
(zfs-ui--invoke :snapshot))
(defun zfs-ui-backup ()
(interactive)
(zfs-ui--invoke :backup))
(defun zfs-ui-create-child ()
(interactive)
(zfs-ui--invoke :create-child))
(defun zfs-ui-copy ()
(interactive)
(zfs-ui--invoke :copy))
(defun zfs-ui-admin ()
(interactive)
(zfs-ui--invoke :admin))
(defun zfs-ui-diff ()
(interactive)
(zfs-ui--invoke :diff))
(defun zfs-ui-rollback ()
(interactive)
(zfs-ui--invoke :rollback))
(defun zfs-ui-mount ()
(interactive)
(zfs-ui--invoke :mount))
(defun zfs-ui-unlock ()
(interactive)
(zfs-ui--invoke :unlock))
(defun zfs-ui-import ()
(interactive)
(zfs-ui--invoke :import))
(defun zfs-ui-targets ()
(interactive)
(zfs-ui--invoke :targets))
(defun zfs-ui-format ()
(interactive)
(zfs-ui--invoke :format))
(defun zfs-ui-export ()
(interactive)
(zfs-ui--invoke :export))
(defun zfs-ui-scrub-start ()
(interactive)
(zfs-ui--invoke :scrub-start))
(defun zfs-ui-scrub-stop ()
(interactive)
(zfs-ui--invoke :scrub-stop))
(define-key zfs-mode-map (kbd "RET") #'zfs-ui-open)
(define-key zfs-mode-map (kbd "<return>") #'zfs-ui-open)
(define-key zfs-mode-map (kbd "l") #'zfs-ui-open)
(define-key zfs-mode-map (kbd "q") #'zfs-ui-back)
(define-key zfs-mode-map (kbd "h") #'zfs-ui-back)
(define-key zfs-mode-map (kbd "<left>") #'zfs-ui-back)
(define-key zfs-mode-map (kbd "DEL") #'zfs-ui-back)
(define-key zfs-mode-map (kbd "<backspace>") #'zfs-ui-back)
(define-key zfs-mode-map (kbd "g") #'zfs-ui-refresh)
(define-key zfs-mode-map (kbd "r") #'zfs-ui-refresh)
(define-key zfs-mode-map (kbd "s") #'zfs-ui-snapshot)
(define-key zfs-mode-map (kbd "b") #'zfs-ui-backup)
(define-key zfs-mode-map (kbd "c") #'zfs-ui-create-child)
(define-key zfs-mode-map (kbd "C") #'zfs-ui-copy)
(define-key zfs-mode-map (kbd "A") #'zfs-ui-admin)
(define-key zfs-mode-map (kbd "d") #'zfs-ui-diff)
(define-key zfs-mode-map (kbd "R") #'zfs-ui-rollback)
(define-key zfs-mode-map (kbd "m") #'zfs-ui-mount)
(define-key zfs-mode-map (kbd "u") #'zfs-ui-unlock)
(define-key zfs-mode-map (kbd "i") #'zfs-ui-import)
(define-key zfs-mode-map (kbd "T") #'zfs-ui-targets)
(define-key zfs-mode-map (kbd "F") #'zfs-ui-format)
(define-key zfs-mode-map (kbd "x") #'zfs-ui-export)
(define-key zfs-mode-map (kbd "1") #'zfs-ui-scrub-start)
(define-key zfs-mode-map (kbd "2") #'zfs-ui-scrub-stop)
(with-eval-after-load 'evil
(evil-set-initial-state 'zfs-mode 'normal)
(evil-define-key* '(normal motion) zfs-mode-map
(kbd "RET") #'zfs-ui-open
(kbd "<return>") #'zfs-ui-open
(kbd "l") #'zfs-ui-open
(kbd "q") #'zfs-ui-back
(kbd "h") #'zfs-ui-back
(kbd "<left>") #'zfs-ui-back
(kbd "DEL") #'zfs-ui-back
(kbd "<backspace>") #'zfs-ui-back
(kbd "g") #'zfs-ui-refresh
(kbd "r") #'zfs-ui-refresh
(kbd "s") #'zfs-ui-snapshot
(kbd "b") #'zfs-ui-backup
(kbd "c") #'zfs-ui-create-child
(kbd "C") #'zfs-ui-copy
(kbd "A") #'zfs-ui-admin
(kbd "d") #'zfs-ui-diff
(kbd "R") #'zfs-ui-rollback
(kbd "m") #'zfs-ui-mount
(kbd "u") #'zfs-ui-unlock
(kbd "i") #'zfs-ui-import
(kbd "T") #'zfs-ui-targets
(kbd "F") #'zfs-ui-format
(kbd "x") #'zfs-ui-export
(kbd "1") #'zfs-ui-scrub-start
(kbd "2") #'zfs-ui-scrub-stop))
;;;###autoload
(defun zfs ()
(interactive)
(let ((buffer (get-buffer-create zfs-buffer-name)))
(with-current-buffer buffer
(unless (eq major-mode 'zfs-mode)
(zfs-mode)
(vui-mount (vui-component 'zfs-app) buffer)
(vui-rerender-on-resize)))
(pop-to-buffer buffer)))
(provide 'zfs)
;;; zfs.el ends here
|