forked from jrsoftware/issrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedirFunc.pas
597 lines (549 loc) · 19 KB
/
RedirFunc.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
unit RedirFunc;
{
Inno Setup
Copyright (C) 1997-2007 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
Functions for dealing with WOW64 file system redirection.
The *Redir functions are counterparts to common functions that offer
built-in support for disabling FS redirection.
$jrsoftware: issrc/Projects/RedirFunc.pas,v 1.11 2007/09/10 11:59:14 mlaan Exp $
}
interface
uses
Windows, SysUtils, FileClass, VerInfo;
type
TPreviousFsRedirectionState = record
DidDisable: Boolean;
OldValue: Pointer;
end;
function AreFsRedirectionFunctionsAvailable: Boolean;
function DisableFsRedirectionIf(const Disable: Boolean;
var PreviousState: TPreviousFsRedirectionState): Boolean;
procedure RestoreFsRedirection(const PreviousState: TPreviousFsRedirectionState);
function CreateDirectoryRedir(const DisableFsRedir: Boolean; const Filename: String): BOOL;
function CreateProcessRedir(const DisableFsRedir: Boolean;
const lpApplicationName: PChar; const lpCommandLine: PChar;
const lpProcessAttributes, lpThreadAttributes: PSecurityAttributes;
const bInheritHandles: BOOL; const dwCreationFlags: DWORD;
const lpEnvironment: Pointer; const lpCurrentDirectory: PChar;
const lpStartupInfo: TStartupInfo;
var lpProcessInformation: TProcessInformation): BOOL;
function CopyFileRedir(const DisableFsRedir: Boolean;
const ExistingFilename, NewFilename: String; const FailIfExists: BOOL): BOOL;
function DeleteFileRedir(const DisableFsRedir: Boolean; const Filename: String): BOOL;
function DirExistsRedir(const DisableFsRedir: Boolean; const Filename: String): Boolean;
function FileOrDirExistsRedir(const DisableFsRedir: Boolean; const Filename: String): Boolean;
function FindFirstFileRedir(const DisableFsRedir: Boolean; const Filename: String;
var FindData: TWin32FindData): THandle;
function GetFileAttributesRedir(const DisableFsRedir: Boolean; const Filename: String): DWORD;
function GetShortNameRedir(const DisableFsRedir: Boolean; const Filename: String): String;
function GetVersionNumbersRedir(const DisableFsRedir: Boolean; const Filename: String;
var VersionNumbers: TFileVersionNumbers): Boolean;
function IsDirectoryAndNotReparsePointRedir(const DisableFsRedir: Boolean;
const Name: String): Boolean;
function MoveFileRedir(const DisableFsRedir: Boolean;
const ExistingFilename, NewFilename: String): BOOL;
function MoveFileExRedir(const DisableFsRedir: Boolean;
const ExistingFilename, NewFilename: String; const Flags: DWORD): BOOL;
function NewFileExistsRedir(const DisableFsRedir: Boolean; const Filename: String): Boolean;
function RemoveDirectoryRedir(const DisableFsRedir: Boolean; const Filename: String): BOOL;
function SetFileAttributesRedir(const DisableFsRedir: Boolean; const Filename: String;
const Attrib: DWORD): BOOL;
function SetNTFSCompressionRedir(const DisableFsRedir: Boolean; const FileOrDir: String; Compress: Boolean): Boolean;
type
TFileRedir = class(TFile)
private
FDisableFsRedir: Boolean;
protected
function CreateHandle(const AFilename: String;
ACreateDisposition: TFileCreateDisposition; AAccess: TFileAccess;
ASharing: TFileSharing): THandle; override;
public
constructor Create(const DisableFsRedir: Boolean; const AFilename: String;
ACreateDisposition: TFileCreateDisposition; AAccess: TFileAccess;
ASharing: TFileSharing);
end;
TTextFileReaderRedir = class(TTextFileReader)
private
FDisableFsRedir: Boolean;
protected
function CreateHandle(const AFilename: String;
ACreateDisposition: TFileCreateDisposition; AAccess: TFileAccess;
ASharing: TFileSharing): THandle; override;
public
constructor Create(const DisableFsRedir: Boolean; const AFilename: String;
ACreateDisposition: TFileCreateDisposition; AAccess: TFileAccess;
ASharing: TFileSharing);
end;
TTextFileWriterRedir = class(TTextFileWriter)
private
FDisableFsRedir: Boolean;
protected
function CreateHandle(const AFilename: String;
ACreateDisposition: TFileCreateDisposition; AAccess: TFileAccess;
ASharing: TFileSharing): THandle; override;
public
constructor Create(const DisableFsRedir: Boolean; const AFilename: String;
ACreateDisposition: TFileCreateDisposition; AAccess: TFileAccess;
ASharing: TFileSharing);
end;
implementation
uses
CmnFunc2, PathFunc;
var
Wow64DisableWow64FsRedirectionFunc: function(var OldValue: Pointer): BOOL; stdcall;
Wow64RevertWow64FsRedirectionFunc: function(OldValue: Pointer): BOOL; stdcall;
FsRedirectionFunctionsAvailable: Boolean;
function AreFsRedirectionFunctionsAvailable: Boolean;
begin
Result := FsRedirectionFunctionsAvailable;
end;
function DisableFsRedirectionIf(const Disable: Boolean;
var PreviousState: TPreviousFsRedirectionState): Boolean;
{ If Disable is False, the function does not change the redirection state and
always returns True.
If Disable is True, the function attempts to disable WOW64 file system
redirection, so that c:\windows\system32 goes to the 64-bit System directory
instead of the 32-bit one.
Returns True if successful, False if not (which normally indicates that
either the user is running 32-bit Windows, or a 64-bit version prior to
Windows Server 2003 SP1). For extended error information when False is
returned, call GetLastError. }
begin
PreviousState.DidDisable := False;
if not Disable then
Result := True
else begin
if FsRedirectionFunctionsAvailable then begin
{ Note: Disassembling Wow64DisableWow64FsRedirection and the Rtl function
it calls, it doesn't appear as if it can ever actually fail on 64-bit
Windows. But it always fails on the 32-bit version of Windows Server
2003 SP1 (with error code 1 - ERROR_INVALID_FUNCTION). }
Result := Wow64DisableWow64FsRedirectionFunc(PreviousState.OldValue);
if Result then
PreviousState.DidDisable := True;
end
else begin
{ The functions do not exist prior to Windows Server 2003 SP1 }
SetLastError(ERROR_INVALID_FUNCTION);
Result := False;
end;
end;
end;
procedure RestoreFsRedirection(const PreviousState: TPreviousFsRedirectionState);
{ Restores the previous WOW64 file system redirection state after a call to
DisableFsRedirectionIf. There is no indication of failure (which is
extremely unlikely). }
begin
if PreviousState.DidDisable then
Wow64RevertWow64FsRedirectionFunc(PreviousState.OldValue);
end;
{ *Redir functions }
function CreateDirectoryRedir(const DisableFsRedir: Boolean; const Filename: String): BOOL;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := CreateDirectory(PChar(Filename), nil);
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
function CreateProcessRedir(const DisableFsRedir: Boolean;
const lpApplicationName: PChar; const lpCommandLine: PChar;
const lpProcessAttributes, lpThreadAttributes: PSecurityAttributes;
const bInheritHandles: BOOL; const dwCreationFlags: DWORD;
const lpEnvironment: Pointer; const lpCurrentDirectory: PChar;
const lpStartupInfo: TStartupInfo;
var lpProcessInformation: TProcessInformation): BOOL;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := CreateProcess(lpApplicationName, lpCommandLine,
lpProcessAttributes, lpThreadAttributes,
bInheritHandles, dwCreationFlags, lpEnvironment,
lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
function CopyFileRedir(const DisableFsRedir: Boolean;
const ExistingFilename, NewFilename: String; const FailIfExists: BOOL): BOOL;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := CopyFile(PChar(ExistingFilename), PChar(NewFilename), FailIfExists);
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
function DeleteFileRedir(const DisableFsRedir: Boolean; const Filename: String): BOOL;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := Windows.DeleteFile(PChar(Filename));
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
function DirExistsRedir(const DisableFsRedir: Boolean; const Filename: String): Boolean;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := DirExists(Filename);
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
function FileOrDirExistsRedir(const DisableFsRedir: Boolean; const Filename: String): Boolean;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := FileOrDirExists(Filename);
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
function FindFirstFileRedir(const DisableFsRedir: Boolean; const Filename: String;
var FindData: TWin32FindData): THandle;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := INVALID_HANDLE_VALUE;
Exit;
end;
try
Result := FindFirstFile(PChar(Filename), FindData);
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
function GetFileAttributesRedir(const DisableFsRedir: Boolean; const Filename: String): DWORD;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := $FFFFFFFF;
Exit;
end;
try
Result := GetFileAttributes(PChar(Filename));
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
function GetShortNameRedir(const DisableFsRedir: Boolean; const Filename: String): String;
var
PrevState: TPreviousFsRedirectionState;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := Filename;
Exit;
end;
try
Result := GetShortName(Filename);
finally
RestoreFsRedirection(PrevState);
end;
end;
function GetVersionNumbersRedir(const DisableFsRedir: Boolean; const Filename: String;
var VersionNumbers: TFileVersionNumbers): Boolean;
var
PrevState: TPreviousFsRedirectionState;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := GetVersionNumbers(Filename, VersionNumbers);
finally
RestoreFsRedirection(PrevState);
end;
end;
function IsDirectoryAndNotReparsePointRedir(const DisableFsRedir: Boolean;
const Name: String): Boolean;
var
PrevState: TPreviousFsRedirectionState;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := IsDirectoryAndNotReparsePoint(Name);
finally
RestoreFsRedirection(PrevState);
end;
end;
function MoveFileRedir(const DisableFsRedir: Boolean;
const ExistingFilename, NewFilename: String): BOOL;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := MoveFile(PChar(ExistingFilename), PChar(NewFilename));
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
function MoveFileExRedir(const DisableFsRedir: Boolean;
const ExistingFilename, NewFilename: String; const Flags: DWORD): BOOL;
var
NewFilenameP: PChar;
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if (NewFilename = '') and (Flags and MOVEFILE_DELAY_UNTIL_REBOOT <> 0) then
NewFilenameP := nil
else
NewFilenameP := PChar(NewFilename);
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := MoveFileEx(PChar(ExistingFilename), NewFilenameP, Flags);
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
function NewFileExistsRedir(const DisableFsRedir: Boolean; const Filename: String): Boolean;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := NewFileExists(Filename);
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
function RemoveDirectoryRedir(const DisableFsRedir: Boolean; const Filename: String): BOOL;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := RemoveDirectory(PChar(Filename));
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
function SetFileAttributesRedir(const DisableFsRedir: Boolean; const Filename: String;
const Attrib: DWORD): BOOL;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := SetFileAttributes(PChar(Filename), Attrib);
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
function SetNTFSCompressionRedir(const DisableFsRedir: Boolean; const FileOrDir: String; Compress: Boolean): Boolean;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(DisableFsRedir, PrevState) then begin
Result := False;
Exit;
end;
try
Result := SetNTFSCompression(FileOrDir, Compress);
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
{ TFileRedir }
constructor TFileRedir.Create(const DisableFsRedir: Boolean; const AFilename: String;
ACreateDisposition: TFileCreateDisposition; AAccess: TFileAccess;
ASharing: TFileSharing);
begin
FDisableFsRedir := DisableFsRedir;
inherited Create(AFilename, ACreateDisposition, AAccess, ASharing);
end;
function TFileRedir.CreateHandle(const AFilename: String;
ACreateDisposition: TFileCreateDisposition; AAccess: TFileAccess;
ASharing: TFileSharing): THandle;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(FDisableFsRedir, PrevState) then begin
Result := INVALID_HANDLE_VALUE;
Exit;
end;
try
Result := inherited CreateHandle(AFilename, ACreateDisposition, AAccess,
ASharing);
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
{ TTextFileReaderRedir }
constructor TTextFileReaderRedir.Create(const DisableFsRedir: Boolean; const AFilename: String;
ACreateDisposition: TFileCreateDisposition; AAccess: TFileAccess;
ASharing: TFileSharing);
begin
FDisableFsRedir := DisableFsRedir;
inherited Create(AFilename, ACreateDisposition, AAccess, ASharing);
end;
function TTextFileReaderRedir.CreateHandle(const AFilename: String;
ACreateDisposition: TFileCreateDisposition; AAccess: TFileAccess;
ASharing: TFileSharing): THandle;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(FDisableFsRedir, PrevState) then begin
Result := INVALID_HANDLE_VALUE;
Exit;
end;
try
Result := inherited CreateHandle(AFilename, ACreateDisposition, AAccess,
ASharing);
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
{ TTextFileWriterRedir }
constructor TTextFileWriterRedir.Create(const DisableFsRedir: Boolean; const AFilename: String;
ACreateDisposition: TFileCreateDisposition; AAccess: TFileAccess;
ASharing: TFileSharing);
begin
FDisableFsRedir := DisableFsRedir;
inherited Create(AFilename, ACreateDisposition, AAccess, ASharing);
end;
function TTextFileWriterRedir.CreateHandle(const AFilename: String;
ACreateDisposition: TFileCreateDisposition; AAccess: TFileAccess;
ASharing: TFileSharing): THandle;
var
PrevState: TPreviousFsRedirectionState;
ErrorCode: DWORD;
begin
if not DisableFsRedirectionIf(FDisableFsRedir, PrevState) then begin
Result := INVALID_HANDLE_VALUE;
Exit;
end;
try
Result := inherited CreateHandle(AFilename, ACreateDisposition, AAccess,
ASharing);
ErrorCode := GetLastError;
finally
RestoreFsRedirection(PrevState);
end;
SetLastError(ErrorCode);
end;
initialization
Wow64DisableWow64FsRedirectionFunc := GetProcAddress(GetModuleHandle(kernel32),
'Wow64DisableWow64FsRedirection');
Wow64RevertWow64FsRedirectionFunc := GetProcAddress(GetModuleHandle(kernel32),
'Wow64RevertWow64FsRedirection');
FsRedirectionFunctionsAvailable := Assigned(Wow64DisableWow64FsRedirectionFunc) and
Assigned(Wow64RevertWow64FsRedirectionFunc);
{ For GetVersionNumbersRedir: Pre-load shell32.dll since GetFileVersionInfo
and GetFileVersionInfoSize will try to load it when reading version info
on 16-bit files. We can't allow the DLL be loaded for the first time while
FS redirection is disabled. }
SafeLoadLibrary(AddBackslash(GetSystemDir) + 'shell32.dll', SEM_NOOPENFILEERRORBOX);
{ FormatMessage might be called with FS redirection disabled, so ensure
that all the DLLs FormatMessage searches in for messages (e.g. netmsg.dll,
ws03res.dll) are pre-loaded by calling it now with a randomly-chosen
message ID -- one that won't result in a match and cause the function to
return early.
(Note: Presently, FormatMessage loads the DLLs as "data files" so it
actually may not matter whether it gets 32- or 64-bit versions. But let's
be on the safe side.) }
Win32ErrorString($4C783AFB);
end.