@@ -122,6 +122,37 @@ def remove_ads_path(self, path):
122122 elif os .path .isfile (path ):
123123 self .remove_ads_file (path )
124124
125+ def get_dir_changes (self , path ):
126+ FILE_LIST_DIRECTORY = 1
127+
128+ # Get handle to directory
129+ hDir = win32file .CreateFile (
130+ path ,
131+ FILE_LIST_DIRECTORY ,
132+ win32con .FILE_SHARE_READ | win32con .FILE_SHARE_WRITE | win32con .FILE_SHARE_DELETE ,
133+ None ,
134+ win32con .OPEN_EXISTING ,
135+ win32con .FILE_FLAG_BACKUP_SEMANTICS ,
136+ None )
137+
138+ # Blocking wait for something in the directory to change or be created
139+ changes = None
140+ try :
141+ changes = wrap_wait_call (win32file .ReadDirectoryChangesW ,
142+ hDir ,
143+ 100 * (4 * 3 + 256 * 2 ), # Enough for 100 FILE_NOTIFY_INFORMATION WMAX_PATH structs
144+ False ,
145+ win32con .FILE_NOTIFY_CHANGE_FILE_NAME |
146+ win32con .FILE_NOTIFY_CHANGE_LAST_WRITE ,
147+ None ,
148+ None )
149+ finally :
150+ # Close the handle so we can make changes to the files
151+ # without being notified about it (infinite loop)
152+ hDir .close ()
153+
154+ return changes
155+
125156 def watch_dir (self , path ):
126157 if not os .path .exists (path ):
127158 raise ValueError ("Path does not exist: {}" .format (path ))
@@ -132,35 +163,20 @@ def watch_dir(self, path):
132163 # Run once to start
133164 self .remove_ads_dir (path )
134165
135- FILE_LIST_DIRECTORY = 1
136166 FILE_ACTION_REMOVED = 2
137167 FILE_ACTION_RENAMED_OLD_NAME = 4
138168
139169 while True :
140- # Get handle to directory
141- hDir = win32file .CreateFile (
142- path ,
143- FILE_LIST_DIRECTORY ,
144- win32con .FILE_SHARE_READ | win32con .FILE_SHARE_WRITE | win32con .FILE_SHARE_DELETE ,
145- None ,
146- win32con .OPEN_EXISTING ,
147- win32con .FILE_FLAG_BACKUP_SEMANTICS ,
148- None )
149-
150- # Blocking wait for something in the directory to change or be created
151170 try :
152- changes = wrap_wait_call (win32file .ReadDirectoryChangesW ,
153- hDir ,
154- 100 * (4 * 3 + 256 * 2 ), # Enough for 100 FILE_NOTIFY_INFORMATION WMAX_PATH structs
155- False ,
156- win32con .FILE_NOTIFY_CHANGE_FILE_NAME |
157- win32con .FILE_NOTIFY_CHANGE_LAST_WRITE ,
158- None ,
159- None )
160- finally :
161- # Close the handle so we can make changes to the files
162- # without being notified about it (infinite loop)
163- hDir .close ()
171+ changes = self .get_dir_changes (path )
172+ except :
173+ logger .exception ("Error getting dir changes" )
174+ # We might have caught Windows updating the files
175+ # Wait a bit and try again
176+ time .sleep (5 )
177+ # Check all the files in dir since we weren't watching
178+ self .remove_ads_dir (path )
179+ continue
164180
165181 processed = set ()
166182 for action , fname in changes :
0 commit comments