Skip to content

Commit 12fdb72

Browse files
committed
Added more debug logging to the service.
1 parent 2af7923 commit 12fdb72

File tree

11 files changed

+182
-42
lines changed

11 files changed

+182
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
*.bkm
55
*.drc
66
*.zip
7+
dcu

Definitions.pas

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ DRIVE_LAYOUT_INFORMATION = record
250250
Function ImDiskOpenDeviceByName(FileName:PUnicodeString; AccessMode:DWORD):THandle;
251251
Function ImScsiOpenScsiAdapter(var PortNumber:Byte):THandle;
252252
Function ImScsiDeviceIoControl(device:THandle; ControlCode: DWORD; var SrbIoControl: TSrbIoControl; Size, Timeout: DWORD; var ReturnLength: DWORD):Boolean;
253+
Function decodeException(code:TRamErrors):String;
253254

254255
implementation
255256

@@ -451,4 +452,18 @@ function GetFreeDriveList: TAssignedDrives;
451452
Result:=['C'..'Z'] - used; // exclude floppy drives
452453
end;
453454

455+
Function decodeException(code:TRamErrors):String;
456+
Begin
457+
Result:='';
458+
Case code Of
459+
RamNotInstalled: Result:='Arsenal Driver is not installed';
460+
RamNotAccessible: Result:='Arsenal Driver is not accessible';
461+
RamCantEnumDrives: Result:='Can not enumerate disk volumes';
462+
RamDriverVersion: Result:='Arsenal Driver is old version';
463+
RamCantCreate: Result:='Could not create RAM-disk';
464+
RamCantFormat: Result:='Could not create a partition on the RAM-disk';
465+
RamNoFreeLetter: Result:='No free drive letters available';
466+
end;
467+
End;
468+
454469
end.

Main.pas

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,9 @@ implementation
6666
Var
6767
ramDiskConfig: TRamDisk;
6868

69-
Procedure decodeException(code:TRamErrors);
70-
Var
71-
msg: String;
72-
Begin
73-
msg:='';
74-
Case code Of
75-
RamNotInstalled: msg:='Arsenal Driver is not installed';
76-
RamNotAccessible: msg:='Arsenal Driver is not accessible';
77-
RamCantEnumDrives: msg:='Can not enumerate disk volumes';
78-
RamDriverVersion: msg:='Arsenal Driver is old version';
79-
RamCantCreate: msg:='Could not create RAM-disk';
80-
RamCantFormat: msg:='Could not create a partition on the RAM-disk';
81-
RamNoFreeLetter: msg:='No free drive letters available';
82-
end;
83-
if msg<>'' then MessageDlg(msg,mtError,[mbOK],0);
84-
End;
85-
8669
procedure TfrmUI.btnApplyClick(Sender: TObject);
70+
var
71+
msg:String;
8772
begin
8873
SaveSettings;
8974
If not TryStrToInt64(vdSize.Text,ramDiskConfig.size) Then MessageDlg('Invalid disk size',mtError,[mbOK],0)
@@ -104,7 +89,11 @@ procedure TfrmUI.btnApplyClick(Sender: TObject);
10489
UpdateMounted;
10590
end;
10691
except
107-
On E:ERamDiskError do decodeException(E.ArsenalCode);
92+
On E:ERamDiskError do
93+
Begin
94+
msg:=decodeException(E.ArsenalCode);
95+
If msg<>'' then MessageDlg(msg,mtError,[mbOK],0);
96+
end;
10897
else raise;
10998
End;
11099
end;

RamCreate.pas

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,23 @@ procedure HideInfo;
104104
dw: DWORD;
105105
Begin
106106
Result:=False;
107+
OutputDebugString('Trying to query the version of Arsenal driver');
107108
ImScsiInitializeSrbIoBlock(check.SrbIoControl, sizeof(check), SMP_IMSCSI_QUERY_VERSION, 0);
108-
if Not DeviceIoControl(Device, IOCTL_SCSI_MINIPORT, @check, sizeof(check), @check, sizeof(check), dw, NIL) then Exit;
109-
if dw < sizeof(check) then Exit;
110-
if check.SrbIoControl.ReturnCode < IMSCSI_DRIVER_VERSION Then Exit;
109+
if Not DeviceIoControl(Device, IOCTL_SCSI_MINIPORT, @check, sizeof(check), @check, sizeof(check), dw, NIL) then
110+
Begin
111+
OutputDebugString('Arsenal driver does not support version checking');
112+
Exit;
113+
end;
114+
if dw < sizeof(check) then
115+
Begin
116+
OutputDebugString(PAnsiChar(Format('Arsenal driver reports the size of data structure for version check as %u which is less than expected %u',[dw,SizeOf(check)])));
117+
Exit;
118+
end;
119+
if check.SrbIoControl.ReturnCode < IMSCSI_DRIVER_VERSION Then
120+
Begin
121+
OutputDebugString(PAnsiChar(Format('Arsenal driver reports version %u which is less than required %u',[check.SrbIoControl.ReturnCode,IMSCSI_DRIVER_VERSION])));
122+
Exit;
123+
end;
111124
Result:=True;
112125
end;
113126

@@ -164,10 +177,16 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
164177
mustFormat, formatDone, mount_point_found:Boolean;
165178
Begin
166179
Result:=False;
180+
OutputDebugString('Trying to create a new RAM-disk');
167181
driver := ImScsiOpenScsiAdapter(portNumber);
168-
if driver = INVALID_HANDLE_VALUE then Exit;
182+
if driver = INVALID_HANDLE_VALUE then
183+
Begin
184+
OutputDebugString('Arsenal driver is not running');
185+
Exit;
186+
end;
169187
if not ImScsiCheckDriverVersion(driver) then
170188
begin
189+
OutputDebugString('Arsenal driver version is not suitable');
171190
CloseHandle(driver);
172191
Raise ERamDiskError.Create(RamDriverVersion);
173192
end;
@@ -176,7 +195,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
176195
if not ImScsiDeviceIoControl(driver, SMP_IMSCSI_CREATE_DEVICE, create_data.SrbIoControl, SizeOf(create_data), 0, dw) then
177196
begin
178197
NtClose(driver);
179-
OutputDebugString(PAnsiChar(SysErrorMessage(GetLastError)));
198+
OutputDebugString(PAnsiChar(Format('Could not create the RAM-disk, error is "%s"',[SysErrorMessage(GetLastError)])));
180199
raise ERamDiskError.Create(RamCantCreate);
181200
end;
182201
NtClose(driver);
@@ -188,6 +207,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
188207

189208
while true do
190209
begin
210+
OutputDebugString('Disk not attached yet, waiting 200 msec');
191211
disk := ImScsiOpenDiskByDeviceNumber(create_data.Fields.DeviceNumber, portNumber, diskNumber);
192212
if disk <> INVALID_HANDLE_VALUE then Break;
193213
//printf("Disk not attached yet, waiting... %c\r", NextWaitChar(&wait_char));
@@ -200,6 +220,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
200220
begin
201221
while WaitForSingleObject(event, 200) = WAIT_TIMEOUT do
202222
begin
223+
OutputDebugString('Rescanning SCSI adapters, disk not attached yet. Waiting 200 msec');
203224
// printf("Disk not attached yet, waiting... %c\r", NextWaitChar(&wait_char));
204225
end;
205226
CloseHandle(event);
@@ -244,6 +265,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
244265
end;
245266
if mustFormat then
246267
begin
268+
OutputDebugString('Will now create a partition on the RAM device');
247269
rand_seed := GetTickCount();
248270
while true do
249271
begin
@@ -260,14 +282,19 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
260282
drive_layout.PartitionEntry[0].RecognizedPartition := TRUE;
261283
drive_layout.PartitionEntry[0].RewritePartition := TRUE;
262284

263-
if DeviceIoControl(disk, IOCTL_DISK_SET_DRIVE_LAYOUT, @drive_layout, sizeof(drive_layout), NIL, 0, dw, NIL) then Break;
285+
if DeviceIoControl(disk, IOCTL_DISK_SET_DRIVE_LAYOUT, @drive_layout, sizeof(drive_layout), NIL, 0, dw, NIL) then
286+
Begin
287+
OutputDebugString('Successfully created the partition');
288+
Break;
289+
end;
264290
if GetLastError <> ERROR_WRITE_PROTECT then
265291
begin
266292
CloseHandle(disk);
267293
Raise ERamDiskError.Create(RamCantFormat);
268294
end;
269295

270296
//printf("Disk not yet ready, waiting... %c\r", NextWaitChar(&wait_char));
297+
OutputDebugString('Disk is not yet ready for partitioning, waiting ...');
271298

272299
ZeroMemory(@disk_attributes, sizeof(disk_attributes));
273300
disk_attributes.AttributesMask := DISK_ATTRIBUTE_OFFLINE or DISK_ATTRIBUTE_READ_ONLY;
@@ -285,6 +312,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
285312
numVolumes:=0;
286313
while true do
287314
begin
315+
OutputDebugString('Trying to find the volume (partition) by name');
288316
volume := FindFirstVolume(volumeName, Length(volumeName));
289317
if volume = INVALID_HANDLE_VALUE then
290318
begin
@@ -297,11 +325,13 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
297325
try
298326
repeat
299327
volumeName[48] := #0;
328+
OutputDebugString(PAnsiChar(Format('Quering volume %s',[volumeName])));
300329
volHandle := CreateFile(volumeName, 0, FILE_SHARE_READ or FILE_SHARE_WRITE, NIL, OPEN_EXISTING, 0, 0);
301330
if volHandle = INVALID_HANDLE_VALUE then Continue;
302331
if not ImScsiVolumeUsesDisk(volHandle, diskNumber) then
303332
begin
304333
CloseHandle(volHandle);
334+
OutputDebugString('This volume is not used (created) by Arsenal');
305335
continue;
306336
end;
307337

@@ -328,13 +358,14 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
328358
// we use the undocumented FMIFS.DLL instead of Format.COM or VDS or WMI or ShFormatDrive - it always takes at least 5 seconds
329359
formatDriveName:=volumeName;
330360
FormatEx(PWideChar(formatDriveName),FMIFS_HARDDISK,'NTFS','RAMDISK',True,4096,@FormatCallBack);
361+
OutputDebugString('Successfully created NTFS filesystem on the RAM-disk');
331362
if ShowProgress then HideInfo;
332363
end;
333364

334365
volumeName[48] := '\';
335366
if Not GetVolumePathNamesForVolumeName(volumeName, mountName, Length(mountName), dw) then
336367
begin
337-
OutputDebugString('Error enumerating mount points');
368+
OutputDebugString(PAnsiChar(Format('Error enumerating mount points for volume %s',[volumeName])));
338369
continue;
339370
end;
340371

@@ -344,9 +375,11 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
344375
mountList.Text:=mountName;
345376
for i:=0 to mountList.Count-1 do
346377
begin
378+
OutputDebugString(PAnsiChar(Format('Now trying to get a drive letter for "%s"',[mountList[i]])));
347379
if mountList[i] = '' then Break;
348380
if CompareText(mountPoint,mountList[i])<>0 then
349381
begin
382+
OutputDebugString('Removing the old mount point');
350383
if Not DeleteVolumeMountPoint(PAnsiChar(mountList[i])) then
351384
begin
352385
dw:=GetLastError;
@@ -356,6 +389,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
356389
else
357390
begin
358391
mount_point_found := true;
392+
OutputDebugString(PAnsiChar(Format('Mounted at %s',[mountPoint])));
359393
// ImScsiOemPrintF(stdout, " Mounted at %1!ws!", mnt);
360394
end;
361395
end;
@@ -366,6 +400,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
366400
MountPoint[1] := ImDiskFindFreeDriveLetter();
367401
if MountPoint[1] = #0 then raise ERamDiskError.Create(RamNoFreeLetter)
368402
Else config.letter:=MountPoint[1];
403+
OutputDebugString(PAnsiChar('Will use drive letter ' + MountPoint[1]));
369404
end;
370405
if not SetVolumeMountPoint(PAnsiChar(MountPoint), volumeName) then
371406
begin
@@ -389,6 +424,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
389424
end;
390425

391426
//printf("Volume not yet attached, waiting... %c\r", NextWaitChar(&wait_char));
427+
OutputDebugString('Volume not yet attached, waiting 200 msec');
392428
Sleep(200);
393429
end;
394430
LoadRamDisk(config);

RamRemove.pas

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,12 @@ function AutoEjectVolume(AVolumeHandle: THandle): boolean;
8888
forceDismount: Boolean;
8989
Begin
9090
Result:=False;
91+
OutputDebugString('Begin DetachRamDisk');
9192
If existing.letter = #0 Then
9293
Begin
94+
OutputDebugString('RamDisk has no drive letter attahed');
9395
adapter := ImScsiOpenScsiAdapter(portNumber);
96+
OutputDebugString(PAnsiChar(Format('SCSI adapter handle = %u',[adapter])));
9497
if adapter = INVALID_HANDLE_VALUE then
9598
begin
9699
dw:=GetLastError;
@@ -113,15 +116,19 @@ function AutoEjectVolume(AVolumeHandle: THandle): boolean;
113116
Exit;
114117
end;
115118
end;
119+
OutputDebugString('RamDisk device has been destroyed');
116120
Result:=True;
117121
Exit;
118122
end;
119123
if existing.synchronize And (existing.persistentFolder<>'') then SaveRamDisk(existing);
120124
forceDismount:=False;
125+
OutputDebugString(PAnsiChar(Format('Trying to open volume %s',[existing.letter])));
121126
device := OpenVolume(existing.letter);
122127
if device = INVALID_HANDLE_VALUE then
123128
begin
124-
case GetLastError of
129+
tmp:=GetLastError;
130+
OutputDebugString(PAnsiChar(Format('Could not open the volume, error is "%s"',[SysErrorMessage(tmp)])));
131+
case tmp of
125132
ERROR_INVALID_PARAMETER:
126133
// "This version of Windows only supports drive letters as mount points.\n"
127134
// "Windows 2000 or higher is required to support subdirectory mount points.\n",
@@ -135,28 +142,44 @@ function AutoEjectVolume(AVolumeHandle: THandle): boolean;
135142
// ImScsiOemPrintF(stderr, "Not a mount point: '%1!ws!'", MountPoint);
136143
Exit;
137144
else
138-
tmp:=GetLastError;
139145
raise Exception.Create(SysErrorMessage(tmp));
140146
end;
141147
End;
142148
// Notify processes that this device is about to be removed.
149+
OutputDebugString('Now notifying other processes that this device is about to be removed');
143150
ImDiskNotifyRemovePending(WideChar(existing.letter));
151+
OutputDebugString('Flushing OS file buffers');
144152
FlushFileBuffers(device);
145153

146154
// Locking volume
147155
try
148-
if Not DeviceIoControl(device, FSCTL_LOCK_VOLUME, NIL, 0, NIL, 0, dw, NIL) then forceDismount := TRUE;
156+
OutputDebugString('Locking the volume');
157+
if Not DeviceIoControl(device, FSCTL_LOCK_VOLUME, NIL, 0, NIL, 0, dw, NIL) then
158+
Begin
159+
forceDismount := TRUE;
160+
OutputDebugString('Could not lock the volume - so trying a forced unmount');
161+
End;
149162
// Unmounting filesystem
150163
try
164+
OutputDebugString('Trying to unmount the filesystem');
151165
if DeviceIoControl(device, FSCTL_DISMOUNT_VOLUME, NIL, 0, NIL, 0, dw, NIL) then
152166
begin
153-
if forceDismount then DeviceIoControl(device, FSCTL_LOCK_VOLUME, NIL, 0, NIL, 0, dw, NIL);
167+
if forceDismount then
168+
Begin
169+
DeviceIoControl(device, FSCTL_LOCK_VOLUME, NIL, 0, NIL, 0, dw, NIL);
170+
OutputDebugString('Doing forced lock');
171+
end;
154172
// Set prevent removal to false and eject the volume
155-
if PreventRemovalOfVolume(device, FALSE) then AutoEjectVolume(device);
173+
if PreventRemovalOfVolume(device, FALSE) then
174+
Begin
175+
AutoEjectVolume(device);
176+
OutputDebugString('Ejected the volume');
177+
End;
156178
Result:=True;
157179
end;
158180
finally
159181
DeviceIoControl(device, FSCTL_UNLOCK_VOLUME, NIL, 0, NIL, 0, dw, NIL);
182+
OutputDebugString('Unlocked the volume');
160183
End;
161184
finally
162185
CloseHandle(device);
@@ -165,4 +188,3 @@ function AutoEjectVolume(AVolumeHandle: THandle): boolean;
165188
end;
166189

167190
end.
168-

RamService.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
-M
3232
-$M16384,1048576
3333
-K$00400000
34+
-E"bin"
3435
-LE"c:\program files (x86)\borland\delphi7\..\Bpl"
3536
-LN"c:\program files (x86)\borland\delphi7\..\Bpl"
3637
-w-UNSAFE_TYPE

RamService.dof

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ MaxStackSize=1048576
9090
ImageBase=4194304
9191
ExeDescription=
9292
[Directories]
93-
OutputDir=
93+
OutputDir=bin
9494
UnitOutputDir=
9595
PackageDLLOutputDir=
9696
PackageDCPOutputDir=
@@ -135,8 +135,15 @@ ProductName=
135135
ProductVersion=1.0.0.0
136136
Comments=
137137
[Excluded Packages]
138-
C:\Program Files (x86)\Borland\Delphi7\Bin\indy70.bpl=Internet Direct (Indy) for D7
138+
c:\Program Files (x86)\Borland\Bpl\prgInternet6.bpl=Progsan Internet Components
139139
c:\Program Files (x86)\Borland\Bpl\kctrls6.bpl=KStringGrid component
140+
[HistoryLists\hlUnitAliases]
141+
Count=1
142+
Item0=WinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE;
143+
[HistoryLists\hlOutputDirectorry]
144+
Count=2
145+
Item0=bin
146+
Item1=DCU
140147
[Exception Log]
141148
EurekaLog Version=6105
142149
Activate=0

RamService.dpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ uses
1010

1111
begin
1212
Application.Initialize;
13+
Application.Title := 'RamDisk service';
1314
Application.CreateForm(TArsenalRamDisk, ArsenalRamDisk);
1415
Application.Run;
1516
end.

RamService.res

21.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)