| 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 _FileUtils { | 5 patch class _FileUtils { |
| 6 /* patch */ static SendPort _newServicePort() native "File_NewServicePort"; | 6 /* patch */ static SendPort _newServicePort() native "File_NewServicePort"; |
| 7 } | 7 } |
| 8 | 8 |
| 9 patch class _File { | 9 patch class _File { |
| 10 /* patch */ static _exists(String path) native "File_Exists"; | 10 /* patch */ static _exists(String path) native "File_Exists"; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 native "File_WriteFrom"; | 36 native "File_WriteFrom"; |
| 37 /* patch */ static _position(int id) native "File_Position"; | 37 /* patch */ static _position(int id) native "File_Position"; |
| 38 /* patch */ static _setPosition(int id, int position) | 38 /* patch */ static _setPosition(int id, int position) |
| 39 native "File_SetPosition"; | 39 native "File_SetPosition"; |
| 40 /* patch */ static _truncate(int id, int length) native "File_Truncate"; | 40 /* patch */ static _truncate(int id, int length) native "File_Truncate"; |
| 41 /* patch */ static _length(int id) native "File_Length"; | 41 /* patch */ static _length(int id) native "File_Length"; |
| 42 /* patch */ static _flush(int id) native "File_Flush"; | 42 /* patch */ static _flush(int id) native "File_Flush"; |
| 43 } | 43 } |
| 44 | 44 |
| 45 patch class _FileSystemWatcher { | 45 patch class _FileSystemWatcher { |
| 46 /* patch */ factory _FileSystemWatcher( |
| 47 String path, int events, bool recursive) |
| 48 => new _FileSystemWatcherImpl(path, events, recursive); |
| 49 } |
| 50 |
| 51 class _FileSystemWatcherImpl |
| 52 extends NativeFieldWrapperClass1 |
| 53 implements _FileSystemWatcher { |
| 54 final String _path; |
| 55 final int _events; |
| 56 final bool _recursive; |
| 57 |
| 58 StreamController _controller; |
| 59 StreamSubscription _subscription; |
| 60 |
| 61 _FileSystemWatcherImpl(this._path, this._events, this._recursive) { |
| 62 _controller = new StreamController(onListen: _listen, onCancel: _cancel); |
| 63 } |
| 64 |
| 65 void _listen() { |
| 66 int socketId; |
| 67 try { |
| 68 socketId = _watchPath(_path, _events, identical(true, _recursive)); |
| 69 } catch (e) { |
| 70 throw new FileException( |
| 71 "Failed to watch path", |
| 72 _path, |
| 73 e); |
| 74 } |
| 75 var socket = new _RawSocket(new _NativeSocket.watch(socketId)); |
| 76 _subscription = socket.expand((event) { |
| 77 var events = []; |
| 78 var pair = {}; |
| 79 if (event == RawSocketEvent.READ) { |
| 80 String getPath(event) { |
| 81 var path = _path; |
| 82 if (event[2] != null) { |
| 83 path += Platform.pathSeparator; |
| 84 path += event[2]; |
| 85 } |
| 86 return path; |
| 87 } |
| 88 while (socket.available() > 0) { |
| 89 for (var event in _readEvents()) { |
| 90 if (event == null) continue; |
| 91 var path = getPath(event); |
| 92 if ((event[0] & FileSystemEvent.CREATE) != 0) { |
| 93 events.add(new FileSystemCreateEvent._(path)); |
| 94 } |
| 95 if ((event[0] & FileSystemEvent.MODIFY) != 0) { |
| 96 events.add(new FileSystemModifyEvent._(path, true)); |
| 97 } |
| 98 if ((event[0] & FileSystemEvent._MODIFY_ATTRIBUTES) != 0) { |
| 99 events.add(new FileSystemModifyEvent._(path, false)); |
| 100 } |
| 101 if ((event[0] & FileSystemEvent.MOVE) != 0) { |
| 102 int link = event[1]; |
| 103 if (link > 0) { |
| 104 if (pair.containsKey(link)) { |
| 105 events.add( |
| 106 new FileSystemMoveEvent._(getPath(pair[link]), path)); |
| 107 pair.remove(link); |
| 108 } else { |
| 109 pair[link] = event; |
| 110 } |
| 111 } else { |
| 112 events.add(new FileSystemMoveEvent._(path, null)); |
| 113 } |
| 114 } |
| 115 if ((event[0] & FileSystemEvent.DELETE) != 0) { |
| 116 events.add(new FileSystemDeleteEvent._(path)); |
| 117 } |
| 118 } |
| 119 } |
| 120 for (var event in pair.values) { |
| 121 events.add(new FileSystemMoveEvent._(getPath(event), null)); |
| 122 } |
| 123 } else if (event == RawSocketEvent.CLOSED) { |
| 124 } else if (event == RawSocketEvent.READ_CLOSED) { |
| 125 } else { |
| 126 assert(false); |
| 127 } |
| 128 return events; |
| 129 }) |
| 130 .where((event) => (event.type & _events) != 0) |
| 131 .listen(_controller.add, onDone: _cancel); |
| 132 } |
| 133 |
| 134 void _cancel() { |
| 135 _unwatchPath(); |
| 136 if (_subscription != null) { |
| 137 _subscription.cancel(); |
| 138 } |
| 139 } |
| 140 |
| 141 Stream<FileSystemEvent> get stream => _controller.stream; |
| 142 |
| 46 int _watchPath(String path, int events, bool recursive) | 143 int _watchPath(String path, int events, bool recursive) |
| 47 native "FileSystemWatcher_WatchPath"; | 144 native "FileSystemWatcher_WatchPath"; |
| 48 void _unwatchPath() native "FileSystemWatcher_UnwatchPath"; | 145 void _unwatchPath() native "FileSystemWatcher_UnwatchPath"; |
| 49 List _readEvents() native "FileSystemWatcher_ReadEvents"; | 146 List _readEvents() native "FileSystemWatcher_ReadEvents"; |
| 50 } | 147 } |
| 51 | 148 |
| 52 Uint8List _makeUint8ListView(Uint8List source, int offsetInBytes, int length) { | 149 Uint8List _makeUint8ListView(Uint8List source, int offsetInBytes, int length) { |
| 53 return new Uint8List.view(source.buffer, offsetInBytes, length); | 150 return new Uint8List.view(source.buffer, offsetInBytes, length); |
| 54 } | 151 } |
| OLD | NEW |