s3util-rs 0.2.0

S3 utility commands (cp, mv, rm, etc.)
Documentation
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
use async_channel::Receiver;
use indicatif::{HumanBytes, ProgressBar, ProgressDrawTarget, ProgressStyle};
use s3util_rs::types::SyncStatistics;
use simple_moving_average::{SMA, SumTreeSMA};
use tokio::task::JoinHandle;
use tokio::time::Instant;
use tracing::info;

const MOVING_AVERAGE_PERIOD_SECS: usize = 10;
const REFRESH_INTERVAL: f32 = 1.0;

fn verification_status(
    etag_verified: u64,
    etag_mismatch: u64,
    checksum_verified: u64,
    checksum_mismatch: u64,
) -> (&'static str, &'static str) {
    let etag = if etag_verified > 0 {
        "ok"
    } else if etag_mismatch > 0 {
        "failed"
    } else {
        "skipped"
    };
    let checksum = if checksum_verified > 0 {
        "ok"
    } else if checksum_mismatch > 0 {
        "failed"
    } else {
        "skipped"
    };
    (etag, checksum)
}

pub fn show_indicator(
    stats_receiver: Receiver<SyncStatistics>,
    show_progress: bool,
    show_result: bool,
    log_sync_summary: bool,
    resolved_target: Option<String>,
    source_key: String,
    target_key: String,
) -> JoinHandle<()> {
    let progress_style = ProgressStyle::with_template("{wide_msg}").unwrap();
    let progress_text = ProgressBar::with_draw_target(Some(0), ProgressDrawTarget::stderr());
    progress_text.set_style(progress_style);

    tokio::spawn(async move {
        let start_time = Instant::now();

        let mut ma_synced_bytes = SumTreeSMA::<_, u64, MOVING_AVERAGE_PERIOD_SECS>::new();

        let mut total_sync_bytes: u64 = 0;
        let mut total_error_count: u64 = 0;
        let mut total_warning_count: u64 = 0;
        let mut total_e_tag_verified_count: u64 = 0;
        let mut total_e_tag_mismatch_count: u64 = 0;
        let mut total_checksum_verified_count: u64 = 0;
        let mut total_checksum_mismatch_count: u64 = 0;

        // stats_receiver tracks high-precision byte counts.
        loop {
            let mut sync_bytes: u64 = 0;

            let period = Instant::now();
            loop {
                while let Ok(sync_stats) = stats_receiver.try_recv() {
                    match sync_stats {
                        SyncStatistics::SyncComplete { .. } => {}
                        SyncStatistics::SyncBytes(size) => {
                            sync_bytes += size;
                            total_sync_bytes += size
                        }
                        SyncStatistics::SyncError { .. } => {
                            total_error_count += 1;
                        }
                        SyncStatistics::SyncWarning { .. } => {
                            total_warning_count += 1;
                        }
                        SyncStatistics::ETagVerified { .. } => {
                            total_e_tag_verified_count += 1;
                        }
                        SyncStatistics::ETagMismatch { .. } => {
                            total_e_tag_mismatch_count += 1;
                            total_warning_count += 1;
                        }
                        SyncStatistics::ChecksumVerified { .. } => {
                            total_checksum_verified_count += 1;
                        }
                        SyncStatistics::ChecksumMismatch { .. } => {
                            total_checksum_mismatch_count += 1;
                            total_warning_count += 1;
                        }
                    }
                }

                if REFRESH_INTERVAL < period.elapsed().as_secs_f32() {
                    break;
                }

                if stats_receiver.is_closed() {
                    let elapsed = start_time.elapsed();
                    let elapsed_secs_f64 = elapsed.as_secs_f64();

                    let mut sync_bytes_per_sec =
                        (total_sync_bytes as f64 / elapsed_secs_f64) as u64;

                    if elapsed_secs_f64 < REFRESH_INTERVAL as f64 {
                        sync_bytes_per_sec = total_sync_bytes;
                    }

                    if log_sync_summary && total_error_count == 0 {
                        info!(
                            message = "Transfer completed.",
                            source_key = source_key,
                            target_key = target_key,
                            transferred_byte = total_sync_bytes,
                            transferred_byte_per_sec = sync_bytes_per_sec,
                            etag_verified = total_e_tag_verified_count,
                            checksum_verified = total_checksum_verified_count,
                            error = total_error_count,
                            warning = total_warning_count,
                            duration_sec = elapsed_secs_f64,
                        );
                    }

                    // Clear live progress before printing final output
                    progress_text.finish_and_clear();

                    // Show resolved destination path first
                    if show_result
                        && total_error_count == 0
                        && let Some(ref resolved) = resolved_target
                    {
                        eprintln!("-> {resolved}");
                    }

                    if show_result && total_error_count == 0 {
                        let mut parts = vec![format!(
                            "Transferred: {} | {}/sec",
                            HumanBytes(total_sync_bytes),
                            HumanBytes(sync_bytes_per_sec)
                        )];

                        let (etag_status, checksum_status) = verification_status(
                            total_e_tag_verified_count,
                            total_e_tag_mismatch_count,
                            total_checksum_verified_count,
                            total_checksum_mismatch_count,
                        );
                        parts.push(format!("etag verify: {etag_status}"));
                        parts.push(format!("additional checksum verify: {checksum_status}"));

                        let result_message = parts.join(", ");
                        eprintln!("{result_message}");
                    }

                    return;
                }

                tokio::time::sleep(std::time::Duration::from_secs_f32(0.05)).await;
            }
            ma_synced_bytes.add_sample(sync_bytes);

            if show_progress {
                let progress_message = format!(
                    "Transferred: {:>3} | {:>3}/sec",
                    HumanBytes(total_sync_bytes),
                    HumanBytes(ma_synced_bytes.get_average())
                );
                progress_text.set_message(progress_message);
            }
        }
    })
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::*;

    const WAITING_TIME_MILLIS_FOR_ASYNC_INDICATOR_SET_MESSAGE: u64 = 1500;

    #[tokio::test]
    async fn indicator_test_show_result() {
        init_dummy_tracing_subscriber();

        let (stats_sender, stats_receiver) = async_channel::unbounded();
        let join_handle = show_indicator(
            stats_receiver,
            true,
            true,
            false,
            None,
            String::new(),
            String::new(),
        );

        stats_sender
            .send(SyncStatistics::SyncBytes(1))
            .await
            .unwrap();
        stats_sender
            .send(SyncStatistics::SyncComplete {
                key: "test".to_string(),
            })
            .await
            .unwrap();
        stats_sender
            .send(SyncStatistics::SyncWarning {
                key: "test".to_string(),
            })
            .await
            .unwrap();
        stats_sender
            .send(SyncStatistics::SyncError {
                key: "test".to_string(),
            })
            .await
            .unwrap();
        stats_sender
            .send(SyncStatistics::ETagVerified {
                key: "test".to_string(),
            })
            .await
            .unwrap();
        stats_sender
            .send(SyncStatistics::ChecksumVerified {
                key: "test".to_string(),
            })
            .await
            .unwrap();

        tokio::time::sleep(Duration::from_millis(
            WAITING_TIME_MILLIS_FOR_ASYNC_INDICATOR_SET_MESSAGE,
        ))
        .await;
        stats_sender.close();

        join_handle.await.unwrap();
    }

    #[tokio::test]
    async fn indicator_test_show_no_result() {
        init_dummy_tracing_subscriber();

        let (stats_sender, stats_receiver) = async_channel::unbounded();
        let join_handle = show_indicator(
            stats_receiver,
            true,
            false,
            true,
            None,
            "src".to_string(),
            "dst".to_string(),
        );

        stats_sender
            .send(SyncStatistics::SyncBytes(1))
            .await
            .unwrap();
        stats_sender
            .send(SyncStatistics::SyncComplete {
                key: "test".to_string(),
            })
            .await
            .unwrap();
        stats_sender
            .send(SyncStatistics::SyncError {
                key: "test".to_string(),
            })
            .await
            .unwrap();
        stats_sender
            .send(SyncStatistics::ETagVerified {
                key: "test".to_string(),
            })
            .await
            .unwrap();
        stats_sender
            .send(SyncStatistics::ChecksumVerified {
                key: "test".to_string(),
            })
            .await
            .unwrap();

        tokio::time::sleep(Duration::from_millis(
            WAITING_TIME_MILLIS_FOR_ASYNC_INDICATOR_SET_MESSAGE,
        ))
        .await;
        stats_sender.close();

        join_handle.await.unwrap();
    }

    #[tokio::test]
    async fn indicator_fast_completion_falls_back_to_raw_total_for_rate() {
        // Closes immediately so elapsed_secs_f64 < REFRESH_INTERVAL — the
        // branch that assigns sync_bytes_per_sec = total_sync_bytes to avoid
        // a misleading spike on sub-100ms transfers.
        init_dummy_tracing_subscriber();
        let (stats_sender, stats_receiver) = async_channel::unbounded();
        let join_handle = show_indicator(
            stats_receiver,
            false,
            true,
            true,
            None,
            "src".to_string(),
            "dst".to_string(),
        );

        stats_sender
            .send(SyncStatistics::SyncBytes(10))
            .await
            .unwrap();
        stats_sender.close();

        join_handle.await.unwrap();
    }

    #[tokio::test]
    async fn indicator_with_resolved_target_prints_destination_line() {
        // Covers the `Some(ref resolved)` arm of resolved_target on successful
        // completion (no errors). Gated on `show_result` so `--show-progress`
        // controls whether the line is printed.
        init_dummy_tracing_subscriber();
        let (stats_sender, stats_receiver) = async_channel::unbounded();
        let join_handle = show_indicator(
            stats_receiver,
            false,
            true,
            false,
            Some("s3://bucket/resolved/key".to_string()),
            String::new(),
            String::new(),
        );

        stats_sender
            .send(SyncStatistics::SyncBytes(1))
            .await
            .unwrap();
        stats_sender.close();

        join_handle.await.unwrap();
    }

    #[tokio::test]
    async fn indicator_warning_without_etag_verified_shows_etag_skipped() {
        // Warning > 0 but no ETagVerified: etag status takes the middle arm
        // ("failed") and checksum status falls through to "skipped".
        init_dummy_tracing_subscriber();
        let (stats_sender, stats_receiver) = async_channel::unbounded();
        let join_handle = show_indicator(
            stats_receiver,
            false,
            true,
            false,
            None,
            String::new(),
            String::new(),
        );

        stats_sender
            .send(SyncStatistics::SyncBytes(1))
            .await
            .unwrap();
        stats_sender
            .send(SyncStatistics::SyncWarning {
                key: "test".to_string(),
            })
            .await
            .unwrap();

        tokio::time::sleep(Duration::from_millis(200)).await;
        stats_sender.close();
        join_handle.await.unwrap();
    }

    #[tokio::test]
    async fn indicator_etag_verified_plus_warning_shows_checksum_failed() {
        // ETagVerified + SyncWarning but no ChecksumVerified: checksum status
        // renders as "failed" (the `else if` middle arm).
        init_dummy_tracing_subscriber();
        let (stats_sender, stats_receiver) = async_channel::unbounded();
        let join_handle = show_indicator(
            stats_receiver,
            false,
            true,
            false,
            None,
            String::new(),
            String::new(),
        );

        stats_sender
            .send(SyncStatistics::SyncBytes(1))
            .await
            .unwrap();
        stats_sender
            .send(SyncStatistics::ETagVerified {
                key: "test".to_string(),
            })
            .await
            .unwrap();
        stats_sender
            .send(SyncStatistics::SyncWarning {
                key: "test".to_string(),
            })
            .await
            .unwrap();

        tokio::time::sleep(Duration::from_millis(200)).await;
        stats_sender.close();
        join_handle.await.unwrap();
    }

    fn init_dummy_tracing_subscriber() {
        let _ = tracing_subscriber::fmt()
            .with_env_filter("dummy=trace")
            .try_init();
    }

    #[test]
    fn verification_status_etag_skipped_and_checksum_failed() {
        // Regression test: ETag verification was never attempted (e.g. SSE-C
        // source), and the additional checksum verification failed. Before the
        // per-verification counters were introduced, the aggregate warning
        // counter made ETag render as "failed" and checksum as "skipped"
        // — both incorrect. Expected: etag skipped, checksum failed.
        let (etag, checksum) = verification_status(
            0, // etag_verified
            0, // etag_mismatch
            0, // checksum_verified
            1, // checksum_mismatch
        );
        assert_eq!(etag, "skipped");
        assert_eq!(checksum, "failed");
    }

    #[test]
    fn verification_status_etag_failed_and_checksum_skipped() {
        // Symmetric case: ETag mismatched, checksum verification not performed.
        let (etag, checksum) = verification_status(0, 1, 0, 0);
        assert_eq!(etag, "failed");
        assert_eq!(checksum, "skipped");
    }

    #[test]
    fn verification_status_both_verified() {
        let (etag, checksum) = verification_status(1, 0, 1, 0);
        assert_eq!(etag, "ok");
        assert_eq!(checksum, "ok");
    }

    #[test]
    fn verification_status_etag_ok_and_checksum_failed() {
        let (etag, checksum) = verification_status(1, 0, 0, 1);
        assert_eq!(etag, "ok");
        assert_eq!(checksum, "failed");
    }

    #[test]
    fn verification_status_etag_failed_and_checksum_ok() {
        let (etag, checksum) = verification_status(0, 1, 1, 0);
        assert_eq!(etag, "failed");
        assert_eq!(checksum, "ok");
    }

    #[test]
    fn verification_status_both_skipped() {
        let (etag, checksum) = verification_status(0, 0, 0, 0);
        assert_eq!(etag, "skipped");
        assert_eq!(checksum, "skipped");
    }
}