Skip to content

Commit 8aa7c94

Browse files
committed
Port dir_metadata to fs_utf8 and cap-async-std.
The `dir_metadata` on `Dir` wasn't defined for the `fs_utf8` version of `Dir`; add it. Also, port it to cap-async-std's `Dir` types. cap-async-std is currently disabled, but keep it current as we can.
1 parent fc66ab9 commit 8aa7c94

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

cap-async-std/src/fs/dir.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,16 @@ impl Dir {
327327
)
328328
}
329329

330+
/// Queries metadata about the underlying directory.
331+
///
332+
/// This is similar to [`std::fs::File::metadata`], but for `Dir` rather
333+
/// than for `File`.
334+
#[inline]
335+
pub async fn dir_metadata(&self) -> io::Result<Metadata> {
336+
let clone = self.clone();
337+
spawn_blocking(move || metadata_from(&*clone.as_filelike_view::<std::fs::File>())).await
338+
}
339+
330340
/// Returns an iterator over the entries within `self`.
331341
#[inline]
332342
pub async fn entries(&self) -> io::Result<ReadDir> {
@@ -863,6 +873,18 @@ impl Dir {
863873
}
864874
}
865875

876+
#[cfg(not(target_os = "wasi"))]
877+
#[inline]
878+
fn metadata_from(file: &std::fs::File) -> io::Result<Metadata> {
879+
Metadata::from_file(file)
880+
}
881+
882+
#[cfg(target_os = "wasi")]
883+
#[inline]
884+
fn metadata_from(file: &std::fs::File) -> io::Result<Metadata> {
885+
file.metadata()
886+
}
887+
866888
// Safety: `FilelikeViewType` is implemented for `std::fs::File`.
867889
unsafe impl io_lifetimes::views::FilelikeViewType for Dir {}
868890

@@ -1005,7 +1027,10 @@ async fn initial_buffer_size(file: &File) -> usize {
10051027
// Allocate one extra byte so the buffer doesn't need to grow before the
10061028
// final `read` call at the end of the file. Don't worry about `usize`
10071029
// overflow because reading will fail regardless in that case.
1008-
file.metadata().map(|m| m.len() as usize + 1).unwrap_or(0)
1030+
file.metadata()
1031+
.await
1032+
.map(|m| m.len() as usize + 1)
1033+
.unwrap_or(0)
10091034
}
10101035

10111036
impl fmt::Debug for Dir {

cap-async-std/src/fs/file.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ impl File {
8888
///
8989
/// This corresponds to [`async_std::fs::File::metadata`].
9090
#[inline]
91-
pub fn metadata(&self) -> io::Result<Metadata> {
92-
let sync = self.std.as_filelike_view::<std::fs::File>();
93-
metadata_from(&*sync)
91+
pub async fn metadata(&self) -> io::Result<Metadata> {
92+
let clone = self.clone();
93+
let sync = clone.std.as_filelike_view::<std::fs::File>();
94+
spawn_blocking(move || metadata_from(&*sync)).await
9495
}
9596

9697
// async_std doesn't have `try_clone`.

cap-async-std/src/fs_utf8/dir.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,15 @@ impl Dir {
201201
self.cap_std.metadata(path).await
202202
}
203203

204+
/// Queries metadata about the underlying directory.
205+
///
206+
/// This is similar to [`std::fs::File::metadata`], but for `Dir` rather
207+
/// than for `File`.
208+
#[inline]
209+
pub async fn dir_metadata(&self) -> io::Result<Metadata> {
210+
self.cap_std.dir_metadata().await
211+
}
212+
204213
/// Returns an iterator over the entries within `self`.
205214
#[inline]
206215
pub async fn entries(&self) -> io::Result<ReadDir> {

cap-async-std/src/fs_utf8/file.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ impl File {
9292
///
9393
/// This corresponds to [`async_std::fs::File::metadata`].
9494
#[inline]
95-
pub fn metadata(&self) -> io::Result<Metadata> {
96-
self.cap_std.metadata()
95+
pub async fn metadata(&self) -> io::Result<Metadata> {
96+
let clone = self.clone();
97+
spawn_blocking(move || clone.metadata()).await
9798
}
9899

99100
// async_std doesn't have `try_clone`.

cap-std/src/fs_utf8/dir.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@ impl Dir {
191191
self.cap_std.metadata(path)
192192
}
193193

194+
/// Queries metadata about the underlying directory.
195+
///
196+
/// This is similar to [`std::fs::File::metadata`], but for `Dir` rather
197+
/// than for `File`.
198+
#[inline]
199+
pub fn dir_metadata(&self) -> io::Result<Metadata> {
200+
self.cap_std.dir_metadata()
201+
}
202+
194203
/// Returns an iterator over the entries within `self`.
195204
#[inline]
196205
pub fn entries(&self) -> io::Result<ReadDir> {

0 commit comments

Comments
 (0)