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#[derive(Debug, Clone, Copy, Default)]
13pub struct Metadata(pub(crate) CF_FS_METADATA);
14
15impl Metadata {
16 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 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 pub fn created(mut self, time: FileTime) -> Self {
40 self.0.BasicInfo.CreationTime = time.try_into().unwrap();
41 self
42 }
43
44 pub fn accessed(mut self, time: FileTime) -> Self {
46 self.0.BasicInfo.LastAccessTime = time.try_into().unwrap();
47 self
48 }
49
50 pub fn written(mut self, time: FileTime) -> Self {
52 self.0.BasicInfo.LastWriteTime = time.try_into().unwrap();
53 self
54 }
55
56 pub fn changed(mut self, time: FileTime) -> Self {
58 self.0.BasicInfo.ChangeTime = time.try_into().unwrap();
59 self
60 }
61
62 pub fn size(mut self, size: u64) -> Self {
64 self.0.FileSize = size as i64;
65 self
66 }
67
68 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 fn change_time(self, time: i64) -> Self;
79
80 fn last_access_time(self, time: i64) -> Self;
83
84 fn last_write_time(self, time: i64) -> Self;
87
88 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}