How to detect if the file is open for reading or writing (cfapi)?
Can I use the CF_CALLBACK_TYPE_NOTIFY_FILE_OPEN_COMPLETION callback to detect if the file is being open for reading or writing?
I would like to lock the file on the server if the file is being opened for writing.
Windows API - Win32
-
Rita Han - MSFT 2,161 Reputation points
2020-10-26T01:30:48.387+00:00 "Callback to inform the sync provider that a placeholder under one of its sync roots has been successfully opened for read/write/delete access." Based on the document, yes, it is what you are looking for. Feel free let me know if it doesn't work for you. And there is an API CfOpenFileWithOplock can combine the
open
andlock
operations in one function call, you can check if it helps.
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
-
WebDAV 106 Reputation points
2020-10-26T18:19:15.847+00:00 So how do I detect if the file is opened for reading, writing, or for deletion? I do not see any info provided in CF_CALLBACK_INFO or in CF_CALLBACK_PARAMETERS. I need to distinguish the file is open for writing from a file open for reading/deletion.
-
Rita Han - MSFT 2,161 Reputation points
2020-10-27T02:36:49.187+00:00 Got what you mean. Yes, there is no direct information indicates a file is opened as read or write. Why do you want to lock a file opened for writing? Could you share your use case?
-
WebDAV 106 Reputation points
2020-10-29T04:11:38.93+00:00 Here is my scenario:
- When a file handle is opened for writing I lock the file automatically in the cloud storage. So the file can not be modified by other users.
- When a file handle is closed I unlock the file in the cloud storage.
-
Rita Han - MSFT 2,161 Reputation points
2020-10-29T06:19:14.383+00:00 Thanks for sharing your scenario. If you want prevent files from modifying make them read-only for other uses.
-
WebDAV 106 Reputation points
2020-10-29T14:03:17.357+00:00 Ok, read-only would work for me. Do I make them read-only when a file is opened for reading in CF_CALLBACK_TYPE_NOTIFY_FILE_OPEN_COMPLETION?
-
Rita Han - MSFT 2,161 Reputation points
2020-10-30T01:18:18.18+00:00 Since it is open completion, so it seems the user has already get the access in hand. So you can set read-only when creating these files or at least before other users open them.
-
Rita Han - MSFT 2,161 Reputation points
2020-11-03T02:34:15.95+00:00 Has this issue been solved? Feel free let me know if you have any concern about it.
-
WebDAV 106 Reputation points
2020-11-04T04:05:16.94+00:00 This issue is not solved. What I need is to know if the file was open for reading or for writing. If the file is opened for writing I will lock it in the cloud storage. How do I detect that the file is opened for writing? Can any Win32 API + file handle help with this?
-
Rita Han - MSFT 2,161 Reputation points
2020-11-04T08:57:06.783+00:00 As we discussed in previous comments, generally, we can set file as read-only for protecting from write access. And generally person A (process A), or one of client side and server side, opens a file exclusively, or open and then lock to prevent others to open it or limit some access rights. However, for your case, the file is opened in client side and will be locked at server side, the server doesn't have that opened handle of the file (directly). And there seems no direct API to check a file is opened as what access right in Win32 APIs. But in driver APIs, there is NtQueryObject (ObjectInformationClass -> ObjectBasicInformation -> GrantedAccess) you can check if it helps.
-
WebDAV 106 Reputation points
2021-11-27T14:19:35.943+00:00 NtQueryObject would not work for me. It requires file handle and there is no file handle in CF_CALLBACK_TYPE_NOTIFY_FILE_OPEN_COMPLETION.
Where do I submit a feature request for Cloud Filter API? I need to know the file-sharing mode and if it was open for reading, writing, or deletion. So I can submit the request into my cloud storage to block the file, so it can not be modified by other users. This will reduce the chance of conflict between client and server.
Sign in to comment