-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud_sync_tuner.adb
More file actions
1619 lines (1475 loc) · 67.5 KB
/
cloud_sync_tuner.adb
File metadata and controls
1619 lines (1475 loc) · 67.5 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
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
-- SPDX-License-Identifier: AGPL-3.0-or-later
-- Cloud Sync Tuner - Ada TUI for rclone mount configuration
-- Manages VFS cache modes, smart sync, and pinned folders
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Fixed;
with Ada.Command_Line;
with Ada.Directories;
with GNAT.OS_Lib;
procedure Cloud_Sync_Tuner is
-- Version
Version : constant String := "1.0.0";
-- ANSI color codes for TUI
Reset : constant String := ASCII.ESC & "[0m";
Bold : constant String := ASCII.ESC & "[1m";
Dim : constant String := ASCII.ESC & "[2m";
Underline : constant String := ASCII.ESC & "[4m";
Green : constant String := ASCII.ESC & "[32m";
Yellow : constant String := ASCII.ESC & "[33m";
Blue : constant String := ASCII.ESC & "[34m";
Cyan : constant String := ASCII.ESC & "[36m";
Red : constant String := ASCII.ESC & "[31m";
White : constant String := ASCII.ESC & "[37m";
BG_Blue : constant String := ASCII.ESC & "[44m";
-- Cache mode enumeration
type Cache_Mode is (Off, Minimal, Writes, Full);
-- Cloud service record
type Cloud_Service is record
Name : Unbounded_String;
Remote : Unbounded_String;
Mount_Point : Unbounded_String;
Service_Name : Unbounded_String;
Enabled : Boolean;
end record;
-- Array of cloud services
type Service_Array is array (Positive range <>) of Cloud_Service;
-- Pinned folder record
type Pinned_Folder is record
Remote_Path : Unbounded_String;
Local_Path : Unbounded_String;
Service_Idx : Positive;
Enabled : Boolean;
end record;
type Pinned_Array is array (1 .. 20) of Pinned_Folder;
-- Conflict resolution strategy
type Conflict_Strategy is (None, Newer, Older, Larger, Smaller, Path1, Path2);
-- Bandwidth schedule entry
type Schedule_Entry is record
Start_Hour : Natural := 0;
End_Hour : Natural := 24;
BW_Limit : Natural := 0; -- 0 = unlimited, else KB/s
end record;
type Schedule_Array is array (1 .. 4) of Schedule_Entry;
-- Rate limit and smart sync settings
type Smart_Sync_Config is record
-- Rate limiting
TPS_Limit : Positive := 4;
TPS_Burst : Positive := 1;
Chunk_Size_MB : Positive := 32;
Cache_Age_Hours : Positive := 72;
Dir_Cache_Min : Positive := 5;
-- Smart sync (like native clients)
Cache_Max_Size_GB : Natural := 10; -- 0 = unlimited
Min_Free_Space_GB : Natural := 5; -- Auto-evict if disk low
Write_Back_Sec : Positive := 5; -- Buffer writes before upload
Poll_Interval_Min : Positive := 1; -- Check remote changes
-- Advanced
Buffer_Size_MB : Positive := 16;
Transfers : Positive := 4;
Checkers : Positive := 8;
-- Conflict resolution (bisync)
Conflict_Resolve : Conflict_Strategy := Newer;
-- Bandwidth scheduling
BW_Schedule : Schedule_Array := (
1 => (Start_Hour => 9, End_Hour => 17, BW_Limit => 5000), -- Work hours: 5MB/s
2 => (Start_Hour => 17, End_Hour => 9, BW_Limit => 0), -- Off hours: unlimited
others => (Start_Hour => 0, End_Hour => 0, BW_Limit => 0)
);
BW_Schedule_Enabled : Boolean := False;
-- Desktop integration
Desktop_Notify : Boolean := True;
Tray_Icon : Boolean := True;
end record;
-- Configuration for cloud services
Services : constant Service_Array (1 .. 3) := (
1 => (To_Unbounded_String ("Dropbox"),
To_Unbounded_String ("dropbox:"),
To_Unbounded_String ("/var/home/hyper/Cloud/Dropbox"),
To_Unbounded_String ("rclone-dropbox"),
True),
2 => (To_Unbounded_String ("Google Drive"),
To_Unbounded_String ("gdrive:"),
To_Unbounded_String ("/var/home/hyper/Cloud/GoogleDrive"),
To_Unbounded_String ("rclone-gdrive"),
True),
3 => (To_Unbounded_String ("OneDrive"),
To_Unbounded_String ("onedrive:"),
To_Unbounded_String ("/var/home/hyper/Cloud/OneDrive"),
To_Unbounded_String ("rclone-onedrive"),
True)
);
-- Current selection (default to Option 2: Writes mode)
Current_Mode : Cache_Mode := Writes;
Smart_Config : Smart_Sync_Config;
Pinned_Folders : Pinned_Array;
Pinned_Count : Natural := 0;
Offline_Dir : constant String := "/home/hyper/Offline";
-- Help context tracking
type Help_Context is (None, Mode_Select, Service_Select, Rate_Config_Help,
Smart_Sync_Help, Pinned_Help, Apply_Help);
Current_Help : Help_Context := None;
procedure Clear_Screen is
begin
Put (ASCII.ESC & "[2J" & ASCII.ESC & "[H");
end Clear_Screen;
procedure Print_Header is
begin
Put_Line (BG_Blue & Bold & White);
Put_Line (" ╔═══════════════════════════════════════════════════════════════╗ ");
Put_Line (" ║ CLOUD SYNC TUNER - rclone Configuration ║ ");
Put_Line (" ║ v" & Version & " ║ ");
Put_Line (" ╚═══════════════════════════════════════════════════════════════╝ ");
Put_Line (Reset);
New_Line;
end Print_Header;
procedure Print_Context_Help is
begin
Put_Line (Dim & "───────────────────────────────────────────────────────────────" & Reset);
case Current_Help is
when None =>
Put_Line (Cyan & " [?]" & Reset & " Press ? for context-sensitive help");
when Mode_Select =>
Put_Line (Bold & " Cache Mode Help:" & Reset);
Put_Line (" " & Green & "writes" & Reset & " is recommended for Dropbox (prevents rate limiting)");
Put_Line (" " & Yellow & "full" & Reset & " caches aggressively but may trigger API limits");
Put_Line (" " & Dim & "off" & Reset & " uses no cache - slowest but always current");
Put_Line (" Press " & Cyan & "[1-4]" & Reset & " to select, " & Cyan & "[?]" & Reset & " to hide help");
when Service_Select =>
Put_Line (Bold & " Service Configuration Help:" & Reset);
Put_Line (" Services are defined in config/config.toml");
Put_Line (" Edit mount points and remote names there");
Put_Line (" " & Green & "●" & Reset & " = enabled, " & Dim & "○" & Reset & " = disabled");
when Rate_Config_Help =>
Put_Line (Bold & " Rate Limiting Help:" & Reset);
Put_Line (" " & Cyan & "TPS Limit" & Reset & ": Max API calls per second (Dropbox: 4)");
Put_Line (" " & Cyan & "TPS Burst" & Reset & ": Extra calls allowed in burst (keep at 1)");
Put_Line (" " & Cyan & "Chunk Size" & Reset & ": Read size per request (32MB optimal)");
Put_Line (" " & Cyan & "Cache Age" & Reset & ": How long to keep cached data (72h default)");
when Smart_Sync_Help =>
Put_Line (Bold & " Smart Sync Help (like Dropbox/OneDrive native):" & Reset);
Put_Line (" " & Cyan & "Cache Max Size" & Reset & ": Auto-evict old files when exceeded");
Put_Line (" " & Cyan & "Min Free Space" & Reset & ": Emergency eviction threshold");
Put_Line (" " & Cyan & "Write-Back" & Reset & ": Buffer writes before uploading");
Put_Line (" " & Cyan & "Poll Interval" & Reset & ": How often to check for remote changes");
when Pinned_Help =>
Put_Line (Bold & " Pinned Folders Help (Offline Access):" & Reset);
Put_Line (" Pin folders to always keep them downloaded locally");
Put_Line (" Like 'Make Available Offline' in native clients");
Put_Line (" Syncs to " & Offline_Dir & "/");
Put_Line (" Creates systemd timer for automatic updates");
when Apply_Help =>
Put_Line (Bold & " Apply Help:" & Reset);
Put_Line (" Generated files go to /tmp/cloud-sync-tuner/output/");
Put_Line (" Copy to ~/.config/systemd/user/ and reload daemon");
Put_Line (" Use --apply to auto-copy and reload (requires sudo for some ops)");
end case;
Put_Line (Dim & "───────────────────────────────────────────────────────────────" & Reset);
end Print_Context_Help;
procedure Print_Mode_Info (Mode : Cache_Mode; Is_Selected : Boolean) is
Prefix : constant String := (if Is_Selected then Green & "► " else " ");
Suffix : constant String := (if Is_Selected then " ◄" & Reset else Reset);
Default_Tag : constant String := (if Mode = Writes then Yellow & " [DEFAULT]" & Reset else "");
Checkbox : constant String := (if Is_Selected then "[×]" else "[ ]");
begin
case Mode is
when Off =>
Put_Line (Prefix & Checkbox & Bold & " 1. Off Mode" & Suffix & Default_Tag);
Put_Line (Dim & " No caching - direct cloud access" & Reset);
Put_Line (" API: " & Red & "Very High" & Reset & " Disk: None");
when Minimal =>
Put_Line (Prefix & Checkbox & Bold & " 2. Minimal Mode" & Suffix & Default_Tag);
Put_Line (Dim & " Caches open files only" & Reset);
Put_Line (" API: " & Yellow & "High" & Reset & " Disk: Low");
when Writes =>
Put_Line (Prefix & Checkbox & Bold & " 3. Writes Mode" & Suffix & Default_Tag);
Put_Line (Dim & " Caches writes, streams reads" & Reset);
Put_Line (" API: " & Green & "Low" & Reset & " Disk: Medium");
when Full =>
Put_Line (Prefix & Checkbox & Bold & " 4. Full Mode" & Suffix & Default_Tag);
Put_Line (Dim & " Full local caching" & Reset);
Put_Line (" API: " & Red & "Very High" & Reset & " Disk: High");
end case;
New_Line;
end Print_Mode_Info;
procedure Print_Services is
begin
Put_Line (Bold & "Connected Cloud Services:" & Reset);
for I in Services'Range loop
Put (" ");
if Services (I).Enabled then
Put (Green & "●" & Reset);
else
Put (Dim & "○" & Reset);
end if;
Put (" " & To_String (Services (I).Name));
Put_Line (Dim & " → " & To_String (Services (I).Mount_Point) & Reset);
end loop;
New_Line;
end Print_Services;
procedure Print_Rate_Limit_Settings is
begin
Put_Line (Bold & "Rate Limiting:" & Reset);
Put (" TPS:" & Integer'Image (Smart_Config.TPS_Limit));
Put (" Burst:" & Integer'Image (Smart_Config.TPS_Burst));
Put (" Chunk:" & Integer'Image (Smart_Config.Chunk_Size_MB) & "M");
Put_Line (" Age:" & Integer'Image (Smart_Config.Cache_Age_Hours) & "h");
end Print_Rate_Limit_Settings;
procedure Print_Smart_Sync_Settings is
begin
Put_Line (Bold & "Smart Sync:" & Reset);
Put (" Cache:");
if Smart_Config.Cache_Max_Size_GB = 0 then
Put (" unlimited");
else
Put (Integer'Image (Smart_Config.Cache_Max_Size_GB) & "G");
end if;
Put (" MinFree:" & Integer'Image (Smart_Config.Min_Free_Space_GB) & "G");
Put (" WriteBack:" & Integer'Image (Smart_Config.Write_Back_Sec) & "s");
Put_Line (" Poll:" & Integer'Image (Smart_Config.Poll_Interval_Min) & "m");
end Print_Smart_Sync_Settings;
procedure Print_Pinned_Folders is
begin
Put_Line (Bold & "Pinned Folders (" & Dim & "offline access" & Reset & Bold & "):" & Reset);
if Pinned_Count = 0 then
Put_Line (Dim & " (none - press P to add)" & Reset);
else
for I in 1 .. Pinned_Count loop
Put (" ");
if Pinned_Folders (I).Enabled then
Put (Green & "●" & Reset);
else
Put (Dim & "○" & Reset);
end if;
Put (" " & To_String (Services (Pinned_Folders (I).Service_Idx).Name) & ":");
Put (To_String (Pinned_Folders (I).Remote_Path));
Put_Line (Dim & " → " & To_String (Pinned_Folders (I).Local_Path) & Reset);
end loop;
end if;
New_Line;
end Print_Pinned_Folders;
function Mode_To_String (Mode : Cache_Mode) return String is
begin
case Mode is
when Off => return "off";
when Minimal => return "minimal";
when Writes => return "writes";
when Full => return "full";
end case;
end Mode_To_String;
function Conflict_To_String (C : Conflict_Strategy) return String is
begin
case C is
when None => return "none";
when Newer => return "newer";
when Older => return "older";
when Larger => return "larger";
when Smaller => return "smaller";
when Path1 => return "path1";
when Path2 => return "path2";
end case;
end Conflict_To_String;
function BW_To_String (BW : Natural) return String is
begin
if BW = 0 then
return "unlimited";
elsif BW >= 1000 then
return Ada.Strings.Fixed.Trim (Natural'Image (BW / 1000), Ada.Strings.Left) & " MB/s";
else
return Ada.Strings.Fixed.Trim (Natural'Image (BW), Ada.Strings.Left) & " KB/s";
end if;
end BW_To_String;
function Validate_Mode (S : String) return Boolean is
begin
return S = "off" or S = "minimal" or S = "writes" or S = "full";
end Validate_Mode;
function Validate_TPS (V : Integer) return Boolean is
begin
return V >= 1 and V <= 100;
end Validate_TPS;
function Validate_Chunk_Size (V : Integer) return Boolean is
begin
return V >= 1 and V <= 512;
end Validate_Chunk_Size;
function Validate_Size_GB (V : Integer) return Boolean is
begin
return V >= 0 and V <= 1000;
end Validate_Size_GB;
procedure Print_Validation_Error (Field : String; Value : String; Valid_Range : String) is
begin
Put_Line (Red & "Error: " & Reset & "Invalid value for " & Bold & Field & Reset);
Put_Line (" Got: " & Value);
Put_Line (" Valid: " & Valid_Range);
end Print_Validation_Error;
procedure Generate_Service_File (Service : Cloud_Service; Mode : Cache_Mode) is
File_Name : constant String :=
"/tmp/cloud-sync-tuner/output/" & To_String (Service.Service_Name) & ".service";
File : File_Type;
Mode_Str : constant String := Mode_To_String (Mode);
begin
Create (File, Out_File, File_Name);
Put_Line (File, "# SPDX-License-Identifier: AGPL-3.0-or-later");
Put_Line (File, "# Generated by Cloud Sync Tuner v" & Version);
Put_Line (File, "# Cache Mode: " & Mode_Str);
Put_Line (File, "[Unit]");
Put_Line (File, "Description=" & To_String (Service.Name) & " FUSE Mount");
Put_Line (File, "After=network-online.target");
Put_Line (File, "Wants=network-online.target");
Put_Line (File, "");
Put_Line (File, "[Service]");
Put_Line (File, "Type=notify");
Put_Line (File, "ExecStart=/home/hyper/.local/bin/rclone mount " &
To_String (Service.Remote) & " " &
To_String (Service.Mount_Point) & " \");
Put_Line (File, " --vfs-cache-mode " & Mode_Str & " \");
-- Smart sync settings
if Smart_Config.Cache_Max_Size_GB > 0 then
Put_Line (File, " --vfs-cache-max-size " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.Cache_Max_Size_GB),
Ada.Strings.Left) & "G \");
end if;
Put_Line (File, " --vfs-cache-min-free-space " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.Min_Free_Space_GB),
Ada.Strings.Left) & "G \");
Put_Line (File, " --vfs-write-back " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.Write_Back_Sec),
Ada.Strings.Left) & "s \");
-- Rate limiting
Put_Line (File, " --tpslimit " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.TPS_Limit),
Ada.Strings.Left) & " \");
Put_Line (File, " --tpslimit-burst " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.TPS_Burst),
Ada.Strings.Left) & " \");
Put_Line (File, " --vfs-read-chunk-size " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.Chunk_Size_MB),
Ada.Strings.Left) & "M \");
Put_Line (File, " --vfs-cache-max-age " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.Cache_Age_Hours),
Ada.Strings.Left) & "h \");
Put_Line (File, " --dir-cache-time " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.Dir_Cache_Min),
Ada.Strings.Left) & "m \");
Put_Line (File, " --poll-interval " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.Poll_Interval_Min),
Ada.Strings.Left) & "m \");
-- Performance tuning
Put_Line (File, " --buffer-size " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.Buffer_Size_MB),
Ada.Strings.Left) & "M \");
Put_Line (File, " --transfers " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.Transfers),
Ada.Strings.Left) & " \");
Put_Line (File, " --checkers " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.Checkers),
Ada.Strings.Left));
Put_Line (File, "ExecStop=/bin/fusermount -uz " & To_String (Service.Mount_Point));
Put_Line (File, "Restart=on-failure");
Put_Line (File, "RestartSec=5");
Put_Line (File, "");
Put_Line (File, "[Install]");
Put_Line (File, "WantedBy=default.target");
Close (File);
end Generate_Service_File;
procedure Generate_Offline_Sync_Service is
Service_File : constant String :=
"/tmp/cloud-sync-tuner/output/rclone-offline-sync.service";
Timer_File : constant String :=
"/tmp/cloud-sync-tuner/output/rclone-offline-sync.timer";
File : File_Type;
begin
if Pinned_Count = 0 then
return;
end if;
-- Generate service file
Create (File, Out_File, Service_File);
Put_Line (File, "# SPDX-License-Identifier: AGPL-3.0-or-later");
Put_Line (File, "# Generated by Cloud Sync Tuner v" & Version);
Put_Line (File, "# Syncs pinned folders for offline access");
Put_Line (File, "[Unit]");
Put_Line (File, "Description=Sync Pinned Cloud Folders for Offline Access");
Put_Line (File, "After=network-online.target");
Put_Line (File, "Wants=network-online.target");
Put_Line (File, "");
Put_Line (File, "[Service]");
Put_Line (File, "Type=oneshot");
for I in 1 .. Pinned_Count loop
if Pinned_Folders (I).Enabled then
Put_Line (File, "ExecStart=/home/hyper/.local/bin/rclone sync " &
To_String (Services (Pinned_Folders (I).Service_Idx).Remote) &
To_String (Pinned_Folders (I).Remote_Path) & " " &
To_String (Pinned_Folders (I).Local_Path) & " \");
Put_Line (File, " --tpslimit " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.TPS_Limit),
Ada.Strings.Left) & " \");
Put_Line (File, " --transfers " &
Ada.Strings.Fixed.Trim (Integer'Image (Smart_Config.Transfers),
Ada.Strings.Left) & " \");
Put_Line (File, " --progress");
end if;
end loop;
Put_Line (File, "");
Put_Line (File, "[Install]");
Put_Line (File, "WantedBy=default.target");
Close (File);
-- Generate timer file
Create (File, Out_File, Timer_File);
Put_Line (File, "# SPDX-License-Identifier: AGPL-3.0-or-later");
Put_Line (File, "# Generated by Cloud Sync Tuner v" & Version);
Put_Line (File, "[Unit]");
Put_Line (File, "Description=Timer for Offline Cloud Folder Sync");
Put_Line (File, "");
Put_Line (File, "[Timer]");
Put_Line (File, "OnBootSec=5min");
Put_Line (File, "OnUnitActiveSec=30min");
Put_Line (File, "Persistent=true");
Put_Line (File, "");
Put_Line (File, "[Install]");
Put_Line (File, "WantedBy=timers.target");
Close (File);
end Generate_Offline_Sync_Service;
procedure Apply_Configuration (Mode : Cache_Mode) is
begin
-- Create output directory
Ada.Directories.Create_Path ("/tmp/cloud-sync-tuner/output");
Put_Line (Bold & "Generating service files for " & Mode_To_String (Mode) & " mode..." & Reset);
New_Line;
for I in Services'Range loop
if Services (I).Enabled then
Generate_Service_File (Services (I), Mode);
Put_Line (Green & " ✓ " & Reset & To_String (Services (I).Service_Name) & ".service");
end if;
end loop;
-- Generate offline sync if we have pinned folders
if Pinned_Count > 0 then
Generate_Offline_Sync_Service;
Put_Line (Green & " ✓ " & Reset & "rclone-offline-sync.service");
Put_Line (Green & " ✓ " & Reset & "rclone-offline-sync.timer");
end if;
New_Line;
Put_Line (Bold & "Service files generated in: " & Reset & "/tmp/cloud-sync-tuner/output/");
New_Line;
Put_Line ("To apply, run:");
Put_Line (Cyan & " cp /tmp/cloud-sync-tuner/output/*.service ~/.config/systemd/user/" & Reset);
Put_Line (Cyan & " cp /tmp/cloud-sync-tuner/output/*.timer ~/.config/systemd/user/" & Reset);
Put_Line (Cyan & " systemctl --user daemon-reload" & Reset);
Put_Line (Cyan & " systemctl --user restart rclone-dropbox rclone-gdrive rclone-onedrive" & Reset);
if Pinned_Count > 0 then
Put_Line (Cyan & " systemctl --user enable --now rclone-offline-sync.timer" & Reset);
end if;
New_Line;
end Apply_Configuration;
procedure Print_Bandwidth_Settings is
begin
Put_Line (Bold & "Bandwidth Schedule:" & Reset);
if not Smart_Config.BW_Schedule_Enabled then
Put_Line (Dim & " (disabled - press B to configure)" & Reset);
else
for I in 1 .. 2 loop
if Smart_Config.BW_Schedule (I).Start_Hour /= Smart_Config.BW_Schedule (I).End_Hour then
Put (" " & Natural'Image (Smart_Config.BW_Schedule (I).Start_Hour) & ":00-");
Put (Natural'Image (Smart_Config.BW_Schedule (I).End_Hour) & ":00 → ");
Put_Line (BW_To_String (Smart_Config.BW_Schedule (I).BW_Limit));
end if;
end loop;
end if;
end Print_Bandwidth_Settings;
procedure Print_Conflict_Settings is
begin
Put ("Conflict: " & Cyan & Conflict_To_String (Smart_Config.Conflict_Resolve) & Reset);
Put_Line (" Notify: " & (if Smart_Config.Desktop_Notify then Green & "on" else Dim & "off") & Reset);
end Print_Conflict_Settings;
procedure Print_Menu is
begin
Put_Line (Bold & "═══════════════════════════════════════════════════════════════" & Reset);
Put_Line (" " & Cyan & "[1-4]" & Reset & " Mode " &
Cyan & "[R]" & Reset & "ate " &
Cyan & "[S]" & Reset & "mart " &
Cyan & "[B]" & Reset & "andwidth " &
Cyan & "[P]" & Reset & "in " &
Cyan & "[A]" & Reset & "pply");
Put_Line (" " & Cyan & "[C]" & Reset & "onflict " &
Cyan & "[N]" & Reset & "otify " &
Cyan & "[?]" & Reset & " Help " &
Cyan & "[Q]" & Reset & "uit");
Put_Line (Bold & "═══════════════════════════════════════════════════════════════" & Reset);
end Print_Menu;
procedure Edit_Bandwidth_Schedule is
Choice : Character;
Input_Line : String (1 .. 10);
Last : Natural;
Value : Integer;
begin
loop
Clear_Screen;
Print_Header;
Put_Line (Bold & "Bandwidth Schedule (like native clients):" & Reset);
Put_Line (Dim & "Limit bandwidth during work hours to avoid saturating connection" & Reset);
New_Line;
Put (" [E] Scheduling: ");
if Smart_Config.BW_Schedule_Enabled then
Put_Line (Green & "ENABLED" & Reset);
else
Put_Line (Dim & "DISABLED" & Reset);
end if;
New_Line;
Put_Line (" Current Schedule:");
for I in 1 .. 2 loop
Put (" [" & Character'Val (Character'Pos ('0') + I) & "] ");
Put (Natural'Image (Smart_Config.BW_Schedule (I).Start_Hour) & ":00 - ");
Put (Natural'Image (Smart_Config.BW_Schedule (I).End_Hour) & ":00 → ");
Put_Line (BW_To_String (Smart_Config.BW_Schedule (I).BW_Limit));
end loop;
New_Line;
Put_Line (" [W] Work hours preset (9-17: 5MB/s, else unlimited)");
Put_Line (" [N] Night sync preset (22-06: unlimited, else 2MB/s)");
Put_Line (" [U] Unlimited always");
Put_Line (" [B] Back to main menu");
New_Line;
Put (" Selection: ");
Get_Immediate (Choice);
New_Line;
case Choice is
when 'e' | 'E' =>
Smart_Config.BW_Schedule_Enabled := not Smart_Config.BW_Schedule_Enabled;
when '1' | '2' =>
declare
Idx : constant Positive := Character'Pos (Choice) - Character'Pos ('0');
begin
Put (" Start hour (0-23): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Value >= 0 and Value <= 23 then
Smart_Config.BW_Schedule (Idx).Start_Hour := Value;
end if;
exception
when others => null;
end;
Put (" End hour (0-24): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Value >= 0 and Value <= 24 then
Smart_Config.BW_Schedule (Idx).End_Hour := Value;
end if;
exception
when others => null;
end;
Put (" Bandwidth KB/s (0=unlimited): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Value >= 0 then
Smart_Config.BW_Schedule (Idx).BW_Limit := Value;
end if;
exception
when others => null;
end;
end;
when 'w' | 'W' =>
Smart_Config.BW_Schedule (1) := (Start_Hour => 9, End_Hour => 17, BW_Limit => 5000);
Smart_Config.BW_Schedule (2) := (Start_Hour => 17, End_Hour => 9, BW_Limit => 0);
Smart_Config.BW_Schedule_Enabled := True;
Put_Line (Green & " Applied work hours preset" & Reset);
delay 1.0;
when 'n' | 'N' =>
Smart_Config.BW_Schedule (1) := (Start_Hour => 22, End_Hour => 6, BW_Limit => 0);
Smart_Config.BW_Schedule (2) := (Start_Hour => 6, End_Hour => 22, BW_Limit => 2000);
Smart_Config.BW_Schedule_Enabled := True;
Put_Line (Green & " Applied night sync preset" & Reset);
delay 1.0;
when 'u' | 'U' =>
Smart_Config.BW_Schedule (1) := (Start_Hour => 0, End_Hour => 24, BW_Limit => 0);
Smart_Config.BW_Schedule (2) := (Start_Hour => 0, End_Hour => 0, BW_Limit => 0);
Smart_Config.BW_Schedule_Enabled := False;
Put_Line (Green & " Unlimited bandwidth" & Reset);
delay 1.0;
when 'b' | 'B' =>
exit;
when others =>
null;
end case;
end loop;
end Edit_Bandwidth_Schedule;
procedure Edit_Conflict_Resolution is
Choice : Character;
begin
loop
Clear_Screen;
Print_Header;
Put_Line (Bold & "Conflict Resolution (for bisync):" & Reset);
Put_Line (Dim & "When files change on both sides, how should we resolve?" & Reset);
New_Line;
Put_Line (" Current: " & Cyan & Conflict_To_String (Smart_Config.Conflict_Resolve) & Reset);
New_Line;
Put_Line (" [1] " & (if Smart_Config.Conflict_Resolve = None then "[×]" else "[ ]") &
" None - keep both versions with conflict suffix");
Put_Line (" [2] " & (if Smart_Config.Conflict_Resolve = Newer then "[×]" else "[ ]") &
" Newer - prefer more recently modified file");
Put_Line (" [3] " & (if Smart_Config.Conflict_Resolve = Older then "[×]" else "[ ]") &
" Older - prefer older file (conservative)");
Put_Line (" [4] " & (if Smart_Config.Conflict_Resolve = Larger then "[×]" else "[ ]") &
" Larger - prefer larger file");
Put_Line (" [5] " & (if Smart_Config.Conflict_Resolve = Smaller then "[×]" else "[ ]") &
" Smaller - prefer smaller file");
Put_Line (" [6] " & (if Smart_Config.Conflict_Resolve = Path1 then "[×]" else "[ ]") &
" Path1 - always prefer local");
Put_Line (" [7] " & (if Smart_Config.Conflict_Resolve = Path2 then "[×]" else "[ ]") &
" Path2 - always prefer remote");
New_Line;
Put_Line (" [B] Back to main menu");
New_Line;
Put (" Selection: ");
Get_Immediate (Choice);
New_Line;
case Choice is
when '1' => Smart_Config.Conflict_Resolve := None;
when '2' => Smart_Config.Conflict_Resolve := Newer;
when '3' => Smart_Config.Conflict_Resolve := Older;
when '4' => Smart_Config.Conflict_Resolve := Larger;
when '5' => Smart_Config.Conflict_Resolve := Smaller;
when '6' => Smart_Config.Conflict_Resolve := Path1;
when '7' => Smart_Config.Conflict_Resolve := Path2;
when 'b' | 'B' => exit;
when others => null;
end case;
end loop;
end Edit_Conflict_Resolution;
procedure Edit_Rate_Config is
Choice : Character;
Input_Line : String (1 .. 10);
Last : Natural;
Value : Integer;
begin
loop
Clear_Screen;
Print_Header;
Put_Line (Bold & "Rate Limit Configuration:" & Reset);
New_Line;
Put_Line (" [1] TPS Limit: " & Integer'Image (Smart_Config.TPS_Limit) &
Dim & " (1-100, Dropbox safe: 4)" & Reset);
Put_Line (" [2] TPS Burst: " & Integer'Image (Smart_Config.TPS_Burst) &
Dim & " (1-10, recommended: 1)" & Reset);
Put_Line (" [3] Chunk Size MB: " & Integer'Image (Smart_Config.Chunk_Size_MB) &
Dim & " (1-512, optimal: 32)" & Reset);
Put_Line (" [4] Cache Age hrs: " & Integer'Image (Smart_Config.Cache_Age_Hours) &
Dim & " (1-168, default: 72)" & Reset);
Put_Line (" [5] Dir Cache min: " & Integer'Image (Smart_Config.Dir_Cache_Min) &
Dim & " (1-60, default: 5)" & Reset);
New_Line;
Put_Line (" [D] Reset to Dropbox-safe defaults");
Put_Line (" [G] Google Drive optimized");
Put_Line (" [O] OneDrive optimized");
Put_Line (" [B] Back to main menu");
New_Line;
Put (" Selection: ");
Get_Immediate (Choice);
New_Line;
case Choice is
when '1' =>
Put (" Enter TPS Limit (1-100): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Validate_TPS (Value) then
Smart_Config.TPS_Limit := Value;
else
Print_Validation_Error ("TPS Limit", Input_Line (1 .. Last), "1-100");
delay 2.0;
end if;
exception
when others =>
Print_Validation_Error ("TPS Limit", Input_Line (1 .. Last), "integer 1-100");
delay 2.0;
end;
when '2' =>
Put (" Enter TPS Burst (1-10): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Value >= 1 and Value <= 10 then
Smart_Config.TPS_Burst := Value;
else
Print_Validation_Error ("TPS Burst", Input_Line (1 .. Last), "1-10");
delay 2.0;
end if;
exception
when others =>
Print_Validation_Error ("TPS Burst", Input_Line (1 .. Last), "integer 1-10");
delay 2.0;
end;
when '3' =>
Put (" Enter Chunk Size MB (1-512): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Validate_Chunk_Size (Value) then
Smart_Config.Chunk_Size_MB := Value;
else
Print_Validation_Error ("Chunk Size", Input_Line (1 .. Last), "1-512 MB");
delay 2.0;
end if;
exception
when others =>
Print_Validation_Error ("Chunk Size", Input_Line (1 .. Last), "integer 1-512");
delay 2.0;
end;
when '4' =>
Put (" Enter Cache Age hours (1-168): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Value >= 1 and Value <= 168 then
Smart_Config.Cache_Age_Hours := Value;
else
Print_Validation_Error ("Cache Age", Input_Line (1 .. Last), "1-168 hours");
delay 2.0;
end if;
exception
when others =>
Print_Validation_Error ("Cache Age", Input_Line (1 .. Last), "integer 1-168");
delay 2.0;
end;
when '5' =>
Put (" Enter Dir Cache minutes (1-60): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Value >= 1 and Value <= 60 then
Smart_Config.Dir_Cache_Min := Value;
else
Print_Validation_Error ("Dir Cache", Input_Line (1 .. Last), "1-60 minutes");
delay 2.0;
end if;
exception
when others =>
Print_Validation_Error ("Dir Cache", Input_Line (1 .. Last), "integer 1-60");
delay 2.0;
end;
when 'd' | 'D' =>
Smart_Config.TPS_Limit := 4;
Smart_Config.TPS_Burst := 1;
Smart_Config.Chunk_Size_MB := 32;
Put_Line (Green & " Applied Dropbox-safe rate limits" & Reset);
delay 1.0;
when 'g' | 'G' =>
Smart_Config.TPS_Limit := 4;
Smart_Config.TPS_Burst := 1;
Smart_Config.Chunk_Size_MB := 32;
Put_Line (Green & " Applied Google Drive optimized rate limits" & Reset);
delay 1.0;
when 'o' | 'O' =>
Smart_Config.TPS_Limit := 6;
Smart_Config.TPS_Burst := 2;
Smart_Config.Chunk_Size_MB := 32;
Put_Line (Green & " Applied OneDrive optimized rate limits" & Reset);
delay 1.0;
when 'b' | 'B' =>
exit;
when others =>
null;
end case;
end loop;
end Edit_Rate_Config;
procedure Edit_Smart_Sync is
Choice : Character;
Input_Line : String (1 .. 10);
Last : Natural;
Value : Integer;
begin
loop
Clear_Screen;
Print_Header;
Put_Line (Bold & "Smart Sync Configuration (like native clients):" & Reset);
New_Line;
Put (" [1] Cache Max Size: ");
if Smart_Config.Cache_Max_Size_GB = 0 then
Put_Line ("unlimited" & Dim & " (0-1000 GB, 0=unlimited)" & Reset);
else
Put_Line (Integer'Image (Smart_Config.Cache_Max_Size_GB) & " GB" &
Dim & " (0-1000 GB, 0=unlimited)" & Reset);
end if;
Put_Line (" [2] Min Free Space: " & Integer'Image (Smart_Config.Min_Free_Space_GB) & " GB" &
Dim & " (0-100 GB, auto-evict threshold)" & Reset);
Put_Line (" [3] Write-Back: " & Integer'Image (Smart_Config.Write_Back_Sec) & " sec" &
Dim & " (1-60 sec, buffer before upload)" & Reset);
Put_Line (" [4] Poll Interval: " & Integer'Image (Smart_Config.Poll_Interval_Min) & " min" &
Dim & " (1-60 min, remote change check)" & Reset);
New_Line;
Put_Line (Bold & "Performance Tuning:" & Reset);
Put_Line (" [5] Buffer Size: " & Integer'Image (Smart_Config.Buffer_Size_MB) & " MB" &
Dim & " (1-128 MB)" & Reset);
Put_Line (" [6] Transfers: " & Integer'Image (Smart_Config.Transfers) &
Dim & " (1-32 concurrent)" & Reset);
Put_Line (" [7] Checkers: " & Integer'Image (Smart_Config.Checkers) &
Dim & " (1-64 concurrent)" & Reset);
New_Line;
Put_Line (" [D] Reset to defaults");
Put_Line (" [B] Back to main menu");
New_Line;
Put (" Selection: ");
Get_Immediate (Choice);
New_Line;
case Choice is
when '1' =>
Put (" Enter Cache Max Size GB (0=unlimited, 1-1000): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Validate_Size_GB (Value) then
Smart_Config.Cache_Max_Size_GB := Value;
else
Print_Validation_Error ("Cache Max Size", Input_Line (1 .. Last), "0-1000 GB");
delay 2.0;
end if;
exception
when others =>
Print_Validation_Error ("Cache Max Size", Input_Line (1 .. Last), "integer 0-1000");
delay 2.0;
end;
when '2' =>
Put (" Enter Min Free Space GB (0-100): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Value >= 0 and Value <= 100 then
Smart_Config.Min_Free_Space_GB := Value;
else
Print_Validation_Error ("Min Free Space", Input_Line (1 .. Last), "0-100 GB");
delay 2.0;
end if;
exception
when others =>
Print_Validation_Error ("Min Free Space", Input_Line (1 .. Last), "integer 0-100");
delay 2.0;
end;
when '3' =>
Put (" Enter Write-Back seconds (1-60): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Value >= 1 and Value <= 60 then
Smart_Config.Write_Back_Sec := Value;
else
Print_Validation_Error ("Write-Back", Input_Line (1 .. Last), "1-60 seconds");
delay 2.0;
end if;
exception
when others =>
Print_Validation_Error ("Write-Back", Input_Line (1 .. Last), "integer 1-60");
delay 2.0;
end;
when '4' =>
Put (" Enter Poll Interval minutes (1-60): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Value >= 1 and Value <= 60 then
Smart_Config.Poll_Interval_Min := Value;
else
Print_Validation_Error ("Poll Interval", Input_Line (1 .. Last), "1-60 minutes");
delay 2.0;
end if;
exception
when others =>
Print_Validation_Error ("Poll Interval", Input_Line (1 .. Last), "integer 1-60");
delay 2.0;
end;
when '5' =>
Put (" Enter Buffer Size MB (1-128): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Value >= 1 and Value <= 128 then
Smart_Config.Buffer_Size_MB := Value;
else
Print_Validation_Error ("Buffer Size", Input_Line (1 .. Last), "1-128 MB");
delay 2.0;
end if;
exception
when others =>
Print_Validation_Error ("Buffer Size", Input_Line (1 .. Last), "integer 1-128");
delay 2.0;
end;
when '6' =>
Put (" Enter Transfers (1-32): ");
Get_Line (Input_Line, Last);
begin
Value := Integer'Value (Input_Line (1 .. Last));
if Value >= 1 and Value <= 32 then
Smart_Config.Transfers := Value;
else
Print_Validation_Error ("Transfers", Input_Line (1 .. Last), "1-32");