From 6ea5aa1a9b7e131f43fbf0979e2f2ee956a5d722 Mon Sep 17 00:00:00 2001 From: ZhangJianxin Date: Thu, 8 May 2025 09:20:02 +0800 Subject: [PATCH] add AsyncSeek for &File Signed-off-by: ZhangJianxin Change-Id: I99385e61a70f94a2349c7bd6cde16f9a47a85c0f --- ylong_runtime/src/fs/async_file.rs | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/ylong_runtime/src/fs/async_file.rs b/ylong_runtime/src/fs/async_file.rs index d974887..323954f 100644 --- a/ylong_runtime/src/fs/async_file.rs +++ b/ylong_runtime/src/fs/async_file.rs @@ -522,6 +522,62 @@ impl AsyncSeek for File { } } +impl AsyncSeek for &File { + fn poll_seek( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + mut pos: SeekFrom, + ) -> Poll> { + let file = self.get_mut(); + let mut inner = match file.inner.try_lock() { + Ok(inner) => inner, + Err(e) => return Poll::Ready(Err(io::Error::other(e))), + }; + loop { + match inner.state { + FileState::Idle(ref mut buf) => { + // after each take, buf will be set right back + let mut r_buf = buf.take().unwrap(); + + // move the cursor back since there's unread data in the buf + let unread = r_buf.drop_unread(); + if unread != 0 { + if let SeekFrom::Current(ref mut idx) = pos { + *idx -= unread + } + } + + let file = file.file.clone(); + inner.state = + FileState::Seeking(spawn_blocking(&TaskBuilder::new(), move || { + let ret = (&*file).seek(pos); + (r_buf, ret) + })); + } + ref mut state => { + let op = state.get_op(); + let res = poll_ready!(Pin::new(state).poll(cx)); + match op { + FileOp::Reading => {} + FileOp::Writing => { + if let Err(e) = res { + // Save the error for the next write. + inner.write_err = Some(e.kind()); + } + } + FileOp::Seeking => { + if let Ok(idx) = res { + inner.idx = idx; + } + return Poll::Ready(res); + } + } + } + } + } + } +} + impl AsyncRead for File { fn poll_read( self: Pin<&mut Self>, -- Gitee