EldoS | Feel safer!

Software components for data protection, secure storage and transfer

CAllbackFS: sample crashes

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.
#5874
Posted: 04/15/2008 03:56:07
by Robin Astle (Basic support level)
Joined: 04/15/2008
Posts: 30

When trying the Mapper sample project it works fine when I map a regular directory like c:\foo
However when I map a networkshare: \\server\share I get an exception on the OpenFile event when the FileName is \:
Code
if (info.Exists)
                    {
                        sfh = CreateFile(mRootPath + FileName, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero);
                        if (sfh.IsInvalid)
                        {
                            throw new IOException("Could not open file stream.", new Win32Exception());
                        }
                        Ctx.hStream = new FileStream(sfh, FileAccess.ReadWrite);
                        FileHandleContext = GCHandle.ToIntPtr(GCHandle.Alloc(Ctx));
                    }

When I run the sample on the same netwrkshare on a different computer, it does work properly. Anyone has an idea about the reasons why this could be?
sfh is indeed invalid and thus the method throws the exception.
#5875
Posted: 04/15/2008 05:03:56
by Volodymyr Zinin (EldoS Corp.)

Hello,

I think the problem is because full access is requested when the directory is opened. At least two solutions exist: either don't open directories at all (i.e. don't obtain handles to them because they aren't necessary in the Mapper sample) and for directories creation use the win32 api CreateDirectory or to open directories with restricted access (perhaps only with FileAccess.Read).
We will correct the samples in the nearest build.
#5876
Posted: 04/15/2008 05:08:26
by Volodymyr Zinin (EldoS Corp.)

Also check whether the share is read-write or not. And please specify an error code (using GetLastError) that the CreateFile call returned.
Thanks.
#5878
Posted: 04/15/2008 05:21:45
by Robin Astle (Basic support level)
Joined: 04/15/2008
Posts: 30

I changed the access from read-write to read and now it works fine everywhere, thanx!
#5899
Posted: 04/16/2008 09:31:22
by Robin Astle (Basic support level)
Joined: 04/15/2008
Posts: 30

I spoke to soon!
Code
if (info.Exists)
                    {
                        sfh = CreateFile(mRootPath + FileName, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero);
                        if (sfh.IsInvalid)
                        {
                            Console.WriteLine("ERROR: OpenFile: " + FileName);
                            throw new IOException("Could not open file stream.", new Win32Exception());
                        }
                        Ctx.hStream = new FileStream(sfh, FileAccess.ReadWrite);
                        FileHandleContext = GCHandle.ToIntPtr(GCHandle.Alloc(Ctx));
                    }

I changed this to:
Code
if (info.Exists)
                    {
                        sfh = CreateFile(mRootPath + FileName, FileAccess.Read, FileShare.Read, IntPtr.Zero, FileMode.Open, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero);
                        if (sfh.IsInvalid)
                        {
                            Console.WriteLine("ERROR: OpenFile: " + FileName);
                            throw new IOException("Could not open file stream.", new Win32Exception());
                        }
                        Ctx.hStream = new FileStream(sfh, FileAccess.Read);
                        FileHandleContext = GCHandle.ToIntPtr(GCHandle.Alloc(Ctx));
                    }

When I now open a word document, change it and try to save it back... I get file permission issues and the saving of the file fails. I'm clueless about where to look, and to what I should change it to work properly.
#5900
Posted: 04/16/2008 12:35:49
by Volodymyr Zinin (EldoS Corp.)

Hello,

You must open files (not directories) with read-write access.
Correct the code like this:
Code
if (info.Exists)
{
  DWORD FileAccess;
  if (FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    FileAccess = FileShare.Read;
  else
    FileAccess = FileShare.ReadWrite;
    
sfh = CreateFile(mRootPath + FileName, FileAccess, FileShare.Read, IntPtr.Zero, FileMode.Open, FILE_FLAG_BACKUP_SEMANTICĀ­S, IntPtr.Zero);
...




#5905
Posted: 04/17/2008 02:08:00
by Robin Astle (Basic support level)
Joined: 04/15/2008
Posts: 30

That's what I'm doing I just ommitted the file code, my entire openfile callback looks like this:

Code
private void CbFsOpenFile(object sender, string FileName, uint FileAttributes, ref IntPtr FileHandleContext)
        {
            SafeFileHandle sfh = null;
            FileContext Ctx = null;
            if (FileHandleContext != IntPtr.Zero)
            {
                Ctx = (FileContext)GCHandle.FromIntPtr(FileHandleContext).Target;
                Ctx.IncrementCounter();
                return;
            }
            else
            {
                Ctx = new FileContext();
            }
            if((FileAttributes & (uint)CbFsFileAttributes.CBFS_FILE_ATTRIBUTE_DIRECTORY) != 0)
            {
                Console.WriteLine("flag1:" + (FileAttributes & (uint)CbFsFileAttributes.CBFS_FILE_ATTRIBUTE_DIRECTORY));
                DirectoryInfo info = new DirectoryInfo(mRootPath + FileName);

                if (!FileName.Equals(@"\") && !info.Exists)
                {
                    FileHandleContext = IntPtr.Zero;
                }
                else
                {
                    if (info.Exists)
                    {
                        Console.WriteLine("info.Exists");
                        sfh = CreateFile(mRootPath + FileName, FileAccess.Read, FileShare.Read, IntPtr.Zero, FileMode.Open, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero);
                        if (sfh.IsInvalid)
                        {
                            throw new IOException("Could not open file stream.", new Win32Exception());
                        }
                        Ctx.hStream = new FileStream(sfh, FileAccess.Read);
                        FileHandleContext = GCHandle.ToIntPtr(GCHandle.Alloc(Ctx));
                    }
                }
            }
            else
            {
                Console.WriteLine("flag2:" + (FileAttributes & (uint)CbFsFileAttributes.CBFS_FILE_ATTRIBUTE_DIRECTORY));
                sfh = CreateFile(mRootPath + FileName, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, (int)FileAttributes, IntPtr.Zero);
                if (sfh.IsInvalid)
                {
                    throw new IOException("Could not open file stream.", new Win32Exception());
                }
                Ctx.hStream = new FileStream(sfh, FileAccess.ReadWrite);
                FileHandleContext = GCHandle.ToIntPtr(GCHandle.Alloc(Ctx));

            }
            Ctx.IncrementCounter();
        }
#5907
Posted: 04/17/2008 03:14:08
by Volodymyr Zinin (EldoS Corp.)

Quote
Robin Astle wrote:
I get file permission issues and the saving of the file fails.

Please specify where the problem occurs. And how can we reproduce it?
Thanks.
#5908
Posted: 04/17/2008 03:53:21
by Robin Astle (Basic support level)
Joined: 04/15/2008
Posts: 30

I've been investigating this further, and still don't fully understand the issue, I've been playing with storagetype, and now when I open a word document edit it and save it, the save as dialog pops up.
I also noticed the callback GetFileSecurity being called on the directory and on the original file being opened.
Anyone has a clue about what permissions MS Word expects in order to edit a file?
#5915
Posted: 04/17/2008 05:52:25
by Robin Astle (Basic support level)
Joined: 04/15/2008
Posts: 30

When I open the directory readwrite I can safe word files correctly when I map a local folder.
But then I can't browse the network share anymore.

So the issue of saving word files is that in my project The OpenFile on a directory is read only as you suggest, but that prevents me from opening and saving word files.
Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.

Reply

Statistics

Topic viewed 11711 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!