EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Save file stream using cbfs

Also by EldoS: CallbackFilter
A component to monitor and control disk activity, track file and directory operations (create, read, write, rename etc.), alter file data, encrypt files, create virtual files.
#31816
Posted: 01/05/2015 21:15:35
by Ram Sharma (Basic support level)
Joined: 01/05/2015
Posts: 5

We have two scenarios when working with an empty file using cbfs (I am using mapper sample as reference, also i am using .net for working on this)

1.When opened we display contents of a remote file, I was able to do it by changing the file path to get the stream in openfile and getfileinfo events which worked for me.

2.When changes are made to it and saved. Its somehow writing back to remote file but not the empty file(locally).. i am not sure what event i should use to save the filestream locally (to the empty file).

Can someone help on accomplishing the second task?

Thanks
#31820
Posted: 01/06/2015 02:10:12
by Volodymyr Zinin (EldoS Corp.)

If it's necessary for you to have local copy as well as remote copy of the "backend" file then add to your callbacks (to the OnWrite, OnSetFileAttributes callbacks) the code which will duplicate the file contents. Another variant - just write data to the local file but at the time the file is closed (the OnClose callback is called) perform copying to the remote file (better if you do it not from the thread context which is calling the OnClose callback, but create some worker thread and perform copying from it, i.e. asynchronously).
#31825
Posted: 01/06/2015 11:39:59
by Ram Sharma (Basic support level)
Joined: 01/05/2015
Posts: 5

Zinin, Thanks for your response.

In my situation I only care about the file contents to be copied locally. I am trying to do this, but the filestream is not having right data in this OnWrite event, am I missing something? Here is the code for reference:

Code
private void CbFsWriteFileNative(
            object sender,
            CbFsFileInfo FileInfo,
            long Position,
            IntPtr Buffer,
            int BytesToWrite,
            ref int BytesWritten)
        {

            BytesWritten = 0;

            FileStreamContext Ctx = (FileStreamContext)GCHandle.FromIntPtr(FileInfo.UserContext).Target;
            FileStream fs = Ctx.hStream;

            if (fs.SafeFileHandle.IsInvalid)
            {
                throw new IOException("Could not open file stream.", new Win32Exception());
            }
            // This is writing to local path
            using (Stream outStream = File.Create(mRootPath + FileInfo.FileName))
            {
                while (fs.Position < fs.Length)
                {
                    outStream.WriteByte((byte)fs.ReadByte());
                }
            }

            System.Threading.NativeOverlapped Overlapped = new System.Threading.NativeOverlapped();
            Overlapped.EventHandle = IntPtr.Zero;
            Overlapped.OffsetLow = (Int32)Position;
            Overlapped.OffsetHigh = (Int32)(Position >> 32);
            if (!WriteFile(fs.SafeFileHandle.DangerousGetHandle(), Buffer, BytesToWrite, out BytesWritten, ref Overlapped))
            {
                UInt32 Error = CallbackFileSystem.GetLastWin32Error();
                throw new ECBFSError("");
            }
        }
#31828
Posted: 01/07/2015 05:16:28
by Volodymyr Zinin (EldoS Corp.)

For the local file you use stream but don't take into account the position to which data must be written. Maybe this is the problem.

Reply

Statistics

Topic viewed 1752 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!