cloud_filter/
metadata.rs

1use std::fs;
2
3use nt_time::FileTime;
4use windows::Win32::Storage::{
5    CloudFilters::CF_FS_METADATA,
6    FileSystem::{FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_NORMAL, FILE_BASIC_INFO},
7};
8
9use crate::sealed;
10
11/// The metadata for placeholder.
12#[derive(Debug, Clone, Copy, Default)]
13pub struct Metadata(pub(crate) CF_FS_METADATA);
14
15impl Metadata {
16    /// The default [Metadata] with `FILE_ATTRIBUTE_NORMAL` attribute.
17    pub fn file() -> Self {
18        Self(CF_FS_METADATA {
19            BasicInfo: FILE_BASIC_INFO {
20                FileAttributes: FILE_ATTRIBUTE_NORMAL.0,
21                ..Default::default()
22            },
23            ..Default::default()
24        })
25    }
26
27    /// The default [Metadata] with `FILE_ATTRIBUTE_DIRECTORY` attribute.
28    pub fn directory() -> Self {
29        Self(CF_FS_METADATA {
30            BasicInfo: FILE_BASIC_INFO {
31                FileAttributes: FILE_ATTRIBUTE_DIRECTORY.0,
32                ..Default::default()
33            },
34            ..Default::default()
35        })
36    }
37
38    /// The time the file/directory was created.
39    pub fn created(mut self, time: FileTime) -> Self {
40        self.0.BasicInfo.CreationTime = time.try_into().unwrap();
41        self
42    }
43
44    /// The time the file/directory was last accessed.
45    pub fn accessed(mut self, time: FileTime) -> Self {
46        self.0.BasicInfo.LastAccessTime = time.try_into().unwrap();
47        self
48    }
49
50    /// The time the file/directory content was last written.
51    pub fn written(mut self, time: FileTime) -> Self {
52        self.0.BasicInfo.LastWriteTime = time.try_into().unwrap();
53        self
54    }
55
56    /// The time the file/directory content or metadata was changed.
57    pub fn changed(mut self, time: FileTime) -> Self {
58        self.0.BasicInfo.ChangeTime = time.try_into().unwrap();
59        self
60    }
61
62    /// The size of the file's content.
63    pub fn size(mut self, size: u64) -> Self {
64        self.0.FileSize = size as i64;
65        self
66    }
67
68    /// File attributes.
69    pub fn attributes(mut self, attributes: u32) -> Self {
70        self.0.BasicInfo.FileAttributes |= attributes;
71        self
72    }
73}
74
75pub trait MetadataExt: sealed::Sealed {
76    /// The time the file was changed in
77    /// [FILETIME](https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime) format.
78    fn change_time(self, time: i64) -> Self;
79
80    /// The time the file was last accessed in
81    /// [FILETIME](https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime) format.
82    fn last_access_time(self, time: i64) -> Self;
83
84    /// The time the file was last written to in
85    /// [FILETIME](https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime) format.
86    fn last_write_time(self, time: i64) -> Self;
87
88    /// The time the file was created in
89    /// [FILETIME](https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime) format.
90    fn creation_time(self, time: i64) -> Self;
91}
92
93impl MetadataExt for Metadata {
94    fn change_time(mut self, time: i64) -> Self {
95        self.0.BasicInfo.ChangeTime = time;
96        self
97    }
98
99    fn last_access_time(mut self, time: i64) -> Self {
100        self.0.BasicInfo.LastAccessTime = time;
101        self
102    }
103
104    fn last_write_time(mut self, time: i64) -> Self {
105        self.0.BasicInfo.LastWriteTime = time;
106        self
107    }
108
109    fn creation_time(mut self, time: i64) -> Self {
110        self.0.BasicInfo.CreationTime = time;
111        self
112    }
113}
114
115impl sealed::Sealed for Metadata {}
116
117impl From<fs::Metadata> for Metadata {
118    fn from(metadata: fs::Metadata) -> Self {
119        use std::os::windows::fs::MetadataExt;
120        Self(CF_FS_METADATA {
121            BasicInfo: FILE_BASIC_INFO {
122                CreationTime: metadata.creation_time() as i64,
123                LastAccessTime: metadata.last_access_time() as i64,
124                LastWriteTime: metadata.last_write_time() as i64,
125                ChangeTime: metadata.last_write_time() as i64,
126                FileAttributes: metadata.file_attributes(),
127            },
128            FileSize: metadata.file_size() as i64,
129        })
130    }
131}