Creating a Backup Application

To perform input or output on a tape, a backup application must first obtain a handle of the tape device. The following code sample shows you how to use the CreateFile function to open a tape device.

HANDLE hTape;   // handle to tape device
 
hTape = CreateFile(TEXT("\\\\.\\TAPE0"),         // tape dev to open
                   GENERIC_READ | GENERIC_WRITE, // read/write access
                   0,                            // not used
                   0,                            // not used
                   OPEN_EXISTING,                // req for tape devs
                   0,                            // not used
                   NULL);                        // not used

To back up a directory tree to a tape, an application must use the FindFirstFile and FindNextFile functions to traverse the directory tree. Each time a file is found, the application should get the file attributes by using the GetFileAttributes function.

If there are hard links, an application should determine the number of links, and save the unique identifier of the file in a table for future comparisons. The first time a file is found, the application should use CreateFile to open the file, and the BackupRead function to begin the backup. Then it can use the WriteFile function repeatedly to transfer all the information in the buffer used by BackupRead to the tape. The second time a file is found (checked against the table of file identifiers when there are hard links), the application can write the general file information to the tape, followed by a stream that has an identifier that is BACKUP_LINK.

When restoring files from tape to disk, an application must use the CreateFile, BackupWrite, and ReadFile functions. For each file on a tape, the application should use CreateFile to create a new file on disk, and BackupWrite to begin restoring the file. Then the application should use ReadFile repeatedly until all the information for the file is read from the tape into the buffer filled by BackupWrite.

If one of the streams in the BackupWrite buffer has a BACKUP_LINK stream identifier, the application must establish a hard link. If the data needed to establish the link does not exist, BackupWrite fails. The application can use a pre-existing catalog to locate and restore the original data, or it can notify the user that the file data to be restored is in a different location.