1use core::{cmp, ffi::c_int};
2
3use bitflags::bitflags;
4
5use crate::path::{Path, PathBuf};
6
7bitflags! {
8 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
11 pub struct FileOpenFlags: c_int {
12 const READ = 0x1;
14 const WRITE = 0x2;
16 const READWRITE = Self::READ.bits() | Self::WRITE.bits();
18 const CREATE = 0x0100;
20 const EXCL = 0x0200;
23 const TRUNCATE = 0x0400;
25 const APPEND = 0x0800;
27 }
28}
29
30#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
32#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
33pub enum FileType {
34 File,
35 Dir,
36}
37
38impl FileType {
39 pub fn is_dir(&self) -> bool {
40 *self == FileType::Dir
41 }
42
43 pub fn is_file(&self) -> bool {
44 *self == FileType::File
45 }
46}
47
48#[derive(Clone, Debug, Eq, PartialEq)]
50#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
51pub struct Metadata {
52 file_type: FileType,
53 size: usize,
54}
55
56impl Metadata {
57 pub fn new(file_type: FileType, size: usize) -> Self {
58 Self { file_type, size }
59 }
60
61 pub fn file_type(&self) -> FileType {
62 self.file_type
63 }
64
65 pub fn is_dir(&self) -> bool {
66 self.file_type().is_dir()
67 }
68
69 pub fn is_file(&self) -> bool {
70 self.file_type().is_file()
71 }
72
73 pub fn len(&self) -> usize {
74 self.size
75 }
76
77 pub fn is_empty(&self) -> bool {
78 self.size == 0
79 }
80}
81
82#[derive(Clone, Debug, Eq, PartialEq)]
83pub struct Attribute<'a> {
91 data: &'a [u8],
92 total_size: usize,
93}
94
95impl<'a> Attribute<'a> {
96 pub const MAX_SIZE: u32 = 1_022;
97
98 pub fn new(data: &'a [u8], total_size: usize) -> Self {
99 let n = cmp::min(data.len(), total_size);
100 let data = &data[..n];
101 Attribute { data, total_size }
102 }
103
104 pub fn data(&self) -> &[u8] {
105 self.data
106 }
107
108 pub fn total_size(&self) -> usize {
109 self.total_size
110 }
111}
112
113#[derive(Clone, Debug, PartialEq, Eq)]
114#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
115pub struct DirEntry {
116 file_name: PathBuf,
117 metadata: Metadata,
118 path: PathBuf,
119}
120
121impl DirEntry {
122 pub fn new(file_name: PathBuf, metadata: Metadata, path: PathBuf) -> Self {
123 Self {
124 file_name,
125 metadata,
126 path,
127 }
128 }
129
130 pub fn metadata(&self) -> Metadata {
132 self.metadata.clone()
133 }
134
135 pub fn file_type(&self) -> FileType {
137 self.metadata.file_type
138 }
139
140 pub fn file_name(&self) -> &Path {
142 &self.file_name
143 }
144
145 pub fn path(&self) -> &Path {
149 &self.path
150 }
151
152 #[doc(hidden)]
153 pub unsafe fn path_buf_mut(&mut self) -> &mut PathBuf {
156 &mut self.path
157 }
158}