nc/platform/linux-types/linux/fs_types.rs
1// Copyright (c) 2020 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
2// Use of this source is governed by Apache-2.0 License that can be found
3// in the LICENSE file.
4
5//! From `include/linux/fs_types.h`
6//!
7//! This is a header for the common implementation of dirent
8//! to fs on-disk file type conversion.
9//!
10//! Although the fs on-disk bits are specific to every file system, in practice,
11//! many file systems use the exact same on-disk format to describe
12//! the lower 3 file type bits that represent the 7 POSIX file types.
13//!
14//! It is important to note that the definitions in this
15//! header MUST NOT change. This would break both the
16//! userspace ABI and the on-disk format of filesystems
17//! using this code.
18//!
19//! All those file systems can use this generic code for the
20//! conversions.
21
22use crate::{mode_t, S_IFMT};
23
24/// struct dirent file types
25/// exposed to user via `getdents(2)`, `readdir(3)`
26///
27/// These match bits 12..15 of `stat.st_mode`
28/// (ie `(i_mode >> 12) & 15`).
29pub const S_DT_SHIFT: mode_t = 12;
30
31#[must_use]
32#[inline]
33pub const fn s_dt(mode: mode_t) -> u8 {
34 ((mode & S_IFMT) >> S_DT_SHIFT) as u8
35}
36
37#[allow(clippy::cast_possible_truncation)]
38pub const S_DT_MASK: u8 = (S_IFMT >> S_DT_SHIFT) as u8;
39
40/// these are defined by POSIX and also present in glibc's dirent.h
41pub const DT_UNKNOWN: u8 = 0;
42pub const DT_FIFO: u8 = 1;
43pub const DT_CHR: u8 = 2;
44pub const DT_DIR: u8 = 4;
45pub const DT_BLK: u8 = 6;
46pub const DT_REG: u8 = 8;
47pub const DT_LNK: u8 = 10;
48pub const DT_SOCK: u8 = 12;
49pub const DT_WHT: u8 = 14;
50
51/// 16
52pub const DT_MAX: u8 = S_DT_MASK + 1;
53
54/// fs on-disk file types.
55///
56/// Only the low 3 bits are used for the POSIX file types.
57/// Other bits are reserved for fs private use.
58/// These definitions are shared and used by multiple filesystems,
59/// and MUST NOT change under any circumstances.
60///
61/// Note that no fs currently stores the whiteout type on-disk,
62/// so whiteout dirents are exposed to user as `DT_CHR`.
63pub const FT_UNKNOWN: u8 = 0;
64pub const FT_REG_FILE: u8 = 1;
65pub const FT_DIR: u8 = 2;
66pub const FT_CHRDEV: u8 = 3;
67pub const FT_BLKDEV: u8 = 4;
68pub const FT_FIFO: u8 = 5;
69pub const FT_SOCK: u8 = 6;
70pub const FT_SYMLINK: u8 = 7;
71pub const FT_MAX: u8 = 8;