Classic Computer Magazine Archive COMPUTE! ISSUE 72 / MAY 1986 / PAGE 101


Bill Wilkinson

Avoiding Disk Errors

I know many of you will find this hard to believe, but I've never encountered a disk error on the Atari which I couldn't explain. Further, I have had very few DOS errors, ever. (The reasons for the few errors I have encountered, by the way, were always related to random access files-a common problem with Atari DOS 2.0 and its derivatives.) Yet after a few hundred phone calls and letters, I know that many of you have experienced the frustration of wiped-out disk files. Why? Well, I can't know each and every reason, and I can't repair damage that's already been done, but maybe I can give you some helpful hints for the future.

Hands Off That Disk
Hint 1: Never, never, never take a disk out of a drive unless the program you're using tells you to. (This goes beyond even the good advice about never removing a disk when the drive is still spinning.) In particular, never swap disks until prompted to do so. Why? Well, because the Atari disk drive has no way to tell the computer that the disk has been removed or changed.
    Consider: How does any DOS know what disk sectors to allocate to a new file? Generally, a DOS keeps a list of unused disk sectors. The next time it needs to find a sector (for example, to extend a file), it takes one from this list. The list (called a Volume Table Of Contents or VTOC in Atari parlance) is usually kept on disk until a file is opened, when it is read into memory. It is rewritten to the disk when a file is closed.
    Okay, now open a file for output, write some information, swap disks, and write more data. What happens? The list of sectors was correct for the first disk, but it's extremely unlikely that it bears any reasonable relation to what exists on the second disk. Most probably, DOS will allocate several sectors which were already part of other files on the second disk. Kablooey!
    If you're using an application program, then, follow the prompts and don't swap disks unless told to. If you're programming and working with disk files, make sure you close all open files before swapping disks (END automatically closes all open files in BASIC). If you're using DOS, you should be safe as long as you change disks only at the DOS prompt. Of course, when duplicating files or disks, you must swap disks when DOS tells you to.

Beware Of  RESET
Hint 2: Never hit the SYSTEM RESET button during a disk operation. For example, if you hit RESET in the middle of a SAVE, it's possible to end up with a completely blown program. In fact, if you then SAVE the program to disk again, you could end up with a blown disk file.
    This results from a really subtle bug in DOS 2.0. When DOS enters what is known as burst I/O mode (to speed up input/output), it "copies" the memory to disk. But DOS 2.0's file organization requires that the last three bytes of each sector contain a link to the next sector in the file. How can it do this when it is writing directly from memory? Answer: By "swapping" three bytes of memory long enough to write a sector, and then restoring the bytes.
    Now suppose you happen to hit RESET when those three bytes are swapped out. Oops...say goodbye to your program.
    There are two ways to fix this problem. First, since DOS gets control after a RESET, it could check to see if a disk write was interrupted. If so, it could restore the three bytes. Or, second, DOS could always copy bytes to be written into a buffer, thus never disturbing the program (or data) in memory. The second approach is successfully used by DOS 2.5.

Missing Sectors
Hint 3: Avoid hitting RESET during disk operations even if you're using DOS 2.5, because you may still mess up the disk a bit. Here's one way: Open a file for write (OPEN #5,8,0, "D: FILE" in BASIC, for example), write some data, OPEN another file for write, write data to both files, CLOSE the first file, write some more data to the second file, and then hit RESET. What happens?
    The VTOC says the sectors in the second file which were written before the CLOSE are now in use (and that was true when the CLOSE took place). If you add the number of free sectors remaining on the disk to the number of sectors used in all files, the total is no longer 707 in single density or 1010 in enhanced density, as it should be. You just lost some of your disk space.
    Hint 4: Everything I just mentioned about RESET also applies to turning off the power. For example, if you have a power failure in the middle of a SAVE from BASIC or while there are some data files open in a business program, be prepared for some problems.
    Fortunately, DOS 2.5 comes with a program called DISKFIX.COM which does a pretty good job of fixing up a "damaged" disk (either DOS 2.0 or 2.5). It allows you to undelete files as long as you haven't written any new files since the deletion. At your choice it will either try to recover or permanently remove a file which was left open for output. And, most importantly, it checks each file on the disk to make sure it is OK, and then reconstructs the VTOC to ensure that all 707 or 1010 sectors are accounted for.