1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
use crate::io; use crate::path::Path; use crate::task::spawn_blocking; /// Reads metadata for a path. /// /// This function will traverse symbolic links to read metadata for the target file or directory. /// If you want to read metadata without following symbolic links, use [`symlink_metadata`] /// instead. /// /// This function is an async version of [`std::fs::metadata`]. /// /// [`symlink_metadata`]: fn.symlink_metadata.html /// [`std::fs::metadata`]: https://doc.rust-lang.org/std/fs/fn.metadata.html /// /// # Errors /// /// An error will be returned in the following situations: /// /// * `path` does not point to an existing file or directory. /// * The current process lacks permissions to read metadata for the path. /// * Some other I/O error occurred. /// /// # Examples /// /// ```no_run /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { /// # /// use async_std::fs; /// /// let perm = fs::metadata("a.txt").await?.permissions(); /// # /// # Ok(()) }) } /// ``` pub async fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> { let path = path.as_ref().to_owned(); spawn_blocking(move || std::fs::metadata(path)).await } cfg_not_docs! { pub use std::fs::Metadata; } cfg_docs! { use std::time::SystemTime; use crate::fs::{FileType, Permissions}; /// Metadata for a file or directory. /// /// Metadata is returned by [`metadata`] and [`symlink_metadata`]. /// /// This type is a re-export of [`std::fs::Metadata`]. /// /// [`metadata`]: fn.metadata.html /// [`symlink_metadata`]: fn.symlink_metadata.html /// [`is_dir`]: #method.is_dir /// [`is_file`]: #method.is_file /// [`std::fs::Metadata`]: https://doc.rust-lang.org/std/fs/struct.Metadata.html #[derive(Clone, Debug)] pub struct Metadata { _private: (), } impl Metadata { /// Returns the file type from this metadata. /// /// # Examples /// /// ```no_run /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { /// # /// use async_std::fs; /// /// let metadata = fs::metadata("a.txt").await?; /// println!("{:?}", metadata.file_type()); /// # /// # Ok(()) }) } /// ``` pub fn file_type(&self) -> FileType { unreachable!("this impl only appears in the rendered docs") } /// Returns `true` if this metadata is for a regular directory. /// /// If this metadata is for a symbolic link, this method returns `false`. /// /// # Examples /// /// ```no_run /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { /// # /// use async_std::fs; /// /// let metadata = fs::metadata(".").await?; /// println!("{:?}", metadata.is_dir()); /// # /// # Ok(()) }) } /// ``` pub fn is_dir(&self) -> bool { unreachable!("this impl only appears in the rendered docs") } /// Returns `true` if this metadata is for a regular file. /// /// If this metadata is for a symbolic link, this method returns `false`. /// /// # Examples /// /// ```no_run /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { /// # /// use async_std::fs; /// /// let metadata = fs::metadata("a.txt").await?; /// println!("{:?}", metadata.is_file()); /// # /// # Ok(()) }) } /// ``` pub fn is_file(&self) -> bool { unreachable!("this impl only appears in the rendered docs") } /// Returns the file size in bytes. /// /// # Examples /// /// ```no_run /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { /// # /// use async_std::fs; /// /// let metadata = fs::metadata("a.txt").await?; /// println!("{}", metadata.len()); /// # /// # Ok(()) }) } /// ``` pub fn len(&self) -> u64 { unreachable!("this impl only appears in the rendered docs") } /// Returns the permissions from this metadata. /// /// # Examples /// /// ```no_run /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { /// # /// use async_std::fs; /// /// let metadata = fs::metadata("a.txt").await?; /// println!("{:?}", metadata.permissions()); /// # /// # Ok(()) }) } /// ``` pub fn permissions(&self) -> Permissions { unreachable!("this impl only appears in the rendered docs") } /// Returns the last modification time. /// /// # Errors /// /// This data may not be available on all platforms, in which case an error will be /// returned. /// /// # Examples /// /// ```no_run /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { /// # /// use async_std::fs; /// /// let metadata = fs::metadata("a.txt").await?; /// println!("{:?}", metadata.modified()); /// # /// # Ok(()) }) } /// ``` pub fn modified(&self) -> io::Result<SystemTime> { unreachable!("this impl only appears in the rendered docs") } /// Returns the last access time. /// /// # Errors /// /// This data may not be available on all platforms, in which case an error will be /// returned. /// /// # Examples /// /// ```no_run /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { /// # /// use async_std::fs; /// /// let metadata = fs::metadata("a.txt").await?; /// println!("{:?}", metadata.accessed()); /// # /// # Ok(()) }) } /// ``` pub fn accessed(&self) -> io::Result<SystemTime> { unreachable!("this impl only appears in the rendered docs") } /// Returns the creation time. /// /// # Errors /// /// This data may not be available on all platforms, in which case an error will be /// returned. /// /// # Examples /// /// ```no_run /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { /// # /// use async_std::fs; /// /// let metadata = fs::metadata("a.txt").await?; /// println!("{:?}", metadata.created()); /// # /// # Ok(()) }) } /// ``` pub fn created(&self) -> io::Result<SystemTime> { unreachable!("this impl only appears in the rendered docs") } } }