Konamiman / Nextor

A disk operating system for MSX computers, forked from MSX-DOS 2.31
Other
183 stars 33 forks source link

Change the mechanism to access mounted files #93

Closed Konamiman closed 2 years ago

Konamiman commented 3 years ago

Nextor allows mounting disk image files to drives since version 2.1.0.

Internally, the mounting mechanism was originally implemented as follows: the mounted file is kept open and whenever sector access is needed for the mounted file, the sector number is translated into a file location which is set as the current file pointer for the mounted file, then the regular file read/write functions are invoked.

This mechanism worked but it was somewhat convoluted since the file access code was not originally designed to be re-entrant and now it was calling itself (reading from a file inside the mounted drive involved executing the file read/write functions, which would in turn invoke themselves again); a "calling level" variable had to be kept and some internal variables had to be backed up before the second call and restored afterwards. This never worked really well, there were instances where accessing a mounted file would cause a computer crash.

This pull request modifies the way files are mounted. Now a mounted file isn't kept open; instead, the unit descriptor for the drive where the file is mounted will store the sector number where the file begins (absolute device sector number), and this number will be added to the sector in the mounted drive being accessed.

This implies that a new restriction is introduced: a file needs to be stored across consecutive sectors in disk in order to be mountable. If that's not the case, trying to mount the file to a drive via the _MAPDRV function call will return an .ICLUS error (whose message has been changed from "Invalid cluster number" to "Invalid cluster number or sequence"); when trying to mount the file via CALL MAPDRV in BASIC, an "Invalid cluster sequence" error will be thrown (this error has code 82 and didn't exist previously).

Note that this change applies only to the mechanism for mounting files to drives using MAPDRV.COM or CALL MAPDRV after booting Nextor in normal mode. The disk emulation mode has been using a "sector offset" based approach and requiring consecutive clusters since the very beginning.

Additionally, the _GDLI function call will now return two extra values for a drive that has a mounted file: "start cluster" and "start sector", see the programmers reference for the details.

grauw commented 2 years ago

Is there an easy way to defragment a file?

It is something that people encounter, and the current fix that I know of is “copy everything to your PC, format the drive, and copy it back” which is not so nice. Would be nice if there was a DOS2 call for it (a conditional copy to unfragmented space), this could then be used by a utility command as well as MAPDRV, EMUFILE, etc.