From aead75797781840aadea45f5314824f06c330741 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 22 Mar 2017 17:33:40 +0100 Subject: [PATCH 1/3] reuse mknod/umask/fstat/lstat from libc --- src/sys/stat.rs | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/sys/stat.rs b/src/sys/stat.rs index f51d9bb89f..b089d3b50c 100644 --- a/src/sys/stat.rs +++ b/src/sys/stat.rs @@ -7,16 +7,6 @@ use libc::{self, mode_t}; use std::mem; use std::os::unix::io::RawFd; -mod ffi { - use libc::{c_char, c_int, mode_t, dev_t}; - pub use libc::{stat, fstat, lstat}; - - extern { - pub fn mknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -> c_int; - pub fn umask(mask: mode_t) -> mode_t; - } -} - libc_bitflags!( pub flags SFlag: mode_t { S_IFIFO, @@ -56,7 +46,7 @@ bitflags! { pub fn mknod(path: &P, kind: SFlag, perm: Mode, dev: dev_t) -> Result<()> { let res = try!(path.with_nix_path(|cstr| { unsafe { - ffi::mknod(cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev) + libc::mknod(cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev) } })); @@ -84,7 +74,7 @@ pub fn makedev(major: u64, minor: u64) -> dev_t { } pub fn umask(mode: Mode) -> Mode { - let prev = unsafe { ffi::umask(mode.bits() as mode_t) }; + let prev = unsafe { libc::umask(mode.bits() as mode_t) }; Mode::from_bits(prev).expect("[BUG] umask returned invalid Mode") } @@ -92,7 +82,7 @@ pub fn stat(path: &P) -> Result { let mut dst = unsafe { mem::uninitialized() }; let res = try!(path.with_nix_path(|cstr| { unsafe { - ffi::stat(cstr.as_ptr(), &mut dst as *mut FileStat) + libc::stat(cstr.as_ptr(), &mut dst as *mut FileStat) } })); @@ -105,7 +95,7 @@ pub fn lstat(path: &P) -> Result { let mut dst = unsafe { mem::uninitialized() }; let res = try!(path.with_nix_path(|cstr| { unsafe { - ffi::lstat(cstr.as_ptr(), &mut dst as *mut FileStat) + libc::lstat(cstr.as_ptr(), &mut dst as *mut FileStat) } })); @@ -116,7 +106,7 @@ pub fn lstat(path: &P) -> Result { pub fn fstat(fd: RawFd) -> Result { let mut dst = unsafe { mem::uninitialized() }; - let res = unsafe { ffi::fstat(fd, &mut dst as *mut FileStat) }; + let res = unsafe { libc::fstat(fd, &mut dst as *mut FileStat) }; try!(Errno::result(res)); From c8267bc3a2daae343ada151af2218fbcb7ca48bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Mon, 20 Mar 2017 21:24:02 +0100 Subject: [PATCH 2/3] README.md: update gethostname api --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cf03cac804..344b4eef18 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,8 @@ call: // libc api (unsafe, requires handling return code/errno) pub unsafe extern fn gethostname(name: *mut c_char, len: size_t) -> c_int; -// nix api (returns a nix::Result) -pub fn gethostname(name: &mut [u8]) -> Result<()>; +// nix api (returns a nix::Result) +pub fn gethostname<'a>(buffer: &'a mut [u8]) -> Result<&'a CStr>; ``` ## Supported Platforms From ad599d201012bdf607f2aace25001141c48b8d74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 24 Mar 2017 19:33:38 +0100 Subject: [PATCH 3/3] sys/statfs: use statfs from libc the previous definition were linux specific. --- CHANGELOG.md | 3 ++ src/sys/statfs.rs | 105 +++------------------------------------------- 2 files changed, 8 insertions(+), 100 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0fb9e2102..5fa342c48e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). respectively. Various functions have been changed to use these new types as arguments. ([#629](https://github.com/nix-rust/nix/pull/629)) - Promoted all Android targets to Tier 2 support +- `nix::sys::statfs::{statfs,fstatfs}` uses statfs definition from `libc::statfs` instead of own linux specific type `nix::sys::Statfs`. + Also file system type constants like `nix::sys::statfs::ADFS_SUPER_MAGIC` were removed in favor of the libc equivalent. + ([#561](https://github.com/nix-rust/nix/pull/561)) ### Removed - Removed io::Error from nix::Error and conversion from nix::Error to Errno diff --git a/src/sys/statfs.rs b/src/sys/statfs.rs index 7c61a3ab31..afa61468fd 100644 --- a/src/sys/statfs.rs +++ b/src/sys/statfs.rs @@ -1,116 +1,21 @@ use {Errno, Result, NixPath}; use std::os::unix::io::AsRawFd; +use libc; -pub mod vfs { - #[cfg(target_pointer_width = "32")] - pub mod hwdep { - use libc::{c_uint}; - pub type FsType = c_uint; - pub type BlockSize = c_uint; - pub type NameLen = c_uint; - pub type FragmentSize = c_uint; - pub type SwordType = c_uint; - } - - #[cfg(target_pointer_width = "64")] - pub mod hwdep { - use libc::{c_long}; - pub type FsType = c_long; - pub type BlockSize = c_long; - pub type NameLen = c_long; - pub type FragmentSize = c_long; - pub type SwordType = c_long; - } - - use sys::statfs::vfs::hwdep::*; - - #[repr(C)] - #[derive(Debug,Copy,Clone)] - pub struct Statfs { - pub f_type: FsType, - pub f_bsize: BlockSize, - pub f_blocks: u64, - pub f_bfree: u64, - pub f_bavail: u64, - pub f_files: u64, - pub f_ffree: u64, - pub f_fsid: u64, - pub f_namelen: NameLen, - pub f_frsize: FragmentSize, - pub f_spare: [SwordType; 5], - } - - pub const ADFS_SUPER_MAGIC : FsType = 0xadf5; - pub const AFFS_SUPER_MAGIC : FsType = 0xADFF; - pub const BEFS_SUPER_MAGIC : FsType = 0x42465331; - pub const BFS_MAGIC : FsType = 0x1BADFACE; - pub const CIFS_MAGIC_NUMBER : FsType = 0xFF534D42; - pub const CODA_SUPER_MAGIC : FsType = 0x73757245; - pub const COH_SUPER_MAGIC : FsType = 0x012FF7B7; - pub const CRAMFS_MAGIC : FsType = 0x28cd3d45; - pub const DEVFS_SUPER_MAGIC : FsType = 0x1373; - pub const EFS_SUPER_MAGIC : FsType = 0x00414A53; - pub const EXT_SUPER_MAGIC : FsType = 0x137D; - pub const EXT2_OLD_SUPER_MAGIC : FsType = 0xEF51; - pub const EXT2_SUPER_MAGIC : FsType = 0xEF53; - pub const EXT3_SUPER_MAGIC : FsType = 0xEF53; - pub const EXT4_SUPER_MAGIC : FsType = 0xEF53; - pub const HFS_SUPER_MAGIC : FsType = 0x4244; - pub const HPFS_SUPER_MAGIC : FsType = 0xF995E849; - pub const HUGETLBFS_MAGIC : FsType = 0x958458f6; - pub const ISOFS_SUPER_MAGIC : FsType = 0x9660; - pub const JFFS2_SUPER_MAGIC : FsType = 0x72b6; - pub const JFS_SUPER_MAGIC : FsType = 0x3153464a; - pub const MINIX_SUPER_MAGIC : FsType = 0x137F; /* orig. minix */ - pub const MINIX_SUPER_MAGIC2 : FsType = 0x138F; /* 30 char minix */ - pub const MINIX2_SUPER_MAGIC : FsType = 0x2468; /* minix V2 */ - pub const MINIX2_SUPER_MAGIC2 : FsType = 0x2478; /* minix V2, 30 char names */ - pub const MSDOS_SUPER_MAGIC : FsType = 0x4d44; - pub const NCP_SUPER_MAGIC : FsType = 0x564c; - pub const NFS_SUPER_MAGIC : FsType = 0x6969; - pub const NTFS_SB_MAGIC : FsType = 0x5346544e; - pub const OPENPROM_SUPER_MAGIC : FsType = 0x9fa1; - pub const PROC_SUPER_MAGIC : FsType = 0x9fa0; - pub const QNX4_SUPER_MAGIC : FsType = 0x002f; - pub const REISERFS_SUPER_MAGIC : FsType = 0x52654973; - pub const ROMFS_MAGIC : FsType = 0x7275; - pub const SMB_SUPER_MAGIC : FsType = 0x517B; - pub const SYSV2_SUPER_MAGIC : FsType = 0x012FF7B6; - pub const SYSV4_SUPER_MAGIC : FsType = 0x012FF7B5; - pub const TMPFS_MAGIC : FsType = 0x01021994; - pub const UDF_SUPER_MAGIC : FsType = 0x15013346; - pub const UFS_MAGIC : FsType = 0x00011954; - pub const USBDEVICE_SUPER_MAGIC : FsType = 0x9fa2; - pub const VXFS_SUPER_MAGIC : FsType = 0xa501FCF5; - pub const XENIX_SUPER_MAGIC : FsType = 0x012FF7B4; - pub const XFS_SUPER_MAGIC : FsType = 0x58465342; - pub const _XIAFS_SUPER_MAGIC : FsType = 0x012FD16D; -} - -mod ffi { - use libc::{c_int,c_char}; - use sys::statfs::vfs; - - extern { - pub fn statfs(path: * const c_char, buf: *mut vfs::Statfs) -> c_int; - pub fn fstatfs(fd: c_int, buf: *mut vfs::Statfs) -> c_int; - } -} - -pub fn statfs(path: &P, stat: &mut vfs::Statfs) -> Result<()> { +pub fn statfs(path: &P, stat: &mut libc::statfs) -> Result<()> { unsafe { Errno::clear(); let res = try!( - path.with_nix_path(|path| ffi::statfs(path.as_ptr(), stat)) + path.with_nix_path(|path| libc::statfs(path.as_ptr(), stat)) ); Errno::result(res).map(drop) } } -pub fn fstatfs(fd: &T, stat: &mut vfs::Statfs) -> Result<()> { +pub fn fstatfs(fd: &T, stat: &mut libc::statfs) -> Result<()> { unsafe { Errno::clear(); - Errno::result(ffi::fstatfs(fd.as_raw_fd(), stat)).map(drop) + Errno::result(libc::fstatfs(fd.as_raw_fd(), stat)).map(drop) } }