Welcome to mirror list, hosted at ThFree Co, Russian Federation.

stat.rs « unix « os « src - github.com/windirstat/walkdir.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a367ed14aa06807115cf10b492a06c3dcf310683 (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use std::ffi::{CStr, CString, OsString};
use std::fmt;
use std::io;
use std::mem;
use std::os::unix::ffi::OsStringExt;
use std::os::unix::io::RawFd;
use std::path::PathBuf;
use std::time::{Duration, SystemTime};

use libc;

#[cfg(not(any(target_os = "linux", target_os = "android",)))]
use libc::{fstatat as fstatat64, lstat as lstat64, stat as stat64};
#[cfg(any(target_os = "linux", target_os = "android",))]
use libc::{fstatat64, lstat64, stat64};

pub struct Metadata {
    stat: stat64,
}

impl Metadata {
    pub fn file_type(&self) -> FileType {
        FileType::from_stat_mode(self.stat.st_mode as u64)
    }

    pub fn len(&self) -> u64 {
        self.stat.st_size as u64
    }

    pub fn dev(&self) -> u64 {
        self.stat.st_dev
    }

    pub fn ino(&self) -> u64 {
        self.stat.st_ino
    }

    pub fn mode(&self) -> u64 {
        self.stat.st_mode as u64
    }

    pub fn permissions(&self) -> ! {
        unimplemented!()
    }
}

#[cfg(target_os = "netbsd")]
impl Metadata {
    pub fn modified(&self) -> io::Result<SystemTime> {
        let dur = Duration::new(
            self.stat.st_mtime as u64,
            self.stat.st_mtimensec as u32,
        );
        Ok(SystemTime::UNIX_EPOCH + dur)
    }

    pub fn accessed(&self) -> io::Result<SystemTime> {
        let dur = Duration::new(
            self.stat.st_atime as u64,
            self.stat.st_atimensec as u32,
        );
        Ok(SystemTime::UNIX_EPOCH + dur)
    }

    pub fn created(&self) -> io::Result<SystemTime> {
        let dur = Duration::new(
            self.stat.st_birthtime as u64,
            self.stat.st_birthtimensec as u32,
        );
        Ok(SystemTime::UNIX_EPOCH + dur)
    }
}

#[cfg(not(target_os = "netbsd"))]
impl Metadata {
    pub fn modified(&self) -> io::Result<SystemTime> {
        let dur = Duration::new(
            self.stat.st_mtime as u64,
            self.stat.st_mtime_nsec as u32,
        );
        Ok(SystemTime::UNIX_EPOCH + dur)
    }

    pub fn accessed(&self) -> io::Result<SystemTime> {
        let dur = Duration::new(
            self.stat.st_atime as u64,
            self.stat.st_atime_nsec as u32,
        );
        Ok(SystemTime::UNIX_EPOCH + dur)
    }

    #[cfg(any(
        target_os = "freebsd",
        target_os = "openbsd",
        target_os = "macos",
        target_os = "ios"
    ))]
    pub fn created(&self) -> io::Result<SystemTime> {
        let dur = Duration::new(
            self.stat.st_birthtime as u64,
            self.stat.st_birthtime_nsec as u32,
        );
        Ok(SystemTime::UNIX_EPOCH + dur)
    }

    #[cfg(not(any(
        target_os = "freebsd",
        target_os = "openbsd",
        target_os = "macos",
        target_os = "ios"
    )))]
    pub fn created(&self) -> io::Result<SystemTime> {
        Err(io::Error::new(
            io::ErrorKind::Other,
            "creation time is not available on this platform currently",
        ))
    }
}

/// One of seven possible file types on Unix.
#[derive(Clone, Copy)]
pub struct FileType(libc::mode_t);

impl fmt::Debug for FileType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let human = if self.is_file() {
            "File"
        } else if self.is_dir() {
            "Directory"
        } else if self.is_symlink() {
            "Symbolic Link"
        } else if self.is_block_device() {
            "Block Device"
        } else if self.is_char_device() {
            "Char Device"
        } else if self.is_fifo() {
            "FIFO"
        } else if self.is_socket() {
            "Socket"
        } else {
            "Unknown"
        };
        write!(f, "FileType({})", human)
    }
}

impl FileType {
    /// Create a new file type from a directory entry's type field.
    ///
    /// If the given type is not recognized or is `DT_UNKNOWN`, then `None`
    /// is returned.
    pub fn from_dirent_type(d_type: u8) -> Option<FileType> {
        Some(FileType(match d_type {
            libc::DT_REG => libc::S_IFREG,
            libc::DT_DIR => libc::S_IFDIR,
            libc::DT_LNK => libc::S_IFLNK,
            libc::DT_BLK => libc::S_IFBLK,
            libc::DT_CHR => libc::S_IFCHR,
            libc::DT_FIFO => libc::S_IFIFO,
            libc::DT_SOCK => libc::S_IFSOCK,
            libc::DT_UNKNOWN => return None,
            _ => return None, // wat?
        }))
    }

    /// Create a new file type from a stat's `st_mode` field.
    pub fn from_stat_mode(st_mode: u64) -> FileType {
        FileType(st_mode as libc::mode_t)
    }

    /// Convert this file type to the platform independent file type.
    pub fn into_api(self) -> crate::FileType {
        crate::FileType::from(self)
    }

    /// Returns true if this file type is a regular file.
    ///
    /// This corresponds to the `S_IFREG` value on Unix.
    pub fn is_file(&self) -> bool {
        self.0 & libc::S_IFMT == libc::S_IFREG
    }

    /// Returns true if this file type is a directory.
    ///
    /// This corresponds to the `S_IFDIR` value on Unix.
    pub fn is_dir(&self) -> bool {
        self.0 & libc::S_IFMT == libc::S_IFDIR
    }

    /// Returns true if this file type is a symbolic link.
    ///
    /// This corresponds to the `S_IFLNK` value on Unix.
    pub fn is_symlink(&self) -> bool {
        self.0 & libc::S_IFMT == libc::S_IFLNK
    }

    /// Returns true if this file type is a block device.
    ///
    /// This corresponds to the `S_IFBLK` value on Unix.
    pub fn is_block_device(&self) -> bool {
        self.0 & libc::S_IFMT == libc::S_IFBLK
    }

    /// Returns true if this file type is a character device.
    ///
    /// This corresponds to the `S_IFCHR` value on Unix.
    pub fn is_char_device(&self) -> bool {
        self.0 & libc::S_IFMT == libc::S_IFCHR
    }

    /// Returns true if this file type is a FIFO.
    ///
    /// This corresponds to the `S_IFIFO` value on Unix.
    pub fn is_fifo(&self) -> bool {
        self.0 & libc::S_IFMT == libc::S_IFIFO
    }

    /// Returns true if this file type is a socket.
    ///
    /// This corresponds to the `S_IFSOCK` value on Unix.
    pub fn is_socket(&self) -> bool {
        self.0 & libc::S_IFMT == libc::S_IFSOCK
    }
}

pub fn stat<P: Into<PathBuf>>(path: P) -> io::Result<Metadata> {
    let bytes = path.into().into_os_string().into_vec();
    stat_c(&CString::new(bytes)?)
}

pub fn stat_c(path: &CStr) -> io::Result<Metadata> {
    let mut stat: stat64 = unsafe { mem::zeroed() };
    let res = unsafe { stat64(path.as_ptr(), &mut stat) };
    if res < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(Metadata { stat })
    }
}

pub fn lstat<P: Into<PathBuf>>(path: P) -> io::Result<Metadata> {
    let bytes = path.into().into_os_string().into_vec();
    lstat_c(&CString::new(bytes)?)
}

pub fn lstat_c(path: &CStr) -> io::Result<Metadata> {
    let mut stat: stat64 = unsafe { mem::zeroed() };
    let res = unsafe { lstat64(path.as_ptr(), &mut stat) };
    if res < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(Metadata { stat })
    }
}

pub fn statat<N: Into<OsString>>(
    parent_dirfd: RawFd,
    name: N,
) -> io::Result<Metadata> {
    let bytes = name.into().into_vec();
    statat_c(parent_dirfd, &CString::new(bytes)?)
}

pub fn statat_c(parent_dirfd: RawFd, name: &CStr) -> io::Result<Metadata> {
    let mut stat: stat64 = unsafe { mem::zeroed() };
    let res = unsafe { fstatat64(parent_dirfd, name.as_ptr(), &mut stat, 0) };
    if res < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(Metadata { stat })
    }
}

pub fn lstatat<N: Into<OsString>>(
    parent_dirfd: RawFd,
    name: N,
) -> io::Result<Metadata> {
    let bytes = name.into().into_vec();
    lstatat_c(parent_dirfd, &CString::new(bytes)?)
}

pub fn lstatat_c(parent_dirfd: RawFd, name: &CStr) -> io::Result<Metadata> {
    let mut stat: stat64 = unsafe { mem::zeroed() };
    let res = unsafe {
        fstatat64(
            parent_dirfd,
            name.as_ptr(),
            &mut stat,
            libc::AT_SYMLINK_NOFOLLOW,
        )
    };
    if res < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(Metadata { stat })
    }
}