Skip to content
This repository has been archived by the owner on Nov 9, 2019. It is now read-only.

Begin sketching out a new high-level fs API. #91

Merged
merged 9 commits into from
Sep 30, 2019
Merged

Begin sketching out a new high-level fs API. #91

merged 9 commits into from
Sep 30, 2019

Conversation

sunfishcode
Copy link
Member

This is a very preliminary sketch of #83. It doesn't even compile yet, but it shows a possible high-level structure of such an API.

src/fs/ctx.rs Outdated
use crate::WasiCtx;

lazy_static! {
// TODO: Should we allow the context to be passed alternate arguments?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I guess we should. How otherwise are we going to pass in the preopens? Or are we going to add another mid-level call for that perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this a little more, I don't think a lazy_static singleton is the way to go, because people using this API may want to have multiple distinct WasiCtxs that are isolated from each other. I've now updated the patch to have Dir and File hold a reference to a WasiCtx. We may further want to make it an Rc, but we'll see how things go.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

@sunfishcode sunfishcode force-pushed the fs branch 5 times, most recently from 78132c9 to 522a9fc Compare September 11, 2019 03:55
@sunfishcode
Copy link
Member Author

This is now complete enough to compile, and a lot of the API surface area is now in place, though most of the functionality isn't implemented yet.

))?;
*/

let ctx = self.ctx;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary here? Could we not simply pass self.ctx directly?

Err(io::Error::from_raw_os_error(raw_os_error))
}

#[cfg(windows)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that in the future it'd be useful to have the same setup as for the hostcalls? That is, separate host specifics in modules and then just do a cfg_if instead of multiple #[cfg(..)]s? Mind you, this is an idea for the future and not now; I'm happy with the state of error.rs as is :-)

///
/// [`std::fs::Metadata`]: https://doc.rust-lang.org/std/fs/struct.Metadata.html
#[derive(Clone)]
pub struct Metadata {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub struct Metadata {}
pub struct Metadata;

Copy link
Member

@kubkon kubkon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Here's a thought: could we actually re-use this interface and the implementation in Rust's libstd? I might be way off base here but browsing through the changes here looks as if it could be re-used in libstd. @alexcrichton thoughts?

@alexcrichton
Copy link
Collaborator

Seems possible perhaps! I suspect this'd want to bake a bit more before going into libstd, but the internal details of libstd are up for grabs to change at any time!

@sunfishcode
Copy link
Member Author

Interesting idea. In addition to obviously baking :-), a few additional pieces we'd need:

  • A way for this fs API to use WASI directly, instead of going through the current wasi-common userspace implementation. Some of that may be as simple as just adding alternate implementations of the API under #[cfg(target_os = wasi)]. What should we do about WasiCtx though? In wasi-common, one can create multiple independent WasiCtxs and they're explicitly passed into the API. In WASI itself, there's currently just one implicit WasiCtx (or equivalent) below the platform boundary. One option is to change WASI itself to have a context argument and support independent contexts, I suppose.

  • Something similar to what libpreopen does within WASI libc, so that when Rust code calls File::open("/absolute/path"), we can remap it to directory fd and relative path. This should be straight-forward.

  • "Rust-idiomatic" wrappers on top of wasi-common's low-level API, similar to the wasi crate, using Results and slices rather than bare errnos and pointers. This is what the Rust libstd support for WASI currently uses, and it would be nice if this new fs layer could use something similar.

@cc newpavlov since this is one possible path to a libc-free libstd for WASI targets.

@sunfishcode
Copy link
Member Author

cc @newpavlov even, since this is one possible path to a libc-free libstd for WASI targets.

@newpavlov
Copy link

this is one possible path to a libc-free libstd for WASI targets.

I am not sure if I understand how it will work. All WASI code still works via Core API at the lowest level, right? And this high-level API is still built on top of Core API?

Right now, we depend on libc in std only for interoperability with C/C++. Removing the remaining libc usages should be fairly trivial if we'll add a separate target (e.g. wasm32-wasi-rust).

Not strictly relevant, but can you explain how __wasilibc_find_relpath works in terms of Core API? I am not completely sure, but it looks like we can remove its usage from std.

@sunfishcode sunfishcode marked this pull request as ready for review September 30, 2019 17:03
@sunfishcode
Copy link
Member Author

This builds, and although most of the functionality is not yet implemented, I'd like to land it and continue to iterate incrementally.

@kubkon
Copy link
Member

kubkon commented Sep 30, 2019

@sunfishcode sounds great to me! Feel free to merge it or I can do that if you want. Also, apologies for little to no activity lately but got swamped with preparation work for the upcoming Devcon5 conference :-)

@sunfishcode sunfishcode merged commit 6b2f3eb into master Sep 30, 2019
@sunfishcode sunfishcode deleted the fs branch September 30, 2019 18:27
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants