-
Notifications
You must be signed in to change notification settings - Fork 7
/
RamSync.pas
348 lines (328 loc) · 10.8 KB
/
RamSync.pas
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
unit RamSync;
interface
Uses Definitions;
procedure LoadRamDisk(Var config:TRamDisk);
procedure SaveRamDisk(Var existing:TRamDisk);
procedure RestoreTempFolder(letter:WideChar);
implementation
uses SysUtils,Windows,TntRegistry,TntSysUtils,TntClasses,Junctions;
const
DIR_ATTR = FILE_ATTRIBUTE_DIRECTORY or FILE_ATTRIBUTE_REPARSE_POINT;
type
TStrArray = Array of WideString;
TPathList = Array Of TStrArray;
procedure CopyTime(const src,dest:WideString);
var
hDir:THandle;
creationTime,accessTime,writeTime:TFileTime;
Begin
hDir := CreateFileW(PWideChar(src), 0, FILE_SHARE_READ, NIL, OPEN_EXISTING, 0, 0);
if hDir <> INVALID_HANDLE_VALUE Then
begin
GetFileTime(hDir,@creationTime,@accessTime,@writeTime);
CloseHandle(hDir);
hDir := CreateFileW(PWideChar(dest), GENERIC_WRITE, FILE_SHARE_WRITE, NIL, OPEN_EXISTING, 0, 0);
if hDir <> INVALID_HANDLE_VALUE Then
begin
SetFileTime(hDir,@creationTime,@accessTime,@writeTime);
CloseHandle(hDir);
end;
end;
end;
procedure TreeCopy(const src,dest:WideString);
var
SR: TSearchRecW;
junction,current,source: WideString;
Begin
if WideFindFirst(src+'*.*',faAnyFile,SR)<>0 then Exit;
repeat
if (SR.Name <> '.') and (SR.Name <> '..') then
begin
//Application.ProcessMessages;
current:=dest + SR.Name;
source:=src + SR.Name;
if (SR.Attr and faDirectory) <> 0 then
begin
WideCreateDir(current);
// check for junction point
if (GetFileAttributesW(PWideChar(source)) and DIR_ATTR) = DIR_ATTR Then
Begin
junction:=GetSymLinkTarget(source);
If junction<>'' Then
Begin
CreateJunction(current, junction);
CopyTime(source,current);
Continue;
end;
end;
TreeCopy(src + SR.Name + '\', dest + SR.Name + '\');
CopyTime(source,current);
end
else
Begin
CopyFileW(PWideChar(source), PWideChar(current),False); // we don't care if it is a symlink
CopyTime(source,current);
End;
end;
Until WideFindNext(SR) <> 0;
WideFindClose(SR);
end;
procedure DelTree(const path:String);
var
SR: TSearchRec;
Begin
if FindFirst(path+'*.*',faAnyFile,SR)<>0 then Exit;
Repeat
if (SR.Name <> '.') and (SR.Name <> '..') then
begin
if (SR.Attr and faDirectory) <> 0 then
Begin
DelTree(path + SR.Name + '\');
RemoveDir(path + SR.Name);
end
else SysUtils.DeleteFile(path + SR.Name);
end;
Until FindNext(SR) <> 0;
SysUtils.FindClose(SR);
RemoveDir(path);
end;
Procedure LoadRamDisk(Var config:TRamDisk);
Var
reg: TTntRegistry;
tempDir:String;
Begin
DebugLog('Configuring RAM-disk');
If (config.persistentFolder<>'') And DirectoryExists(config.persistentFolder) Then
Begin
TreeCopy(WideIncludeTrailingPathDelimiter(config.persistentFolder),config.letter+':\');
DebugLog('RAM-disk was populated with content from ' + config.persistentFolder);
end;
If config.useTemp Then
Begin
tempDir:=config.letter+':\TEMP';
DebugLog('Configuring TEMP folder as ' + tempDir);
if CreateDir(tempDir) Then
Begin
reg:=Nil;
Try
reg:=TTntRegistry.Create(KEY_WRITE);
reg.RootKey:=HKEY_LOCAL_MACHINE;
if Reg.OpenKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', True) then
Begin
reg.WriteExpandString('TMP',tempDir);
reg.WriteExpandString('TEMP',tempDir);
DebugLog('TMP and TEMP folders for all users were set');
end;
reg.CloseKey;
reg.RootKey:=HKEY_CURRENT_USER;
if Reg.OpenKey('Environment', True) then
Begin
reg.WriteExpandString('TMP',tempDir);
reg.WriteExpandString('TEMP',tempDir);
DebugLog('TMP and TEMP folders for the current user were set');
end;
reg.CloseKey;
finally
reg.Free;
end;
end;
end;
// remove $RECYCLE.BIN
DelTree(config.letter+':\$RECYCLE.BIN\');
end;
procedure RestoreTempFolder(letter:WideChar);
Var
reg: TTntRegistry;
tmpFolder, tempFolder: String;
tmp:WideString;
Begin
reg:=Nil;
try
DebugLog('Switching to the default TEMP folder');
reg:=TTntRegistry.Create(KEY_ALL_ACCESS);
// read defaults
reg.RootKey:=HKEY_USERS;
if Reg.OpenKey('.DEFAULT\Environment', False) then
Begin
tmpFolder:=reg.ReadString('TMP');
DebugLog(Format('Default TMP folder = %s',[tmpFolder]));
tempFolder:=reg.ReadString('TEMP');
DebugLog(Format('Default TEMP folder = %s',[tempFolder]));
end;
reg.CloseKey;
// set active values
reg.RootKey:=HKEY_LOCAL_MACHINE;
if Reg.OpenKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', True) then
Begin
// restore default only if current setting was using the just unmounted Ramdisk
tmp:=WideUpperCase(reg.ReadString('TMP'));
If (tmp<>'')And(tmp[1] = letter) then
Begin
reg.WriteExpandString('TMP',tmpFolder);
DebugLog('Restoring TMP folder for all users');
End;
tmp:=WideUpperCase(reg.ReadString('TEMP'));
If (tmp<>'')and(tmp[1] = letter) then
Begin
reg.WriteExpandString('TEMP',tempFolder);
DebugLog('Restoring TEMP folder for all users');
End;
end;
reg.CloseKey;
reg.RootKey:=HKEY_CURRENT_USER;
if Reg.OpenKey('Environment', True) then
Begin
tmp:=WideUpperCase(reg.ReadString('TMP'));
If (tmp<>'')and(tmp[1] = letter) then
Begin
reg.WriteExpandString('TMP',tmpFolder);
DebugLog('Restoring TMP folder for the current user');
end;
tmp:=WideUpperCase(reg.ReadString('TEMP'));
If (tmp<>'')and(tmp[1] = letter) then
Begin
reg.WriteExpandString('TEMP',tempFolder);
DebugLog('Restoring TMP folder for the current user');
end;
end;
reg.CloseKey;
finally
reg.Free;
end;
end;
Function NewerSource(const src,dest:WideString):Boolean;
var
hDir:THandle;
srcCreation,srcAccess,srcModify,destCreation,destAccess,destModify:TFileTime;
Begin
Result:=False;
hDir := CreateFileW(PWideChar(src), 0, FILE_SHARE_READ, NIL, OPEN_EXISTING, 0, 0);
if hDir <> INVALID_HANDLE_VALUE Then
begin
GetFileTime(hDir,@srcCreation,@srcAccess,@srcModify);
CloseHandle(hDir);
hDir := CreateFileW(PWideChar(dest), 0, FILE_SHARE_READ, NIL, OPEN_EXISTING, 0, 0);
if hDir <> INVALID_HANDLE_VALUE Then
begin
GetFileTime(hDir,@destCreation,@destAccess,@destModify);
CloseHandle(hDir);
//-1, Source is older than Destination
//0, Source is the same age as Destination
//+1 Source is younger than Destination
Result:=(CompareFileTime(srcModify,destModify)>0) or (CompareFileTime(srcCreation,destCreation)>0);
end
Else Result:=True; // destination probably does not exist
end;
end;
// copy from RAM-disk to the persistent folder, excluding disabled paths
procedure TreeSave(const src,dest:WideString;excluded:TTntStringList);
var
SR: TSearchRecW;
junction,current,source: WideString;
Begin
DebugLog(WideFormat('Now persisting folder %s',[src]));
if WideFindFirst(src+'*.*',faAnyFile,SR)<>0 then Exit;
repeat
if (SR.Name <> '.') and (SR.Name <> '..') then
begin
//Application.ProcessMessages;
current:=dest + SR.Name;
source:=src + SR.Name;
if (SR.Attr and faDirectory) <> 0 then
begin
if Assigned(excluded) And (excluded.IndexOf(WideUpperCase(SR.Name)) <> -1) then Continue;
WideCreateDir(current);
// check for junction point
if (GetFileAttributesW(PWideChar(source)) and DIR_ATTR) = DIR_ATTR Then
Begin
junction:=GetSymLinkTarget(source);
If junction<>'' Then
Begin
CreateJunction(current, junction);
Continue;
end;
end;
TreeSave(source + '\', current + '\',Nil);
end
else
Begin
if NewerSource(source,current) then CopyFileW(PWideChar(source), PWideChar(current),False); // overwrite existing
End;
end;
Until WideFindNext(SR) <> 0;
WideFindClose(SR);
end;
// delete from persistent folder items which are no longer present on the RAM-disk
procedure TreeDelete(const src,dest:WideString;excluded:TTntStringList);
var
SR: TSearchRecW;
Begin
DebugLog(WideFormat('Now removing folder %s',[src]));
if WideFindFirst(src+'*.*',faAnyFile,SR)<>0 then Exit;
repeat
if (SR.Name <> '.') and (SR.Name <> '..') then
begin
//Application.ProcessMessages;
if (SR.Attr and faDirectory) <> 0 then
begin
if Assigned(excluded) And (excluded.IndexOf(WideUpperCase(SR.Name)) <> -1) then Continue;
TreeDelete(src + SR.Name + '\', dest + SR.Name + '\',Nil);
if not DirectoryExists(dest + SR.Name) then WideRemoveDir(src + SR.Name);
end
else
Begin
if Not FileExists(dest + SR.Name) Then WideDeleteFile(src + SR.Name);
End;
end;
Until WideFindNext(SR) <> 0;
WideFindClose(SR);
end;
Procedure SplitPath(const path:WideString;var list:TStrArray);
var
oldPos,newPos,k:Integer;
Begin
SetLength(List,Length(path));
k:=0;
oldPos:=1;
Repeat
newPos:=WidePosEx('\',path,oldPos);
if newPos=0 then list[k]:=Copy(path,oldPos,MaxInt)
Else
Begin
list[k]:=Copy(path,oldPos,newPos);
oldPos:=newPos;
end;
Inc(k);
Until newPos=0;
SetLength(list,k);
end;
Procedure SaveRamDisk(Var existing:TRamDisk);
var
list:TTntStringList;
Begin
DebugLog('Trying to persist RamDisk before unmount');
if WideDirectoryExists(existing.persistentFolder) then
Begin
list:=Nil;
try
list:=TTntStringList.Create;
list.Text:=WideUpperCase(existing.excludedList);
list.Add('TEMP'); // always exclude TEMP folder and system folders
list.Add('$RECYCLE.BIN');
list.Add('System Volume Information');
// first we persist RAM-disk, excluding disabled paths
TreeSave(existing.letter+':\',WideIncludeTrailingPathDelimiter(existing.persistentFolder),list);
DebugLog('RamDisk content was persisted');
// then we delete the data that is not present on the RAM-disk
if existing.deleteOld then
Begin
TreeDelete(WideIncludeTrailingPathDelimiter(existing.persistentFolder),existing.letter+':\',list);
DebugLog('Obsolete data inside the synchronization folder was removed');
End;
Finally
list.Free;
end;
End
else DebugLog(WideFormat('Folder "%s" does not exist',[existing.persistentFolder]),EVENTLOG_ERROR_TYPE);
end;
end.