diff options
| author | Aaron Turon <aturon@mozilla.com> | 2014-05-02 10:56:26 -0700 |
|---|---|---|
| committer | Aaron Turon <aturon@mozilla.com> | 2014-05-05 15:24:36 -0700 |
| commit | 8d1d7d9b5f3920d70b1edcc258a86106527e83f7 (patch) | |
| tree | 2c699707e94f9c3676bfdc3ee0492d728d3ff641 /src/libstd | |
| parent | c00d8fd9a07244a13d1cc7162a80c3d618935ce8 (diff) | |
| download | rust-8d1d7d9b5f3920d70b1edcc258a86106527e83f7.tar.gz rust-8d1d7d9b5f3920d70b1edcc258a86106527e83f7.zip | |
Change std::io::FilePermission to a typesafe representation
This patch changes `std::io::FilePermissions` from an exposed `u32` representation to a typesafe representation (that only allows valid flag combinations) using the `std::bitflags`, thus ensuring a greater degree of safety on the Rust side. Despite the change to the type, most code should continue to work as-is, sincde the new type provides bit operations in the style of C flags. To get at the underlying integer representation, use the `bits` method; to (unsafely) convert to `FilePermissions`, use `FilePermissions::from_bits`. Closes #6085. [breaking-change]
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io/fs.rs | 6 | ||||
| -rw-r--r-- | src/libstd/io/mod.rs | 71 |
2 files changed, 41 insertions, 36 deletions
diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs index cd304250b19..6d48b9eee35 100644 --- a/src/libstd/io/fs.rs +++ b/src/libstd/io/fs.rs @@ -1119,7 +1119,7 @@ mod test { check!(File::create(&input)); check!(chmod(&input, io::UserRead)); check!(copy(&input, &out)); - assert!(check!(out.stat()).perm & io::UserWrite == 0); + assert!(!check!(out.stat()).perm.intersects(io::UserWrite)); check!(chmod(&input, io::UserFile)); check!(chmod(&out, io::UserFile)); @@ -1193,9 +1193,9 @@ mod test { let file = tmpdir.join("in.txt"); check!(File::create(&file)); - assert!(check!(stat(&file)).perm & io::UserWrite == io::UserWrite); + assert!(check!(stat(&file)).perm.contains(io::UserWrite)); check!(chmod(&file, io::UserRead)); - assert!(check!(stat(&file)).perm & io::UserWrite == 0); + assert!(!check!(stat(&file)).perm.contains(io::UserWrite)); match chmod(&tmpdir.join("foo"), io::UserRWX) { Ok(..) => fail!("wanted a failure"), diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index d948738ac56..ff276d02028 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -224,6 +224,7 @@ use fmt; use int; use iter::Iterator; use libc; +use ops::{BitOr, BitAnd, Sub}; use os; use option::{Option, Some, None}; use path::Path; @@ -1558,36 +1559,40 @@ pub struct UnstableFileStat { pub gen: u64, } -/// A set of permissions for a file or directory is represented by a set of -/// flags which are or'd together. -pub type FilePermission = u32; - -// Each permission bit -pub static UserRead: FilePermission = 0x100; -pub static UserWrite: FilePermission = 0x080; -pub static UserExecute: FilePermission = 0x040; -pub static GroupRead: FilePermission = 0x020; -pub static GroupWrite: FilePermission = 0x010; -pub static GroupExecute: FilePermission = 0x008; -pub static OtherRead: FilePermission = 0x004; -pub static OtherWrite: FilePermission = 0x002; -pub static OtherExecute: FilePermission = 0x001; - -// Common combinations of these bits -pub static UserRWX: FilePermission = UserRead | UserWrite | UserExecute; -pub static GroupRWX: FilePermission = GroupRead | GroupWrite | GroupExecute; -pub static OtherRWX: FilePermission = OtherRead | OtherWrite | OtherExecute; - -/// A set of permissions for user owned files, this is equivalent to 0644 on -/// unix-like systems. -pub static UserFile: FilePermission = UserRead | UserWrite | GroupRead | OtherRead; -/// A set of permissions for user owned directories, this is equivalent to 0755 -/// on unix-like systems. -pub static UserDir: FilePermission = UserRWX | GroupRead | GroupExecute | - OtherRead | OtherExecute; -/// A set of permissions for user owned executables, this is equivalent to 0755 -/// on unix-like systems. -pub static UserExec: FilePermission = UserDir; - -/// A mask for all possible permission bits -pub static AllPermissions: FilePermission = 0x1ff; +bitflags!( + #[doc="A set of permissions for a file or directory is represented +by a set of flags which are or'd together."] + #[deriving(Hash)] + #[deriving(Show)] + flags FilePermission: u32 { + static UserRead = 0o400, + static UserWrite = 0o200, + static UserExecute = 0o100, + static GroupRead = 0o040, + static GroupWrite = 0o020, + static GroupExecute = 0o010, + static OtherRead = 0o004, + static OtherWrite = 0o002, + static OtherExecute = 0o001, + + static UserRWX = UserRead.bits | UserWrite.bits | UserExecute.bits, + static GroupRWX = GroupRead.bits | GroupWrite.bits | GroupExecute.bits, + static OtherRWX = OtherRead.bits | OtherWrite.bits | OtherExecute.bits, + + #[doc="Permissions for user owned files, equivalent to 0644 on +unix-like systems."] + static UserFile = UserRead.bits | UserWrite.bits | GroupRead.bits | OtherRead.bits, + + #[doc="Permissions for user owned directories, equivalent to 0755 on +unix-like systems."] + static UserDir = UserRWX.bits | GroupRead.bits | GroupExecute.bits | + OtherRead.bits | OtherExecute.bits, + + #[doc="Permissions for user owned executables, equivalent to 0755 +on unix-like systems."] + static UserExec = UserDir.bits, + + #[doc="All possible permissions enabled."] + static AllPermissions = UserRWX.bits | GroupRWX.bits | OtherRWX.bits + } +) |
