Skip to content

Commit e7e906c

Browse files
authored
Don't apply a mask on the flags in PermissionsExt.
For consistency with std, don't apply a mask to the flags bits in `PermissionsExt`. Fixes #249.
1 parent 1b60d34 commit e7e906c

File tree

5 files changed

+12
-13
lines changed

5 files changed

+12
-13
lines changed

cap-primitives/src/rustix/fs/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::io;
22

3+
#[cfg(any(target_os = "android", target_os = "linux"))]
34
#[cold]
45
pub(crate) fn invalid_flags() -> io::Error {
56
rustix::io::Errno::INVAL.into()

cap-primitives/src/rustix/fs/permissions_ext.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl PermissionsExt {
1616
pub(crate) fn from_std(std: fs::Permissions) -> Self {
1717
use std::os::unix::fs::PermissionsExt;
1818
Self {
19-
mode: std.mode() as RawMode & 0o7777,
19+
mode: std.mode() as RawMode,
2020
}
2121
}
2222

@@ -26,9 +26,7 @@ impl PermissionsExt {
2626
pub(crate) const fn from_raw_mode(mode: RawMode) -> Permissions {
2727
Permissions {
2828
readonly: Self::readonly(mode),
29-
ext: Self {
30-
mode: mode & 0o7777,
31-
},
29+
ext: Self { mode },
3230
}
3331
}
3432

cap-primitives/src/rustix/fs/set_permissions_impl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::fs::{errors, open, OpenOptions, Permissions};
1+
use crate::fs::{open, OpenOptions, Permissions};
22
use rustix::fs::{fchmod, Mode};
33
use rustix::io::Errno;
44
use std::convert::TryInto;
@@ -43,8 +43,8 @@ pub(crate) fn set_permissions_impl(
4343
}
4444

4545
pub(crate) fn set_file_permissions(file: &fs::File, perm: fs::Permissions) -> io::Result<()> {
46-
#[allow(clippy::useless_conversion)]
47-
let mode =
48-
Mode::from_bits(perm.mode().try_into().unwrap()).ok_or_else(errors::invalid_flags)?;
46+
// Use `from_bits_truncate` for compatibility with std, which allows
47+
// non-permission bits to propagate through.
48+
let mode = Mode::from_bits_truncate(perm.mode().try_into().unwrap());
4949
Ok(fchmod(file, mode)?)
5050
}

cap-primitives/src/rustix/linux/fs/set_permissions_impl.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::procfs::set_permissions_through_proc_self_fd;
2-
use crate::fs::{errors, open, OpenOptions, Permissions};
2+
use crate::fs::{open, OpenOptions, Permissions};
33
use rustix::fs::{fchmod, Mode, RawMode};
44
use std::os::unix::fs::PermissionsExt;
55
use std::path::Path;
@@ -48,6 +48,8 @@ pub(crate) fn set_permissions_impl(
4848
}
4949

5050
fn set_file_permissions(file: &fs::File, perm: fs::Permissions) -> io::Result<()> {
51-
let mode = Mode::from_bits(perm.mode() as RawMode).ok_or_else(errors::invalid_flags)?;
51+
// Use `from_bits_truncate` for compatibility with std, which allows
52+
// non-permission bits to propagate through.
53+
let mode = Mode::from_bits_truncate(perm.mode() as RawMode);
5254
Ok(fchmod(file, mode)?)
5355
}

tests/fs_additional.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -931,9 +931,7 @@ fn check_metadata(std: &std::fs::Metadata, cap: &cap_std::fs::Metadata) {
931931
#[cfg(unix)]
932932
{
933933
use std::os::unix::fs::PermissionsExt;
934-
// The standard library returns file format bits with `mode()`, whereas
935-
// cap-std currently doesn't.
936-
assert_eq!(std.permissions().mode() & 0o7777, cap.permissions().mode());
934+
assert_eq!(std.permissions().mode(), cap.permissions().mode());
937935
}
938936

939937
// If the standard library supports file modified/accessed/created times,

0 commit comments

Comments
 (0)