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 | 5 |
6 // Used for holding error code and error message for failed OS system calls. | 6 // Used for holding error code and error message for failed OS system calls. |
7 class _OSStatus { | 7 class _OSStatus { |
8 int _errorCode; | 8 int _errorCode; |
9 String _errorMessage; | 9 String _errorMessage; |
10 } | 10 } |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 void exists() { | 29 void exists() { |
30 if (_directoryService == null) { | 30 if (_directoryService == null) { |
31 _directoryService = _newServicePort(); | 31 _directoryService = _newServicePort(); |
32 } | 32 } |
33 List request = new List(2); | 33 List request = new List(2); |
34 request[0] = kExistsRequest; | 34 request[0] = kExistsRequest; |
35 request[1] = _path; | 35 request[1] = _path; |
36 _directoryService.call(request).receive((result, replyTo) { | 36 _directoryService.call(request).receive((result, replyTo) { |
37 var handler = | 37 var handler = |
38 (_existsHandler != null) ? _existsHandler : (result) => null; | 38 (_onExists != null) ? _onExists : (result) => null; |
39 if (result < 0) { | 39 if (result < 0) { |
40 if (_errorHandler != null) { | 40 if (_onError != null) { |
41 _errorHandler("Diretory exists test failed: $_path"); | 41 _onError("Diretory exists test failed: $_path"); |
42 } | 42 } |
43 } else { | 43 } else { |
44 handler(result == 1); | 44 handler(result == 1); |
45 } | 45 } |
46 }); | 46 }); |
47 } | 47 } |
48 | 48 |
49 bool existsSync() { | 49 bool existsSync() { |
50 int exists = _exists(_path); | 50 int exists = _exists(_path); |
51 if (exists < 0) { | 51 if (exists < 0) { |
52 throw new DirectoryException("Diretory exists test failed: $_path"); | 52 throw new DirectoryException("Diretory exists test failed: $_path"); |
53 } | 53 } |
54 return (exists == 1); | 54 return (exists == 1); |
55 } | 55 } |
56 | 56 |
57 void create() { | 57 void create() { |
58 if (_directoryService == null) { | 58 if (_directoryService == null) { |
59 _directoryService = _newServicePort(); | 59 _directoryService = _newServicePort(); |
60 } | 60 } |
61 List request = new List(2); | 61 List request = new List(2); |
62 request[0] = kCreateRequest; | 62 request[0] = kCreateRequest; |
63 request[1] = _path; | 63 request[1] = _path; |
64 _directoryService.call(request).receive((result, replyTo) { | 64 _directoryService.call(request).receive((result, replyTo) { |
65 if (result) { | 65 if (result) { |
66 if (_createHandler != null) _createHandler(); | 66 if (_onCreate != null) _onCreate(); |
67 } else if (_errorHandler != null) { | 67 } else if (_onError != null) { |
68 _errorHandler("Directory creation failed: $_path"); | 68 _onError("Directory creation failed: $_path"); |
69 } | 69 } |
70 }); | 70 }); |
71 } | 71 } |
72 | 72 |
73 void createSync() { | 73 void createSync() { |
74 if (!_create(_path)) { | 74 if (!_create(_path)) { |
75 throw new DirectoryException("Directory creation failed: $_path"); | 75 throw new DirectoryException("Directory creation failed: $_path"); |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
79 void createTemp() { | 79 void createTemp() { |
80 if (_directoryService == null) { | 80 if (_directoryService == null) { |
81 _directoryService = _newServicePort(); | 81 _directoryService = _newServicePort(); |
82 } | 82 } |
83 List request = new List(2); | 83 List request = new List(2); |
84 request[0] = kCreateTempRequest; | 84 request[0] = kCreateTempRequest; |
85 request[1] = _path; | 85 request[1] = _path; |
86 _directoryService.call(request).receive((result, replyTo) { | 86 _directoryService.call(request).receive((result, replyTo) { |
87 if (result is !List) { | 87 if (result is !List) { |
88 _path = result; | 88 _path = result; |
89 if (_createTempHandler != null) _createTempHandler(); | 89 if (_onCreateTemp != null) _onCreateTemp(); |
90 } else if (_errorHandler != null) { | 90 } else if (_onError != null) { |
91 _errorHandler("Could not create temporary directory [$_path]: " + | 91 _onError("Could not create temporary directory [$_path]: " + |
92 "${result[1]}"); | 92 "${result[1]}"); |
93 } | 93 } |
94 }); | 94 }); |
95 } | 95 } |
96 | 96 |
97 void createTempSync() { | 97 void createTempSync() { |
98 var status = new _OSStatus(); | 98 var status = new _OSStatus(); |
99 var result = _createTemp(path, status); | 99 var result = _createTemp(path, status); |
100 if (result != null) { | 100 if (result != null) { |
101 _path = result; | 101 _path = result; |
102 } else { | 102 } else { |
103 throw new DirectoryException( | 103 throw new DirectoryException( |
104 "Could not create temporary directory [$_path]: " + | 104 "Could not create temporary directory [$_path]: " + |
105 "${status._errorMessage}", | 105 "${status._errorMessage}", |
106 status._errorCode); | 106 status._errorCode); |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
110 void delete([bool recursive = false]) { | 110 void delete([bool recursive = false]) { |
111 if (_directoryService == null) { | 111 if (_directoryService == null) { |
112 _directoryService = _newServicePort(); | 112 _directoryService = _newServicePort(); |
113 } | 113 } |
114 List request = new List(3); | 114 List request = new List(3); |
115 request[0] = kDeleteRequest; | 115 request[0] = kDeleteRequest; |
116 request[1] = _path; | 116 request[1] = _path; |
117 request[2] = recursive; | 117 request[2] = recursive; |
118 _directoryService.call(request).receive((result, replyTo) { | 118 _directoryService.call(request).receive((result, replyTo) { |
119 if (result) { | 119 if (result) { |
120 if (_deleteHandler != null) _deleteHandler(); | 120 if (_onDelete != null) _onDelete(); |
121 } else if (_errorHandler != null) { | 121 } else if (_onError != null) { |
122 if (recursive) { | 122 if (recursive) { |
123 _errorHandler("Recursive directory deletion failed: $_path"); | 123 _onError("Recursive directory deletion failed: $_path"); |
124 } else { | 124 } else { |
125 _errorHandler("Non-recursive directory deletion failed: $_path"); | 125 _onError("Non-recursive directory deletion failed: $_path"); |
126 } | 126 } |
127 } | 127 } |
128 }); | 128 }); |
129 } | 129 } |
130 | 130 |
131 void deleteSync([bool recursive = false]) { | 131 void deleteSync([bool recursive = false]) { |
132 if (!_delete(_path, recursive)) { | 132 if (!_delete(_path, recursive)) { |
133 throw new DirectoryException("Directory deletion failed: $_path"); | 133 throw new DirectoryException("Directory deletion failed: $_path"); |
134 } | 134 } |
135 } | 135 } |
136 | 136 |
137 void list([bool recursive = false]) { | 137 void list([bool recursive = false]) { |
138 final int kListDirectory = 0; | 138 final int kListDirectory = 0; |
139 final int kListFile = 1; | 139 final int kListFile = 1; |
140 final int kListError = 2; | 140 final int kListError = 2; |
141 final int kListDone = 3; | 141 final int kListDone = 3; |
142 | 142 |
143 List request = new List(3); | 143 List request = new List(3); |
144 request[0] = kListRequest; | 144 request[0] = kListRequest; |
145 request[1] = _path; | 145 request[1] = _path; |
146 request[2] = recursive; | 146 request[2] = recursive; |
147 ReceivePort responsePort = new ReceivePort(); | 147 ReceivePort responsePort = new ReceivePort(); |
148 // Use a separate directory service port for each listing as | 148 // Use a separate directory service port for each listing as |
149 // listing operations on the same directory can run in parallel. | 149 // listing operations on the same directory can run in parallel. |
150 _newServicePort().send(request, responsePort.toSendPort()); | 150 _newServicePort().send(request, responsePort.toSendPort()); |
151 responsePort.receive((message, replyTo) { | 151 responsePort.receive((message, replyTo) { |
152 if (message is !List || message[0] is !int) { | 152 if (message is !List || message[0] is !int) { |
153 responsePort.close(); | 153 responsePort.close(); |
154 if (_errorHandler != null) _errorHandler("Internal error"); | 154 if (_onError != null) _onError("Internal error"); |
155 return; | 155 return; |
156 } | 156 } |
157 switch (message[0]) { | 157 switch (message[0]) { |
158 case kListDirectory: | 158 case kListDirectory: |
159 if (_dirHandler != null) _dirHandler(message[1]); | 159 if (_onDir != null) _onDir(message[1]); |
160 break; | 160 break; |
161 case kListFile: | 161 case kListFile: |
162 if (_fileHandler != null) _fileHandler(message[1]); | 162 if (_onFile != null) _onFile(message[1]); |
163 break; | 163 break; |
164 case kListError: | 164 case kListError: |
165 if (_errorHandler != null) _errorHandler(message[1]); | 165 if (_onError != null) _onError(message[1]); |
166 break; | 166 break; |
167 case kListDone: | 167 case kListDone: |
168 responsePort.close(); | 168 responsePort.close(); |
169 if (_doneHandler != null) _doneHandler(message[1]); | 169 if (_onDone != null) _onDone(message[1]); |
170 break; | 170 break; |
171 } | 171 } |
172 }); | 172 }); |
173 } | 173 } |
174 | 174 |
175 void set dirHandler(void dirHandler(String dir)) { | 175 void set onDir(void onDir(String dir)) { |
176 _dirHandler = dirHandler; | 176 _onDir = onDir; |
177 } | 177 } |
178 | 178 |
179 void set fileHandler(void fileHandler(String file)) { | 179 void set onFile(void onFile(String file)) { |
180 _fileHandler = fileHandler; | 180 _onFile = onFile; |
181 } | 181 } |
182 | 182 |
183 void set doneHandler(void doneHandler(bool completed)) { | 183 void set onDone(void onDone(bool completed)) { |
184 _doneHandler = doneHandler; | 184 _onDone = onDone; |
185 } | 185 } |
186 | 186 |
187 void set existsHandler(void existsHandler(bool exists)) { | 187 void set onExists(void onExists(bool exists)) { |
188 _existsHandler = existsHandler; | 188 _onExists = onExists; |
189 } | 189 } |
190 | 190 |
191 void set createHandler(void createHandler()) { | 191 void set onCreate(void onCreate()) { |
192 _createHandler = createHandler; | 192 _onCreate = onCreate; |
193 } | 193 } |
194 | 194 |
195 void set createTempHandler(void createTempHandler()) { | 195 void set onCreateTemp(void onCreateTemp()) { |
196 _createTempHandler = createTempHandler; | 196 _onCreateTemp = onCreateTemp; |
197 } | 197 } |
198 | 198 |
199 void set deleteHandler(void deleteHandler()) { | 199 void set onDelete(void onDelete()) { |
200 _deleteHandler = deleteHandler; | 200 _onDelete = onDelete; |
201 } | 201 } |
202 | 202 |
203 void set errorHandler(void errorHandler(String error)) { | 203 void set onError(void onError(String error)) { |
204 _errorHandler = errorHandler; | 204 _onError = onError; |
205 } | 205 } |
206 | 206 |
207 void _closePort(ReceivePort port) { | 207 void _closePort(ReceivePort port) { |
208 if (port !== null) { | 208 if (port !== null) { |
209 port.close(); | 209 port.close(); |
210 } | 210 } |
211 } | 211 } |
212 | 212 |
213 String get path() { return _path; } | 213 String get path() { return _path; } |
214 | 214 |
215 var _dirHandler; | 215 var _onDir; |
216 var _fileHandler; | 216 var _onFile; |
217 var _doneHandler; | 217 var _onDone; |
218 var _existsHandler; | 218 var _onExists; |
219 var _createHandler; | 219 var _onCreate; |
220 var _createTempHandler; | 220 var _onCreateTemp; |
221 var _deleteHandler; | 221 var _onDelete; |
222 var _errorHandler; | 222 var _onError; |
223 | 223 |
224 String _path; | 224 String _path; |
225 SendPort _directoryService; | 225 SendPort _directoryService; |
226 } | 226 } |
OLD | NEW |