-
Notifications
You must be signed in to change notification settings - Fork 6
/
register.rs
382 lines (325 loc) · 11.9 KB
/
register.rs
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
use std::path::Path;
use widestring::{U16Str, U16String};
use windows::{
core::{self, GUID},
Foundation::Uri,
Storage::{
Provider::{
StorageProviderHardlinkPolicy, StorageProviderHydrationPolicy,
StorageProviderHydrationPolicyModifier, StorageProviderInSyncPolicy,
StorageProviderPopulationPolicy, StorageProviderProtectionMode,
StorageProviderSyncRootInfo, StorageProviderSyncRootManager,
},
StorageFolder,
Streams::DataWriter,
},
Win32::Storage::CloudFilters::{
self, CF_HYDRATION_POLICY_MODIFIER_USHORT, CF_HYDRATION_POLICY_PRIMARY,
CF_HYDRATION_POLICY_PRIMARY_USHORT, CF_INSYNC_POLICY, CF_POPULATION_POLICY_PRIMARY,
CF_POPULATION_POLICY_PRIMARY_USHORT,
},
};
use crate::{utility::ToHString, SyncRootId};
#[derive(Debug, Clone)]
pub struct Registration<'a> {
sync_root_id: &'a SyncRootId,
show_siblings_as_group: bool,
allow_pinning: bool,
allow_hardlinks: bool,
display_name: &'a U16Str,
recycle_bin_uri: Option<&'a U16Str>,
version: Option<&'a U16Str>,
hydration_type: HydrationType,
hydration_policy: HydrationPolicy,
population_type: PopulationType,
protection_mode: ProtectionMode,
provider_id: Option<GUID>,
supported_attributes: SupportedAttributes,
icon: U16String,
blob: Option<&'a [u8]>,
}
impl<'a> Registration<'a> {
pub fn from_sync_root_id(sync_root_id: &'a SyncRootId) -> Self {
Self {
sync_root_id,
display_name: sync_root_id.as_u16str(),
recycle_bin_uri: None,
show_siblings_as_group: false,
allow_pinning: false,
version: None,
provider_id: None,
protection_mode: ProtectionMode::Unknown,
allow_hardlinks: false,
hydration_type: HydrationType::Progressive, // stated as default in the docs
hydration_policy: HydrationPolicy::default(),
population_type: PopulationType::Full,
supported_attributes: SupportedAttributes::default(),
icon: U16String::from_str("C:\\Windows\\System32\\imageres.dll,1525"),
blob: None,
}
}
pub fn hydration_type(mut self, hydration_type: HydrationType) -> Self {
self.hydration_type = hydration_type;
self
}
pub fn allow_pinning(mut self) -> Self {
self.allow_pinning = true;
self
}
pub fn allow_hardlinks(mut self) -> Self {
self.allow_hardlinks = true;
self
}
// This field is required
pub fn display_name(mut self, display_name: &'a U16Str) -> Self {
self.display_name = display_name;
self
}
pub fn recycle_bin_uri(mut self, uri: &'a U16Str) -> Self {
self.recycle_bin_uri = Some(uri);
self
}
// I think this is for sync roots with the same provider name?
pub fn show_siblings_as_group(mut self) -> Self {
self.show_siblings_as_group = true;
self
}
pub fn population_type(mut self, population_type: PopulationType) -> Self {
self.population_type = population_type;
self
}
pub fn version(mut self, version: &'a U16Str) -> Self {
assert!(
version.len() <= CloudFilters::CF_MAX_PROVIDER_VERSION_LENGTH as usize,
"version length must not exceed {} characters, got {} characters",
CloudFilters::CF_MAX_PROVIDER_VERSION_LENGTH,
version.len()
);
self.version = Some(version);
self
}
pub fn protection_mode(mut self, protection_mode: ProtectionMode) -> Self {
self.protection_mode = protection_mode;
self
}
pub fn supported_attributes(mut self, supported_attributes: SupportedAttributes) -> Self {
self.supported_attributes = supported_attributes;
self
}
pub fn hydration_policy(mut self, hydration_policy: HydrationPolicy) -> Self {
self.hydration_policy = hydration_policy;
self
}
// TODO: this field is required
// https://docs.microsoft.com/en-us/windows/win32/menurc/icon-resource
pub fn icon(mut self, mut path: U16String, index: u16) -> Self {
path.push_str(format!(",{index}"));
self.icon = path;
self
}
pub fn blob(mut self, blob: &'a [u8]) -> Self {
assert!(
blob.len() <= 65536,
"blob size must not exceed 65536 bytes, got {} bytes",
blob.len()
);
self.blob = Some(blob);
self
}
pub fn register<P: AsRef<Path>>(&self, path: P) -> core::Result<()> {
let info = StorageProviderSyncRootInfo::new()?;
info.SetProtectionMode(self.protection_mode.into())?;
info.SetShowSiblingsAsGroup(self.show_siblings_as_group)?;
info.SetHydrationPolicy(self.hydration_type.into())?;
info.SetHydrationPolicyModifier(self.hydration_policy.0)?;
info.SetPopulationPolicy(self.population_type.into())?;
info.SetInSyncPolicy(self.supported_attributes.0)?;
info.SetDisplayNameResource(self.display_name.to_hstring())?;
info.SetIconResource(self.icon.to_hstring())?;
info.SetPath(
StorageFolder::GetFolderFromPathAsync(
&U16String::from_os_str(path.as_ref().as_os_str()).to_hstring(),
)?
.get()?,
)?;
info.SetHardlinkPolicy(if self.allow_hardlinks {
StorageProviderHardlinkPolicy::Allowed
} else {
StorageProviderHardlinkPolicy::None
})?;
info.SetId(self.sync_root_id.as_hstring())?;
if let Some(provider_id) = self.provider_id {
info.SetProviderId(provider_id)?;
}
if let Some(version) = &self.version {
info.SetVersion(version.to_hstring())?;
}
if let Some(uri) = &self.recycle_bin_uri {
info.SetRecycleBinUri(Uri::CreateUri(uri.to_hstring())?)?;
}
if let Some(blob) = &self.blob {
// TODO: implement IBuffer interface for slices to avoid a copy
let writer = DataWriter::new()?;
writer.WriteBytes(blob)?;
info.SetContext(writer.DetachBuffer()?)?;
}
StorageProviderSyncRootManager::Register(info)
}
}
#[derive(Debug, Clone, Copy)]
pub enum ProtectionMode {
Personal,
Unknown,
}
impl From<ProtectionMode> for StorageProviderProtectionMode {
fn from(mode: ProtectionMode) -> Self {
match mode {
ProtectionMode::Personal => StorageProviderProtectionMode::Personal,
ProtectionMode::Unknown => StorageProviderProtectionMode::Unknown,
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum HydrationType {
Partial,
Progressive,
Full,
AlwaysFull,
}
impl From<HydrationType> for StorageProviderHydrationPolicy {
fn from(hydration_type: HydrationType) -> Self {
match hydration_type {
HydrationType::Partial => StorageProviderHydrationPolicy::Partial,
HydrationType::Progressive => StorageProviderHydrationPolicy::Progressive,
HydrationType::Full => StorageProviderHydrationPolicy::Full,
HydrationType::AlwaysFull => StorageProviderHydrationPolicy::AlwaysFull,
}
}
}
impl From<CF_HYDRATION_POLICY_PRIMARY_USHORT> for HydrationType {
fn from(primary: CF_HYDRATION_POLICY_PRIMARY_USHORT) -> Self {
match CF_HYDRATION_POLICY_PRIMARY(primary.us) {
CloudFilters::CF_HYDRATION_POLICY_PARTIAL => HydrationType::Partial,
CloudFilters::CF_HYDRATION_POLICY_PROGRESSIVE => HydrationType::Progressive,
CloudFilters::CF_HYDRATION_POLICY_FULL => HydrationType::Full,
CloudFilters::CF_HYDRATION_POLICY_ALWAYS_FULL => HydrationType::AlwaysFull,
_ => unreachable!(),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct HydrationPolicy(pub(crate) StorageProviderHydrationPolicyModifier);
impl HydrationPolicy {
pub fn new() -> Self {
Self::default()
}
pub fn require_validation(mut self) -> Self {
self.0 |= StorageProviderHydrationPolicyModifier::ValidationRequired;
self
}
// TODO: assert this, it is incompatible with the validation required parameter
// https://docs.microsoft.com/en-us/windows/win32/api/cfapi/ne-cfapi-cf_hydration_policy_modifier
pub fn allow_streaming(mut self) -> Self {
self.0 |= StorageProviderHydrationPolicyModifier::StreamingAllowed;
self
}
pub fn allow_platform_dehydration(mut self) -> Self {
self.0 |= StorageProviderHydrationPolicyModifier::AutoDehydrationAllowed;
self
}
pub fn allow_full_restart_hydration(mut self) -> Self {
self.0 |= StorageProviderHydrationPolicyModifier::AllowFullRestartHydration;
self
}
}
impl Default for HydrationPolicy {
fn default() -> Self {
Self(StorageProviderHydrationPolicyModifier::None)
}
}
impl From<CF_HYDRATION_POLICY_MODIFIER_USHORT> for HydrationPolicy {
fn from(primary: CF_HYDRATION_POLICY_MODIFIER_USHORT) -> Self {
Self(StorageProviderHydrationPolicyModifier(primary.us as u32))
}
}
#[derive(Debug, Clone, Copy)]
pub enum PopulationType {
Full,
AlwaysFull,
}
impl From<PopulationType> for StorageProviderPopulationPolicy {
fn from(population_type: PopulationType) -> StorageProviderPopulationPolicy {
match population_type {
PopulationType::Full => StorageProviderPopulationPolicy::Full,
PopulationType::AlwaysFull => StorageProviderPopulationPolicy::AlwaysFull,
}
}
}
impl From<CF_POPULATION_POLICY_PRIMARY_USHORT> for PopulationType {
fn from(primary: CF_POPULATION_POLICY_PRIMARY_USHORT) -> Self {
match CF_POPULATION_POLICY_PRIMARY(primary.us) {
CloudFilters::CF_POPULATION_POLICY_FULL => PopulationType::Full,
CloudFilters::CF_POPULATION_POLICY_ALWAYS_FULL => PopulationType::AlwaysFull,
_ => unreachable!(),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct SupportedAttributes(pub(crate) StorageProviderInSyncPolicy);
impl SupportedAttributes {
pub fn new() -> Self {
Self::default()
}
pub fn file_creation_time(mut self) -> Self {
self.0 |= StorageProviderInSyncPolicy::FileCreationTime;
self
}
pub fn file_readonly(mut self) -> Self {
self.0 |= StorageProviderInSyncPolicy::FileReadOnlyAttribute;
self
}
pub fn file_hidden(mut self) -> Self {
self.0 |= StorageProviderInSyncPolicy::FileHiddenAttribute;
self
}
pub fn file_system(mut self) -> Self {
self.0 |= StorageProviderInSyncPolicy::FileSystemAttribute;
self
}
pub fn file_last_write_time(mut self) -> Self {
self.0 |= StorageProviderInSyncPolicy::FileLastWriteTime;
self
}
pub fn directory_creation_time(mut self) -> Self {
self.0 |= StorageProviderInSyncPolicy::DirectoryCreationTime;
self
}
pub fn directory_readonly(mut self) -> Self {
self.0 |= StorageProviderInSyncPolicy::DirectoryReadOnlyAttribute;
self
}
pub fn directory_hidden(mut self) -> Self {
self.0 |= StorageProviderInSyncPolicy::DirectoryHiddenAttribute;
self
}
pub fn directory_last_write_time(mut self) -> Self {
self.0 |= StorageProviderInSyncPolicy::DirectoryLastWriteTime;
self
}
// TODO: I'm not sure how this differs from the default policy,
// https://docs.microsoft.com/en-us/answers/questions/760677/how-does-cf-insync-policy-none-differ-from-cf-insy.html
pub fn none(mut self) -> Self {
self.0 |= StorageProviderInSyncPolicy::PreserveInsyncForSyncEngine;
self
}
}
impl Default for SupportedAttributes {
fn default() -> Self {
Self(StorageProviderInSyncPolicy::Default)
}
}
impl From<CF_INSYNC_POLICY> for SupportedAttributes {
fn from(policy: CF_INSYNC_POLICY) -> Self {
Self(StorageProviderInSyncPolicy(policy.0))
}
}