GCC Code Coverage Report


Directory: src/gate/
File: src/gate/system/backups.c
Date: 2025-09-14 13:10:38
Exec Total Coverage
Lines: 0 4 0.0%
Functions: 0 2 0.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2025, Stefan Meislinger |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/system/backups.h"
30
31 #if defined(GATE_SYS_WIN) && !defined(GATE_SYS_WIN16)
32 # define GATE_SYSTEM_BACKUPS_WINAPI_IMPL 1
33 #else
34 # define GATE_SYSTEM_BACKUPS_NO_IMPL 1
35 #endif
36
37
38 #if defined(GATE_SYSTEM_BACKUPS_WINAPI_IMPL)
39
40 #include "gate/system/platform/backups_fmapi.h"
41
42
43 static char const gate_restore_provider_undelete[] = "win-undelete";
44
45
46 typedef struct undelete_impl
47 {
48 GATE_INTERFACE_VTBL(gate_restore_provider) const* vtbl;
49
50 gate_atomic_int_t ref_counter;
51 gate_atomic_int_t opened;
52 PFILE_RESTORE_CONTEXT context;
53 WCHAR source_path[GATE_MAX_FILEPATH_LENGTH];
54
55 } undelete_impl_t;
56
57
58 static char const* undelete_get_interface_name(void* this_ptr)
59 {
60 undelete_impl_t* impl = (undelete_impl_t*)this_ptr;
61 return GATE_INTERFACE_NAME_RESTORE_PROVIDER;
62 }
63 static void undelete_release(void* this_ptr)
64 {
65 undelete_impl_t* impl = (undelete_impl_t*)this_ptr;
66 if (0 == gate_atomic_int_dec(&impl->ref_counter))
67 {
68 gate_mem_dealloc(impl);
69 }
70 }
71 static int undelete_retain(void* this_ptr)
72 {
73 undelete_impl_t* impl = (undelete_impl_t*)this_ptr;
74 return gate_atomic_int_inc(&impl->ref_counter);
75 }
76
77 static char const* undelete_get_provider_name(void* this_ptr)
78 {
79 undelete_impl_t* impl = (undelete_impl_t*)this_ptr;
80 return gate_restore_provider_undelete;
81 }
82 static gate_result_t undelete_open_source(void* this_ptr, gate_string_t const* source_path)
83 {
84 undelete_impl_t* impl = (undelete_impl_t*)this_ptr;
85 gate_result_t ret = GATE_RESULT_FAILED;
86 BOOL success;
87
88 do
89 {
90 gate_str_utf8_2_utf16(source_path->str, source_path->length,
91 impl->source_path, sizeof(impl->source_path) / sizeof(impl->source_path[0]));
92
93 if (0 != gate_atomic_int_set(&impl->opened, 1))
94 {
95 ret = GATE_RESULT_INVALIDSTATE;
96 break;
97 }
98
99 success = fmapi.CreateFileRestoreContext(impl->source_path,
100 ContextFlagVolume | FlagScanRemovedFiles | FlagScanIncludeRemovedDirectories,
101 0, 0,
102 FILE_RESTORE_VERSION_2, &impl->context);
103 if (!success)
104 {
105 success = fmapi.CreateFileRestoreContext(impl->source_path,
106 ContextFlagVolume | FlagScanRemovedFiles | FlagScanIncludeRemovedDirectories,
107 0, 0,
108 FILE_RESTORE_VERSION_1, &impl->context);
109 }
110 if (!success)
111 {
112 gate_atomic_int_set(&impl->opened, 0);
113 ret = GATE_RESULT_FAILED;
114 }
115 ret = GATE_RESULT_OK;
116
117 } while (0);
118 return ret;
119 }
120
121 static gate_result_t undelete_close_source(void* this_ptr)
122 {
123 undelete_impl_t* impl = (undelete_impl_t*)this_ptr;
124 gate_result_t ret = GATE_RESULT_FAILED;
125 do
126 {
127 if (0 == gate_atomic_int_set(&impl->opened, 0))
128 {
129 ret = GATE_RESULT_INVALIDSTATE;
130 break;
131 }
132 fmapi.CloseFileRestoreContext(impl->context);
133 impl->context = NULL;
134 ret = GATE_RESULT_OK;
135 break;
136 } while (0);
137 return ret;
138 }
139
140 union file_info
141 {
142 RESTORABLE_FILE_INFO base_info;
143 char buffer[1024 * 12];
144 };
145
146 static gate_result_t find_next_restorable_file(undelete_impl_t* impl, char* item_path, gate_size_t item_path_length,
147 gate_uint32_t* ptr_type, gate_uint32_t* ptr_subitems, gate_uint64_t* ptr_size)
148 {
149 gate_result_t ret = GATE_RESULT_FAILED;
150 union file_info info;
151 ULONG info_used = sizeof(info);
152
153 if (fmapi.ScanRestorableFiles(impl->context, impl->source_path, info_used, &info.base_info, &info_used))
154 {
155 if (ptr_type != NULL)
156 {
157 *ptr_type = 0;
158 }
159 if (ptr_subitems != NULL)
160 {
161 *ptr_subitems = 1;
162 }
163 if (ptr_size != NULL)
164 {
165 *ptr_size = info.base_info.FileSize;
166 }
167
168 ret = GATE_RESULT_OK;
169 }
170 else
171 {
172 ret = GATE_RESULT_FAILED;
173 }
174 return ret;
175 }
176
177 static gate_result_t undelete_list_items(void* this_ptr, gate_delegate_t const* callback, void* callback_param)
178 {
179 undelete_impl_t* impl = (undelete_impl_t*)this_ptr;
180 gate_result_t ret = GATE_RESULT_FAILED;
181 gate_restore_item_t items;
182 char item_path[GATE_MAX_FILEPATH_LENGTH];
183
184 do
185 {
186 /* TODO */
187
188
189 } while (0);
190 return ret;
191 }
192 static gate_result_t undelete_get_item_paths(void* this_ptr, gate_array_t* item_paths)
193 {
194 undelete_impl_t* impl = (undelete_impl_t*)this_ptr;
195 gate_result_t ret = GATE_RESULT_FAILED;
196 do
197 {
198 /* TODO */
199
200 } while (0);
201 return ret;
202 }
203
204
205 static gate_result_t undelete_restore_item(void* this_ptr, gate_string_t const* item_path, gate_string_t const* target_path)
206 {
207 undelete_impl_t* impl = (undelete_impl_t*)this_ptr;
208 gate_result_t ret = GATE_RESULT_FAILED;
209 do
210 {
211
212 } while (0);
213 return ret;
214 }
215
216
217 static GATE_INTERFACE_VTBL(gate_restore_provider) gate_undelete_vtbl;
218 static void gate_init_undelete_vtbl()
219 {
220 if (!gate_undelete_vtbl.get_interface_name)
221 {
222 GATE_INTERFACE_VTBL(gate_restore_provider) const local_vtbl =
223 {
224 &undelete_get_interface_name,
225 &undelete_release,
226 &undelete_retain,
227
228 &undelete_get_provider_name,
229 &undelete_open_source,
230 &undelete_close_source,
231
232 &undelete_list_items,
233 &undelete_get_item_paths,
234
235 &undelete_restore_item
236 };
237 gate_undelete_vtbl = local_vtbl;
238 }
239 }
240
241
242 static gate_restore_provider_t* undelete_provider_create()
243 {
244 gate_restore_provider_t* ret = NULL;
245 undelete_impl_t* impl = NULL;
246 gate_result_t result;
247
248 do
249 {
250 result = load_fmapi_functions();
251 GATE_BREAK_IF_FAILED(result);
252
253 impl = (undelete_impl_t*)gate_mem_alloc(sizeof(undelete_impl_t));
254 if (NULL == impl)
255 {
256 break;
257 }
258 gate_mem_clear(impl, sizeof(undelete_impl_t));
259
260 gate_init_undelete_vtbl();
261 impl->vtbl = &gate_undelete_vtbl;
262 gate_atomic_int_init(&impl->ref_counter, 1);
263
264
265 ret = (gate_restore_provider_t*)impl;
266 impl = NULL;
267 } while (0);
268
269 if (impl != NULL)
270 {
271 gate_object_release(impl);
272 }
273 return ret;
274 }
275
276 gate_size_t gate_restore_provider_names(char const** name_list, gate_size_t name_list_size)
277 {
278 gate_size_t provider_count = 0;
279 gate_result_t result;
280
281 do
282 {
283 result = load_fmapi_functions();
284 if (GATE_SUCCEEDED(result))
285 {
286 if (name_list_size > 0)
287 {
288 *name_list = gate_restore_provider_undelete;
289 ++name_list;
290 --name_list_size;
291 ++provider_count;
292 }
293 }
294
295 } while (0);
296
297 return provider_count;
298 }
299
300 gate_restore_provider_t* gate_restore_provider_get(char const* name)
301 {
302 gate_restore_provider_t* ret = NULL;
303 do
304 {
305 if (gate_str_comp(name, gate_restore_provider_undelete))
306 {
307 ret = undelete_provider_create();
308 break;
309 }
310
311 } while (0);
312 return ret;
313 }
314
315
316 #endif /* GATE_SYSTEM_BACKUPS_WINAPI_IMPL */
317
318
319
320
321 #if defined(GATE_SYSTEM_BACKUPS_NO_IMPL)
322
323 gate_size_t gate_restore_provider_names(char const** name_list, gate_size_t name_list_size)
324 {
325 /*TODO*/
326 return 0;
327 }
328
329 gate_restore_provider_t* gate_restore_provider_get(char const* name)
330 {
331 /*TODO*/
332 return NULL;
333 }
334
335 #endif /* GATE_SYSTEM_BACKUPS_NO_IMPL */
336
337