Necessity
Naraeon SSD Tools has various objects those need path. From TPhysicalDrive as the name shows, a lot of Getter objects need the destination drive path. Other than that, some objects require path of a partition. In those cases, it is reasonable to memorize the path and use many times.
Someone would think this is unnecessary, but in this project this pattern is widely used. So it is not a option to understand this subject.
Because of the length, I divided this into two parts. PhysicalDrive part would be next one.
Basic form: TOSFile
The basic form is TOSFile. (Without verifying) memorizes a path, and changing OS error code into exception object. It is easy to get a bug by forgetting to write GetLastError checking code. But this exception-based approach just needs writing ‘IfOSErrorRaiseException’ after each API call. Next code block shows the implementation of it.
[snippet slug=ifoserrorraiseexception lang=pascal]
Many functions need the path without prefix. For example, ‘7’ from ‘\\.\PhysicalDrive7’, and ‘C:’ from ‘\\.\C:’. The GetPathOfFileAccessingWithoutPrefix function does it.
[snippet slug=getpathoffileaccessingwithoutprefix lang=pascal]
Windows path is not case sensitive. So internally Naraeon SSD Tools converts all the path into upper case path. Path comparing can be done with IsPathEqual function.
[snippet slug=uppercase lang=pascal]
When handle is needed: TOSFileWithHandle
Sometimes APIs require handles, not path. Then we can use TOSFileWithHandle. You can override GetMinimumPrivilege function and the handle would have the right permission. And also, Security descriptor would be set for each handles. This would improve security of the handles.
[snippet slug=createhandle lang=pascal]
Most of the things are done in TFileHandle object. And there is a function named ‘Unlock’. This is used when there are some storage that is both removable storage and SAT(SCSI) storage, like Z80. With the function, the handles would be closed during the unlock object is referred from anywhere. After the unlock object is destroyed, the handles would be created again and set.
[snippet slug=tosfileunlock lang=pascal]
When ioctl is needed: TIoControlFile
ioctl is used frequently in this app. So it is managed separately. And by using metaclass, I made the code to double-check buffer types. Because I made several mistakes with the types and size of them.
[snippet slug=buildosbufferby lang=pascal]
TL;DR
Naraeon SSD Tools’ objects need path. And repeated portions are defined as following classes.
- TOSFile: Path
- TOSFileWithHandle: Path + Handle
- TIoControlFile: Path + Handle + Ioctl