asanypath-native 2.3.2__tar.gz → 2.4.0__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.
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/Cargo.lock +13 -13
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/Cargo.toml +2 -2
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/PKG-INFO +1 -1
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/asanypath_native.pyi +0 -3
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/pyproject.toml +10 -0
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/src/artifactory.rs +77 -30
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/src/azure.rs +88 -123
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/src/gcs.rs +17 -67
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/src/lib.rs +1 -3
- asanypath_native-2.4.0/src/listing.rs +131 -0
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/src/s3.rs +73 -140
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/src/backend.rs +0 -0
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/src/cache.rs +0 -0
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/src/http.rs +0 -0
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/src/ssh.rs +0 -0
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/src/xml.rs +0 -0
- {asanypath_native-2.3.2 → asanypath_native-2.4.0}/uv.lock +0 -0
|
@@ -96,7 +96,7 @@ dependencies = [
|
|
|
96
96
|
|
|
97
97
|
[[package]]
|
|
98
98
|
name = "asanypath-native"
|
|
99
|
-
version = "2.
|
|
99
|
+
version = "2.4.0"
|
|
100
100
|
dependencies = [
|
|
101
101
|
"anyhow",
|
|
102
102
|
"aws-config",
|
|
@@ -2496,9 +2496,9 @@ dependencies = [
|
|
|
2496
2496
|
|
|
2497
2497
|
[[package]]
|
|
2498
2498
|
name = "pyo3"
|
|
2499
|
-
version = "0.29.
|
|
2499
|
+
version = "0.29.3"
|
|
2500
2500
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
2501
|
-
checksum = "
|
|
2501
|
+
checksum = "700d18fa267b73b9b521fd7e13580e2f446916f176cacee1ab63fcc8191f1655"
|
|
2502
2502
|
dependencies = [
|
|
2503
2503
|
"libc",
|
|
2504
2504
|
"once_cell",
|
|
@@ -2524,18 +2524,18 @@ dependencies = [
|
|
|
2524
2524
|
|
|
2525
2525
|
[[package]]
|
|
2526
2526
|
name = "pyo3-build-config"
|
|
2527
|
-
version = "0.29.
|
|
2527
|
+
version = "0.29.3"
|
|
2528
2528
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
2529
|
-
checksum = "
|
|
2529
|
+
checksum = "7b3fc0c4d08f6bb10e71fe39dfb9e2f59c6eb6854e22ec8092f50c69a4499adb"
|
|
2530
2530
|
dependencies = [
|
|
2531
2531
|
"target-lexicon",
|
|
2532
2532
|
]
|
|
2533
2533
|
|
|
2534
2534
|
[[package]]
|
|
2535
2535
|
name = "pyo3-ffi"
|
|
2536
|
-
version = "0.29.
|
|
2536
|
+
version = "0.29.3"
|
|
2537
2537
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
2538
|
-
checksum = "
|
|
2538
|
+
checksum = "dfc0b8e19df29aad7086cf977bb0c2a2f143e30567eb113e9cf72b62ca698330"
|
|
2539
2539
|
dependencies = [
|
|
2540
2540
|
"libc",
|
|
2541
2541
|
"pyo3-build-config",
|
|
@@ -2543,9 +2543,9 @@ dependencies = [
|
|
|
2543
2543
|
|
|
2544
2544
|
[[package]]
|
|
2545
2545
|
name = "pyo3-macros"
|
|
2546
|
-
version = "0.29.
|
|
2546
|
+
version = "0.29.3"
|
|
2547
2547
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
2548
|
-
checksum = "
|
|
2548
|
+
checksum = "6100e8a4b5eba53afaa5ed078364851a0b2499c553a44c31026b929049b49dc6"
|
|
2549
2549
|
dependencies = [
|
|
2550
2550
|
"proc-macro2",
|
|
2551
2551
|
"pyo3-macros-backend",
|
|
@@ -2555,9 +2555,9 @@ dependencies = [
|
|
|
2555
2555
|
|
|
2556
2556
|
[[package]]
|
|
2557
2557
|
name = "pyo3-macros-backend"
|
|
2558
|
-
version = "0.29.
|
|
2558
|
+
version = "0.29.3"
|
|
2559
2559
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
2560
|
-
checksum = "
|
|
2560
|
+
checksum = "6143877a16e82b5a727b7127ff4cd86858a24a28d745d72f43e6f227c7b1bdb3"
|
|
2561
2561
|
dependencies = [
|
|
2562
2562
|
"heck",
|
|
2563
2563
|
"proc-macro2",
|
|
@@ -2878,9 +2878,9 @@ dependencies = [
|
|
|
2878
2878
|
|
|
2879
2879
|
[[package]]
|
|
2880
2880
|
name = "russh"
|
|
2881
|
-
version = "0.
|
|
2881
|
+
version = "0.64.1"
|
|
2882
2882
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
2883
|
-
checksum = "
|
|
2883
|
+
checksum = "ba61e87b9ec9a39a59a6bbed4c0b8ff7fb07073405b24a767a1f2c8560ac8f58"
|
|
2884
2884
|
dependencies = [
|
|
2885
2885
|
"aes",
|
|
2886
2886
|
"aws-lc-rs",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "asanypath-native"
|
|
3
|
-
version = "2.
|
|
3
|
+
version = "2.4.0"
|
|
4
4
|
edition = "2024"
|
|
5
5
|
license = "MIT"
|
|
6
6
|
publish = false
|
|
@@ -32,7 +32,7 @@ ring = "0.17"
|
|
|
32
32
|
pem = "4"
|
|
33
33
|
scraper = { version = "0.27", default-features = false }
|
|
34
34
|
url = "2"
|
|
35
|
-
russh = "0.
|
|
35
|
+
russh = "0.64"
|
|
36
36
|
russh-sftp = "3"
|
|
37
37
|
tokio-util = { version = "0.7", features = ["io"] }
|
|
38
38
|
anyhow = "1"
|
|
@@ -31,7 +31,6 @@ async def s3_exists_batch(*args: Any, **kwargs: Any) -> Any: ...
|
|
|
31
31
|
async def s3_list_batch(*args: Any, **kwargs: Any) -> Any: ...
|
|
32
32
|
async def s3_head_batch(*args: Any, **kwargs: Any) -> Any: ...
|
|
33
33
|
async def s3_is_dir(*args: Any, **kwargs: Any) -> Any: ...
|
|
34
|
-
async def s3_is_dir_batch(*args: Any, **kwargs: Any) -> Any: ...
|
|
35
34
|
async def s3_list_buckets(*args: Any, **kwargs: Any) -> Any: ...
|
|
36
35
|
async def s3_get_acl(*args: Any, **kwargs: Any) -> Any: ...
|
|
37
36
|
async def s3_put_acl(*args: Any, **kwargs: Any) -> Any: ...
|
|
@@ -66,7 +65,6 @@ async def gcs_list_batch(*args: Any, **kwargs: Any) -> Any: ...
|
|
|
66
65
|
async def gcs_head(*args: Any, **kwargs: Any) -> Any: ...
|
|
67
66
|
async def gcs_head_batch(*args: Any, **kwargs: Any) -> Any: ...
|
|
68
67
|
async def gcs_is_dir(*args: Any, **kwargs: Any) -> Any: ...
|
|
69
|
-
async def gcs_is_dir_batch(*args: Any, **kwargs: Any) -> Any: ...
|
|
70
68
|
async def gcs_list_buckets(*args: Any, **kwargs: Any) -> Any: ...
|
|
71
69
|
def gcs_presign(
|
|
72
70
|
bucket: str,
|
|
@@ -87,7 +85,6 @@ async def az_exists_batch(*args: Any, **kwargs: Any) -> Any: ...
|
|
|
87
85
|
async def az_list_batch(*args: Any, **kwargs: Any) -> Any: ...
|
|
88
86
|
async def az_head_batch(*args: Any, **kwargs: Any) -> Any: ...
|
|
89
87
|
async def az_is_dir(*args: Any, **kwargs: Any) -> Any: ...
|
|
90
|
-
async def az_is_dir_batch(*args: Any, **kwargs: Any) -> Any: ...
|
|
91
88
|
async def az_list_containers(*args: Any, **kwargs: Any) -> Any: ...
|
|
92
89
|
def az_presign(
|
|
93
90
|
endpoint: str,
|
|
@@ -13,3 +13,13 @@ license = "MIT"
|
|
|
13
13
|
manifest-path = "Cargo.toml"
|
|
14
14
|
module-name = "asanypath_native"
|
|
15
15
|
features = ["pyo3/extension-module"]
|
|
16
|
+
|
|
17
|
+
[tool.uv]
|
|
18
|
+
# Rebuild local installs when native sources change; the version alone does not.
|
|
19
|
+
cache-keys = [
|
|
20
|
+
{ file = "pyproject.toml" },
|
|
21
|
+
{ file = "Cargo.toml" },
|
|
22
|
+
{ file = "Cargo.lock" },
|
|
23
|
+
{ file = "asanypath_native.pyi" },
|
|
24
|
+
{ file = "src/**/*.rs" },
|
|
25
|
+
]
|
|
@@ -56,40 +56,95 @@ struct RepoEntry {
|
|
|
56
56
|
// ---------------------------------------------------------------------------
|
|
57
57
|
|
|
58
58
|
pub(crate) struct ArtCreds {
|
|
59
|
-
///
|
|
60
|
-
|
|
59
|
+
/// Complete service URL, e.g. "https://artifacts.example.com/artifactory".
|
|
60
|
+
endpoint_url: String,
|
|
61
61
|
token: String,
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
#[cfg(test)]
|
|
65
|
+
mod endpoint_tests {
|
|
66
|
+
use super::ArtCreds;
|
|
67
|
+
|
|
68
|
+
#[test]
|
|
69
|
+
fn complete_and_legacy_endpoints_build_the_same_urls() {
|
|
70
|
+
for endpoint in [
|
|
71
|
+
"example.com/artifactory",
|
|
72
|
+
"https://example.com/artifactory",
|
|
73
|
+
"https://example.com/artifactory/",
|
|
74
|
+
] {
|
|
75
|
+
let creds = ArtCreds::new(endpoint.into(), "token".into());
|
|
76
|
+
assert_eq!(
|
|
77
|
+
creds.content_url("/repo/key"),
|
|
78
|
+
"https://example.com/artifactory/repo/key"
|
|
79
|
+
);
|
|
80
|
+
assert_eq!(
|
|
81
|
+
creds.storage_url("/repo/key"),
|
|
82
|
+
"https://example.com/artifactory/api/storage/repo/key"
|
|
83
|
+
);
|
|
84
|
+
assert_eq!(
|
|
85
|
+
creds.permission_target_url("read all"),
|
|
86
|
+
"https://example.com/artifactory/api/security/permissions/read%20all"
|
|
87
|
+
);
|
|
88
|
+
assert_eq!(
|
|
89
|
+
creds.path_url("repo/key"),
|
|
90
|
+
"art://example.com/artifactory/repo/key"
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
#[test]
|
|
96
|
+
fn explicit_http_endpoint_is_preserved() {
|
|
97
|
+
let creds = ArtCreds::new("http://localhost:8080/artifactory".into(), "token".into());
|
|
98
|
+
assert_eq!(
|
|
99
|
+
creds.content_url("repo/key"),
|
|
100
|
+
"http://localhost:8080/artifactory/repo/key"
|
|
101
|
+
);
|
|
102
|
+
assert_eq!(
|
|
103
|
+
creds.path_url("repo/key"),
|
|
104
|
+
"art://localhost:8080/artifactory/repo/key"
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
64
109
|
impl ArtCreds {
|
|
65
110
|
pub(crate) fn new(base_url: String, token: String) -> Self {
|
|
66
|
-
|
|
111
|
+
// Accept legacy native callers passing a scheme-less base_url.
|
|
112
|
+
let endpoint_url = if base_url.starts_with("https://") || base_url.starts_with("http://") {
|
|
113
|
+
base_url
|
|
114
|
+
} else {
|
|
115
|
+
format!("https://{}", base_url)
|
|
116
|
+
};
|
|
117
|
+
Self {
|
|
118
|
+
endpoint_url: endpoint_url.trim_end_matches('/').to_string(),
|
|
119
|
+
token,
|
|
120
|
+
}
|
|
67
121
|
}
|
|
68
122
|
|
|
69
123
|
fn auth_headers(&self) -> Vec<(String, String)> {
|
|
70
124
|
bearer_headers(&self.token)
|
|
71
125
|
}
|
|
72
126
|
|
|
73
|
-
/// Build the content URL: https://{base_url}/{path}
|
|
74
127
|
fn content_url(&self, path: &str) -> String {
|
|
75
|
-
format!("
|
|
128
|
+
format!("{}/{}", self.endpoint_url, path.trim_start_matches('/'))
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
fn path_url(&self, path: &str) -> String {
|
|
132
|
+
let base = self.endpoint_url.split_once("://").unwrap().1;
|
|
133
|
+
format!("art://{}/{}", base, path.trim_start_matches('/'))
|
|
76
134
|
}
|
|
77
135
|
|
|
78
136
|
/// Build the storage API URL: https://{base}/api/storage/{repo_path}
|
|
79
137
|
/// Input path is like "repo/folder/file.txt"
|
|
80
138
|
fn storage_url(&self, path: &str) -> String {
|
|
81
|
-
|
|
82
|
-
// base_url = "host/artifactory", path = "repo/path"
|
|
83
|
-
// result = "https://host/artifactory/api/storage/repo/path"
|
|
84
|
-
let base = self.base_url.trim_end_matches('/');
|
|
139
|
+
let base = &self.endpoint_url;
|
|
85
140
|
let trimmed_path = path.trim_start_matches('/');
|
|
86
|
-
format!("
|
|
141
|
+
format!("{}/api/storage/{}", base, trimmed_path)
|
|
87
142
|
}
|
|
88
143
|
|
|
89
144
|
fn permission_target_url(&self, name: &str) -> String {
|
|
90
|
-
let base = self.
|
|
145
|
+
let base = &self.endpoint_url;
|
|
91
146
|
format!(
|
|
92
|
-
"
|
|
147
|
+
"{}/api/security/permissions/{}",
|
|
93
148
|
base,
|
|
94
149
|
urlencoding::encode(name)
|
|
95
150
|
)
|
|
@@ -98,10 +153,7 @@ impl ArtCreds {
|
|
|
98
153
|
|
|
99
154
|
macro_rules! art_creds {
|
|
100
155
|
($base_url:expr, $token:expr) => {
|
|
101
|
-
ArtCreds
|
|
102
|
-
base_url: $base_url,
|
|
103
|
-
token: $token,
|
|
104
|
-
}
|
|
156
|
+
ArtCreds::new($base_url, $token)
|
|
105
157
|
};
|
|
106
158
|
}
|
|
107
159
|
|
|
@@ -148,7 +200,7 @@ impl CloudBackend for ArtCreds {
|
|
|
148
200
|
}
|
|
149
201
|
|
|
150
202
|
fn error_path(&self, path: &str) -> String {
|
|
151
|
-
|
|
203
|
+
self.path_url(path)
|
|
152
204
|
}
|
|
153
205
|
|
|
154
206
|
fn put(
|
|
@@ -189,11 +241,11 @@ impl CloudBackend for ArtCreds {
|
|
|
189
241
|
let options = options.clone();
|
|
190
242
|
async move {
|
|
191
243
|
crate::backend::require_empty_provider(&options)?;
|
|
192
|
-
let base = self.
|
|
244
|
+
let base = &self.endpoint_url;
|
|
193
245
|
let src_trim = src.trim_start_matches('/');
|
|
194
246
|
let dst_trim = dst.trim_start_matches('/');
|
|
195
247
|
let url = crate::backend::append_query(
|
|
196
|
-
&format!("
|
|
248
|
+
&format!("{}/api/copy/{}?to=/{}", base, src_trim, dst_trim),
|
|
197
249
|
&options.query,
|
|
198
250
|
);
|
|
199
251
|
let mut headers = self.auth_headers();
|
|
@@ -285,11 +337,7 @@ async fn do_art_storage_info(
|
|
|
285
337
|
.await
|
|
286
338
|
.map_err(|e| PyRuntimeError::new_err(e))?;
|
|
287
339
|
if !status.is_success() {
|
|
288
|
-
return Err(status_to_pyerr(
|
|
289
|
-
status,
|
|
290
|
-
&format!("art://{}/{}", creds.base_url, path),
|
|
291
|
-
&body,
|
|
292
|
-
));
|
|
340
|
+
return Err(status_to_pyerr(status, &creds.path_url(path), &body));
|
|
293
341
|
}
|
|
294
342
|
let info: StorageInfo = sonic_rs::from_slice(&body)
|
|
295
343
|
.map_err(|e| PyRuntimeError::new_err(format!("JSON parse error: {e}")))?;
|
|
@@ -304,7 +352,7 @@ async fn do_art_list(
|
|
|
304
352
|
let info = do_art_storage_info(creds, path, use_h2).await?;
|
|
305
353
|
match info.children {
|
|
306
354
|
Some(children) => {
|
|
307
|
-
let base =
|
|
355
|
+
let base = creds.path_url(path.trim_end_matches('/'));
|
|
308
356
|
let results: Vec<(String, bool)> = children
|
|
309
357
|
.iter()
|
|
310
358
|
.map(|child| {
|
|
@@ -315,16 +363,15 @@ async fn do_art_list(
|
|
|
315
363
|
Ok(results)
|
|
316
364
|
}
|
|
317
365
|
None => Err(pyo3::exceptions::PyNotADirectoryError::new_err(format!(
|
|
318
|
-
"Not a directory: '
|
|
319
|
-
creds.
|
|
366
|
+
"Not a directory: '{}'",
|
|
367
|
+
creds.path_url(path)
|
|
320
368
|
))),
|
|
321
369
|
}
|
|
322
370
|
}
|
|
323
371
|
|
|
324
372
|
/// List all repositories (GET /api/repositories). Returns bare repository keys.
|
|
325
373
|
async fn do_art_list_repos(creds: &ArtCreds, use_h2: bool) -> Result<Vec<String>, PyErr> {
|
|
326
|
-
let
|
|
327
|
-
let url = format!("https://{}/api/repositories", base);
|
|
374
|
+
let url = format!("{}/api/repositories", creds.endpoint_url);
|
|
328
375
|
let headers = creds.auth_headers();
|
|
329
376
|
let (status, body) = do_request_light("GET", &url, &headers, None, use_h2)
|
|
330
377
|
.await
|
|
@@ -332,7 +379,7 @@ async fn do_art_list_repos(creds: &ArtCreds, use_h2: bool) -> Result<Vec<String>
|
|
|
332
379
|
if !status.is_success() {
|
|
333
380
|
return Err(status_to_pyerr(
|
|
334
381
|
status,
|
|
335
|
-
|
|
382
|
+
creds.path_url("").trim_end_matches('/'),
|
|
336
383
|
&body,
|
|
337
384
|
));
|
|
338
385
|
}
|
|
@@ -21,9 +21,9 @@ use quick_xml::Reader;
|
|
|
21
21
|
use quick_xml::events::Event;
|
|
22
22
|
use serde::{Deserialize, Serialize};
|
|
23
23
|
use sha2::Sha256;
|
|
24
|
-
use std::collections::HashSet;
|
|
25
24
|
|
|
26
25
|
use crate::http::{do_request, do_request_light, status_to_pyerr};
|
|
26
|
+
use crate::listing::{ListEntry, Listing};
|
|
27
27
|
use crate::xml::local_name;
|
|
28
28
|
|
|
29
29
|
type HmacSha256 = Hmac<Sha256>;
|
|
@@ -428,16 +428,9 @@ async fn do_az_is_dir(creds: &AzCreds, prefix: &str, use_h2: bool) -> Result<boo
|
|
|
428
428
|
Ok(false)
|
|
429
429
|
}
|
|
430
430
|
|
|
431
|
-
async fn do_az_list(creds: &AzCreds, prefix: &str, use_h2: bool) -> Result<Vec<
|
|
432
|
-
let
|
|
433
|
-
let search_prefix =
|
|
434
|
-
String::new()
|
|
435
|
-
} else {
|
|
436
|
-
format!("{}/", current_key)
|
|
437
|
-
};
|
|
438
|
-
|
|
439
|
-
let mut found_all = HashSet::new();
|
|
440
|
-
let mut uris = Vec::new();
|
|
431
|
+
async fn do_az_list(creds: &AzCreds, prefix: &str, use_h2: bool) -> Result<Vec<ListEntry>, PyErr> {
|
|
432
|
+
let mut listing = Listing::new(format!("az://{}", creds.container), prefix);
|
|
433
|
+
let search_prefix = listing.search_prefix().to_string();
|
|
441
434
|
let mut marker: Option<String> = None;
|
|
442
435
|
|
|
443
436
|
loop {
|
|
@@ -464,100 +457,68 @@ async fn do_az_list(creds: &AzCreds, prefix: &str, use_h2: bool) -> Result<Vec<S
|
|
|
464
457
|
&body,
|
|
465
458
|
));
|
|
466
459
|
}
|
|
460
|
+
marker = parse_az_listing_page(&body, &mut listing);
|
|
461
|
+
if marker.is_none() {
|
|
462
|
+
break;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
467
465
|
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
reader.config_mut().trim_text(true);
|
|
471
|
-
let mut buf = Vec::new();
|
|
472
|
-
let mut next_marker: Option<String> = None;
|
|
473
|
-
let mut in_blob_prefix = false;
|
|
466
|
+
Ok(listing.finish())
|
|
467
|
+
}
|
|
474
468
|
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
}
|
|
498
|
-
} else {
|
|
499
|
-
// Regular blob
|
|
500
|
-
let name = &text;
|
|
501
|
-
if name.is_empty() || name.trim_end_matches('/') == current_key {
|
|
502
|
-
// skip
|
|
503
|
-
} else {
|
|
504
|
-
let relative = if !search_prefix.is_empty()
|
|
505
|
-
&& name.starts_with(&search_prefix)
|
|
506
|
-
{
|
|
507
|
-
&name[search_prefix.len()..]
|
|
508
|
-
} else {
|
|
509
|
-
name.as_str()
|
|
510
|
-
};
|
|
511
|
-
let relative = relative.trim_start_matches('/');
|
|
512
|
-
let resolved = if !relative.is_empty() {
|
|
513
|
-
let first = relative.split('/').next().unwrap_or(relative);
|
|
514
|
-
format!("{}{}", search_prefix, first)
|
|
515
|
-
} else {
|
|
516
|
-
name.to_string()
|
|
517
|
-
};
|
|
518
|
-
let value = resolved.trim_end_matches('/').to_string();
|
|
519
|
-
if !value.is_empty()
|
|
520
|
-
&& value != current_key
|
|
521
|
-
&& found_all.insert(value.clone())
|
|
522
|
-
{
|
|
523
|
-
uris.push(format!("az://{}/{}", creds.container, value));
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
"NextMarker" => {
|
|
529
|
-
let text = reader
|
|
530
|
-
.read_text(e.name())
|
|
531
|
-
.ok()
|
|
532
|
-
.map(|text| text.xml10_content().into_owned())
|
|
533
|
-
.unwrap_or_default();
|
|
534
|
-
if !text.is_empty() {
|
|
535
|
-
next_marker = Some(text);
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
_ => {}
|
|
469
|
+
fn parse_az_listing_page(body: &[u8], listing: &mut Listing) -> Option<String> {
|
|
470
|
+
let mut reader = Reader::from_reader(body);
|
|
471
|
+
reader.config_mut().trim_text(true);
|
|
472
|
+
let mut buf = Vec::new();
|
|
473
|
+
let mut text = String::new();
|
|
474
|
+
let mut in_blob = false;
|
|
475
|
+
let mut in_prefix = false;
|
|
476
|
+
let mut name = String::new();
|
|
477
|
+
let mut size = None;
|
|
478
|
+
let mut modified = None;
|
|
479
|
+
let mut next_marker = None;
|
|
480
|
+
|
|
481
|
+
loop {
|
|
482
|
+
match reader.read_event_into(&mut buf) {
|
|
483
|
+
Ok(Event::Start(e)) => {
|
|
484
|
+
let tag = e.name();
|
|
485
|
+
match local_name(tag.as_ref()) {
|
|
486
|
+
"Blob" => {
|
|
487
|
+
in_blob = true;
|
|
488
|
+
name.clear();
|
|
489
|
+
size = None;
|
|
490
|
+
modified = None;
|
|
539
491
|
}
|
|
492
|
+
"BlobPrefix" => in_prefix = true,
|
|
493
|
+
_ => {}
|
|
540
494
|
}
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
495
|
+
text.clear();
|
|
496
|
+
}
|
|
497
|
+
Ok(Event::Text(e)) => text.push_str(&e.xml10_content()),
|
|
498
|
+
Ok(Event::End(e)) => {
|
|
499
|
+
let tag = e.name();
|
|
500
|
+
match local_name(tag.as_ref()) {
|
|
501
|
+
"Name" if in_prefix => listing.add_prefix(&text),
|
|
502
|
+
"Name" if in_blob => name = std::mem::take(&mut text),
|
|
503
|
+
"Content-Length" if in_blob => size = text.parse().ok(),
|
|
504
|
+
"Last-Modified" if in_blob => modified = Some(std::mem::take(&mut text)),
|
|
505
|
+
"Blob" => {
|
|
506
|
+
in_blob = false;
|
|
507
|
+
listing.add_object(&name, size, modified.take());
|
|
545
508
|
}
|
|
509
|
+
"BlobPrefix" => in_prefix = false,
|
|
510
|
+
"NextMarker" if !text.is_empty() => {
|
|
511
|
+
next_marker = Some(std::mem::take(&mut text));
|
|
512
|
+
}
|
|
513
|
+
_ => {}
|
|
546
514
|
}
|
|
547
|
-
Ok(Event::Eof) => break,
|
|
548
|
-
Err(_) => break,
|
|
549
|
-
_ => {}
|
|
550
515
|
}
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
marker = next_marker;
|
|
555
|
-
if marker.is_none() {
|
|
556
|
-
break;
|
|
516
|
+
Ok(Event::Eof) | Err(_) => break,
|
|
517
|
+
_ => {}
|
|
557
518
|
}
|
|
519
|
+
buf.clear();
|
|
558
520
|
}
|
|
559
|
-
|
|
560
|
-
Ok(uris)
|
|
521
|
+
next_marker
|
|
561
522
|
}
|
|
562
523
|
|
|
563
524
|
/// List all containers in the account (GET /?comp=list). Returns bare names.
|
|
@@ -1096,33 +1057,6 @@ pub fn az_exists_batch<'py>(
|
|
|
1096
1057
|
})
|
|
1097
1058
|
}
|
|
1098
1059
|
|
|
1099
|
-
#[pyfunction]
|
|
1100
|
-
pub fn az_is_dir_batch<'py>(
|
|
1101
|
-
py: Python<'py>,
|
|
1102
|
-
endpoint: String,
|
|
1103
|
-
container: String,
|
|
1104
|
-
prefixes: Vec<String>,
|
|
1105
|
-
account_name: String,
|
|
1106
|
-
account_key: Option<String>,
|
|
1107
|
-
sas_token: Option<String>,
|
|
1108
|
-
use_h2: Option<bool>,
|
|
1109
|
-
) -> PyResult<Bound<'py, PyAny>> {
|
|
1110
|
-
let h2 = use_h2.unwrap_or(false);
|
|
1111
|
-
pyo3_async_runtimes::tokio::future_into_py(py, async move {
|
|
1112
|
-
let creds = az_creds!(endpoint, container, account_name, account_key, sas_token);
|
|
1113
|
-
let futs: Vec<_> = prefixes
|
|
1114
|
-
.iter()
|
|
1115
|
-
.map(|p| do_az_is_dir(&creds, p, h2))
|
|
1116
|
-
.collect();
|
|
1117
|
-
let results = join_all(futs).await;
|
|
1118
|
-
let mut out = Vec::with_capacity(results.len());
|
|
1119
|
-
for r in results {
|
|
1120
|
-
out.push(r?);
|
|
1121
|
-
}
|
|
1122
|
-
Ok(out)
|
|
1123
|
-
})
|
|
1124
|
-
}
|
|
1125
|
-
|
|
1126
1060
|
// ---------------------------------------------------------------------------
|
|
1127
1061
|
// Presigned URL generation (Service SAS)
|
|
1128
1062
|
// ---------------------------------------------------------------------------
|
|
@@ -1226,3 +1160,34 @@ fn format_iso8601(secs: u64) -> String {
|
|
|
1226
1160
|
year, month, day, hours, minutes, seconds
|
|
1227
1161
|
)
|
|
1228
1162
|
}
|
|
1163
|
+
|
|
1164
|
+
#[cfg(test)]
|
|
1165
|
+
mod tests {
|
|
1166
|
+
use super::{Listing, parse_az_listing_page};
|
|
1167
|
+
|
|
1168
|
+
#[test]
|
|
1169
|
+
fn parses_listing_page_metadata() {
|
|
1170
|
+
let body = br#"<?xml version="1.0" encoding="utf-8"?>
|
|
1171
|
+
<EnumerationResults ContainerName="c"><Prefix>dir/</Prefix><Blobs>
|
|
1172
|
+
<Blob><Name>dir/a.txt</Name><Properties>
|
|
1173
|
+
<Last-Modified>Tue, 02 Jan 2024 03:04:05 GMT</Last-Modified>
|
|
1174
|
+
<Content-Length>42</Content-Length>
|
|
1175
|
+
</Properties></Blob>
|
|
1176
|
+
<BlobPrefix><Name>dir/sub/</Name></BlobPrefix>
|
|
1177
|
+
</Blobs><NextMarker /></EnumerationResults>"#;
|
|
1178
|
+
let mut listing = Listing::new("az://c".into(), "dir");
|
|
1179
|
+
assert_eq!(parse_az_listing_page(body, &mut listing), None);
|
|
1180
|
+
assert_eq!(
|
|
1181
|
+
listing.finish(),
|
|
1182
|
+
vec![
|
|
1183
|
+
(
|
|
1184
|
+
"az://c/dir/a.txt".into(),
|
|
1185
|
+
false,
|
|
1186
|
+
Some(42),
|
|
1187
|
+
Some("Tue, 02 Jan 2024 03:04:05 GMT".into())
|
|
1188
|
+
),
|
|
1189
|
+
("az://c/dir/sub".into(), true, None, None),
|
|
1190
|
+
]
|
|
1191
|
+
);
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
@@ -21,11 +21,12 @@ use pyo3::prelude::*;
|
|
|
21
21
|
use pyo3::pybacked::PyBackedBytes;
|
|
22
22
|
use serde::{Deserialize, Serialize};
|
|
23
23
|
use sonic_rs::JsonValueTrait;
|
|
24
|
-
use std::collections::
|
|
24
|
+
use std::collections::HashMap;
|
|
25
25
|
use std::sync::{Mutex, OnceLock};
|
|
26
26
|
use std::time::{Duration, Instant};
|
|
27
27
|
|
|
28
28
|
use crate::http::{bearer_headers, do_request, do_request_light, status_to_pyerr};
|
|
29
|
+
use crate::listing::{ListEntry, Listing};
|
|
29
30
|
|
|
30
31
|
// ---------------------------------------------------------------------------
|
|
31
32
|
// Typed JSON response structs (avoids Value allocations)
|
|
@@ -44,6 +45,9 @@ struct ListResponse {
|
|
|
44
45
|
#[derive(Deserialize)]
|
|
45
46
|
struct ListItem {
|
|
46
47
|
name: String,
|
|
48
|
+
// The JSON API encodes uint64 values as strings.
|
|
49
|
+
size: Option<String>,
|
|
50
|
+
updated: Option<String>,
|
|
47
51
|
}
|
|
48
52
|
|
|
49
53
|
/// Response for service-level bucket listing (GET /storage/v1/b).
|
|
@@ -651,16 +655,13 @@ async fn do_gcs_put_acl(creds: &GcsCreds, object_path: &str, acl_json: &str) ->
|
|
|
651
655
|
Ok(())
|
|
652
656
|
}
|
|
653
657
|
|
|
654
|
-
async fn do_gcs_list(
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
let mut found_all = HashSet::new();
|
|
663
|
-
let mut uris = Vec::new();
|
|
658
|
+
async fn do_gcs_list(
|
|
659
|
+
creds: &GcsCreds,
|
|
660
|
+
prefix: &str,
|
|
661
|
+
use_h2: bool,
|
|
662
|
+
) -> Result<Vec<ListEntry>, PyErr> {
|
|
663
|
+
let mut listing = Listing::new(format!("gs://{}", creds.bucket), prefix);
|
|
664
|
+
let search_prefix = listing.search_prefix().to_string();
|
|
664
665
|
let mut page_token: Option<String> = None;
|
|
665
666
|
let headers = creds.auth_headers().await;
|
|
666
667
|
|
|
@@ -692,37 +693,12 @@ async fn do_gcs_list(creds: &GcsCreds, prefix: &str, use_h2: bool) -> Result<Vec
|
|
|
692
693
|
|
|
693
694
|
let resp: ListResponse = sonic_rs::from_slice(&body)
|
|
694
695
|
.map_err(|e| PyRuntimeError::new_err(format!("JSON parse error: {e}")))?;
|
|
695
|
-
|
|
696
|
-
// Prefixes (subdirectories)
|
|
697
696
|
for p in &resp.prefixes {
|
|
698
|
-
|
|
699
|
-
if !value.is_empty() && value != current_key && found_all.insert(value.to_string()) {
|
|
700
|
-
uris.push(format!("gs://{}/{}", creds.bucket, value));
|
|
701
|
-
}
|
|
697
|
+
listing.add_prefix(p);
|
|
702
698
|
}
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
let name = &item.name;
|
|
707
|
-
if name.is_empty() || name.trim_end_matches('/') == current_key {
|
|
708
|
-
continue;
|
|
709
|
-
}
|
|
710
|
-
let relative = if !search_prefix.is_empty() && name.starts_with(&search_prefix) {
|
|
711
|
-
&name[search_prefix.len()..]
|
|
712
|
-
} else {
|
|
713
|
-
name.as_str()
|
|
714
|
-
};
|
|
715
|
-
let relative = relative.trim_start_matches('/');
|
|
716
|
-
let resolved = if !relative.is_empty() {
|
|
717
|
-
let first = relative.split('/').next().unwrap_or(relative);
|
|
718
|
-
format!("{}{}", search_prefix, first)
|
|
719
|
-
} else {
|
|
720
|
-
name.to_string()
|
|
721
|
-
};
|
|
722
|
-
let value = resolved.trim_end_matches('/').to_string();
|
|
723
|
-
if !value.is_empty() && value != current_key && found_all.insert(value.clone()) {
|
|
724
|
-
uris.push(format!("gs://{}/{}", creds.bucket, value));
|
|
725
|
-
}
|
|
699
|
+
for item in resp.items {
|
|
700
|
+
let size = item.size.and_then(|s| s.parse().ok());
|
|
701
|
+
listing.add_object(&item.name, size, item.updated);
|
|
726
702
|
}
|
|
727
703
|
|
|
728
704
|
page_token = resp.next_page_token;
|
|
@@ -731,7 +707,7 @@ async fn do_gcs_list(creds: &GcsCreds, prefix: &str, use_h2: bool) -> Result<Vec
|
|
|
731
707
|
}
|
|
732
708
|
}
|
|
733
709
|
|
|
734
|
-
Ok(
|
|
710
|
+
Ok(listing.finish())
|
|
735
711
|
}
|
|
736
712
|
|
|
737
713
|
/// List all buckets in a project (GET /storage/v1/b?project=). Returns bare names.
|
|
@@ -1087,32 +1063,6 @@ pub fn gcs_exists_batch<'py>(
|
|
|
1087
1063
|
})
|
|
1088
1064
|
}
|
|
1089
1065
|
|
|
1090
|
-
/// Check is_dir for multiple prefixes concurrently. Returns list of bools.
|
|
1091
|
-
#[pyfunction]
|
|
1092
|
-
pub fn gcs_is_dir_batch<'py>(
|
|
1093
|
-
py: Python<'py>,
|
|
1094
|
-
endpoint: String,
|
|
1095
|
-
bucket: String,
|
|
1096
|
-
prefixes: Vec<String>,
|
|
1097
|
-
access_token: Option<String>,
|
|
1098
|
-
use_h2: Option<bool>,
|
|
1099
|
-
) -> PyResult<Bound<'py, PyAny>> {
|
|
1100
|
-
let h2 = use_h2.unwrap_or(false);
|
|
1101
|
-
pyo3_async_runtimes::tokio::future_into_py(py, async move {
|
|
1102
|
-
let creds = gcs_creds!(endpoint, bucket, access_token);
|
|
1103
|
-
let futs: Vec<_> = prefixes
|
|
1104
|
-
.iter()
|
|
1105
|
-
.map(|p| do_gcs_is_dir(&creds, p, h2))
|
|
1106
|
-
.collect();
|
|
1107
|
-
let results = join_all(futs).await;
|
|
1108
|
-
let mut out = Vec::with_capacity(results.len());
|
|
1109
|
-
for r in results {
|
|
1110
|
-
out.push(r?);
|
|
1111
|
-
}
|
|
1112
|
-
Ok(out)
|
|
1113
|
-
})
|
|
1114
|
-
}
|
|
1115
|
-
|
|
1116
1066
|
// ---------------------------------------------------------------------------
|
|
1117
1067
|
// Presigned URL generation (V4 RSA-SHA256)
|
|
1118
1068
|
// ---------------------------------------------------------------------------
|
|
@@ -16,6 +16,7 @@ use pyo3::prelude::*;
|
|
|
16
16
|
pub mod backend;
|
|
17
17
|
pub mod cache;
|
|
18
18
|
pub mod http;
|
|
19
|
+
pub mod listing;
|
|
19
20
|
pub mod xml;
|
|
20
21
|
|
|
21
22
|
pub mod artifactory;
|
|
@@ -48,7 +49,6 @@ fn asanypath_native(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
|
48
49
|
m.add_function(wrap_pyfunction!(s3::s3_abort_multipart, m)?)?;
|
|
49
50
|
m.add_function(wrap_pyfunction!(s3::s3_delete_batch, m)?)?;
|
|
50
51
|
m.add_function(wrap_pyfunction!(s3::s3_exists_batch, m)?)?;
|
|
51
|
-
m.add_function(wrap_pyfunction!(s3::s3_is_dir_batch, m)?)?;
|
|
52
52
|
m.add_function(wrap_pyfunction!(s3::s3_copy_batch, m)?)?;
|
|
53
53
|
m.add_function(wrap_pyfunction!(s3::s3_list_buckets, m)?)?;
|
|
54
54
|
// GCS
|
|
@@ -66,7 +66,6 @@ fn asanypath_native(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
|
66
66
|
m.add_function(wrap_pyfunction!(gcs::gcs_upload_chunk, m)?)?;
|
|
67
67
|
m.add_function(wrap_pyfunction!(gcs::gcs_delete_batch, m)?)?;
|
|
68
68
|
m.add_function(wrap_pyfunction!(gcs::gcs_exists_batch, m)?)?;
|
|
69
|
-
m.add_function(wrap_pyfunction!(gcs::gcs_is_dir_batch, m)?)?;
|
|
70
69
|
m.add_function(wrap_pyfunction!(gcs::gcs_copy_batch, m)?)?;
|
|
71
70
|
m.add_function(wrap_pyfunction!(gcs::gcs_list_buckets, m)?)?;
|
|
72
71
|
// Azure
|
|
@@ -83,7 +82,6 @@ fn asanypath_native(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
|
83
82
|
m.add_function(wrap_pyfunction!(azure::az_put_block_list, m)?)?;
|
|
84
83
|
m.add_function(wrap_pyfunction!(azure::az_delete_batch, m)?)?;
|
|
85
84
|
m.add_function(wrap_pyfunction!(azure::az_exists_batch, m)?)?;
|
|
86
|
-
m.add_function(wrap_pyfunction!(azure::az_is_dir_batch, m)?)?;
|
|
87
85
|
m.add_function(wrap_pyfunction!(azure::az_copy_batch, m)?)?;
|
|
88
86
|
m.add_function(wrap_pyfunction!(azure::az_list_containers, m)?)?;
|
|
89
87
|
// Artifactory
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
//! Shared one-level listing builder for prefix-delimited object stores.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Copyright (C) 2026 Axis Communications AB, Lund, Sweden
|
|
5
|
+
*
|
|
6
|
+
* Use of this source code is governed by an MIT-style
|
|
7
|
+
* license that can be found in the LICENSE file or at
|
|
8
|
+
* https://opensource.org/licenses/MIT.
|
|
9
|
+
*/
|
|
10
|
+
use std::collections::HashMap;
|
|
11
|
+
|
|
12
|
+
/// `(uri, is_dir, size, modified)` for one direct child; `modified` is the
|
|
13
|
+
/// backend's raw timestamp text, parsed on the Python side.
|
|
14
|
+
pub type ListEntry = (String, bool, Option<u64>, Option<String>);
|
|
15
|
+
|
|
16
|
+
/// Collects the direct children of `prefix`, deduplicated by name.
|
|
17
|
+
pub struct Listing {
|
|
18
|
+
base: String,
|
|
19
|
+
current_key: String,
|
|
20
|
+
search_prefix: String,
|
|
21
|
+
index: HashMap<String, usize>,
|
|
22
|
+
entries: Vec<ListEntry>,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
impl Listing {
|
|
26
|
+
/// `base` is the URI root such as `s3://bucket`.
|
|
27
|
+
pub fn new(base: String, prefix: &str) -> Self {
|
|
28
|
+
let current_key = prefix.trim_matches('/').to_string();
|
|
29
|
+
let search_prefix = if current_key.is_empty() {
|
|
30
|
+
String::new()
|
|
31
|
+
} else {
|
|
32
|
+
format!("{current_key}/")
|
|
33
|
+
};
|
|
34
|
+
Self {
|
|
35
|
+
base,
|
|
36
|
+
current_key,
|
|
37
|
+
search_prefix,
|
|
38
|
+
index: HashMap::new(),
|
|
39
|
+
entries: Vec::new(),
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
pub fn search_prefix(&self) -> &str {
|
|
44
|
+
&self.search_prefix
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// Add a delimiter-rolled-up common prefix (a directory).
|
|
48
|
+
pub fn add_prefix(&mut self, prefix: &str) {
|
|
49
|
+
self.insert(prefix.trim_end_matches('/'), true, None, None);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// Add an object key; keys below a deeper `/` collapse to their directory.
|
|
53
|
+
pub fn add_object(&mut self, key: &str, size: Option<u64>, modified: Option<String>) {
|
|
54
|
+
let relative = key
|
|
55
|
+
.strip_prefix(self.search_prefix.as_str())
|
|
56
|
+
.unwrap_or(key)
|
|
57
|
+
.trim_start_matches('/');
|
|
58
|
+
match relative.split_once('/') {
|
|
59
|
+
Some((first, _)) => {
|
|
60
|
+
let name = format!("{}{first}", self.search_prefix);
|
|
61
|
+
self.insert(&name, true, None, None);
|
|
62
|
+
}
|
|
63
|
+
None => self.insert(key.trim_end_matches('/'), false, size, modified),
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
pub fn finish(self) -> Vec<ListEntry> {
|
|
68
|
+
self.entries
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
fn insert(&mut self, name: &str, is_dir: bool, size: Option<u64>, modified: Option<String>) {
|
|
72
|
+
if name.is_empty() || name == self.current_key {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if let Some(&position) = self.index.get(name) {
|
|
76
|
+
// A name that is both an object and a prefix lists as a directory.
|
|
77
|
+
if is_dir {
|
|
78
|
+
self.entries[position] = (self.entries[position].0.clone(), true, None, None);
|
|
79
|
+
}
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
self.index.insert(name.to_string(), self.entries.len());
|
|
83
|
+
self.entries
|
|
84
|
+
.push((format!("{}/{name}", self.base), is_dir, size, modified));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
#[cfg(test)]
|
|
89
|
+
mod tests {
|
|
90
|
+
use super::*;
|
|
91
|
+
|
|
92
|
+
#[test]
|
|
93
|
+
fn classifies_children_and_skips_self() {
|
|
94
|
+
let mut listing = Listing::new("s3://b".into(), "/root/");
|
|
95
|
+
listing.add_object("root/", Some(0), None);
|
|
96
|
+
listing.add_object(
|
|
97
|
+
"root/a.txt",
|
|
98
|
+
Some(3),
|
|
99
|
+
Some("2024-01-02T03:04:05.000Z".into()),
|
|
100
|
+
);
|
|
101
|
+
listing.add_prefix("root/sub/");
|
|
102
|
+
listing.add_object("root/marker/", Some(0), None);
|
|
103
|
+
listing.add_object("root/deep/x/y", Some(1), None);
|
|
104
|
+
assert_eq!(
|
|
105
|
+
listing.finish(),
|
|
106
|
+
vec![
|
|
107
|
+
(
|
|
108
|
+
"s3://b/root/a.txt".into(),
|
|
109
|
+
false,
|
|
110
|
+
Some(3),
|
|
111
|
+
Some("2024-01-02T03:04:05.000Z".into())
|
|
112
|
+
),
|
|
113
|
+
("s3://b/root/sub".into(), true, None, None),
|
|
114
|
+
("s3://b/root/marker".into(), true, None, None),
|
|
115
|
+
("s3://b/root/deep".into(), true, None, None),
|
|
116
|
+
]
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
#[test]
|
|
121
|
+
fn prefix_wins_over_same_named_object() {
|
|
122
|
+
let mut listing = Listing::new("az://c".into(), "");
|
|
123
|
+
listing.add_object("a", Some(1), None);
|
|
124
|
+
listing.add_prefix("a/");
|
|
125
|
+
listing.add_prefix("a/");
|
|
126
|
+
assert_eq!(
|
|
127
|
+
listing.finish(),
|
|
128
|
+
vec![("az://c/a".into(), true, None, None)]
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -31,6 +31,7 @@ use quick_xml::{XmlVersion, events::Event};
|
|
|
31
31
|
use serde::{Deserialize, Serialize};
|
|
32
32
|
|
|
33
33
|
use crate::http::{do_request, extract_host, status_to_pyerr};
|
|
34
|
+
use crate::listing::{ListEntry, Listing};
|
|
34
35
|
use crate::xml::local_name;
|
|
35
36
|
use futures::future::join_all;
|
|
36
37
|
use tokio::sync::Mutex as AsyncMutex;
|
|
@@ -292,87 +293,55 @@ async fn do_s3_request(
|
|
|
292
293
|
// XML listing parser
|
|
293
294
|
// ---------------------------------------------------------------------------
|
|
294
295
|
|
|
295
|
-
pub(crate) fn parse_listing_page(
|
|
296
|
-
body: &[u8],
|
|
297
|
-
search_prefix: &str,
|
|
298
|
-
current_key: &str,
|
|
299
|
-
bucket: &str,
|
|
300
|
-
found_all: &mut HashMap<String, ()>,
|
|
301
|
-
uris: &mut Vec<String>,
|
|
302
|
-
) -> Option<String> {
|
|
296
|
+
pub(crate) fn parse_listing_page(body: &[u8], listing: &mut Listing) -> Option<String> {
|
|
303
297
|
let mut reader = Reader::from_reader(body);
|
|
304
298
|
reader.config_mut().trim_text(true);
|
|
305
299
|
|
|
306
300
|
let mut buf = Vec::with_capacity(256);
|
|
307
|
-
let mut
|
|
308
|
-
let mut
|
|
309
|
-
let mut
|
|
301
|
+
let mut text = String::new();
|
|
302
|
+
let mut in_contents = false;
|
|
303
|
+
let mut in_prefixes = false;
|
|
304
|
+
let mut key = String::new();
|
|
305
|
+
let mut size = None;
|
|
306
|
+
let mut modified = None;
|
|
307
|
+
let mut continuation_token = None;
|
|
310
308
|
|
|
311
309
|
loop {
|
|
312
310
|
match reader.read_event_into(&mut buf) {
|
|
313
311
|
Ok(Event::Start(e)) => {
|
|
314
312
|
let name = e.name();
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
313
|
+
match local_name(name.as_ref()) {
|
|
314
|
+
"Contents" => {
|
|
315
|
+
in_contents = true;
|
|
316
|
+
key.clear();
|
|
317
|
+
size = None;
|
|
318
|
+
modified = None;
|
|
321
319
|
}
|
|
320
|
+
"CommonPrefixes" => in_prefixes = true,
|
|
322
321
|
_ => {}
|
|
323
322
|
}
|
|
323
|
+
text.clear();
|
|
324
324
|
}
|
|
325
|
-
Ok(Event::Text(e)) =>
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
if !found.is_empty() {
|
|
336
|
-
continuation_token = Some(found);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
"Prefix" => {
|
|
340
|
-
if found == search_prefix || found.is_empty() {
|
|
341
|
-
continue;
|
|
342
|
-
}
|
|
343
|
-
let trimmed = found.trim_end_matches('/');
|
|
344
|
-
add_uri(trimmed, current_key, bucket, found_all, uris);
|
|
345
|
-
}
|
|
346
|
-
"Key" => {
|
|
347
|
-
if !search_prefix.is_empty()
|
|
348
|
-
&& found.trim_end_matches('/')
|
|
349
|
-
== search_prefix.trim_end_matches('/')
|
|
350
|
-
{
|
|
351
|
-
continue;
|
|
352
|
-
}
|
|
353
|
-
let relative =
|
|
354
|
-
if !search_prefix.is_empty() && found.starts_with(search_prefix) {
|
|
355
|
-
&found[search_prefix.len()..]
|
|
356
|
-
} else {
|
|
357
|
-
&found
|
|
358
|
-
};
|
|
359
|
-
let relative = relative.trim_start_matches('/');
|
|
360
|
-
let resolved = if !relative.is_empty() {
|
|
361
|
-
let first_component =
|
|
362
|
-
relative.split('/').next().unwrap_or(relative);
|
|
363
|
-
format!("{}{}", search_prefix, first_component)
|
|
364
|
-
} else {
|
|
365
|
-
found.clone()
|
|
366
|
-
};
|
|
367
|
-
let trimmed = resolved.trim_end_matches('/');
|
|
368
|
-
add_uri(trimmed, current_key, bucket, found_all, uris);
|
|
369
|
-
}
|
|
370
|
-
_ => {}
|
|
325
|
+
Ok(Event::Text(e)) => text.push_str(&e.xml10_content()),
|
|
326
|
+
Ok(Event::End(e)) => {
|
|
327
|
+
let name = e.name();
|
|
328
|
+
match local_name(name.as_ref()) {
|
|
329
|
+
"Key" if in_contents => key = std::mem::take(&mut text),
|
|
330
|
+
"Size" if in_contents => size = text.parse().ok(),
|
|
331
|
+
"LastModified" if in_contents => modified = Some(std::mem::take(&mut text)),
|
|
332
|
+
"Contents" => {
|
|
333
|
+
in_contents = false;
|
|
334
|
+
listing.add_object(&key, size, modified.take());
|
|
371
335
|
}
|
|
336
|
+
"Prefix" if in_prefixes => listing.add_prefix(&text),
|
|
337
|
+
"CommonPrefixes" => in_prefixes = false,
|
|
338
|
+
"NextContinuationToken" if !text.is_empty() => {
|
|
339
|
+
continuation_token = Some(std::mem::take(&mut text));
|
|
340
|
+
}
|
|
341
|
+
_ => {}
|
|
372
342
|
}
|
|
373
343
|
}
|
|
374
|
-
Ok(Event::Eof) => break,
|
|
375
|
-
Err(_) => break,
|
|
344
|
+
Ok(Event::Eof) | Err(_) => break,
|
|
376
345
|
_ => {}
|
|
377
346
|
}
|
|
378
347
|
buf.clear();
|
|
@@ -381,23 +350,6 @@ pub(crate) fn parse_listing_page(
|
|
|
381
350
|
continuation_token
|
|
382
351
|
}
|
|
383
352
|
|
|
384
|
-
fn add_uri(
|
|
385
|
-
path: &str,
|
|
386
|
-
current_key: &str,
|
|
387
|
-
bucket: &str,
|
|
388
|
-
found_all: &mut HashMap<String, ()>,
|
|
389
|
-
uris: &mut Vec<String>,
|
|
390
|
-
) {
|
|
391
|
-
if path.is_empty() || path.trim_end_matches('/') == current_key {
|
|
392
|
-
return;
|
|
393
|
-
}
|
|
394
|
-
if found_all.contains_key(path) {
|
|
395
|
-
return;
|
|
396
|
-
}
|
|
397
|
-
found_all.insert(path.to_string(), ());
|
|
398
|
-
uris.push(format!("s3://{}/{}", bucket, path));
|
|
399
|
-
}
|
|
400
|
-
|
|
401
353
|
// ---------------------------------------------------------------------------
|
|
402
354
|
// Helpers to build S3Creds from Python keyword args
|
|
403
355
|
// ---------------------------------------------------------------------------
|
|
@@ -544,16 +496,9 @@ impl CloudBackend for S3Creds {
|
|
|
544
496
|
// Internal async helpers (used by both single + batch pyfunctions)
|
|
545
497
|
// ---------------------------------------------------------------------------
|
|
546
498
|
|
|
547
|
-
async fn do_s3_list(creds: &S3Creds, prefix: &str, use_h2: bool) -> Result<Vec<
|
|
548
|
-
let
|
|
549
|
-
let search_prefix =
|
|
550
|
-
String::new()
|
|
551
|
-
} else {
|
|
552
|
-
format!("{}/", current_key)
|
|
553
|
-
};
|
|
554
|
-
|
|
555
|
-
let mut found_all = HashMap::new();
|
|
556
|
-
let mut uris = Vec::new();
|
|
499
|
+
async fn do_s3_list(creds: &S3Creds, prefix: &str, use_h2: bool) -> Result<Vec<ListEntry>, PyErr> {
|
|
500
|
+
let mut listing = Listing::new(format!("s3://{}", creds.bucket), prefix);
|
|
501
|
+
let search_prefix = listing.search_prefix().to_string();
|
|
557
502
|
let mut continuation_token: Option<String> = None;
|
|
558
503
|
|
|
559
504
|
loop {
|
|
@@ -578,21 +523,13 @@ async fn do_s3_list(creds: &S3Creds, prefix: &str, use_h2: bool) -> Result<Vec<S
|
|
|
578
523
|
));
|
|
579
524
|
}
|
|
580
525
|
|
|
581
|
-
continuation_token = parse_listing_page(
|
|
582
|
-
&body,
|
|
583
|
-
&search_prefix,
|
|
584
|
-
¤t_key,
|
|
585
|
-
&creds.bucket,
|
|
586
|
-
&mut found_all,
|
|
587
|
-
&mut uris,
|
|
588
|
-
);
|
|
589
|
-
|
|
526
|
+
continuation_token = parse_listing_page(&body, &mut listing);
|
|
590
527
|
if continuation_token.is_none() {
|
|
591
528
|
break;
|
|
592
529
|
}
|
|
593
530
|
}
|
|
594
531
|
|
|
595
|
-
Ok(
|
|
532
|
+
Ok(listing.finish())
|
|
596
533
|
}
|
|
597
534
|
|
|
598
535
|
/// List all buckets (service-level ListBuckets). Returns bare bucket names.
|
|
@@ -1380,43 +1317,7 @@ pub fn s3_exists_batch<'py>(
|
|
|
1380
1317
|
})
|
|
1381
1318
|
}
|
|
1382
1319
|
|
|
1383
|
-
///
|
|
1384
|
-
#[pyfunction]
|
|
1385
|
-
pub fn s3_is_dir_batch<'py>(
|
|
1386
|
-
py: Python<'py>,
|
|
1387
|
-
endpoint: String,
|
|
1388
|
-
bucket: String,
|
|
1389
|
-
prefixes: Vec<String>,
|
|
1390
|
-
region: String,
|
|
1391
|
-
access_key: String,
|
|
1392
|
-
secret_key: String,
|
|
1393
|
-
session_token: Option<String>,
|
|
1394
|
-
use_h2: Option<bool>,
|
|
1395
|
-
) -> PyResult<Bound<'py, PyAny>> {
|
|
1396
|
-
let h2 = use_h2.unwrap_or(false);
|
|
1397
|
-
pyo3_async_runtimes::tokio::future_into_py(py, async move {
|
|
1398
|
-
let creds = s3_creds!(
|
|
1399
|
-
endpoint,
|
|
1400
|
-
bucket,
|
|
1401
|
-
region,
|
|
1402
|
-
access_key,
|
|
1403
|
-
secret_key,
|
|
1404
|
-
session_token
|
|
1405
|
-
);
|
|
1406
|
-
let futs: Vec<_> = prefixes
|
|
1407
|
-
.iter()
|
|
1408
|
-
.map(|p| do_s3_is_dir(&creds, p, h2))
|
|
1409
|
-
.collect();
|
|
1410
|
-
let results = join_all(futs).await;
|
|
1411
|
-
let mut out = Vec::with_capacity(results.len());
|
|
1412
|
-
for r in results {
|
|
1413
|
-
out.push(r?);
|
|
1414
|
-
}
|
|
1415
|
-
Ok(out)
|
|
1416
|
-
})
|
|
1417
|
-
}
|
|
1418
|
-
|
|
1419
|
-
/// List multiple prefixes concurrently. Returns list of URI lists.
|
|
1320
|
+
/// List multiple prefixes concurrently. Returns `(uri, is_dir, size, modified)` lists.
|
|
1420
1321
|
#[pyfunction]
|
|
1421
1322
|
pub fn s3_list_batch<'py>(
|
|
1422
1323
|
py: Python<'py>,
|
|
@@ -1476,7 +1377,39 @@ pub fn s3_list_buckets<'py>(
|
|
|
1476
1377
|
|
|
1477
1378
|
#[cfg(test)]
|
|
1478
1379
|
mod tests {
|
|
1479
|
-
use super::parse_s3_acl;
|
|
1380
|
+
use super::{Listing, parse_listing_page, parse_s3_acl};
|
|
1381
|
+
|
|
1382
|
+
#[test]
|
|
1383
|
+
fn parses_listing_page_metadata() {
|
|
1384
|
+
let body = br#"<?xml version="1.0" encoding="UTF-8"?>
|
|
1385
|
+
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
|
1386
|
+
<Name>b</Name><Prefix>dir/</Prefix><Delimiter>/</Delimiter>
|
|
1387
|
+
<Contents><Key>dir/</Key><Size>0</Size></Contents>
|
|
1388
|
+
<Contents>
|
|
1389
|
+
<Key>dir/a.txt</Key><LastModified>2024-01-02T03:04:05.000Z</LastModified>
|
|
1390
|
+
<ETag>"x"</ETag><Size>42</Size>
|
|
1391
|
+
</Contents>
|
|
1392
|
+
<CommonPrefixes><Prefix>dir/sub/</Prefix></CommonPrefixes>
|
|
1393
|
+
<NextContinuationToken>next</NextContinuationToken>
|
|
1394
|
+
</ListBucketResult>"#;
|
|
1395
|
+
let mut listing = Listing::new("s3://b".into(), "dir");
|
|
1396
|
+
assert_eq!(
|
|
1397
|
+
parse_listing_page(body, &mut listing).as_deref(),
|
|
1398
|
+
Some("next")
|
|
1399
|
+
);
|
|
1400
|
+
assert_eq!(
|
|
1401
|
+
listing.finish(),
|
|
1402
|
+
vec![
|
|
1403
|
+
(
|
|
1404
|
+
"s3://b/dir/a.txt".into(),
|
|
1405
|
+
false,
|
|
1406
|
+
Some(42),
|
|
1407
|
+
Some("2024-01-02T03:04:05.000Z".into())
|
|
1408
|
+
),
|
|
1409
|
+
("s3://b/dir/sub".into(), true, None, None),
|
|
1410
|
+
]
|
|
1411
|
+
);
|
|
1412
|
+
}
|
|
1480
1413
|
|
|
1481
1414
|
#[test]
|
|
1482
1415
|
fn parses_owner_and_grants() {
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|