EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Save the file when closing the application

Posted: 01/17/2013 04:04:10
by Davide Baccaglini (Standard support level)
Joined: 11/19/2012
Posts: 48

Hello, I've a question about saving file.

In my project I want to save file only when user close file's application not when press save. In this context I use OnWriteFile to save in memory the updated file but I want to send the modified file to server only when the current application is closed.

I try to use OnCleanupFile but I saw that is not correct, in fact this event is fired more times during saving and opening file.

Is there a way to do this?

Thanks in advance,
Posted: 01/17/2013 06:23:46
by Volodymyr Zinin (Team)

OnCleanupFile is called when a handle to a file is being closed (when a program is calling CloseHandle).
After all handles to a file have been closed it's possible that write requests occur (the OnWrite callback is called). It can be because the system components (or third-party components like file system filters) perform flushing of the cached data.
In your case use the OnCloseFile callback. Close the "backend" file only when the last OnClose callback for a file is called. Implement in the FileInfo context an open counter and increment it in all OnCreate/OnOpen callback calls and decrement in OnClose. In the case the CallAllOpenCloseCallbacks property is set to true the counter isn't necessary.
Posted: 01/17/2013 11:32:28
by Davide Baccaglini (Standard support level)
Joined: 11/19/2012
Posts: 48

Hi Vladimir, I tested the counter in mapper sample project.

It not works like you tell me. The last OnCloseFile is fired when application are still open. In fact when I save a file CBFS perform a series of OnWriteFile and the same number of OnOpenFile/OnCloseFile.

My problem is about word files. I wanto to delay the backend commit at the moment when user close Word, with the aim of not create/delete tmp files.

Can you send me an example of word flush file?

Thanks in advance,
Posted: 01/17/2013 11:40:46
by Eugene Mayevski (Team)

Word writes data to temporary file which is then renamed to permanent name. You can upload the file upon renaming.

Sincerely yours
Eugene Mayevski
Posted: 01/18/2013 02:53:57
by Davide Baccaglini (Standard support level)
Joined: 11/19/2012
Posts: 48

Hello Eugene,
I can't perform updating file during renaming because my need is different.

These are the steps I would do :
- when user open file, and modify it, in OnWriteFile and OnCreateFile I update and create the tmp file only in memory (without commit to server)
- when user close Word (not the single document but the entire application) I want to perform the commit of data of doc/docx file, excluding tmp files (that will be delete from memory)

I would like to do this because my Word file can be large in size so I don't want to commit also tmp file to the server.

Is it possible to do this with CBFS?

Thank you,
Posted: 01/18/2013 04:20:34
by Eugene Mayevski (Team)

If you want to track the operation of Word application being closed, you need to use some Windows API hooks (probably) or just capture the list of running processes using Win32 API, and see when all Word instances are closed. This functionality is not related to CBFS in any way.

However, targeting just one particular application explicitly is not very good idea unless your application is a document management system that works only with MS Office.

Sincerely yours
Eugene Mayevski
Posted: 01/18/2013 06:05:07
by Davide Baccaglini (Standard support level)
Joined: 11/19/2012
Posts: 48

MS Office was only an example, the procedure can be associated with any application that use tmp file.

However, I don't think it's it is necessary to use Win32 API.
I can use CBFS properties :
- GetOriginatorToken
- GetOriginatorProcessId
- GetOriginatorProcessName
save it OnWriteFile and check, in OnCloseFile is file is modified and the originator process is still open.

I thinked to use a procedure like this (delayed commit only for current file and not tmp files) for this reason : in my project a retrieve an error when I try to save a Word file.

Word, for some reasons, after create and write the tmp file (~WRD0000.tmp) it can't rename the current file (MyDocument.docx) to "~WRL0001.tmp". I try to use check it with Process Monitor and I didn't see any error : all is done until the creation of first tmp (~WRD0000.tmp) but after that Word can't rename the file (from MyDocument.docx to ~WRL0001.tmp) and retrive an error about permission on MyDocument.docx.

For these reasons I want to commit the file after closing application.
Posted: 01/18/2013 06:18:24
by Eugene Mayevski (Team)

Indeed you can use capture GetOriginatorProcessID and check if it's still alive. I didn't think about this approach (probably because I don't understand the problem the way you do).

As for renaming - please check mapper sample. All problems with Office and in particular with renaming of files are usually caused by not exactly correct handling of certain events. In particular, you can set CallAllOpenCloseCallbacks to true in order to open the file in correct share mode and more than once when needed. But there can be other quirks as well.

Sincerely yours
Eugene Mayevski



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