-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathmain.rs
More file actions
1178 lines (1097 loc) · 39.9 KB
/
main.rs
File metadata and controls
1178 lines (1097 loc) · 39.9 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
//! Command line arguments.
use std::{
collections::BTreeMap,
fmt::{Display, Formatter},
net::{SocketAddrV4, SocketAddrV6},
path::{Component, Path, PathBuf},
str::FromStr,
sync::{Arc, Mutex},
time::{Duration, Instant},
};
use anyhow::Context;
use clap::{
error::{ContextKind, ErrorKind},
CommandFactory, Parser, Subcommand,
};
use console::style;
use data_encoding::HEXLOWER;
use futures_buffered::BufferedStreamExt;
use indicatif::{
HumanBytes, HumanDuration, MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle,
};
use iroh::{
discovery::{dns::DnsDiscovery, pkarr::PkarrPublisher},
Endpoint, EndpointAddr, RelayMode, RelayUrl, SecretKey, TransportAddr,
};
use iroh_blobs::{
api::{
blobs::{
AddPathOptions, AddProgressItem, ExportMode, ExportOptions, ExportProgressItem,
ImportMode,
},
remote::GetProgressItem,
Store, TempTag,
},
format::collection::Collection,
get::{request::get_hash_seq_and_sizes, GetError, Stats},
provider::{
self,
events::{ConnectMode, EventMask, EventSender, ProviderMessage, RequestUpdate},
},
store::fs::FsStore,
ticket::BlobTicket,
BlobFormat, BlobsProtocol, Hash,
};
use n0_future::{task::AbortOnDropHandle, FuturesUnordered, StreamExt};
use rand::Rng;
use serde::{Deserialize, Serialize};
use tokio::{select, sync::mpsc};
use tracing::{error, trace};
use walkdir::WalkDir;
/// Send a file or directory between two machines, using blake3 verified streaming.
///
/// For all subcommands, you can specify a secret key using the IROH_SECRET
/// environment variable. If you don't, a random one will be generated.
///
/// You can also specify a port for the magicsocket. If you don't, a random one
/// will be chosen.
#[derive(Parser, Debug)]
#[command(version, about)]
pub struct Args {
#[clap(subcommand)]
pub command: Commands,
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Format {
#[default]
Hex,
Cid,
}
impl FromStr for Format {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"hex" => Ok(Format::Hex),
"cid" => Ok(Format::Cid),
_ => Err(anyhow::anyhow!("invalid format")),
}
}
}
impl Display for Format {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Format::Hex => write!(f, "hex"),
Format::Cid => write!(f, "cid"),
}
}
}
fn print_hash(hash: &Hash, format: Format) -> String {
match format {
Format::Hex => hash.to_hex().to_string(),
Format::Cid => hash.to_string(),
}
}
#[derive(Subcommand, Debug)]
pub enum Commands {
/// Send a file or directory.
Send(SendArgs),
/// Receive a file or directory.
#[clap(visible_alias = "recv")]
Receive(ReceiveArgs),
}
#[derive(Parser, Debug)]
pub struct CommonArgs {
/// The IPv4 address that magicsocket will listen on.
///
/// If None, defaults to a random free port, but it can be useful to specify a fixed
/// port, e.g. to configure a firewall rule.
#[clap(long, default_value = None)]
pub magic_ipv4_addr: Option<SocketAddrV4>,
/// The IPv6 address that magicsocket will listen on.
///
/// If None, defaults to a random free port, but it can be useful to specify a fixed
/// port, e.g. to configure a firewall rule.
#[clap(long, default_value = None)]
pub magic_ipv6_addr: Option<SocketAddrV6>,
#[clap(long, default_value_t = Format::Hex)]
pub format: Format,
#[clap(short = 'v', long, action = clap::ArgAction::Count)]
pub verbose: u8,
/// Suppress progress bars.
#[clap(long, default_value_t = false)]
pub no_progress: bool,
/// The relay URL to use as a home relay,
///
/// Can be set to "disabled" to disable relay servers and "default"
/// to configure default servers.
#[clap(long, default_value_t = RelayModeOption::Default)]
pub relay: RelayModeOption,
#[clap(long)]
pub show_secret: bool,
/// Number of parallel jobs to use while importing files.
///
/// Defaults to the number of logical CPU cores.
#[clap(short = 'j', long)]
pub jobs: Option<usize>,
}
/// Available command line options for configuring relays.
#[derive(Clone, Debug)]
pub enum RelayModeOption {
/// Disables relays altogether.
Disabled,
/// Uses the default relay servers.
Default,
/// Uses a single, custom relay server by URL.
Custom(RelayUrl),
}
impl FromStr for RelayModeOption {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"disabled" => Ok(Self::Disabled),
"default" => Ok(Self::Default),
_ => Ok(Self::Custom(RelayUrl::from_str(s)?)),
}
}
}
impl Display for RelayModeOption {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Disabled => f.write_str("disabled"),
Self::Default => f.write_str("default"),
Self::Custom(url) => url.fmt(f),
}
}
}
impl From<RelayModeOption> for RelayMode {
fn from(value: RelayModeOption) -> Self {
match value {
RelayModeOption::Disabled => RelayMode::Disabled,
RelayModeOption::Default => RelayMode::Default,
RelayModeOption::Custom(url) => RelayMode::Custom(url.into()),
}
}
}
#[derive(Parser, Debug)]
pub struct SendArgs {
/// Path to the file or directory to send.
///
/// The last component of the path will be used as the name of the data
/// being shared.
pub path: PathBuf,
/// What type of ticket to use.
///
/// Use "id" for the shortest type only including the endpoint ID,
/// "addresses" to only add IP addresses without a relay url,
/// "relay" to only add a relay address, and leave the option out
/// to use the biggest type of ticket that includes both relay and
/// address information.
///
/// Generally, the more information the higher the likelyhood of
/// a successful connection, but also the bigger a ticket to connect.
///
/// This is most useful for debugging which methods of connection
/// establishment work well.
#[clap(long, default_value_t = AddrInfoOptions::RelayAndAddresses)]
pub ticket_type: AddrInfoOptions,
#[clap(flatten)]
pub common: CommonArgs,
/// Store the receive command in the clipboard.
#[cfg(feature = "clipboard")]
#[clap(short = 'c', long)]
pub clipboard: bool,
}
#[derive(Parser, Debug)]
pub struct ReceiveArgs {
/// The ticket to use to connect to the sender.
pub ticket: BlobTicket,
#[clap(flatten)]
pub common: CommonArgs,
}
/// Options to configure what is included in a [`EndpointAddr`]
#[derive(
Copy,
Clone,
PartialEq,
Eq,
Default,
Debug,
derive_more::Display,
derive_more::FromStr,
Serialize,
Deserialize,
)]
pub enum AddrInfoOptions {
/// Only the Endpoint ID is added.
///
/// This usually means that iroh-dns discovery is used to find address information.
#[default]
Id,
/// Includes the Endpoint ID and both the relay URL, and the direct addresses.
RelayAndAddresses,
/// Includes the Endpoint ID and the relay URL.
Relay,
/// Includes the Endpoint ID and the direct addresses.
Addresses,
}
fn apply_options(addr: &mut EndpointAddr, opts: AddrInfoOptions) {
match opts {
AddrInfoOptions::Id => {
addr.addrs = Default::default();
}
AddrInfoOptions::RelayAndAddresses => {
// nothing to do
}
AddrInfoOptions::Relay => {
addr.addrs = addr
.addrs
.iter()
.filter(|addr| matches!(addr, TransportAddr::Relay(_)))
.cloned()
.collect();
}
AddrInfoOptions::Addresses => {
addr.addrs = addr
.addrs
.iter()
.filter(|addr| matches!(addr, TransportAddr::Ip(_)))
.cloned()
.collect();
}
}
}
/// Get the secret key or generate a new one.
///
/// Print the secret key to stderr if it was generated, so the user can save it.
fn get_or_create_secret(print: bool) -> anyhow::Result<SecretKey> {
match std::env::var("IROH_SECRET") {
Ok(secret) => SecretKey::from_str(&secret).context("invalid secret"),
Err(_) => {
let key = SecretKey::generate(&mut rand::rng());
if print {
let key = hex::encode(key.to_bytes());
eprintln!("using secret key {key}");
}
Ok(key)
}
}
}
fn validate_path_component(component: &str) -> anyhow::Result<()> {
anyhow::ensure!(
!component.contains('/'),
"path components must not contain the only correct path separator, /"
);
Ok(())
}
/// This function converts an already canonicalized path to a string.
///
/// If `must_be_relative` is true, the function will fail if any component of the path is
/// `Component::RootDir`
///
/// This function will also fail if the path is non canonical, i.e. contains
/// `..` or `.`, or if the path components contain any windows or unix path
/// separators.
pub fn canonicalized_path_to_string(
path: impl AsRef<Path>,
must_be_relative: bool,
) -> anyhow::Result<String> {
let mut path_str = String::new();
let parts = path
.as_ref()
.components()
.filter_map(|c| match c {
Component::Normal(x) => {
let c = match x.to_str() {
Some(c) => c,
None => return Some(Err(anyhow::anyhow!("invalid character in path"))),
};
if !c.contains('/') && !c.contains('\\') {
Some(Ok(c))
} else {
Some(Err(anyhow::anyhow!("invalid path component {:?}", c)))
}
}
Component::RootDir => {
if must_be_relative {
Some(Err(anyhow::anyhow!("invalid path component {:?}", c)))
} else {
path_str.push('/');
None
}
}
_ => Some(Err(anyhow::anyhow!("invalid path component {:?}", c))),
})
.collect::<anyhow::Result<Vec<_>>>()?;
let parts = parts.join("/");
path_str.push_str(&parts);
Ok(path_str)
}
/// Import from a file or directory into the database.
///
/// The returned tag always refers to a collection. If the input is a file, this
/// is a collection with a single blob, named like the file.
///
/// If the input is a directory, the collection contains all the files in the
/// directory.
async fn import(
path: PathBuf,
db: &Store,
mp: &mut MultiProgress,
jobs: Option<usize>,
) -> anyhow::Result<(TempTag, u64, Collection)> {
let parallelism = jobs.unwrap_or_else(num_cpus::get);
let path = path.canonicalize()?;
anyhow::ensure!(path.exists(), "path {} does not exist", path.display());
let root = path.parent().context("context get parent")?;
// walkdir also works for files, so we don't need to special case them
let files = WalkDir::new(path.clone()).into_iter();
// flatten the directory structure into a list of (name, path) pairs.
// ignore symlinks.
let data_sources: Vec<(String, PathBuf)> = files
.map(|entry| {
let entry = entry?;
if !entry.file_type().is_file() {
// Skip symlinks. Directories are handled by WalkDir.
return Ok(None);
}
let path = entry.into_path();
let relative = path.strip_prefix(root)?;
let name = canonicalized_path_to_string(relative, true)?;
anyhow::Ok(Some((name, path)))
})
.filter_map(Result::transpose)
.collect::<anyhow::Result<Vec<_>>>()?;
// import all the files, using num_cpus workers, return names and temp tags
let op = mp.add(make_import_overall_progress());
op.set_message(format!("importing {} files", data_sources.len()));
op.set_length(data_sources.len() as u64);
let mut names_and_tags = n0_future::stream::iter(data_sources)
.map(|(name, path)| {
let db = db.clone();
let op = op.clone();
let mp = mp.clone();
async move {
op.inc(1);
let pb = mp.add(make_import_item_progress());
pb.set_message(format!("copying {name}"));
let import = db.add_path_with_opts(AddPathOptions {
path,
mode: ImportMode::TryReference,
format: BlobFormat::Raw,
});
let mut stream = import.stream().await;
let mut item_size = 0;
let temp_tag = loop {
let item = stream
.next()
.await
.context("import stream ended without a tag")?;
trace!("importing {name} {item:?}");
match item {
AddProgressItem::Size(size) => {
item_size = size;
pb.set_length(size);
}
AddProgressItem::CopyProgress(offset) => {
pb.set_position(offset);
}
AddProgressItem::CopyDone => {
pb.set_message(format!("computing outboard {name}"));
pb.set_position(0);
}
AddProgressItem::OutboardProgress(offset) => {
pb.set_position(offset);
}
AddProgressItem::Error(cause) => {
pb.finish_and_clear();
anyhow::bail!("error importing {}: {}", name, cause);
}
AddProgressItem::Done(tt) => {
pb.finish_and_clear();
break tt;
}
}
};
anyhow::Ok((name, temp_tag, item_size))
}
})
.buffered_unordered(parallelism)
.collect::<Vec<_>>()
.await
.into_iter()
.collect::<anyhow::Result<Vec<_>>>()?;
op.finish_and_clear();
names_and_tags.sort_by(|(a, _, _), (b, _, _)| a.cmp(b));
// total size of all files
let size = names_and_tags.iter().map(|(_, _, size)| *size).sum::<u64>();
// collect the (name, hash) tuples into a collection
// we must also keep the tags around so the data does not get gced.
let (collection, tags) = names_and_tags
.into_iter()
.map(|(name, tag, _)| ((name, tag.hash()), tag))
.unzip::<_, _, Collection, Vec<_>>();
let temp_tag = collection.clone().store(db).await?;
// now that the collection is stored, we can drop the tags
// data is protected by the collection
drop(tags);
Ok((temp_tag, size, collection))
}
fn get_export_path(root: &Path, name: &str) -> anyhow::Result<PathBuf> {
let parts = name.split('/');
let mut path = root.to_path_buf();
for part in parts {
validate_path_component(part)?;
path.push(part);
}
Ok(path)
}
async fn export(db: &Store, collection: Collection, mp: &mut MultiProgress) -> anyhow::Result<()> {
let root = std::env::current_dir()?;
let op = mp.add(make_export_overall_progress());
op.set_length(collection.len() as u64);
for (i, (name, hash)) in collection.iter().enumerate() {
op.set_position(i as u64);
let target = get_export_path(&root, name)?;
if target.exists() {
eprintln!(
"target {} already exists. Export stopped.",
target.display()
);
eprintln!(
"You can remove the file or directory and try again. The download will not be repeated."
);
anyhow::bail!("target {} already exists", target.display());
}
let mut stream = db
.export_with_opts(ExportOptions {
hash: *hash,
target,
mode: ExportMode::Copy,
})
.stream()
.await;
let pb = mp.add(make_export_item_progress());
pb.set_message(format!("exporting {name}"));
while let Some(item) = stream.next().await {
match item {
ExportProgressItem::Size(size) => {
pb.set_length(size);
}
ExportProgressItem::CopyProgress(offset) => {
pb.set_position(offset);
}
ExportProgressItem::Done => {
pb.finish_and_clear();
}
ExportProgressItem::Error(cause) => {
pb.finish_and_clear();
anyhow::bail!("error exporting {}: {}", name, cause);
}
}
}
}
op.finish_and_clear();
Ok(())
}
#[derive(Debug)]
struct PerConnectionProgress {
endpoint_id: String,
requests: BTreeMap<u64, ProgressBar>,
}
async fn per_request_progress(
mp: MultiProgress,
connection_id: u64,
request_id: u64,
connections: Arc<Mutex<BTreeMap<u64, PerConnectionProgress>>>,
mut rx: irpc::channel::mpsc::Receiver<RequestUpdate>,
) {
let pb = mp.add(ProgressBar::hidden());
let endpoint_id = if let Some(connection) = connections.lock().unwrap().get_mut(&connection_id)
{
connection.requests.insert(request_id, pb.clone());
connection.endpoint_id.clone()
} else {
error!("got request for unknown connection {connection_id}");
return;
};
pb.set_style(
ProgressStyle::with_template(
"{msg}{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes}",
).unwrap()
.progress_chars("#>-"),
);
while let Ok(Some(msg)) = rx.recv().await {
match msg {
RequestUpdate::Started(msg) => {
pb.set_message(format!(
"n {} r {}/{} i {} # {}",
endpoint_id,
connection_id,
request_id,
msg.index,
msg.hash.fmt_short()
));
pb.set_length(msg.size);
}
RequestUpdate::Progress(msg) => {
pb.set_position(msg.end_offset);
}
RequestUpdate::Completed(_) => {
if let Some(msg) = connections.lock().unwrap().get_mut(&connection_id) {
msg.requests.remove(&request_id);
};
}
RequestUpdate::Aborted(_) => {
if let Some(msg) = connections.lock().unwrap().get_mut(&connection_id) {
msg.requests.remove(&request_id);
};
}
}
}
pb.finish_and_clear();
mp.remove(&pb);
}
async fn show_provide_progress(
mp: MultiProgress,
mut recv: mpsc::Receiver<ProviderMessage>,
) -> anyhow::Result<()> {
let connections = Arc::new(Mutex::new(BTreeMap::new()));
let mut tasks = FuturesUnordered::new();
loop {
tokio::select! {
biased;
item = recv.recv() => {
let Some(item) = item else {
break;
};
trace!("got event {item:?}");
match item {
ProviderMessage::ClientConnectedNotify(msg) => {
let endpoint_id = msg.endpoint_id.map(|id| id.fmt_short().to_string()).unwrap_or_else(|| "?".to_string());
let connection_id = msg.connection_id;
connections.lock().unwrap().insert(
connection_id,
PerConnectionProgress {
requests: BTreeMap::new(),
endpoint_id,
},
);
}
ProviderMessage::ConnectionClosed(msg) => {
if let Some(connection) = connections.lock().unwrap().remove(&msg.connection_id) {
for pb in connection.requests.values() {
pb.finish_and_clear();
mp.remove(pb);
}
}
}
ProviderMessage::GetRequestReceivedNotify(msg) => {
let request_id = msg.request_id;
let connection_id = msg.connection_id;
let connections = connections.clone();
let mp = mp.clone();
tasks.push(per_request_progress(mp, connection_id, request_id, connections, msg.rx));
}
_ => {}
}
}
Some(_) = tasks.next(), if !tasks.is_empty() => {}
}
}
while tasks.next().await.is_some() {}
Ok(())
}
async fn send(args: SendArgs) -> anyhow::Result<()> {
let secret_key = get_or_create_secret(args.common.verbose > 0)?;
if args.common.show_secret {
let secret_key = hex::encode(secret_key.to_bytes());
eprintln!("using secret key {secret_key}");
}
// create a magicsocket endpoint
let relay_mode: RelayMode = args.common.relay.into();
let mut builder = Endpoint::builder()
.alpns(vec![iroh_blobs::protocol::ALPN.to_vec()])
.secret_key(secret_key)
.relay_mode(relay_mode.clone());
if args.ticket_type == AddrInfoOptions::Id {
builder = builder.discovery(PkarrPublisher::n0_dns());
}
if let Some(addr) = args.common.magic_ipv4_addr {
builder = builder.bind_addr_v4(addr);
}
if let Some(addr) = args.common.magic_ipv6_addr {
builder = builder.bind_addr_v6(addr);
}
// use a flat store - todo: use a partial in mem store instead
let suffix = rand::rng().random::<[u8; 16]>();
let cwd = std::env::current_dir()?;
let blobs_data_dir = cwd.join(format!(".sendme-send-{}", HEXLOWER.encode(&suffix)));
if blobs_data_dir.exists() {
println!(
"can not share twice from the same directory: {}",
cwd.display(),
);
std::process::exit(1);
}
// todo: remove this as soon as we have a mem store that does not require a temp dir,
// or create a temp dir outside the current directory.
if cwd.join(&args.path) == cwd {
println!("can not share from the current directory");
std::process::exit(1);
}
let mut mp = MultiProgress::new();
let mp2 = mp.clone();
let path = args.path;
let path2 = path.clone();
let blobs_data_dir2 = blobs_data_dir.clone();
let (progress_tx, progress_rx) = mpsc::channel(32);
let progress = AbortOnDropHandle::new(n0_future::task::spawn(show_provide_progress(
mp2,
progress_rx,
)));
let setup = async move {
let t0 = Instant::now();
tokio::fs::create_dir_all(&blobs_data_dir2).await?;
let endpoint = builder.bind().await?;
let draw_target = if args.common.no_progress {
ProgressDrawTarget::hidden()
} else {
ProgressDrawTarget::stderr()
};
mp.set_draw_target(draw_target);
let store = FsStore::load(&blobs_data_dir2).await?;
let blobs = BlobsProtocol::new(
&store,
Some(EventSender::new(
progress_tx,
EventMask {
connected: ConnectMode::Notify,
get: provider::events::RequestMode::NotifyLog,
..EventMask::DEFAULT
},
)),
);
let import_result = import(path2, blobs.store(), &mut mp, args.common.jobs).await?;
let dt = t0.elapsed();
let router = iroh::protocol::Router::builder(endpoint)
.accept(iroh_blobs::ALPN, blobs.clone())
.spawn();
// wait for the endpoint to figure out its address before making a ticket
let ep = router.endpoint();
tokio::time::timeout(Duration::from_secs(30), async move {
if !matches!(relay_mode, RelayMode::Disabled) {
let _ = ep.online().await;
}
})
.await?;
anyhow::Ok((router, import_result, dt))
};
let (router, (temp_tag, size, collection), dt) = select! {
x = setup => x?,
_ = tokio::signal::ctrl_c() => {
std::process::exit(130);
}
};
let hash = temp_tag.hash();
// make a ticket
let mut addr = router.endpoint().addr();
apply_options(&mut addr, args.ticket_type);
let ticket = BlobTicket::new(addr, hash, BlobFormat::HashSeq);
let entry_type = if path.is_file() { "file" } else { "directory" };
println!(
"imported {} {}, {}, hash {}",
entry_type,
path.display(),
HumanBytes(size),
print_hash(&hash, args.common.format),
);
if args.common.verbose > 1 {
for (name, hash) in collection.iter() {
println!(" {} {name}", print_hash(hash, args.common.format));
}
println!(
"{}s, {}/s",
dt.as_secs_f64(),
HumanBytes(((size as f64) / dt.as_secs_f64()).floor() as u64)
);
}
println!("to get this data, use");
println!("sendme receive {ticket}");
#[cfg(feature = "clipboard")]
handle_key_press(args.clipboard, ticket);
tokio::signal::ctrl_c().await?;
drop(temp_tag);
println!("shutting down");
tokio::time::timeout(Duration::from_secs(2), router.shutdown()).await??;
tokio::fs::remove_dir_all(blobs_data_dir).await?;
// drop everything that owns blobs to close the progress sender
drop(router);
// await progress completion so the progress bar is cleared
progress.await.ok();
Ok(())
}
#[cfg(feature = "clipboard")]
fn handle_key_press(set_clipboard: bool, ticket: BlobTicket) {
#[cfg(any(unix, windows))]
use std::io;
use crossterm::{
event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
terminal::{disable_raw_mode, enable_raw_mode},
};
#[cfg(unix)]
use libc::{raise, SIGINT};
#[cfg(windows)]
use windows_sys::Win32::System::Console::{GenerateConsoleCtrlEvent, CTRL_C_EVENT};
if set_clipboard {
add_to_clipboard(&ticket);
}
let _keyboard = tokio::task::spawn(async move {
println!("press c to copy command to clipboard, or use the --clipboard argument");
// `enable_raw_mode` will remember the current terminal mode
// and restore it when `disable_raw_mode` is called.
enable_raw_mode().unwrap_or_else(|err| eprintln!("Failed to enable raw mode: {err}"));
EventStream::new()
.for_each(move |e| match e {
Err(err) => eprintln!("Failed to process event: {err}"),
// c is pressed
Ok(Event::Key(KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press,
..
})) => add_to_clipboard(&ticket),
// Ctrl+c is pressed
Ok(Event::Key(KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,
kind: KeyEventKind::Press,
..
})) => {
disable_raw_mode()
.unwrap_or_else(|e| eprintln!("Failed to disable raw mode: {e}"));
#[cfg(unix)]
// Safety: Raw syscall to re-send the SIGINT signal to the console.
// `raise` returns nonzero for failure.
if unsafe { raise(SIGINT) } != 0 {
eprintln!("Failed to raise signal: {}", io::Error::last_os_error());
}
#[cfg(windows)]
// Safety: Raw syscall to re-send the `CTRL_C_EVENT` to the console.
// `GenerateConsoleCtrlEvent` returns 0 for failure.
if unsafe { GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0) } == 0 {
eprintln!(
"Failed to generate console event: {}",
io::Error::last_os_error()
);
}
}
_ => {}
})
.await
});
}
#[cfg(feature = "clipboard")]
fn add_to_clipboard(ticket: &BlobTicket) {
use std::io::stdout;
use crossterm::{clipboard::CopyToClipboard, execute};
execute!(
stdout(),
CopyToClipboard::to_clipboard_from(format!("sendme receive {ticket}"))
)
.unwrap_or_else(|e| eprintln!("Failed to copy to clipboard: {e}"));
}
const TICK_MS: u64 = 250;
fn make_import_overall_progress() -> ProgressBar {
let pb = ProgressBar::hidden();
pb.enable_steady_tick(std::time::Duration::from_millis(TICK_MS));
pb.set_style(
ProgressStyle::with_template(
"{msg}{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos}/{len}",
)
.unwrap()
.progress_chars("#>-"),
);
pb
}
fn make_import_item_progress() -> ProgressBar {
let pb = ProgressBar::hidden();
pb.enable_steady_tick(std::time::Duration::from_millis(TICK_MS));
pb.set_style(
ProgressStyle::with_template("{msg}{spinner:.green} XXXX [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes}")
.unwrap()
.progress_chars("#>-"),
);
pb
}
fn make_connect_progress() -> ProgressBar {
let pb = ProgressBar::hidden();
pb.set_style(
ProgressStyle::with_template("{prefix}{spinner:.green} Connecting ... [{elapsed_precise}]")
.unwrap(),
);
pb.set_prefix(format!("{} ", style("[1/4]").bold().dim()));
pb.enable_steady_tick(Duration::from_millis(TICK_MS));
pb
}
fn make_get_sizes_progress() -> ProgressBar {
let pb = ProgressBar::hidden();
pb.set_style(
ProgressStyle::with_template(
"{prefix}{spinner:.green} Getting sizes... [{elapsed_precise}]",
)
.unwrap(),
);
pb.set_prefix(format!("{} ", style("[2/4]").bold().dim()));
pb.enable_steady_tick(Duration::from_millis(TICK_MS));
pb
}
fn make_download_progress() -> ProgressBar {
let pb = ProgressBar::hidden();
pb.enable_steady_tick(std::time::Duration::from_millis(TICK_MS));
pb.set_style(
ProgressStyle::with_template("{prefix}{spinner:.green}{msg} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} {binary_bytes_per_sec}")
.unwrap()
.progress_chars("#>-"),
);
pb.set_prefix(format!("{} ", style("[3/4]").bold().dim()));
pb.set_message("Downloading ...".to_string());
pb
}
fn make_export_overall_progress() -> ProgressBar {
let pb = ProgressBar::hidden();
pb.enable_steady_tick(std::time::Duration::from_millis(TICK_MS));
pb.set_style(
ProgressStyle::with_template("{prefix}{msg}{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {human_pos}/{human_len} {per_sec}")
.unwrap()
.progress_chars("#>-"),
);
pb.set_prefix(format!("{}", style("[4/4]").bold().dim()));
pb
}
fn make_export_item_progress() -> ProgressBar {
let pb = ProgressBar::hidden();
pb.enable_steady_tick(std::time::Duration::from_millis(100));
pb.set_style(
ProgressStyle::with_template(
"{msg}{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes}",
)
.unwrap()
.progress_chars("#>-"),
);
pb
}
pub async fn show_download_progress(
mp: MultiProgress,
mut recv: mpsc::Receiver<u64>,
local_size: u64,
total_size: u64,
) -> anyhow::Result<()> {
let op = mp.add(make_download_progress());
op.set_length(total_size);
while let Some(offset) = recv.recv().await {
op.set_position(local_size + offset);
}
op.finish_and_clear();
Ok(())
}
fn show_get_error(e: GetError) -> GetError {
match &e {
GetError::InitialNext { source, .. } => eprintln!(
"{}",
style(format!("initial connection error: {source}")).yellow()
),
GetError::ConnectedNext { source, .. } => {
eprintln!("{}", style(format!("connected error: {source}")).yellow())
}
GetError::AtBlobHeaderNext { source, .. } => eprintln!(
"{}",
style(format!("reading blob header error: {source}")).yellow()
),
GetError::Decode { source, .. } => {
eprintln!("{}", style(format!("decoding error: {source}")).yellow())
}
GetError::IrpcSend { source, .. } => eprintln!(
"{}",
style(format!("error sending over irpc: {source}")).yellow()
),
GetError::AtClosingNext { source, .. } => {
eprintln!("{}", style(format!("error at closing: {source}")).yellow())
}
GetError::BadRequest { .. } => eprintln!("{}", style("bad request").yellow()),
GetError::LocalFailure { source, .. } => {
eprintln!("{} {source:?}", style("local failure").yellow())