Deleting Files and File Forks
You can delete a file by callingFSpDelete
,HDelete
, orPBHDelete
. These functions delete both forks of a file by removing the catalog entry for the file and adjusting the volume information block and volume bitmap accordingly. These functions do not actually erase the disk areas occupied by the file, so there is a reasonable chance that a good disk utility might be able to salvage a deleted file if the user hasn't allocated any new file blocks in the meantime.Sometimes you might want to truncate just one fork of a file. Listing 2-2 illustrates how you can truncate a file's resource fork while preserving the data fork.
Listing 2-2 Deleting a file's resource fork
FUNCTION TruncateRF (myFileSpec: FSSpec): OSErr; VAR myErr: OSErr; {result code} myFile: Integer; {file reference number} BEGIN myErr := FSpOpenRF(myFileSpec, fsRdWrPerm, myFile); IF myErr = noErr THEN myErr := SetEOF(myFile, 0); IF myErr = noErr THEN myErr := FSClose(myFile); IF myErr = noErr THEN myErr := FlushVol(myFileSpec.vRefNum); TruncateRF := myErr; END;The function TruncateRF defined in Listing 2-2 opens the file's resource fork with exclusive read/write permission and sets its logical end-of-file to 0. This effectively releases all the space occupied by the resource fork on the volume. Then TruncateRF closes the file and updates the volume.