Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!hplabs!hpfcso!hpldola!hpctdlb!hpctdls!wei From: wei@hpctdls.HP.COM (Bill Ives) Newsgroups: comp.sys.ibm.pc Subject: Re: DOS FAT table updates Message-ID: <2010024@hpctdls.HP.COM> Date: 25 May 90 21:15:52 GMT References: <30365@ut-emx.UUCP> Organization: Hewlett-Packard CTD, Colo. Spgs. Lines: 55 > / croley@walt.cc.utexas.edu (David T. Croley) / 10:58 am May 23, 1990 / >I am curious. When DOS is saving a file, does it write the file >to disk and then update the FAT and write it to disk, or is the order >reversed. When saving a file, if the file has not changed size, there is no need for DOS to update the FAT. Updating the file data area is sufficient. In the case of growing a file ( or changing its number of allocated clusters in the FAT ) I do not know for sure, although I would guess it doesn't matter which one is updated first since in the event of power failure the FAT could be corrupted in either case. My best guess is that DOS determines which cluster is free for adding to the file, writes its data to it, then updates the FAT. In this case if power fails as the data is written, the FAT is still okay for the original file size ( although the extra data in the new cluster is not reliable ). Then DOS must update the FAT which may involve 1 or 2 sector updates (in our case of adding only one cluster). It may mark the free cluster as the the EOF (0xFFFF in 16 bit FAT), write it to its sector, then update the old EOF to point to the new EOF cluster number. If power failed between these operations you would have the famous "lost cluster" condition often flaged by CHKDSK. I believe it may still be possible to corrupt the disk FAT if power fails at the most in-oportune time -- reconstruction utilities come in handy then... >Also, does anyone know where I can get info on the routines DOS uses >to allocate clusters to a file. I would like to replace these with my >own routines if possible. My guess is that these are MS DOS secrets :-) . I have written my own utilities to handle the FAT and ROOT DIR for an in-house project and its not too hard to figure it out. DOS appears to just allocate the next available cluster whether never used before or part of a removed file cluster chain. >I have looked in every DOS reference that I can find, and none are >detailed enough to have this type of information. I found the same thing. You can figure out most of it by poking around the disk sector by sector. An okay starting point book is "Advanced MS-DOS" by Ray Duncan -- I like his articles and his book seems to follow the same easy mannered tone IMHO ..... A hint to get you started -- the cluster numbers ( whether in 12 or 16 bit FATs) have to be adjusted down by two to determine the logical sector number used by DOS absolute disk I/O interrupts 25h and 26h. This is due to the first two clusters being dummied out to hold the FAT ID byte. You probably already knew this, but hey just in case :-) Good Luck Bill Ives HP CTD #include