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
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::file_type::FileType;
use crate::inode::Inode;

/// Metadata information about a file.
pub struct Metadata {
    inode: Inode,
}

impl Metadata {
    pub(crate) fn new(inode: Inode) -> Self {
        Self { inode }
    }

    pub fn file_type(&self) -> FileType {
        self.inode.file_type
    }

    pub fn is_dir(&self) -> bool {
        self.inode.file_type.is_dir()
    }

    pub fn is_symlink(&self) -> bool {
        self.inode.file_type.is_symlink()
    }

    /// Get the size in bytes of the file.
    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> u64 {
        self.inode.size_in_bytes
    }

    /// Get the file's UNIX permission bits.
    pub fn mode(&self) -> u32 {
        let mode = self.inode.mode.bits() & 0xfff;
        // Convert from u16 to u32 to match the std `PermissionsExt` interface.
        u32::from(mode)
    }
}