This project is read-only.

Snapshot crashes when reading filestream

Aug 15, 2011 at 6:10 PM
Edited Aug 16, 2011 at 10:34 AM
I try to compress a file from a snapshot using DotNetZip. I read the file from the snapshot using the AlphaFS streamreader "OpenBackupRead" or "Open".
The problem is, when DotNetZip's stream-writer writes the first buffer saved in the byte array called "Buffer" the snapshot closes down?
I don't understand how that can be....the way I see it, the DonNetZip stream-writer has no "contact" with the snapshot or the AlphaFS stream reader,
it only write the data stored in the "Buffer" array and therefore it shouldn't be able to crash the snapshot, right?
But it does...why?
Is there some kind of memory conflict in my code? 
My code is:
Sub ComprFileStreamNetZip(ByVal SourceFile As String, ByVal TargetFile As String, Optional ByVal Password As String = "")
        Dim ReadFile As System.IO.FileStream
        Dim ZipOut As Ionic.Zip.ZipOutputStream
            ReadFile = Alphaleonis.Win32.Filesystem.File.OpenBackupRead(SourceFile)

            Dim buffer(25600) As Byte, n As Integer = 1
            ZipOut = New Ionic.Zip.ZipOutputStream(TargetFile)
            ZipOut.EnableZip64 = Ionic.Zip.Zip64Option.AsNecessary
            ZipOut.ParallelDeflateThreshold = 0
            If Not Password = "" Then
                ZipOut.Password = Password
                ZipOut.Encryption = Ionic.Zip.EncryptionAlgorithm.WinZipAes256
            End If
            ZipOut.PutNextEntry(GetFilename(SourceFile, "FULLF"))
            Do While n > 0
                n = ReadFile.Read(buffer, 0, buffer.Length)
                ZipOut.Write(buffer, 0, n)
        Catch ex As Exception
            Debug.Print("Error in ComprFileStreamNetZip: " & vbCr & ex.Message & vbCrLf & ex.StackTrace)
        End Try
    End Sub
I have made a function which print out the number of open/active snapshots, and if I insert it after the line "ZipOut.Write(buffer, 0, n)" it counts 0 snapshot = it has crashed/closed.
Next stream-reader loop, line: "n = ReadFile.Read(buffer, 0, buffer.Length)" trow the exception "The device is not ready." because the snapshot is now closed :-(
My snapshot list function:
    Sub ListAllSnapShots()
        Dim SnapShotsProp As Vss.VssSnapshotProperties
        Dim VSSImpl As Vss.IVssImplementation
        Dim oVSS As Vss.IVssBackupComponents
        VSSImpl = Vss.VssUtils.LoadImplementation
        oVSS = VSSImpl.CreateVssBackupComponents
        Debug.Print("Number of snapshots:" & oVSS.QuerySnapshots.Count.ToString)
    End Sub
Aug 15, 2011 at 7:32 PM
Edited Aug 16, 2011 at 12:16 AM

Got it!

Had to make oVSS a public variable instead of a local variable in my "OpenVSSSnapShop" procedure

Now it will keep open until I close it myself :o)

In declare section:
Public oVSS As Alphaleonis.Win32.Vss.IVssBackupComponents

    Function OpenVSSSnapShop(ByVal VolumeDrive As String) As String() 'Return an array: 0:SnapshotID, 1:SnapShotSetID, 2:SnapshotLongPath
        Dim ReturnVal(2) As String, SnapshotSetID As Guid, SnapshotID As Guid
        Dim VSSImpl As Vss.IVssImplementation
            VSSImpl = Vss.VssUtils.LoadImplementation
            oVSS = VSSImpl.CreateVssBackupComponents
            oVSS.SetBackupState(True, False, Vss.VssBackupType.Copy, False)
            Using async As Vss.IVssAsync = oVSS.GatherWriterMetadata()
            End Using
            SnapshotSetID = oVSS.StartSnapshotSet()
            SnapshotID = oVSS.AddToSnapshotSet(VolumeDrive, Guid.Empty)
            Using async As Alphaleonis.Win32.Vss.IVssAsync = oVSS.PrepareForBackup()
            End Using
            Using async As Alphaleonis.Win32.Vss.IVssAsync = oVSS.DoSnapshotSet
            End Using
            ReturnVal(0) = SnapshotID.ToString
            ReturnVal(1) = SnapshotSetID.ToString
            ReturnVal(2) = oVSS.GetSnapshotProperties(SnapshotID).SnapshotDeviceObject & "\" 'Get snapshot device object (long path string to snapshot)
            OpenVSSSnapShop = ReturnVal
        Catch ex As Exception
            Debug.Print("Error in OpenVSSSnapShop: " & vbCr & ex.Message & vbCrLf & ex.StackTrace)
        End Try
    End Function


Aug 16, 2011 at 10:33 AM
Edited Aug 17, 2011 at 11:33 AM

Hmmm, I still have problems with the snapshot closes down when making a stream read with AlphaFS :-(

Sep 7, 2011 at 8:27 PM

Sounds really strange. It really sounds as if the garbage collector goes in and disposes the oVSS instance. Are you keeping the reference to it alive until you are done with the snapshot?

Sep 7, 2011 at 8:47 PM
Edited Sep 7, 2011 at 8:47 PM

Hi decaf

I might have solved the problem....I open each snapshot in its own thread and keep the thread upen in a loop until I don't need it anymore. I guess it make sense in a way....I just expected the snapshot would be open until it was close directly or not used for a period, but it seems to be related/linked in a way to the thread/process that run/opens it.



Sep 7, 2011 at 8:50 PM

Hmm.. still think it sounds strange. The snapshot should be open until you dispose of the oVSS instance (or the garbage collector does it for you) as far as I know.

I've never seen the behavior you describe myself. But as long as you found a solution things are good I guess!? :)

Regards, Peter.