jborean93 / smbprotocol

Python SMBv2 and v3 Client
MIT License
316 stars 73 forks source link

FileSystemWatcher with multiple requests require multiple threads? #211

Closed Alexinio97 closed 1 year ago

Alexinio97 commented 1 year ago

Hello, I have two questions related to FileSystemWatcher and file locking.

  1. I saw the tests with FileSystemWatcher https://github.com/jborean93/smbprotocol/blob/5c1af5f08181a3130cda99d90e2a4429826554d1/tests/test_change_notify.py#L121 and I was wondering if I want to listen for multiple requests do I need to create a thread for each one? https://github.com/jborean93/smbprotocol/blob/5c1af5f08181a3130cda99d90e2a4429826554d1/tests/test_change_notify.py#L160
  2. The only way to lock a file is to open a read/write handle to it right?

Thank you!

jborean93 commented 1 year ago

and I was wondering if I want to listen for multiple requests do I need to create a thread for each one?

Right now the code for FileSystemWatcher actually creates a thread and runs it for you for every change notify request made https://github.com/jborean93/smbprotocol/blob/master/src/smbprotocol/change_notify.py#L298. It's not very efficient but it's currently how it is implemented. Unfortuantely there is no callback mechanism right now to allow you to run something when an event is fired but there is a very rudimentary event on the object you might be able to take advantage off.

The only way to lock a file is to open a read/write handle to it right?

For Windows pretty much, if you open a file without providing the FileShare access then nothing else should be able to open their own handle to that file. There are also the concept of OpLocks in SMB which I haven't really played with too much but from what I understand they are more a way to be notified if someone else opens a handle to the same file rather than an actual lock.

Alexinio97 commented 1 year ago

Thanks a lot for your answer! Regarding the file system watcher, I experienced some differences between a linux share and a windows one. Some files are not captured by the watcher on linux when I try to copy a batch of them into my windows share. I guess this is because of the windows OS right?

Alexinio97 commented 1 year ago

Regarding my last question, I saw the example you gave here https://github.com/jborean93/smbprotocol/issues/172. The foreach was missing from my implementation, now every changed folder/file gets printed. Thank you! I'll close this.