Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Side by Side Diff: lib/src/file_watcher/native.dart

Issue 1228703007: Fix a race condition in file watcher. (Closed) Base URL: git@github.com:dart-lang/watcher@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library watcher.file_watcher.native; 5 library watcher.file_watcher.native;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:io'; 8 import 'dart:io';
9 9
10 import '../file_watcher.dart'; 10 import '../file_watcher.dart';
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 if (batch.any((event) => event.type == FileSystemEvent.DELETE)) { 53 if (batch.any((event) => event.type == FileSystemEvent.DELETE)) {
54 // If the file is deleted, the underlying stream will close. We handle 54 // If the file is deleted, the underlying stream will close. We handle
55 // emitting our own REMOVE event in [_onDone]. 55 // emitting our own REMOVE event in [_onDone].
56 return; 56 return;
57 } 57 }
58 58
59 _eventsController.add(new WatchEvent(ChangeType.MODIFY, path)); 59 _eventsController.add(new WatchEvent(ChangeType.MODIFY, path));
60 } 60 }
61 61
62 _onDone() async { 62 _onDone() async {
63 // If the file exists now, it was probably removed and quickly replaced; 63 var fileExists = await new File(path).exists();
64 // this can happen for example when another file is moved on top of it. 64
65 // Re-subscribe and report a modify event. 65 // Check for this after checking whether the file exists because it's
66 if (await new File(path).exists()) { 66 // possible that [close] was called between [File.exists] being called and
67 // it completing.
68 if (_eventsController.isClosed) return;
69
70 if (fileExists) {
71 // If the file exists now, it was probably removed and quickly replaced;
72 // this can happen for example when another file is moved on top of it.
73 // Re-subscribe and report a modify event.
67 _eventsController.add(new WatchEvent(ChangeType.MODIFY, path)); 74 _eventsController.add(new WatchEvent(ChangeType.MODIFY, path));
68 _listen(); 75 _listen();
69 } else { 76 } else {
70 _eventsController.add(new WatchEvent(ChangeType.REMOVE, path)); 77 _eventsController.add(new WatchEvent(ChangeType.REMOVE, path));
71 close(); 78 close();
72 } 79 }
73 } 80 }
74 81
75 void close() { 82 void close() {
76 if (_subscription != null) _subscription.cancel(); 83 if (_subscription != null) _subscription.cancel();
77 _subscription = null; 84 _subscription = null;
78 _eventsController.close(); 85 _eventsController.close();
79 } 86 }
80 } 87 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698