예제 #1
0
        //public void WriteFileToTape(BackupFile objBackupFile)
        //{
        //    TapeOperator objTapeOperator;
        //    TapeOperator.MediaInfo objMediaInfo;
        //    FileStream objFileStreamToWrite;
        //    byte[] objWriteBytes;
        //    Console.WriteLine("WriteFileToTape Method");
        //    Console.WriteLine("Initializing the tape drive...");
        //    objTapeOperator = new TapeOperator();
        //    objTapeOperator.Load(TapeName);
        //    Console.WriteLine("Setting Tape Position...");
        //    objTapeOperator.SetTapePosition(objBackupFile.StartTapeLocation);
        //    Console.WriteLine("Tape Position is " + objTapeOperator.GetTapePosition());
        //    //for some reason, I kept getting an error until I added the below statements
        //    Console.WriteLine("Getting the Tape Info...");
        //    objMediaInfo = objTapeOperator.GetTapeMediaParameters();
        //    //Console.WriteLine(objMediaInfo.BlockSize);
        //    //Console.WriteLine(objTapeOperator.BlockSize);
        //    Console.WriteLine("Opening the file...");
        //    objFileStreamToWrite = new FileStream(objBackupFile.FileNameAndPath, FileMode.Open, FileAccess.Read);
        //    objBackupFile.Size = objFileStreamToWrite.Length;
        //    //This code is where modification will be required to accomodate consecutive reads and writes to the tape for large file sizes
        //    if (objBackupFile.isLargeFile)
        //    {
        //        Console.WriteLine("This file will take " + objBackupFile.FileChunkCount + " Passes...");
        //        for (int intCounter = 1; intCounter <= objBackupFile.FileChunkCount; intCounter++)
        //        {
        //            if (intCounter < objBackupFile.FileChunkCount)
        //            {
        //                Console.WriteLine("This is pass " + intCounter);
        //                //Console.WriteLine("Initilizing Array");
        //                objWriteBytes = new byte[BackupFile.MaxMemoryChunk];
        //                //Console.WriteLine("Done Initilizing Array");
        //                Console.WriteLine("Reading file to Memory...");
        //                //Console.WriteLine(objFileStreamToWrite.Position);
        //                //lngPosition = objFileStreamToWrite.Position;
        //                objFileStreamToWrite.Read(objWriteBytes, 0, BackupFile.MaxMemoryChunk);
        //                //Console.WriteLine(objTapeOperator.GetTapePosition());
        //                Console.WriteLine("Writing Filestream to Tape");
        //                objTapeOperator.Write(objTapeOperator.GetTapePosition(), objWriteBytes);
        //                //Console.WriteLine("Finished Writing to Tape");
        //                //Console.WriteLine(objTapeOperator.GetTapePosition());
        //            }
        //            else
        //            {
        //                int intLeftOverBytes = (int)(objBackupFile.Size - BackupFile.MaxMemoryChunk * (intCounter - 1));
        //                Console.WriteLine("This is pass " + intCounter);
        //                //Console.WriteLine("Initilizing Array");
        //                objWriteBytes = new byte[intLeftOverBytes];
        //                //Console.WriteLine("Done Initilizing Array");
        //                Console.WriteLine("Reading file to Memory...");
        //                //lngPosition = objFileStreamToWrite.Position;
        //                objFileStreamToWrite.Read(objWriteBytes, 0, intLeftOverBytes);
        //                Console.WriteLine("Writing Filestream to Tape");
        //                objTapeOperator.Write(objTapeOperator.GetTapePosition(), objWriteBytes);
        //                //Console.WriteLine("Finished Writing to Tape");
        //            }
        //        }
        //    }
        //    else
        //    {
        //        //Console.WriteLine("Initilizing Array");
        //        objWriteBytes = new byte[objFileStreamToWrite.Length];//This one generates an IOException if the file size is greater than 2GB
        //        //Console.WriteLine("Done Initilizing Array");
        //        Console.WriteLine("Reading file to Memory...");
        //        objFileStreamToWrite.Read(objWriteBytes, 0, (int)objFileStreamToWrite.Length);
        //        Console.WriteLine("Writing Filestream to Tape");
        //        objTapeOperator.Write(objBackupFile.StartTapeLocation, objWriteBytes);
        //        Console.WriteLine("Finished Writing to Tape");
        //    }
        //    objBackupFile.EndTapeLocation = objTapeOperator.GetTapePosition();
        //    Console.WriteLine("Tape Position is " + objBackupFile.EndTapeLocation);
        //    Console.WriteLine("Now I'm Cleaning up resources...");
        //    objTapeOperator.Close();
        //    objFileStreamToWrite.Close();
        //    Console.WriteLine("Now I'm Done!");
        //    Console.WriteLine();
        //}
        public void WriteFilesToTape(BackupFiles objBackupFiles, bool isSingleFile = false)
        {
            Console.WriteLine("WriteFileToTape Method");
            Console.WriteLine("Initializing the tape drive...");
            TapeOperator = new TapeOperator();
            TapeOperator.Load(TapeName);

            Console.WriteLine("Setting Tape Position...");
            //If it's a single file than I use the tape location specified on the BackupFile object, else I parse the collection for the next available location
            TapeOperator.SetTapePosition(isSingleFile ? objBackupFiles[0].StartTapeLocation : objBackupFiles.NextAvailableTapeLocation);
            Console.WriteLine("Tape Position is " + TapeOperator.GetTapePosition());

            //for some reason, I kept getting an error until I added the below statements
            Console.WriteLine("Getting the Tape Info...");
            MediaInfo = TapeOperator.GetTapeMediaParameters();
            //Console.WriteLine(objMediaInfo.BlockSize);
            //Console.WriteLine(objTapeOperator.BlockSize);

            foreach (BackupFile objBackupFile in objBackupFiles)
            {
                Console.WriteLine("Opening the file...");
                FileStreamToWrite = new FileStream(objBackupFile.FileNameAndPath, FileMode.Open, FileAccess.Read);
                objBackupFile.Size = FileStreamToWrite.Length;
                objBackupFile.StartTapeLocation = TapeOperator.GetTapePosition();

                //This code is where modification will be required to accomodate consecutive reads and writes to the tape for large file sizes
                if (objBackupFile.isLargeFile)
                {
                    Console.WriteLine("This file will take " + objBackupFile.FileChunkCount + " Passes...");
                    for (int intCounter = 1; intCounter <= objBackupFile.FileChunkCount; intCounter++)
                    {
                        if (intCounter < objBackupFile.FileChunkCount)
                        {
                            WriteBytes = null;//clears the memory...
                            GC.Collect();

                            Console.WriteLine("This is pass " + intCounter);
                            //Console.WriteLine("Initilizing Array");
                            WriteBytes = new byte[BackupFile.MaxMemoryChunk];
                            //Console.WriteLine("Done Initilizing Array");

                            Console.WriteLine("Reading file to Memory...");
                            //Console.WriteLine(objFileStreamToWrite.Position);
                            //lngPosition = objFileStreamToWrite.Position;
                            FileStreamToWrite.Read(WriteBytes, 0, BackupFile.MaxMemoryChunk);

                            //Console.WriteLine(objTapeOperator.GetTapePosition());
                            Console.WriteLine("Writing Filestream to Tape");
                            TapeOperator.Write(TapeOperator.GetTapePosition(), WriteBytes);
                            //Console.WriteLine("Finished Writing to Tape");
                            //Console.WriteLine(objTapeOperator.GetTapePosition());
                        }
                        else
                        {
                            int intLeftOverBytes = (int)(objBackupFile.Size - BackupFile.MaxMemoryChunk * (intCounter - 1));

                            WriteBytes = null;//clears the memory...
                            GC.Collect();

                            Console.WriteLine("This is pass " + intCounter);
                            //Console.WriteLine("Initilizing Array");
                            WriteBytes = new byte[intLeftOverBytes];
                            //Console.WriteLine("Done Initilizing Array");

                            Console.WriteLine("Reading file to Memory...");
                            //lngPosition = objFileStreamToWrite.Position;
                            FileStreamToWrite.Read(WriteBytes, 0, intLeftOverBytes);

                            Console.WriteLine("Writing Filestream to Tape");
                            TapeOperator.Write(TapeOperator.GetTapePosition(), WriteBytes);
                            //Console.WriteLine("Finished Writing to Tape");
                        }

                    }
                }
                else
                {
                    WriteBytes = null;//clears the memory...
                    GC.Collect();

                    //Console.WriteLine("Initilizing Array");
                    WriteBytes = new byte[FileStreamToWrite.Length];//This one generates an IOException if the file size is greater than 2GB
                    //Console.WriteLine("Done Initilizing Array");

                    Console.WriteLine("Reading file to Memory...");
                    FileStreamToWrite.Read(WriteBytes, 0, (int)FileStreamToWrite.Length);

                    Console.WriteLine("Writing Filestream to Tape");
                    //TapeOperator.Write(objBackupFile.StartTapeLocation, WriteBytes);
                    TapeOperator.Write(isSingleFile ? objBackupFile.StartTapeLocation : objBackupFiles.NextAvailableTapeLocation, WriteBytes);
                    //Console.WriteLine("Finished Writing to Tape");
                }

                objBackupFile.EndTapeLocation = TapeOperator.GetTapePosition();
                Console.WriteLine("Tape Position is " + objBackupFile.EndTapeLocation);
            }

            Console.WriteLine("Now I'm Cleaning up resources...");
            TapeOperator.Close();
            FileStreamToWrite.Close();
            Console.WriteLine("Now I'm Done!");
            Console.WriteLine();
        }