Fix unused function warnings on Windows.
Unfortunately the helpers added in #154 were only used from non-Windows implementations, which caused compiler warnings on Windows. This commit moves the helper to be unix-specific and removes the tiny wrapper around calling `str_to_cstring` on `PathGet.path`.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
use crate::{Error, Result};
|
||||
use std::convert::TryInto;
|
||||
use std::ffi::CString;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
pub(crate) fn systemtime_to_timestamp(st: SystemTime) -> Result<u64> {
|
||||
@@ -10,7 +9,3 @@ pub(crate) fn systemtime_to_timestamp(st: SystemTime) -> Result<u64> {
|
||||
.try_into()
|
||||
.map_err(Into::into) // u128 doesn't fit into u64
|
||||
}
|
||||
|
||||
pub(crate) fn str_to_cstring(s: &str) -> Result<CString> {
|
||||
CString::new(s.as_bytes()).map_err(|_| Error::EILSEQ)
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
use crate::helpers::str_to_cstring;
|
||||
use crate::sys::host_impl;
|
||||
use crate::sys::hostcalls_impl::fs_helpers::*;
|
||||
use crate::{fdentry::FdEntry, host, Error, Result};
|
||||
use std::ffi::CString;
|
||||
use std::fs::File;
|
||||
use std::path::{Component, Path};
|
||||
|
||||
@@ -21,10 +19,6 @@ impl PathGet {
|
||||
pub(crate) fn path(&self) -> &str {
|
||||
&self.path
|
||||
}
|
||||
|
||||
pub(crate) fn path_cstring(&self) -> Result<CString> {
|
||||
str_to_cstring(&self.path)
|
||||
}
|
||||
}
|
||||
|
||||
/// Normalizes a path to ensure that the target path is located under the directory provided.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::osfile::OsFile;
|
||||
use crate::helpers::str_to_cstring;
|
||||
use crate::hostcalls_impl::PathGet;
|
||||
use crate::sys::host_impl;
|
||||
use crate::sys::unix::str_to_cstring;
|
||||
use crate::{host, Error, Result};
|
||||
use nix::libc::{self, c_long, c_void};
|
||||
use std::convert::TryInto;
|
||||
@@ -12,7 +12,7 @@ pub(crate) fn path_unlink_file(resolved: PathGet) -> Result<()> {
|
||||
use nix::errno;
|
||||
use nix::libc::unlinkat;
|
||||
|
||||
let path_cstr = resolved.path_cstring()?;
|
||||
let path_cstr = str_to_cstring(resolved.path())?;
|
||||
|
||||
// nix doesn't expose unlinkat() yet
|
||||
match unsafe { unlinkat(resolved.dirfd().as_raw_fd(), path_cstr.as_ptr(), 0) } {
|
||||
@@ -53,7 +53,7 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> {
|
||||
use nix::{errno::Errno, fcntl::AtFlags, libc::symlinkat, sys::stat::fstatat};
|
||||
|
||||
let old_path_cstr = str_to_cstring(old_path)?;
|
||||
let new_path_cstr = resolved.path_cstring()?;
|
||||
let new_path_cstr = str_to_cstring(resolved.path())?;
|
||||
|
||||
log::debug!("path_symlink old_path = {:?}", old_path);
|
||||
log::debug!("path_symlink resolved = {:?}", resolved);
|
||||
@@ -93,8 +93,8 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> {
|
||||
|
||||
pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> {
|
||||
use nix::{errno::Errno, fcntl::AtFlags, libc::renameat, sys::stat::fstatat};
|
||||
let old_path_cstr = resolved_old.path_cstring()?;
|
||||
let new_path_cstr = resolved_new.path_cstring()?;
|
||||
let old_path_cstr = str_to_cstring(resolved_old.path())?;
|
||||
let new_path_cstr = str_to_cstring(resolved_new.path())?;
|
||||
|
||||
let res = unsafe {
|
||||
renameat(
|
||||
|
||||
@@ -4,6 +4,7 @@ use super::fs_helpers::*;
|
||||
use crate::helpers::systemtime_to_timestamp;
|
||||
use crate::hostcalls_impl::{FileType, PathGet};
|
||||
use crate::sys::host_impl;
|
||||
use crate::sys::unix::str_to_cstring;
|
||||
use crate::{host, Error, Result};
|
||||
use nix::libc;
|
||||
use std::convert::TryInto;
|
||||
@@ -57,7 +58,7 @@ pub(crate) fn fd_fdstat_set_flags(fd: &File, fdflags: host::__wasi_fdflags_t) ->
|
||||
|
||||
pub(crate) fn path_create_directory(resolved: PathGet) -> Result<()> {
|
||||
use nix::libc::mkdirat;
|
||||
let path_cstr = resolved.path_cstring()?;
|
||||
let path_cstr = str_to_cstring(resolved.path())?;
|
||||
// nix doesn't expose mkdirat() yet
|
||||
match unsafe { mkdirat(resolved.dirfd().as_raw_fd(), path_cstr.as_ptr(), 0o777) } {
|
||||
0 => Ok(()),
|
||||
@@ -67,8 +68,8 @@ pub(crate) fn path_create_directory(resolved: PathGet) -> Result<()> {
|
||||
|
||||
pub(crate) fn path_link(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> {
|
||||
use nix::libc::linkat;
|
||||
let old_path_cstr = resolved_old.path_cstring()?;
|
||||
let new_path_cstr = resolved_new.path_cstring()?;
|
||||
let old_path_cstr = str_to_cstring(resolved_old.path())?;
|
||||
let new_path_cstr = str_to_cstring(resolved_new.path())?;
|
||||
|
||||
// Not setting AT_SYMLINK_FOLLOW fails on most filesystems
|
||||
let atflags = libc::AT_SYMLINK_FOLLOW;
|
||||
@@ -183,7 +184,7 @@ pub(crate) fn path_open(
|
||||
|
||||
pub(crate) fn path_readlink(resolved: PathGet, buf: &mut [u8]) -> Result<usize> {
|
||||
use nix::errno::Errno;
|
||||
let path_cstr = resolved.path_cstring()?;
|
||||
let path_cstr = str_to_cstring(resolved.path())?;
|
||||
|
||||
// Linux requires that the buffer size is positive, whereas POSIX does not.
|
||||
// Use a fake buffer to store the results if the size is zero.
|
||||
@@ -340,7 +341,7 @@ pub(crate) fn path_remove_directory(resolved: PathGet) -> Result<()> {
|
||||
use nix::errno;
|
||||
use nix::libc::{unlinkat, AT_REMOVEDIR};
|
||||
|
||||
let path_cstr = resolved.path_cstring()?;
|
||||
let path_cstr = str_to_cstring(resolved.path())?;
|
||||
|
||||
// nix doesn't expose unlinkat() yet
|
||||
match unsafe {
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
use super::osfile::OsFile;
|
||||
use crate::helpers::str_to_cstring;
|
||||
use crate::hostcalls_impl::PathGet;
|
||||
use crate::sys::host_impl;
|
||||
use crate::sys::unix::str_to_cstring;
|
||||
use crate::{host, Error, Result};
|
||||
use nix::libc::{self, c_long, c_void};
|
||||
use std::convert::TryInto;
|
||||
use std::ffi::CString;
|
||||
use std::fs::File;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
@@ -14,7 +13,7 @@ pub(crate) fn path_unlink_file(resolved: PathGet) -> Result<()> {
|
||||
use nix::errno;
|
||||
use nix::libc::unlinkat;
|
||||
|
||||
let path_cstr = resolved.path_cstring()?;
|
||||
let path_cstr = str_to_cstring(resolved.path())?;
|
||||
|
||||
// nix doesn't expose unlinkat() yet
|
||||
let res = unsafe { unlinkat(resolved.dirfd().as_raw_fd(), path_cstr.as_ptr(), 0) };
|
||||
@@ -29,7 +28,7 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> {
|
||||
use nix::{errno::Errno, libc::symlinkat};
|
||||
|
||||
let old_path_cstr = str_to_cstring(old_path)?;
|
||||
let new_path_cstr = resolved.path_cstring()?;
|
||||
let new_path_cstr = str_to_cstring(resolved.path())?;
|
||||
|
||||
log::debug!("path_symlink old_path = {:?}", old_path);
|
||||
log::debug!("path_symlink resolved = {:?}", resolved);
|
||||
@@ -50,8 +49,8 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> {
|
||||
|
||||
pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> {
|
||||
use nix::libc::renameat;
|
||||
let old_path_cstr = resolved_old.path_cstring()?;
|
||||
let new_path_cstr = resolved_new.path_cstring()?;
|
||||
let old_path_cstr = str_to_cstring(resolved_old.path())?;
|
||||
let new_path_cstr = str_to_cstring(resolved_new.path())?;
|
||||
|
||||
let res = unsafe {
|
||||
renameat(
|
||||
|
||||
@@ -14,7 +14,8 @@ mod bsd;
|
||||
#[cfg(target_os = "linux")]
|
||||
mod linux;
|
||||
|
||||
use crate::Result;
|
||||
use crate::{Error, Result};
|
||||
use std::ffi::CString;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::path::Path;
|
||||
|
||||
@@ -26,6 +27,10 @@ pub(crate) fn dev_null() -> Result<File> {
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn str_to_cstring(s: &str) -> Result<CString> {
|
||||
CString::new(s.as_bytes()).map_err(|_| Error::EILSEQ)
|
||||
}
|
||||
|
||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
|
||||
File::open(path).map_err(Into::into)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user