EldoS | Feel safer!

Software components for data protection, secure storage and transfer

How to restrict write operation on a file without setting the readonly

Posted: 01/19/2016 20:56:32
by Devender Reddy (Standard support level)
Joined: 11/25/2015
Posts: 12


I have a requirement, where based on some criteria, writes should be blocked, but at the same time move/replacing this file with another version of this file should succeed (Effectively I want to maintain this file as a view/read only copy. At the same time, if somebody modified it, then I want to get the latest version for this file).

In order to achieve this I tried with both methods.

1) Setting the file readonly attribute:
With this approach, I was able to block the user from writing, but the move is failing with access denied permission.

        public void LockOrUnlockFile(string filePath, bool toLock)
            string fileName = filePath.Remove(0, 2);
            int attr = _solFSStorage.GetFileAttributes(fileName);

            if (toLock) {
                attr |= SolFSFileAttribute.attrReadOnly;
                lockedFiles.Add(fileName, AccessTypes.Read);
            } else {
                attr &= ~SolFSFileAttribute.attrReadOnly;
            _solFSStorage.SetFileAttributes(fileName, attr);

2) By putting the checks in OnWriteFile event.
With this nothing is working, the user was able to modify the file.
_callBackFS.OnWriteFile = new CbFsWriteFileEvent(CbFsWriteFile);

        private void CbFsWriteFile(CallbackFileSystem Sender, CbFsFileInfo FileInfo, long Position,
                                   byte[] Buffer, int BytesToWrite, ref int BytesWritten)
            manageAccess(AccessActionTypes.Write, FileInfo.FileName);  // Access Control
            if (isFileOwnedByOther(FileInfo.FileName)) {
                throw new ECBFSError(ERROR_ACCESS_DENIED);

            try {
                FileStreamContext Ctx = (FileStreamContext)GCHandle.FromIntPtr(FileInfo.UserContext).Target;
                SolFSStream fs = Ctx.hStream;

                fs.Seek(Position, SeekOrigin.Begin);
                fs.Write(Buffer, 0, BytesToWrite);
                BytesWritten = BytesToWrite;
            } catch (ESolFSError E) {

So can you please let me know, what is the best way to achieve this functionality.
Posted: 01/20/2016 04:30:14
by Volodymyr Zinin (Team)

You can try the second variant but with setting the CallbackFileSystem.FileCacheEnabled property to false or setting the CallbackFileSystem.CachePolicyWriteThrough property to true (in the last case the caching is still enabled but for any write operation the OnWrite callback will be called immediately).
Unfortunately there is one case when it doesn't work "as expected" - when the file is written by the use of the memory mapped section (MapViewOfFile, etc). In this case file data, which is being written, is written in memory first (and CBFS doesn't know about this). And only later the system component "memory manager" flushes the data to the file. Of course the OnWrite callback will be called, but if you finish it with error then the memory manager will decide to repeat the operation several times and then show on the active desktop an error message (something like this i.imgur.com/iBA8D9A.png). Although for this case you can try to return "success" from the OnWrite callback if the GetOriginatorProcessId == 4 (which means that the SYSTEM process is performing write).
Posted: 01/21/2016 00:22:03
by Devender Reddy (Standard support level)
Joined: 11/25/2015
Posts: 12

Hi Volodymyr,

Thanks for the quick response. I tried both options and none of them is triggering the OnWriteFile Event. This event is getting for the other file, but not the one I am interested in(I have a DB file inside my SolidFS and this event is getting triggered for the db journal file). So most probably, it appears to me that the cache is still in place and the CBFS might be doing a lazy write.

So just wondering any other approach through which we can achieve this functionality.
Posted: 01/21/2016 03:54:58
by Volodymyr Zinin (Team)

Please specify what version of Callback File System you are using?
Posted: 01/29/2016 17:58:49
by Devender Reddy (Standard support level)
Joined: 11/25/2015
Posts: 12

Hi Volodymyr,

Sorry for the delayed response, I was a bit occupied with other stuff.

Regarding the version, I am using CBFS5.0.

Posted: 02/01/2016 02:54:43
by Volodymyr Zinin (Team)

In the Callback File System version 6 the file data caching was significantly modified. Please try the problem with that version.




Topic viewed 4019 times

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


Back to top

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

Got it!