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 class _Directory implements Directory { | 6 class _Directory implements Directory { |
7 static final kCreateRequest = 0; | 7 static final kCreateRequest = 0; |
8 static final kDeleteRequest = 1; | 8 static final kDeleteRequest = 1; |
9 static final kExistsRequest = 2; | 9 static final kExistsRequest = 2; |
10 static final kCreateTempRequest = 3; | 10 static final kCreateTempRequest = 3; |
11 static final kListRequest = 4; | 11 static final kListRequest = 4; |
| 12 static final kRenameRequest = 5; |
12 | 13 |
13 static final kSuccessResponse = 0; | 14 static final kSuccessResponse = 0; |
14 static final kIllegalArgumentResponse = 1; | 15 static final kIllegalArgumentResponse = 1; |
15 static final kOSErrorResponse = 2; | 16 static final kOSErrorResponse = 2; |
16 | 17 |
17 _Directory(String this._path); | 18 _Directory(String this._path); |
18 _Directory.current() : _path = _current(); | 19 _Directory.current() : _path = _current(); |
19 | 20 |
20 static String _current() native "Directory_Current"; | 21 static String _current() native "Directory_Current"; |
21 static _createTemp(String template) native "Directory_CreateTemp"; | 22 static _createTemp(String template) native "Directory_CreateTemp"; |
22 static int _exists(String path) native "Directory_Exists"; | 23 static int _exists(String path) native "Directory_Exists"; |
23 static _create(String path) native "Directory_Create"; | 24 static _create(String path) native "Directory_Create"; |
24 static _delete(String path, bool recursive) native "Directory_Delete"; | 25 static _delete(String path, bool recursive) native "Directory_Delete"; |
| 26 static _rename(String path, String newPath) native "Directory_Rename"; |
25 static SendPort _newServicePort() native "Directory_NewServicePort"; | 27 static SendPort _newServicePort() native "Directory_NewServicePort"; |
26 | 28 |
27 Future<bool> exists() { | 29 Future<bool> exists() { |
28 _ensureDirectoryService(); | 30 _ensureDirectoryService(); |
29 Completer<bool> completer = new Completer<bool>(); | |
30 List request = new List(2); | 31 List request = new List(2); |
31 request[0] = kExistsRequest; | 32 request[0] = kExistsRequest; |
32 request[1] = _path; | 33 request[1] = _path; |
33 _directoryService.call(request).then((response) { | 34 return _directoryService.call(request).transform((response) { |
34 if (_isErrorResponse(response)) { | 35 if (_isErrorResponse(response)) { |
35 var e = _exceptionFromResponse(response, "Exists failed"); | 36 throw _exceptionFromResponse(response, "Exists failed"); |
36 completer.completeException(e); | |
37 } else { | |
38 completer.complete(response == 1); | |
39 } | 37 } |
| 38 return response == 1; |
40 }); | 39 }); |
41 return completer.future; | |
42 } | 40 } |
43 | 41 |
44 bool existsSync() { | 42 bool existsSync() { |
45 if (_path is !String) { | 43 if (_path is !String) { |
46 throw new IllegalArgumentException(); | 44 throw new IllegalArgumentException(); |
47 } | 45 } |
48 var result = _exists(_path); | 46 var result = _exists(_path); |
49 if (result is OSError) { | 47 if (result is OSError) { |
50 throw new DirectoryIOException("Exists failed", _path, result); | 48 throw new DirectoryIOException("Exists failed", _path, result); |
51 } | 49 } |
52 return (result == 1); | 50 return (result == 1); |
53 } | 51 } |
54 | 52 |
55 Future<Directory> create() { | 53 Future<Directory> create() { |
56 _ensureDirectoryService(); | 54 _ensureDirectoryService(); |
57 Completer<Directory> completer = new Completer<Directory>(); | |
58 List request = new List(2); | 55 List request = new List(2); |
59 request[0] = kCreateRequest; | 56 request[0] = kCreateRequest; |
60 request[1] = _path; | 57 request[1] = _path; |
61 _directoryService.call(request).then((response) { | 58 return _directoryService.call(request).transform((response) { |
62 if (_isErrorResponse(response)) { | 59 if (_isErrorResponse(response)) { |
63 var e = _exceptionFromResponse(response, "Creation failed"); | 60 throw _exceptionFromResponse(response, "Creation failed"); |
64 completer.completeException(e); | |
65 } else { | |
66 completer.complete(this); | |
67 } | 61 } |
| 62 return this; |
68 }); | 63 }); |
69 return completer.future; | |
70 } | 64 } |
71 | 65 |
72 void createSync() { | 66 void createSync() { |
73 if (_path is !String) { | 67 if (_path is !String) { |
74 throw new IllegalArgumentException(); | 68 throw new IllegalArgumentException(); |
75 } | 69 } |
76 var result = _create(_path); | 70 var result = _create(_path); |
77 if (result is OSError) { | 71 if (result is OSError) { |
78 throw new DirectoryIOException("Creation failed", _path, result); | 72 throw new DirectoryIOException("Creation failed", _path, result); |
79 } | 73 } |
80 } | 74 } |
81 | 75 |
82 Future<Directory> createTemp() { | 76 Future<Directory> createTemp() { |
83 _ensureDirectoryService(); | 77 _ensureDirectoryService(); |
84 Completer<Directory> completer = new Completer<Directory>(); | |
85 List request = new List(2); | 78 List request = new List(2); |
86 request[0] = kCreateTempRequest; | 79 request[0] = kCreateTempRequest; |
87 request[1] = _path; | 80 request[1] = _path; |
88 _directoryService.call(request).then((response) { | 81 return _directoryService.call(request).transform((response) { |
89 if (_isErrorResponse(response)) { | 82 if (_isErrorResponse(response)) { |
90 var e = | 83 throw _exceptionFromResponse(response, |
91 _exceptionFromResponse(response, | 84 "Creation of temporary directory failed"); |
92 "Creation of temporary directory failed"); | |
93 completer.completeException(e); | |
94 } else { | |
95 completer.complete(new Directory(response)); | |
96 } | 85 } |
| 86 return new Directory(response); |
97 }); | 87 }); |
98 return completer.future; | |
99 } | 88 } |
100 | 89 |
101 void createTempSync() { | 90 void createTempSync() { |
102 if (_path is !String) { | 91 if (_path is !String) { |
103 throw new IllegalArgumentException(); | 92 throw new IllegalArgumentException(); |
104 } | 93 } |
105 var result = _createTemp(path); | 94 var result = _createTemp(path); |
106 if (result is OSError) { | 95 if (result is OSError) { |
107 throw new DirectoryIOException("Creation of temporary directory failed", | 96 throw new DirectoryIOException("Creation of temporary directory failed", |
108 _path, | 97 _path, |
109 result); | 98 result); |
110 } | 99 } |
111 return new Directory(result); | 100 return new Directory(result); |
112 } | 101 } |
113 | 102 |
114 Future<Directory> _deleteHelper(bool recursive, String errorMsg) { | 103 Future<Directory> _deleteHelper(bool recursive, String errorMsg) { |
115 _ensureDirectoryService(); | 104 _ensureDirectoryService(); |
116 Completer<Directory> completer = new Completer<Directory>(); | |
117 List request = new List(3); | 105 List request = new List(3); |
118 request[0] = kDeleteRequest; | 106 request[0] = kDeleteRequest; |
119 request[1] = _path; | 107 request[1] = _path; |
120 request[2] = recursive; | 108 request[2] = recursive; |
121 _directoryService.call(request).then((response) { | 109 return _directoryService.call(request).transform((response) { |
122 if (_isErrorResponse(response)) { | 110 if (_isErrorResponse(response)) { |
123 var e = _exceptionFromResponse(response, errorMsg); | 111 throw _exceptionFromResponse(response, errorMsg); |
124 completer.completeException(e); | |
125 } else { | |
126 completer.complete(this); | |
127 } | 112 } |
| 113 return this; |
128 }); | 114 }); |
129 return completer.future; | 115 return completer.future; |
130 } | 116 } |
131 | 117 |
132 Future<Directory> delete() { | 118 Future<Directory> delete() { |
133 return _deleteHelper(false, "Deletion failed"); | 119 return _deleteHelper(false, "Deletion failed"); |
134 } | 120 } |
135 | 121 |
136 Future<Directory> deleteRecursively() { | |
137 return _deleteHelper(true, "Deletion failed"); | |
138 } | |
139 | |
140 void deleteSync() { | 122 void deleteSync() { |
141 if (_path is !String) { | 123 if (_path is !String) { |
142 throw new IllegalArgumentException(); | 124 throw new IllegalArgumentException(); |
143 } | 125 } |
144 var result = _delete(_path, false); | 126 var result = _delete(_path, false); |
145 if (result is OSError) { | 127 if (result is OSError) { |
146 throw new DirectoryIOException("Deletion failed", _path, result); | 128 throw new DirectoryIOException("Deletion failed", _path, result); |
147 } | 129 } |
148 } | 130 } |
149 | 131 |
| 132 Future<Directory> deleteRecursively() { |
| 133 return _deleteHelper(true, "Deletion failed"); |
| 134 } |
| 135 |
150 void deleteRecursivelySync() { | 136 void deleteRecursivelySync() { |
151 if (_path is !String) { | 137 if (_path is !String) { |
152 throw new IllegalArgumentException(); | 138 throw new IllegalArgumentException(); |
153 } | 139 } |
154 var result = _delete(_path, true); | 140 var result = _delete(_path, true); |
155 if (result is OSError) { | 141 if (result is OSError) { |
156 throw new DirectoryIOException("Deletion failed", _path, result); | 142 throw new DirectoryIOException("Deletion failed", _path, result); |
157 } | 143 } |
158 } | 144 } |
159 | 145 |
| 146 Future<Directory> rename(String newPath) { |
| 147 _ensureDirectoryService(); |
| 148 List request = new List(3); |
| 149 request[0] = kRenameRequest; |
| 150 request[1] = _path; |
| 151 request[2] = newPath; |
| 152 return _directoryService.call(request).transform((response) { |
| 153 if (_isErrorResponse(response)) { |
| 154 throw _exceptionFromResponse(response, "Rename failed"); |
| 155 } |
| 156 return new Directory(newPath); |
| 157 }); |
| 158 } |
| 159 |
| 160 Directory renameSync(String newPath) { |
| 161 if (_path is !String || newPath is !String) { |
| 162 throw new IllegalArgumentException(); |
| 163 } |
| 164 var result = _rename(_path, newPath); |
| 165 if (result is OSError) { |
| 166 throw new DirectoryIOException("Rename failed", _path, result); |
| 167 } |
| 168 return new Directory(newPath); |
| 169 } |
| 170 |
160 DirectoryLister list([bool recursive = false]) { | 171 DirectoryLister list([bool recursive = false]) { |
161 return new _DirectoryLister(_path, recursive); | 172 return new _DirectoryLister(_path, recursive); |
162 } | 173 } |
163 | 174 |
164 String get path() { return _path; } | 175 String get path() { return _path; } |
165 | 176 |
166 bool _isErrorResponse(response) { | 177 bool _isErrorResponse(response) { |
167 return response is List && response[0] != _FileUtils.kSuccessResponse; | 178 return response is List && response[0] != _FileUtils.kSuccessResponse; |
168 } | 179 } |
169 | 180 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 } else { | 281 } else { |
271 throw e; | 282 throw e; |
272 } | 283 } |
273 } | 284 } |
274 | 285 |
275 Function _onDir; | 286 Function _onDir; |
276 Function _onFile; | 287 Function _onFile; |
277 Function _onDone; | 288 Function _onDone; |
278 Function _onError; | 289 Function _onError; |
279 } | 290 } |
OLD | NEW |