To ensure that the file is always in a consistent state, recovery information is saved before any updating is done to the file. The recovery information consists of a timestamp and a pointer to the record that is being updated. Recovery information is not saved when the file is being created.
The following sequence of events takes place when a file is updated:
-
The recovery information is saved in segment 0 of the file.
-
Information is written to the file in a careful order so that data is not lost if the WRITE operations are not completed. The data record affected by the update is the last record changed.
If the update is terminated before step 2 is finished, the update is completed the next time the file is opened. The recovery process begins when the record referenced by the recovery information in segment 0 is read. The file is then updated with that record. During the update process, if a block is read that has a timestamp equal to that of the recovery timestamp, that block is treated as already reflecting the update. After the update is done, the recovery information is zeroed out, and segment zero is rewritten.