Skip to content

Commit

Permalink
Add string utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusGrass committed Oct 10, 2023
1 parent b04cac3 commit 2c17da9
Show file tree
Hide file tree
Showing 14 changed files with 572 additions and 44 deletions.
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
rustflags = [
"-Wclippy::all",
"-Wclippy::pedantic",
"-Wclippy::mod_module_files",
"-Aclippy::cast_lossless",
"-Aclippy::cast_possible_truncation",
"-Aclippy::cast_sign_loss",
Expand Down
4 changes: 3 additions & 1 deletion rusl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ integration-test = []

[dependencies]
linux-rust-bindings = { version = "0.1.1", features = ["all"] }
sc = "0.2.7"
sc = "0.2.7"

[dev-dependencies]
4 changes: 4 additions & 0 deletions rusl/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

### Added
- Utility methods for `UnixStr` to make it easier to navigate them
as paths
- Find-method for `UnixStr`
- Accessors for some inner fields of `Statx`

### Changed

Expand Down
1 change: 1 addition & 0 deletions rusl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![cfg_attr(not(test), no_std)]
#![warn(clippy::pedantic)]
#![allow(clippy::module_name_repetitions, clippy::similar_names)]
#![cfg_attr(test, allow(clippy::ignored_unit_patterns))]

#[cfg(feature = "alloc")]
extern crate alloc;
Expand Down
10 changes: 8 additions & 2 deletions rusl/src/platform/compat/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,20 @@ impl SocketAddress {

/// Tries to construct a `SocketAddress` from a `UnixStr` path
/// # Errors
/// The path is longer than 108 bytes (null termination included)
/// The path is longer than 108 bytes (null termination included).
/// The path contains byte values out of the 7-bit ASCII range.
pub fn try_from_unix(path: &UnixStr) -> crate::Result<SocketArg> {
let mut ind = 0;
let buf = unsafe {
let mut buf = [0; 108];
let mut ptr = path.as_ptr();
while !ptr.is_null() {
buf[ind] = ptr.read() as core::ffi::c_char;
let val = core::ffi::c_char::try_from(ptr.read()).map_err(|_e| {
Error::no_code(
"Socket paths need to be 7-bit ASCII, path contained value in 8-bit range",
)
})?;
buf[ind] = val;
if ind == 107 && buf[ind] != 0 {
return Err(Error::no_code("Socket address too long"));
} else if buf[ind] == 0 {
Expand Down
47 changes: 47 additions & 0 deletions rusl/src/platform/compat/stat.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@
use crate::platform::numbers::NonNegativeI32;
use crate::platform::TimeSpec;

pub type Stat = linux_rust_bindings::stat::stat;

#[repr(transparent)]
#[derive(Debug)]
pub struct Statx(pub(crate) linux_rust_bindings::stat::statx);

impl Statx {
#[inline]
#[must_use]
pub const fn mask(&self) -> StatxMask {
StatxMask(self.0.stx_mask)
}

#[inline]
#[must_use]
pub const fn size(&self) -> u64 {
self.0.stx_size
}

/// Last access
#[inline]
#[must_use]
pub const fn access_time(&self) -> TimeSpec {
let ts = self.0.stx_atime;
TimeSpec::new(ts.tv_sec, ts.tv_nsec as i64)
}

/// Creation
#[inline]
#[must_use]
pub const fn birth_time(&self) -> TimeSpec {
let ts = self.0.stx_btime;
TimeSpec::new(ts.tv_sec, ts.tv_nsec as i64)
}

/// Content modification
#[inline]
#[must_use]
pub const fn modified_time(&self) -> TimeSpec {
let ts = self.0.stx_mtime;
TimeSpec::new(ts.tv_sec, ts.tv_nsec as i64)
}

/// Metadata modification
#[inline]
#[must_use]
pub const fn changed_time(&self) -> TimeSpec {
let ts = self.0.stx_ctime;
TimeSpec::new(ts.tv_sec, ts.tv_nsec as i64)
}
}

transparent_bitflags! {
pub struct StatxMask: u32 {
const DEFAULT = 0;
Expand Down
Loading

0 comments on commit 2c17da9

Please sign in to comment.