asanypath-native 2.5.0__tar.gz → 2.6.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -96,7 +96,7 @@ dependencies = [
96
96
 
97
97
  [[package]]
98
98
  name = "asanypath-native"
99
- version = "2.5.0"
99
+ version = "2.6.1"
100
100
  dependencies = [
101
101
  "anyhow",
102
102
  "aws-config",
@@ -108,6 +108,7 @@ dependencies = [
108
108
  "futures",
109
109
  "hmac",
110
110
  "http 1.5.0",
111
+ "md-5",
111
112
  "pem",
112
113
  "pyo3",
113
114
  "pyo3-async-runtimes",
@@ -173,9 +174,9 @@ dependencies = [
173
174
 
174
175
  [[package]]
175
176
  name = "aws-credential-types"
176
- version = "1.3.0"
177
+ version = "1.3.1"
177
178
  source = "registry+https://github.com/rust-lang/crates.io-index"
178
- checksum = "e93964ffdaf57857f544be3666a5f57570bb699e934700f11b49708f61bb556e"
179
+ checksum = "576e8da35751dc85409ec1cd15999f692c277a1fbe9336780fe6f15e49dd637d"
179
180
  dependencies = [
180
181
  "aws-smithy-async",
181
182
  "aws-smithy-runtime-api",
@@ -335,9 +336,9 @@ dependencies = [
335
336
 
336
337
  [[package]]
337
338
  name = "aws-smithy-async"
338
- version = "1.3.0"
339
+ version = "1.3.1"
339
340
  source = "registry+https://github.com/rust-lang/crates.io-index"
340
- checksum = "f02e407fb3b54891734224b9ffac8a71fdd35f542500fa1af95754a6b2beb316"
341
+ checksum = "de681a9df7ff79823b759ee17061076ec049cd61a8fa28ece4293d89c9f6964e"
341
342
  dependencies = [
342
343
  "futures-util",
343
344
  "pin-project-lite",
@@ -450,9 +451,9 @@ dependencies = [
450
451
 
451
452
  [[package]]
452
453
  name = "aws-smithy-runtime-api"
453
- version = "1.19.0"
454
+ version = "1.19.2"
454
455
  source = "registry+https://github.com/rust-lang/crates.io-index"
455
- checksum = "c0730c16f91124c6a2abb4932c77e299288b3dd9f967ea2e9ec48cc6731e87a4"
456
+ checksum = "8c4e85cffe9944b10d576b075432bd8f98dbc081e8681cb37d5b2d87dc27bc84"
456
457
  dependencies = [
457
458
  "aws-smithy-async",
458
459
  "aws-smithy-runtime-api-macros",
@@ -467,9 +468,9 @@ dependencies = [
467
468
 
468
469
  [[package]]
469
470
  name = "aws-smithy-runtime-api-macros"
470
- version = "1.1.0"
471
+ version = "1.2.0"
471
472
  source = "registry+https://github.com/rust-lang/crates.io-index"
472
- checksum = "221eaa237ddf1ca79b60d1372aad77e47f9c0ea5b3ce5099da8c61d027dc77b3"
473
+ checksum = "ce237f890a2283c9b328955c3b0918d2d5bf66a1cb6ceccd36a0047246fbc6e6"
473
474
  dependencies = [
474
475
  "proc-macro2",
475
476
  "quote",
@@ -489,9 +490,9 @@ dependencies = [
489
490
 
490
491
  [[package]]
491
492
  name = "aws-smithy-types"
492
- version = "1.8.1"
493
+ version = "1.9.1"
493
494
  source = "registry+https://github.com/rust-lang/crates.io-index"
494
- checksum = "69bb407740a197147da48238ecc94498493c9e85445732360cec180296ca45f1"
495
+ checksum = "cf183d70774ea2cfcc101ca74a8884e4c76be8b9df65cd303f6a1175c88e8f80"
495
496
  dependencies = [
496
497
  "base64-simd",
497
498
  "bytes",
@@ -504,7 +505,6 @@ dependencies = [
504
505
  "itoa",
505
506
  "num-integer",
506
507
  "pin-project-lite",
507
- "pin-utils",
508
508
  "ryu",
509
509
  "serde",
510
510
  "time",
@@ -1984,6 +1984,16 @@ dependencies = [
1984
1984
  "web_atoms",
1985
1985
  ]
1986
1986
 
1987
+ [[package]]
1988
+ name = "md-5"
1989
+ version = "0.11.0"
1990
+ source = "registry+https://github.com/rust-lang/crates.io-index"
1991
+ checksum = "69b6441f590336821bb897fb28fc622898ccceb1d6cea3fde5ea86b090c4de98"
1992
+ dependencies = [
1993
+ "cfg-if",
1994
+ "digest 0.11.3",
1995
+ ]
1996
+
1987
1997
  [[package]]
1988
1998
  name = "md5"
1989
1999
  version = "0.8.1"
@@ -2183,9 +2193,9 @@ dependencies = [
2183
2193
 
2184
2194
  [[package]]
2185
2195
  name = "pageant"
2186
- version = "0.2.3"
2196
+ version = "0.2.5"
2187
2197
  source = "registry+https://github.com/rust-lang/crates.io-index"
2188
- checksum = "6d8eab09a361a4afe0b1668be978cd97e4f052e927a92b0b608cf902965d49ce"
2198
+ checksum = "e0bc922d9aa8cfcf598de27b3c6122cb2eeda9d6fe71527a22ebcb7f633540ff"
2189
2199
  dependencies = [
2190
2200
  "base16ct",
2191
2201
  "byteorder",
@@ -2878,9 +2888,9 @@ dependencies = [
2878
2888
 
2879
2889
  [[package]]
2880
2890
  name = "russh"
2881
- version = "0.64.1"
2891
+ version = "0.65.0"
2882
2892
  source = "registry+https://github.com/rust-lang/crates.io-index"
2883
- checksum = "ba61e87b9ec9a39a59a6bbed4c0b8ff7fb07073405b24a767a1f2c8560ac8f58"
2893
+ checksum = "3bb77a4b259312b2abbc541bca1c13cb4e9b61c560a7fd894068c3b92e0c9dc3"
2884
2894
  dependencies = [
2885
2895
  "aes",
2886
2896
  "aws-lc-rs",
@@ -3008,9 +3018,9 @@ dependencies = [
3008
3018
 
3009
3019
  [[package]]
3010
3020
  name = "rustls"
3011
- version = "0.23.45"
3021
+ version = "0.23.46"
3012
3022
  source = "registry+https://github.com/rust-lang/crates.io-index"
3013
- checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634"
3023
+ checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf"
3014
3024
  dependencies = [
3015
3025
  "aws-lc-rs",
3016
3026
  "once_cell",
@@ -3288,9 +3298,9 @@ dependencies = [
3288
3298
 
3289
3299
  [[package]]
3290
3300
  name = "sha2"
3291
- version = "0.11.0"
3301
+ version = "0.11.1"
3292
3302
  source = "registry+https://github.com/rust-lang/crates.io-index"
3293
- checksum = "446ba717509524cb3f22f17ecc096f10f4822d76ab5c0b9822c5f9c284e825f4"
3303
+ checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19"
3294
3304
  dependencies = [
3295
3305
  "cfg-if",
3296
3306
  "cpufeatures 0.3.1",
@@ -3512,9 +3522,9 @@ dependencies = [
3512
3522
 
3513
3523
  [[package]]
3514
3524
  name = "ssh2-config"
3515
- version = "0.8.1"
3525
+ version = "0.9.0"
3516
3526
  source = "registry+https://github.com/rust-lang/crates.io-index"
3517
- checksum = "4b817975ac67aeb9583619989b802d6072bccc402ffc00744038c704e7246bb7"
3527
+ checksum = "3f8aade2961db6d07192035b809806be8fc32ca8875f9395e9e97239eeb2ed10"
3518
3528
  dependencies = [
3519
3529
  "bitflags",
3520
3530
  "dirs",
@@ -3685,9 +3695,9 @@ checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee"
3685
3695
 
3686
3696
  [[package]]
3687
3697
  name = "tokio"
3688
- version = "1.53.1"
3698
+ version = "1.54.1"
3689
3699
  source = "registry+https://github.com/rust-lang/crates.io-index"
3690
- checksum = "202caea871b69668250d242070849eb495be178ed697a3e98aebce5bc81a0bed"
3700
+ checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3"
3691
3701
  dependencies = [
3692
3702
  "bytes",
3693
3703
  "libc",
@@ -3722,9 +3732,9 @@ dependencies = [
3722
3732
 
3723
3733
  [[package]]
3724
3734
  name = "tokio-util"
3725
- version = "0.7.19"
3735
+ version = "0.7.20"
3726
3736
  source = "registry+https://github.com/rust-lang/crates.io-index"
3727
- checksum = "494815d09bf52b5548659851081238f0ca39ff638363907596da739561c62c52"
3737
+ checksum = "e464cf451ba96ebfc6f9b6542f17ee8b8956e33f1e40d9690624e59d7a7f8a4b"
3728
3738
  dependencies = [
3729
3739
  "bytes",
3730
3740
  "futures-core",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "asanypath-native"
3
- version = "2.5.0"
3
+ version = "2.6.1"
4
4
  edition = "2024"
5
5
  license = "MIT"
6
6
  publish = false
@@ -32,12 +32,13 @@ ring = "0.17"
32
32
  pem = "4"
33
33
  scraper = { version = "0.27", default-features = false }
34
34
  url = "2"
35
- russh = "0.64"
35
+ russh = "0.65"
36
36
  russh-sftp = "3"
37
37
  tokio-util = { version = "0.7", features = ["io"] }
38
38
  anyhow = "1"
39
- ssh2-config = { version = "0.8", default-features = false, features = ["nolog"] }
39
+ ssh2-config = { version = "0.9", default-features = false, features = ["nolog"] }
40
40
  rusqlite = { version = "0.40", features = ["bundled"] }
41
+ md-5 = "0.11"
41
42
 
42
43
  [profile.release]
43
44
  strip = "symbols"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: asanypath-native
3
- Version: 2.5.0
3
+ Version: 2.6.1
4
4
  Summary: Native cloud acceleration for asanypath (Rust/tokio/reqwest)
5
5
  License-Expression: MIT
6
6
  Requires-Python: >=3.10
@@ -27,6 +27,7 @@ async def s3_complete_multipart(*args: Any, **kwargs: Any) -> Any: ...
27
27
  async def s3_abort_multipart(*args: Any, **kwargs: Any) -> Any: ...
28
28
  async def s3_copy_batch(*args: Any, **kwargs: Any) -> Any: ...
29
29
  async def s3_delete_batch(*args: Any, **kwargs: Any) -> Any: ...
30
+ async def s3_delete_tree(*args: Any, **kwargs: Any) -> list[str]: ...
30
31
  async def s3_exists_batch(*args: Any, **kwargs: Any) -> Any: ...
31
32
  async def s3_list_batch(*args: Any, **kwargs: Any) -> Any: ...
32
33
  async def s3_head_batch(*args: Any, **kwargs: Any) -> Any: ...
@@ -60,6 +61,7 @@ async def gcs_start_resumable(*args: Any, **kwargs: Any) -> Any: ...
60
61
  async def gcs_upload_chunk(*args: Any, **kwargs: Any) -> Any: ...
61
62
  async def gcs_copy_batch(*args: Any, **kwargs: Any) -> Any: ...
62
63
  async def gcs_delete_batch(*args: Any, **kwargs: Any) -> Any: ...
64
+ async def gcs_delete_tree(*args: Any, **kwargs: Any) -> list[str]: ...
63
65
  async def gcs_exists_batch(*args: Any, **kwargs: Any) -> Any: ...
64
66
  async def gcs_list_batch(*args: Any, **kwargs: Any) -> Any: ...
65
67
  async def gcs_head(*args: Any, **kwargs: Any) -> Any: ...
@@ -81,6 +83,7 @@ async def az_put_block(*args: Any, **kwargs: Any) -> Any: ...
81
83
  async def az_put_block_list(*args: Any, **kwargs: Any) -> Any: ...
82
84
  async def az_copy_batch(*args: Any, **kwargs: Any) -> Any: ...
83
85
  async def az_delete_batch(*args: Any, **kwargs: Any) -> Any: ...
86
+ async def az_delete_tree(*args: Any, **kwargs: Any) -> list[str]: ...
84
87
  async def az_exists_batch(*args: Any, **kwargs: Any) -> Any: ...
85
88
  async def az_list_batch(*args: Any, **kwargs: Any) -> Any: ...
86
89
  async def az_head_batch(*args: Any, **kwargs: Any) -> Any: ...
@@ -65,6 +65,15 @@ pub(crate) struct ArtCreds {
65
65
  mod endpoint_tests {
66
66
  use super::ArtCreds;
67
67
 
68
+ #[test]
69
+ fn empty_token_sends_no_authorization() {
70
+ let anonymous = ArtCreds::new("example.com/artifactory".into(), String::new());
71
+ assert!(anonymous.auth_headers().is_empty());
72
+ let authenticated = ArtCreds::new("example.com/artifactory".into(), "t".into());
73
+ assert_eq!(authenticated.auth_headers().len(), 1);
74
+ assert_eq!(authenticated.auth_headers()[0].0, "authorization");
75
+ }
76
+
68
77
  #[test]
69
78
  fn complete_and_legacy_endpoints_build_the_same_urls() {
70
79
  for endpoint in [
@@ -121,7 +130,12 @@ impl ArtCreds {
121
130
  }
122
131
 
123
132
  fn auth_headers(&self) -> Vec<(String, String)> {
124
- bearer_headers(&self.token)
133
+ // An empty token means anonymous access.
134
+ if self.token.is_empty() {
135
+ Vec::new()
136
+ } else {
137
+ bearer_headers(&self.token)
138
+ }
125
139
  }
126
140
 
127
141
  fn content_url(&self, path: &str) -> String {
@@ -288,10 +302,16 @@ impl CloudBackend for ArtCreds {
288
302
  async move {
289
303
  let url = self.storage_url(path);
290
304
  let headers = self.auth_headers();
291
- let (status, _body) = do_request_light("GET", &url, &headers, None, use_h2)
305
+ let (status, body) = do_request_light("GET", &url, &headers, None, use_h2)
292
306
  .await
293
307
  .map_err(|e| PyRuntimeError::new_err(e))?;
294
- Ok(status.is_success())
308
+ if status.is_success() {
309
+ Ok(true)
310
+ } else if status == reqwest::StatusCode::NOT_FOUND {
311
+ Ok(false)
312
+ } else {
313
+ Err(status_to_pyerr(status, &self.error_path(path), &body))
314
+ }
295
315
  }
296
316
  }
297
317
 
@@ -23,7 +23,7 @@ use serde::{Deserialize, Serialize};
23
23
  use sha2::Sha256;
24
24
 
25
25
  use crate::http::{do_request, do_request_light, status_to_pyerr};
26
- use crate::listing::{ListEntry, Listing};
26
+ use crate::listing::{Keys, ListEntry, ListSink, Listing};
27
27
  use crate::xml::local_name;
28
28
 
29
29
  type HmacSha256 = Hmac<Sha256>;
@@ -111,7 +111,18 @@ impl AzCreds {
111
111
  let now = chrono_now_rfc2822();
112
112
  headers.push(("x-ms-date".to_string(), now));
113
113
  headers.push(("x-ms-version".to_string(), API_VERSION.to_string()));
114
+ self.sign_headers(method, url, headers, params);
115
+ }
114
116
 
117
+ /// Add the SharedKey `authorization` for `headers` as given (batch
118
+ /// sub-requests carry `x-ms-date` but no `x-ms-version`).
119
+ fn sign_headers(
120
+ &self,
121
+ method: &str,
122
+ url: &str,
123
+ headers: &mut Vec<(String, String)>,
124
+ params: Option<&[(&str, &str)]>,
125
+ ) {
115
126
  let key_bytes = match &self.key_bytes {
116
127
  Some(kb) => kb,
117
128
  None => return, // SAS auth or no key, no signing needed
@@ -428,47 +439,131 @@ async fn do_az_is_dir(creds: &AzCreds, prefix: &str, use_h2: bool) -> Result<boo
428
439
  Ok(false)
429
440
  }
430
441
 
442
+ /// Fetch one List Blobs page into `sink`; returns the next marker.
443
+ /// Without `delimiter` the page holds every blob at any depth.
444
+ async fn az_list_page(
445
+ creds: &AzCreds,
446
+ search_prefix: &str,
447
+ delimiter: bool,
448
+ marker: Option<String>,
449
+ use_h2: bool,
450
+ sink: &mut impl ListSink,
451
+ ) -> Result<Option<String>, PyErr> {
452
+ let mut params: Vec<(&str, &str)> = vec![
453
+ ("restype", "container"),
454
+ ("comp", "list"),
455
+ ("prefix", search_prefix),
456
+ ];
457
+ if delimiter {
458
+ params.push(("delimiter", "/"));
459
+ }
460
+ if let Some(marker) = marker.as_deref().filter(|m| !m.is_empty()) {
461
+ params.push(("marker", marker));
462
+ }
463
+ let url = creds.list_url(&params);
464
+ let mut headers = Vec::new();
465
+ creds.sign("GET", &url, &mut headers, Some(&params));
466
+ let (status, body) = do_request_light("GET", &url, &headers, None, use_h2)
467
+ .await
468
+ .map_err(PyRuntimeError::new_err)?;
469
+ if !status.is_success() {
470
+ return Err(status_to_pyerr(
471
+ status,
472
+ &format!("az://{}/{}", creds.container, search_prefix),
473
+ &body,
474
+ ));
475
+ }
476
+ Ok(parse_az_listing_page(&body, sink))
477
+ }
478
+
431
479
  async fn do_az_list(creds: &AzCreds, prefix: &str, use_h2: bool) -> Result<Vec<ListEntry>, PyErr> {
432
480
  let mut listing = Listing::new(format!("az://{}", creds.container), prefix);
433
481
  let search_prefix = listing.search_prefix().to_string();
434
- let mut marker: Option<String> = None;
435
-
482
+ let mut marker = None;
436
483
  loop {
437
- let marker_str = marker.clone().unwrap_or_default();
438
- let mut params: Vec<(&str, &str)> = vec![
439
- ("restype", "container"),
440
- ("comp", "list"),
441
- ("prefix", &search_prefix),
442
- ("delimiter", "/"),
443
- ];
444
- if !marker_str.is_empty() {
445
- params.push(("marker", &marker_str));
446
- }
447
- let url = creds.list_url(&params);
448
- let mut headers = Vec::new();
449
- creds.sign("GET", &url, &mut headers, Some(&params));
450
- let (status, body) = do_request_light("GET", &url, &headers, None, use_h2)
451
- .await
452
- .map_err(|e| PyRuntimeError::new_err(e))?;
453
- if !status.is_success() {
454
- return Err(status_to_pyerr(
455
- status,
456
- &format!("az://{}/{}", creds.container, search_prefix),
457
- &body,
458
- ));
459
- }
460
- marker = parse_az_listing_page(&body, &mut listing);
484
+ marker = az_list_page(creds, &search_prefix, true, marker, use_h2, &mut listing).await?;
461
485
  if marker.is_none() {
462
- break;
486
+ return Ok(listing.finish());
463
487
  }
464
488
  }
489
+ }
490
+
491
+ impl crate::backend::TreeDelete for AzCreds {
492
+ async fn list_keys_page(
493
+ &self,
494
+ search_prefix: &str,
495
+ token: Option<String>,
496
+ use_h2: bool,
497
+ ) -> Result<(Vec<String>, Option<String>), PyErr> {
498
+ let mut keys = Keys::default();
499
+ let next = az_list_page(self, search_prefix, false, token, use_h2, &mut keys).await?;
500
+ Ok((keys.0, next))
501
+ }
465
502
 
466
- Ok(listing.finish())
503
+ /// One Blob Batch request per 256 blobs.
504
+ async fn delete_keys(&self, keys: Vec<String>, use_h2: bool) -> Result<(), PyErr> {
505
+ let batches: Vec<_> = keys
506
+ .chunks(AZ_BATCH_MAX)
507
+ .map(|blobs| az_batch_delete(self, blobs, use_h2))
508
+ .collect();
509
+ crate::backend::run_batches(batches).await
510
+ }
467
511
  }
468
512
 
469
- fn parse_az_listing_page(body: &[u8], listing: &mut Listing) -> Option<String> {
513
+ /// Sub-requests per Blob Batch request (the Azure maximum).
514
+ const AZ_BATCH_MAX: usize = 256;
515
+
516
+ /// Delete up to 256 blobs with one container-scoped Blob Batch request.
517
+ /// Each sub-request is authorized on its own and carries no `x-ms-version`:
518
+ /// the batch's version applies.
519
+ async fn az_batch_delete(creds: &AzCreds, blobs: &[String], use_h2: bool) -> Result<(), PyErr> {
520
+ let requests: Vec<String> = blobs
521
+ .iter()
522
+ .map(|blob| {
523
+ let url = creds.blob_url(blob);
524
+ let mut headers = vec![
525
+ ("x-ms-date".to_string(), chrono_now_rfc2822()),
526
+ ("content-length".to_string(), "0".to_string()),
527
+ ];
528
+ creds.sign_headers("DELETE", &url, &mut headers, None);
529
+ let mut request = format!("DELETE {} HTTP/1.1", crate::multipart::path_of(&url));
530
+ for (name, value) in &headers {
531
+ request.push_str(&format!("\r\n{name}: {value}"));
532
+ }
533
+ request
534
+ })
535
+ .collect();
536
+ let boundary = crate::multipart::boundary();
537
+ let body = crate::multipart::body(&boundary, &requests);
538
+ let params = [("restype", "container"), ("comp", "batch")];
539
+ let url = creds.list_url(&params);
540
+ let mut headers = vec![
541
+ (
542
+ "content-type".to_string(),
543
+ format!("multipart/mixed; boundary={boundary}"),
544
+ ),
545
+ ("content-length".to_string(), body.len().to_string()),
546
+ ];
547
+ creds.sign("POST", &url, &mut headers, Some(&params));
548
+ let (status, response) =
549
+ do_request_light("POST", &url, &headers, Some(Bytes::from(body)), use_h2)
550
+ .await
551
+ .map_err(PyRuntimeError::new_err)?;
552
+ if !status.is_success() {
553
+ return Err(status_to_pyerr(
554
+ status,
555
+ &format!("az://{}", creds.container),
556
+ &response,
557
+ ));
558
+ }
559
+ let statuses = crate::multipart::statuses(&response);
560
+ crate::multipart::check_deletes(statuses, blobs, |blob| creds.error_path(blob))
561
+ }
562
+
563
+ fn parse_az_listing_page(body: &[u8], listing: &mut impl ListSink) -> Option<String> {
564
+ // Untrimmed: blob names may start or end with spaces. Text is only read
565
+ // inside leaf elements, so whitespace between elements is never used.
470
566
  let mut reader = Reader::from_reader(body);
471
- reader.config_mut().trim_text(true);
472
567
  let mut buf = Vec::new();
473
568
  let mut text = String::new();
474
569
  let mut in_blob = false;
@@ -495,6 +590,7 @@ fn parse_az_listing_page(body: &[u8], listing: &mut Listing) -> Option<String> {
495
590
  text.clear();
496
591
  }
497
592
  Ok(Event::Text(e)) => text.push_str(&e.xml10_content()),
593
+ Ok(Event::GeneralRef(e)) => crate::xml::push_reference(&mut text, &e),
498
594
  Ok(Event::End(e)) => {
499
595
  let tag = e.name();
500
596
  match local_name(tag.as_ref()) {
@@ -1039,6 +1135,25 @@ pub fn az_delete_batch<'py>(
1039
1135
  })
1040
1136
  }
1041
1137
 
1138
+ /// Delete every blob under `prefix`.
1139
+ #[pyfunction]
1140
+ pub fn az_delete_tree<'py>(
1141
+ py: Python<'py>,
1142
+ endpoint: String,
1143
+ container: String,
1144
+ prefix: String,
1145
+ account_name: String,
1146
+ account_key: Option<String>,
1147
+ sas_token: Option<String>,
1148
+ use_h2: Option<bool>,
1149
+ ) -> PyResult<Bound<'py, PyAny>> {
1150
+ let h2 = use_h2.unwrap_or(false);
1151
+ pyo3_async_runtimes::tokio::future_into_py(py, async move {
1152
+ let creds = az_creds!(endpoint, container, account_name, account_key, sas_token);
1153
+ crate::backend::TreeDelete::delete_tree(&creds, &prefix, h2).await
1154
+ })
1155
+ }
1156
+
1042
1157
  #[pyfunction]
1043
1158
  pub fn az_exists_batch<'py>(
1044
1159
  py: Python<'py>,
@@ -1190,4 +1305,17 @@ mod tests {
1190
1305
  ]
1191
1306
  );
1192
1307
  }
1308
+
1309
+ #[test]
1310
+ fn listing_keeps_escaped_characters_and_edge_spaces() {
1311
+ let body = br#"<EnumerationResults><Blobs>
1312
+ <Blob><Name>dir/a &amp; b&#46;txt</Name></Blob>
1313
+ <Blob>
1314
+ <Name> dir/ lead &quot;x&quot; </Name>
1315
+ </Blob>
1316
+ </Blobs><NextMarker /></EnumerationResults>"#;
1317
+ let mut keys = super::Keys::default();
1318
+ assert_eq!(parse_az_listing_page(body, &mut keys), None);
1319
+ assert_eq!(keys.0, vec!["dir/a & b.txt", " dir/ lead \"x\" "]);
1320
+ }
1193
1321
  }
@@ -13,6 +13,7 @@
13
13
  * https://opensource.org/licenses/MIT.
14
14
  */
15
15
  use bytes::Bytes;
16
+ use futures::{StreamExt, TryStreamExt};
16
17
  use pyo3::PyErr;
17
18
  use pyo3::exceptions::PyRuntimeError;
18
19
  use pyo3::exceptions::PyValueError;
@@ -281,6 +282,105 @@ pub trait CloudBackend {
281
282
  ) -> impl std::future::Future<Output = Result<(), PyErr>>;
282
283
  }
283
284
 
285
+ /// Object DELETEs in flight per tree delete. An unbounded fan-out opens one
286
+ /// connection per object; at a 20 ms round trip, 64 deletes about 3,200 keys a
287
+ /// second, under S3's 3,500 writes per second per prefix.
288
+ pub const TREE_DELETE_CONCURRENCY: usize = 64;
289
+
290
+ /// Batch requests in flight per tree-delete page: a 1,000-key GCS page is 10
291
+ /// batches of 100. 1/2/4/8 in flight took 0.37/0.21/0.17/0.15 s for 1,000
292
+ /// objects at a 20 ms round trip; 4 keeps at most 400 deletes in flight.
293
+ pub const BATCHES_IN_FLIGHT: usize = 4;
294
+
295
+ /// Run `batches` (one future per batch request), at most
296
+ /// [`BATCHES_IN_FLIGHT`] at a time.
297
+ pub async fn run_batches<Fut>(batches: Vec<Fut>) -> Result<(), PyErr>
298
+ where
299
+ Fut: std::future::Future<Output = Result<(), PyErr>>,
300
+ {
301
+ futures::stream::iter(batches)
302
+ .buffer_unordered(BATCHES_IN_FLIGHT)
303
+ .try_for_each(|()| async { Ok(()) })
304
+ .await
305
+ }
306
+
307
+ /// One DELETE per key, at most [`TREE_DELETE_CONCURRENCY`] in flight; a key
308
+ /// that is already gone counts as deleted.
309
+ pub async fn delete_each<B: CloudBackend + ?Sized>(
310
+ backend: &B,
311
+ keys: Vec<String>,
312
+ use_h2: bool,
313
+ ) -> Result<(), PyErr> {
314
+ futures::stream::iter(keys)
315
+ .map(|key| async move {
316
+ let (status, body) = backend
317
+ .do_request("DELETE", &key, None, use_h2, &[])
318
+ .await
319
+ .map_err(PyRuntimeError::new_err)?;
320
+ if status.is_success() || status == StatusCode::NOT_FOUND {
321
+ Ok(())
322
+ } else {
323
+ Err(status_to_pyerr(status, &backend.error_path(&key), &body))
324
+ }
325
+ })
326
+ .buffer_unordered(TREE_DELETE_CONCURRENCY)
327
+ .try_for_each(|()| async { Ok(()) })
328
+ .await
329
+ }
330
+
331
+ /// Recursive delete for object stores addressed by key prefix.
332
+ pub trait TreeDelete: CloudBackend {
333
+ /// One page of every object key under `search_prefix`, listed without a
334
+ /// delimiter, and the token of the next page.
335
+ fn list_keys_page(
336
+ &self,
337
+ search_prefix: &str,
338
+ token: Option<String>,
339
+ use_h2: bool,
340
+ ) -> impl std::future::Future<Output = Result<(Vec<String>, Option<String>), PyErr>>;
341
+
342
+ /// Delete `keys`; a key that is already gone counts as deleted.
343
+ fn delete_keys(
344
+ &self,
345
+ keys: Vec<String>,
346
+ use_h2: bool,
347
+ ) -> impl std::future::Future<Output = Result<(), PyErr>> {
348
+ delete_each(self, keys, use_h2)
349
+ }
350
+
351
+ /// Delete every object under `prefix` (marker keys included), deleting
352
+ /// each listing page while the next one is fetched. Returns the keys left
353
+ /// in place because a request path could not address them (see
354
+ /// [`crate::listing::plain_key`]).
355
+ fn delete_tree(
356
+ &self,
357
+ prefix: &str,
358
+ use_h2: bool,
359
+ ) -> impl std::future::Future<Output = Result<Vec<String>, PyErr>> {
360
+ async move {
361
+ let search = crate::listing::search_prefix(prefix);
362
+ let mut skipped = Vec::new();
363
+ let (mut keys, mut token) = self.list_keys_page(&search, None, use_h2).await?;
364
+ loop {
365
+ let (plain, other): (Vec<_>, Vec<_>) = std::mem::take(&mut keys)
366
+ .into_iter()
367
+ .partition(|key| crate::listing::plain_key(key, &search));
368
+ skipped.extend(other);
369
+ let Some(next) = token.take() else {
370
+ self.delete_keys(plain, use_h2).await?;
371
+ return Ok(skipped);
372
+ };
373
+ let (deleted, listed) = tokio::join!(
374
+ self.delete_keys(plain, use_h2),
375
+ self.list_keys_page(&search, Some(next), use_h2),
376
+ );
377
+ deleted?;
378
+ (keys, token) = listed?;
379
+ }
380
+ }
381
+ }
382
+ }
383
+
284
384
  // ---------------------------------------------------------------------------
285
385
  // Batch macros — eliminate repetitive join_all boilerplate in pyfunctions
286
386
  // ---------------------------------------------------------------------------