]> git.lizzy.rs Git - rust.git/commitdiff
Print file permissions in octal form.
authorJonas Hietala <tradet.h@gmail.com>
Wed, 3 Sep 2014 21:57:02 +0000 (23:57 +0200)
committerJonas Hietala <tradet.h@gmail.com>
Wed, 3 Sep 2014 21:59:22 +0000 (23:59 +0200)
Closes #16943.

src/libstd/io/fs.rs
src/libstd/io/mod.rs

index 725d3b10def85f17daa3b54588eacb92f19ccf11..1313bf5c1054f0154afc45fd830bb5c28526e0f4 100644 (file)
@@ -1274,7 +1274,7 @@ pub fn tmpdir() -> TempDir {
 
         error!(result, "couldn't recursively mkdir");
         error!(result, "couldn't create directory");
-        error!(result, "mode=FilePermission { bits: 448 }");
+        error!(result, "mode=700");
         error!(result, format!("path={}", file.display()));
     })
 
index 905012b7bf33abf1ec4024bba353d81f0e83888f..cc51ac8e3baf35351e1c67c3b84125573b943f57 100644 (file)
@@ -1797,7 +1797,6 @@ pub struct UnstableFileStat {
 bitflags!(
     #[doc="A set of permissions for a file or directory is represented
 by a set of flags which are or'd together."]
-    #[deriving(Show)]
     flags FilePermission: u32 {
         static UserRead     = 0o400,
         static UserWrite    = 0o200,
@@ -1836,6 +1835,14 @@ impl Default for FilePermission {
     fn default() -> FilePermission { FilePermission::empty() }
 }
 
+impl fmt::Show for FilePermission {
+    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+        formatter.fill = '0';
+        formatter.width = Some(3);
+        (&self.bits as &fmt::Octal).fmt(formatter)
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::{IoResult, Reader, MemReader, NoProgress, InvalidInput};
@@ -1937,4 +1944,18 @@ fn test_push_at_least() {
         let mut r = MemReader::new(Vec::from_slice(b"hello, world!"));
         assert_eq!(r.push_at_least(5, 1, &mut buf).unwrap_err().kind, InvalidInput);
     }
+
+    #[test]
+    fn test_show() {
+        use super::*;
+
+        assert_eq!(format!("{}", UserRead), "400".to_string());
+        assert_eq!(format!("{}", UserFile), "644".to_string());
+        assert_eq!(format!("{}", UserExec), "755".to_string());
+        assert_eq!(format!("{}", UserRWX),  "700".to_string());
+        assert_eq!(format!("{}", GroupRWX), "070".to_string());
+        assert_eq!(format!("{}", OtherRWX), "007".to_string());
+        assert_eq!(format!("{}", AllPermissions), "777".to_string());
+        assert_eq!(format!("{}", UserRead | UserWrite | OtherWrite), "602".to_string());
+    }
 }