OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 patch class _File { | 5 patch class _File { |
6 /* patch */ static _exists(String path) native "File_Exists"; | 6 /* patch */ static _exists(String path) native "File_Exists"; |
7 /* patch */ static _create(String path) native "File_Create"; | 7 /* patch */ static _create(String path) native "File_Create"; |
8 /* patch */ static _createLink(String path, String target) | 8 /* patch */ static _createLink(String path, String target) |
9 native "File_CreateLink"; | 9 native "File_CreateLink"; |
10 /* patch */ static _linkTarget(String path) native "File_LinkTarget"; | 10 /* patch */ static _linkTarget(String path) native "File_LinkTarget"; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 try { | 69 try { |
70 socketId = _watchPath(_path, _events, identical(true, _recursive)); | 70 socketId = _watchPath(_path, _events, identical(true, _recursive)); |
71 } catch (e) { | 71 } catch (e) { |
72 throw new FileException( | 72 throw new FileException( |
73 "Failed to watch path", | 73 "Failed to watch path", |
74 _path, | 74 _path, |
75 e); | 75 e); |
76 } | 76 } |
77 var socket = new _RawSocket(new _NativeSocket.watch(socketId)); | 77 var socket = new _RawSocket(new _NativeSocket.watch(socketId)); |
78 _subscription = socket.expand((event) { | 78 _subscription = socket.expand((event) { |
| 79 bool stop = false; |
79 var events = []; | 80 var events = []; |
80 var pair = {}; | 81 var pair = {}; |
81 if (event == RawSocketEvent.READ) { | 82 if (event == RawSocketEvent.READ) { |
82 String getPath(event) { | 83 String getPath(event) { |
83 var path = _path; | 84 var path = _path; |
84 if (event[2] != null) { | 85 if (event[2] != null) { |
85 path += Platform.pathSeparator; | 86 path += Platform.pathSeparator; |
86 path += event[2]; | 87 path += event[2]; |
87 } | 88 } |
88 return path; | 89 return path; |
89 } | 90 } |
| 91 void add(event) { |
| 92 if ((event.type & _events) == 0) return; |
| 93 events.add(event); |
| 94 } |
90 while (socket.available() > 0) { | 95 while (socket.available() > 0) { |
91 for (var event in _readEvents()) { | 96 for (var event in _readEvents()) { |
92 if (event == null) continue; | 97 if (event == null) continue; |
93 var path = getPath(event); | 98 var path = getPath(event); |
94 if ((event[0] & FileSystemEvent.CREATE) != 0) { | 99 if ((event[0] & FileSystemEvent.CREATE) != 0) { |
95 events.add(new FileSystemCreateEvent._(path)); | 100 add(new FileSystemCreateEvent._(path)); |
96 } | 101 } |
97 if ((event[0] & FileSystemEvent.MODIFY) != 0) { | 102 if ((event[0] & FileSystemEvent.MODIFY) != 0) { |
98 events.add(new FileSystemModifyEvent._(path, true)); | 103 add(new FileSystemModifyEvent._(path, true)); |
99 } | 104 } |
100 if ((event[0] & FileSystemEvent._MODIFY_ATTRIBUTES) != 0) { | 105 if ((event[0] & FileSystemEvent._MODIFY_ATTRIBUTES) != 0) { |
101 events.add(new FileSystemModifyEvent._(path, false)); | 106 add(new FileSystemModifyEvent._(path, false)); |
102 } | 107 } |
103 if ((event[0] & FileSystemEvent.MOVE) != 0) { | 108 if ((event[0] & FileSystemEvent.MOVE) != 0) { |
104 int link = event[1]; | 109 int link = event[1]; |
105 if (link > 0) { | 110 if (link > 0) { |
106 if (pair.containsKey(link)) { | 111 if (pair.containsKey(link)) { |
107 events.add( | 112 events.add( |
108 new FileSystemMoveEvent._(getPath(pair[link]), path)); | 113 new FileSystemMoveEvent._(getPath(pair[link]), path)); |
109 pair.remove(link); | 114 pair.remove(link); |
110 } else { | 115 } else { |
111 pair[link] = event; | 116 pair[link] = event; |
112 } | 117 } |
113 } else { | 118 } else { |
114 events.add(new FileSystemMoveEvent._(path, null)); | 119 add(new FileSystemMoveEvent._(path, null)); |
115 } | 120 } |
116 } | 121 } |
117 if ((event[0] & FileSystemEvent.DELETE) != 0) { | 122 if ((event[0] & FileSystemEvent.DELETE) != 0) { |
118 events.add(new FileSystemDeleteEvent._(path)); | 123 add(new FileSystemDeleteEvent._(path)); |
| 124 } |
| 125 if ((event[0] & FileSystemEvent._DELETE_SELF) != 0) { |
| 126 add(new FileSystemDeleteEvent._(path)); |
| 127 stop = true; |
119 } | 128 } |
120 } | 129 } |
121 } | 130 } |
122 for (var event in pair.values) { | 131 for (var event in pair.values) { |
123 events.add(new FileSystemMoveEvent._(getPath(event), null)); | 132 events.add(new FileSystemMoveEvent._(getPath(event), null)); |
124 } | 133 } |
125 } else if (event == RawSocketEvent.CLOSED) { | 134 } else if (event == RawSocketEvent.CLOSED) { |
126 } else if (event == RawSocketEvent.READ_CLOSED) { | 135 } else if (event == RawSocketEvent.READ_CLOSED) { |
127 } else { | 136 } else { |
128 assert(false); | 137 assert(false); |
129 } | 138 } |
| 139 if (stop) socket.close(); |
130 return events; | 140 return events; |
131 }) | 141 }) |
132 .where((event) => (event.type & _events) != 0) | |
133 .listen(_controller.add, onDone: _cancel); | 142 .listen(_controller.add, onDone: _cancel); |
134 } | 143 } |
135 | 144 |
136 void _cancel() { | 145 void _cancel() { |
137 _unwatchPath(); | 146 _unwatchPath(); |
138 if (_subscription != null) { | 147 if (_subscription != null) { |
139 _subscription.cancel(); | 148 _subscription.cancel(); |
140 } | 149 } |
| 150 _controller.close(); |
141 } | 151 } |
142 | 152 |
143 Stream<FileSystemEvent> get stream => _controller.stream; | 153 Stream<FileSystemEvent> get stream => _controller.stream; |
144 | 154 |
145 static bool get isSupported native "FileSystemWatcher_IsSupported"; | 155 static bool get isSupported native "FileSystemWatcher_IsSupported"; |
146 | 156 |
147 int _watchPath(String path, int events, bool recursive) | 157 int _watchPath(String path, int events, bool recursive) |
148 native "FileSystemWatcher_WatchPath"; | 158 native "FileSystemWatcher_WatchPath"; |
149 void _unwatchPath() native "FileSystemWatcher_UnwatchPath"; | 159 void _unwatchPath() native "FileSystemWatcher_UnwatchPath"; |
150 List _readEvents() native "FileSystemWatcher_ReadEvents"; | 160 List _readEvents() native "FileSystemWatcher_ReadEvents"; |
151 } | 161 } |
152 | 162 |
153 Uint8List _makeUint8ListView(Uint8List source, int offsetInBytes, int length) { | 163 Uint8List _makeUint8ListView(Uint8List source, int offsetInBytes, int length) { |
154 return new Uint8List.view(source.buffer, offsetInBytes, length); | 164 return new Uint8List.view(source.buffer, offsetInBytes, length); |
155 } | 165 } |
OLD | NEW |