]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #47268 - EdSchouten:cloudabi-libstd, r=alexcrichton
authorbors <bors@rust-lang.org>
Sun, 14 Jan 2018 08:49:10 +0000 (08:49 +0000)
committerbors <bors@rust-lang.org>
Sun, 14 Jan 2018 08:49:10 +0000 (08:49 +0000)
Implement libstd for CloudABI.

Though CloudABI is strongly inspired by POSIX, its absence of features that don't work well with capability-based sandboxing makes it different enough that adding bits to `sys/unix` will make things a mess. This change therefore adds CloudABI specific platform code under `sys/cloudabi`.

One of the goals of this implementation is to build as much as possible directly on top of CloudABI's system call layer, as opposed to using the C library. This is preferred, as the system call layer is supposed to be stable, whereas the C library ABI technically is not. An advantage of this approach is that it allows us to implement certain interfaces, such as mutexes and condition variables more optimally. They can be lighter than the ones provided by pthreads.

This change disables some modules that cannot realistically be implemented right now. For example, libstd's pathname abstraction is not designed with POSIX `*at()` (e.g., `openat()`) in mind. The `*at()` functions are the only set of file system APIs available on CloudABI. There is no global file system namespace, nor a process working directory. Discussions on how to port these modules over are outside the scope of this change.

1  2 
src/libstd/fs.rs

diff --combined src/libstd/fs.rs
index 51cb9609120e36a5657aa5aa1f75d15979e54de7,82ba1a8774caa394c9a1c9bce64e2e967364493e..4e0ff450cabdc84b9622db12b04fa01fd79d08f8
@@@ -211,14 -211,6 +211,14 @@@ pub struct DirBuilder 
      recursive: bool,
  }
  
 +/// How large a buffer to pre-allocate before reading the entire file at `path`.
 +fn initial_buffer_size<P: AsRef<Path>>(path: P) -> usize {
 +    // Allocate one extra byte so the buffer doesn't need to grow before the
 +    // final `read` call at the end of the file.  Don't worry about `usize`
 +    // overflow because reading will fail regardless in that case.
 +    metadata(path).map(|m| m.len() as usize + 1).unwrap_or(0)
 +}
 +
  /// Read the entire contents of a file into a bytes vector.
  ///
  /// This is a convenience function for using [`File::open`] and [`read_to_end`]
  /// ```
  #[unstable(feature = "fs_read_write", issue = "46588")]
  pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
 -    let mut bytes = Vec::new();
 +    let mut bytes = Vec::with_capacity(initial_buffer_size(&path));
      File::open(path)?.read_to_end(&mut bytes)?;
      Ok(bytes)
  }
  /// ```
  #[unstable(feature = "fs_read_write", issue = "46588")]
  pub fn read_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
 -    let mut string = String::new();
 +    let mut string = String::with_capacity(initial_buffer_size(&path));
      File::open(path)?.read_to_string(&mut string)?;
      Ok(string)
  }
@@@ -1989,7 -1981,7 +1989,7 @@@ impl AsInnerMut<fs_imp::DirBuilder> fo
      }
  }
  
- #[cfg(all(test, not(target_os = "emscripten")))]
+ #[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten"))))]
  mod tests {
      use io::prelude::*;