OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 import 'dart:_mojo_services/mojo/files/types.mojom.dart' as types; | 5 import 'dart:_mojo_services/mojo/files/types.mojom.dart' as types; |
6 | 6 |
7 // | 7 // |
8 // Implementation of Directory, File, and RandomAccessFile for Mojo. | 8 // Implementation of Directory, File, and RandomAccessFile for Mojo. |
9 // | 9 // |
10 | 10 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 flags = types.kOpenFlagWrite | | 83 flags = types.kOpenFlagWrite | |
84 types.kOpenFlagAppend | | 84 types.kOpenFlagAppend | |
85 types.kOpenFlagCreate; | 85 types.kOpenFlagCreate; |
86 break; | 86 break; |
87 default: | 87 default: |
88 throw new UnimplementedError(); | 88 throw new UnimplementedError(); |
89 } | 89 } |
90 return flags; | 90 return flags; |
91 } | 91 } |
92 | 92 |
| 93 |
93 patch class _Directory { | 94 patch class _Directory { |
94 // We start at the root of the file system. | 95 // We start at the root of the file system. |
95 static String _currentDirectoryPath = '/'; | 96 static String _currentDirectoryPath = '/'; |
96 | 97 |
97 /* patch */ Future<Directory> create({bool recursive: false}) async { | 98 /* patch */ Future<Directory> create({bool recursive: false}) async { |
98 if (recursive) { | 99 if (recursive) { |
99 return exists().then((exists) { | 100 return exists().then((exists) { |
100 if (exists) return this; | 101 if (exists) return this; |
101 if (path != parent.path) { | 102 if (path != parent.path) { |
102 return parent.create(recursive: true).then((_) { | 103 return parent.create(recursive: true).then((_) { |
103 return create(); | 104 return create(); |
104 }); | 105 }); |
105 } else { | 106 } else { |
106 return create(); | 107 return create(); |
107 } | 108 } |
108 }); | 109 }); |
109 } | 110 } |
110 DirectoryProxy rootDirectory = await _getRootDirectory(); | 111 _DirectoryProxy rootDirectory = await _getRootDirectory(); |
111 int flags = | 112 int flags = |
112 types.kOpenFlagRead | types.kOpenFlagWrite | types.kOpenFlagCreate; | 113 types.kOpenFlagRead | types.kOpenFlagWrite | types.kOpenFlagCreate; |
113 var response = | 114 var response = |
114 await rootDirectory.responseOrError( | 115 await rootDirectory.responseOrError( |
115 rootDirectory.openDirectory(_ensurePathIsRelative(path), | 116 rootDirectory.openDirectory(_ensurePathIsRelative(path), |
116 null, | 117 null, |
117 flags)); | 118 flags)); |
118 if (response.error != types.Error.ok) { | 119 if (response.error != types.Error.ok) { |
119 throw _OSErrorFromError(response.error); | 120 throw _OSErrorFromError(response.error); |
120 } | 121 } |
121 return this; | 122 return this; |
122 } | 123 } |
123 | 124 |
124 /* patch */ void createSync({bool recursive: false}) => _onSyncOperation(); | 125 /* patch */ void createSync({bool recursive: false}) => _onSyncOperation(); |
125 | 126 |
126 /* patch */ Future<Directory> createTemp([String prefix]) async { | 127 /* patch */ Future<Directory> createTemp([String prefix]) async { |
127 DirectoryProxy rootDirectory = await _getRootDirectory(); | 128 _DirectoryProxy rootDirectory = await _getRootDirectory(); |
128 // Create directory and fail if it already exists. | 129 // Create directory and fail if it already exists. |
129 int flags = types.kOpenFlagRead | types.kOpenFlagWrite | | 130 int flags = types.kOpenFlagRead | types.kOpenFlagWrite | |
130 types.kOpenFlagCreate | types.kOpenFlagExclusive; | 131 types.kOpenFlagCreate | types.kOpenFlagExclusive; |
131 String tempPath = '$path/$prefix'; | 132 String tempPath = '$path/$prefix'; |
132 while (true) { | 133 while (true) { |
133 var response = | 134 var response = |
134 await rootDirectory.responseOrError( | 135 await rootDirectory.responseOrError( |
135 rootDirectory.openDirectory(tempPath, null, flags)); | 136 rootDirectory.openDirectory(tempPath, null, flags)); |
136 if (response.error == types.Error.ok) { | 137 if (response.error == types.Error.ok) { |
137 // Success. | 138 // Success. |
138 break; | 139 break; |
139 } | 140 } |
140 // Alter the path and try again. | 141 // Alter the path and try again. |
141 // TODO(johnmccutchan): Append a randomly generated character. | 142 // TODO(johnmccutchan): Append a randomly generated character. |
142 tempPath = tempPath + 'a'; | 143 tempPath = tempPath + 'a'; |
143 } | 144 } |
144 return new Directory(tempPath); | 145 return new Directory(tempPath); |
145 } | 146 } |
146 | 147 |
147 /* patch */ Directory createTempSync([String prefix]) => _onSyncOperation(); | 148 /* patch */ Directory createTempSync([String prefix]) => _onSyncOperation(); |
148 | 149 |
149 /* patch */ Future<bool> exists() async { | 150 /* patch */ Future<bool> exists() async { |
150 DirectoryProxy rootDirectory = await _getRootDirectory(); | 151 _DirectoryProxy rootDirectory = await _getRootDirectory(); |
151 int flags = types.kOpenFlagRead | types.kOpenFlagWrite; | 152 int flags = types.kOpenFlagRead | types.kOpenFlagWrite; |
152 var response = | 153 var response = |
153 await await rootDirectory.responseOrError( | 154 await await rootDirectory.responseOrError( |
154 rootDirectory.openDirectory(_ensurePathIsRelative(path), | 155 rootDirectory.openDirectory(_ensurePathIsRelative(path), |
155 null, | 156 null, |
156 flags)); | 157 flags)); |
157 // If we can open it, it exists. | 158 // If we can open it, it exists. |
158 return response.error == types.Error.ok; | 159 return response.error == types.Error.ok; |
159 } | 160 } |
160 | 161 |
(...skipping 12 matching lines...) Expand all Loading... |
173 return _onSyncOperation(); | 174 return _onSyncOperation(); |
174 } | 175 } |
175 | 176 |
176 /* patch */ Future<FileStat> stat() { | 177 /* patch */ Future<FileStat> stat() { |
177 return FileStat.stat(path); | 178 return FileStat.stat(path); |
178 } | 179 } |
179 | 180 |
180 /* patch */ FileStat statSync() => _onSyncOperation(); | 181 /* patch */ FileStat statSync() => _onSyncOperation(); |
181 | 182 |
182 /* patch */ Future<Directory> rename(String newPath) async { | 183 /* patch */ Future<Directory> rename(String newPath) async { |
183 DirectoryProxy rootDirectory = await _getRootDirectory(); | 184 _DirectoryProxy rootDirectory = await _getRootDirectory(); |
184 var response = await rootDirectory.responseOrError( | 185 var response = await rootDirectory.responseOrError( |
185 rootDirectory.rename(_ensurePathIsRelative(path), | 186 rootDirectory.rename(_ensurePathIsRelative(path), |
186 _ensurePathIsRelative(newPath))); | 187 _ensurePathIsRelative(newPath))); |
187 if (response.error != types.Error.ok) { | 188 if (response.error != types.Error.ok) { |
188 throw _OSErrorFromError(response.error); | 189 throw _OSErrorFromError(response.error); |
189 } | 190 } |
190 return new Directory(newPath); | 191 return new Directory(newPath); |
191 } | 192 } |
192 | 193 |
193 /* patch */ Directory renameSync(String newPath) => _onSyncOperation(); | 194 /* patch */ Directory renameSync(String newPath) => _onSyncOperation(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 class _DirectoryLister { | 234 class _DirectoryLister { |
234 final String _path; | 235 final String _path; |
235 final bool _recursive; | 236 final bool _recursive; |
236 final List<String> _directoriesToList = new List<String>(); | 237 final List<String> _directoriesToList = new List<String>(); |
237 | 238 |
238 _DirectoryLister(this._path, this._recursive); | 239 _DirectoryLister(this._path, this._recursive); |
239 | 240 |
240 list(StreamController streamController) async { | 241 list(StreamController streamController) async { |
241 _directoriesToList.add(_path); | 242 _directoriesToList.add(_path); |
242 | 243 |
243 DirectoryProxy rootDirectory = await _getRootDirectory(); | 244 _DirectoryProxy rootDirectory = await _getRootDirectory(); |
244 int flags = types.kOpenFlagRead | types.kOpenFlagWrite; | 245 int flags = types.kOpenFlagRead | types.kOpenFlagWrite; |
245 | 246 |
246 while (_directoriesToList.length > 0) { | 247 while (_directoriesToList.length > 0) { |
247 // Remove head. | 248 // Remove head. |
248 String path = _directoriesToList.removeAt(0); | 249 String path = _directoriesToList.removeAt(0); |
249 // Open directory. | 250 // Open directory. |
250 DirectoryProxy directory = new DirectoryProxy.unbound(); | 251 _DirectoryProxy directory = new _DirectoryProxy.unbound(); |
251 var response = | 252 var response = |
252 await rootDirectory.responseOrError( | 253 await rootDirectory.responseOrError( |
253 rootDirectory.openDirectory(_ensurePathIsRelative(path), | 254 rootDirectory.openDirectory(_ensurePathIsRelative(path), |
254 directory, | 255 directory.proxy, |
255 flags)); | 256 flags)); |
256 if (response.error != types.Error.ok) { | 257 if (response.error != types.Error.ok) { |
257 // Skip if we can't open it. | 258 // Skip if we can't open it. |
258 continue; | 259 continue; |
259 } | 260 } |
260 // Read contents. | 261 // Read contents. |
261 var readResponse = await directory.responseOrError(directory.read()); | 262 var readResponse = await directory.responseOrError(directory.read()); |
262 // We are done with the directory now. | 263 // We are done with the directory now. |
263 directory.close(immediate: true); | 264 directory.close(immediate: true); |
264 if (readResponse.error != types.Error.ok) { | 265 if (readResponse.error != types.Error.ok) { |
(...skipping 15 matching lines...) Expand all Loading... |
280 streamController.add(new File(childPath)); | 281 streamController.add(new File(childPath)); |
281 } | 282 } |
282 } | 283 } |
283 } | 284 } |
284 streamController.close(); | 285 streamController.close(); |
285 } | 286 } |
286 } | 287 } |
287 | 288 |
288 patch class _File { | 289 patch class _File { |
289 /* patch */ Future<bool> exists() async { | 290 /* patch */ Future<bool> exists() async { |
290 DirectoryProxy rootDirectory = await _getRootDirectory(); | 291 _DirectoryProxy rootDirectory = await _getRootDirectory(); |
291 int flags = types.kOpenFlagRead; | 292 int flags = types.kOpenFlagRead; |
292 var response = | 293 var response = |
293 await rootDirectory.responseOrError( | 294 await rootDirectory.responseOrError( |
294 rootDirectory.openFile(_ensurePathIsRelative(path), | 295 rootDirectory.openFile(_ensurePathIsRelative(path), |
295 null, | 296 null, |
296 flags)); | 297 flags)); |
297 // If we can open it, it exists. | 298 // If we can open it, it exists. |
298 return response.error == types.Error.ok; | 299 return response.error == types.Error.ok; |
299 } | 300 } |
300 | 301 |
301 /* patch */ bool existsSync() => _onSyncOperation(); | 302 /* patch */ bool existsSync() => _onSyncOperation(); |
302 | 303 |
303 /* patch */ FileStat statSync() => _onSyncOperation(); | 304 /* patch */ FileStat statSync() => _onSyncOperation(); |
304 | 305 |
305 /* patch */ Future<File> create({bool recursive: false}) async { | 306 /* patch */ Future<File> create({bool recursive: false}) async { |
306 if (recursive) { | 307 if (recursive) { |
307 // Create any parent directories. | 308 // Create any parent directories. |
308 await parent.create(recursive: true); | 309 await parent.create(recursive: true); |
309 } | 310 } |
310 DirectoryProxy rootDirectory = await _getRootDirectory(); | 311 _DirectoryProxy rootDirectory = await _getRootDirectory(); |
311 int flags = types.kOpenFlagWrite | types.kOpenFlagCreate; | 312 int flags = types.kOpenFlagWrite | types.kOpenFlagCreate; |
312 var response = | 313 var response = |
313 await rootDirectory.responseOrError( | 314 await rootDirectory.responseOrError( |
314 rootDirectory.openFile(_ensurePathIsRelative(path), | 315 rootDirectory.openFile(_ensurePathIsRelative(path), |
315 null, | 316 null, |
316 flags)); | 317 flags)); |
317 if (response.error != types.Error.ok) { | 318 if (response.error != types.Error.ok) { |
318 throw _OSErrorFromError(response.error); | 319 throw _OSErrorFromError(response.error); |
319 } | 320 } |
320 return this; | 321 return this; |
321 } | 322 } |
322 | 323 |
323 /* patch */ void createSync({bool recursive: false}) => _onSyncOperation(); | 324 /* patch */ void createSync({bool recursive: false}) => _onSyncOperation(); |
324 | 325 |
325 /* patch */ Future<File> rename(String newPath) async { | 326 /* patch */ Future<File> rename(String newPath) async { |
326 DirectoryProxy rootDirectory = await _getRootDirectory(); | 327 _DirectoryProxy rootDirectory = await _getRootDirectory(); |
327 var response = await rootDirectory.responseOrError( | 328 var response = await rootDirectory.responseOrError( |
328 rootDirectory.rename(_ensurePathIsRelative(path), | 329 rootDirectory.rename(_ensurePathIsRelative(path), |
329 _ensurePathIsRelative(newPath))); | 330 _ensurePathIsRelative(newPath))); |
330 if (response.error != types.Error.ok) { | 331 if (response.error != types.Error.ok) { |
331 throw _OSErrorFromError(response.error); | 332 throw _OSErrorFromError(response.error); |
332 } | 333 } |
333 return new File(newPath); | 334 return new File(newPath); |
334 } | 335 } |
335 | 336 |
336 /* patch */ File renameSync(String newPath) => _onSyncOperation(); | 337 /* patch */ File renameSync(String newPath) => _onSyncOperation(); |
337 | 338 |
338 /* patch */ Future<File> copy(String newPath) async { | 339 /* patch */ Future<File> copy(String newPath) async { |
339 File copyFile = new File(newPath); | 340 File copyFile = new File(newPath); |
340 Stream<List<int>> input = openRead(); | 341 Stream<List<int>> input = openRead(); |
341 IOSink output = copyFile.openWrite(); | 342 IOSink output = copyFile.openWrite(); |
342 // Copy contents. | 343 // Copy contents. |
343 await output.addStream(input); | 344 await output.addStream(input); |
344 // Close. | 345 // Close. |
345 await output.close(); | 346 await output.close(); |
346 return copyFile; | 347 return copyFile; |
347 } | 348 } |
348 | 349 |
349 /* patch */ File copySync(String newPath) => _onSyncOperation(); | 350 /* patch */ File copySync(String newPath) => _onSyncOperation(); |
350 | 351 |
351 /* patch */ Future<RandomAccessFile> open( | 352 /* patch */ Future<RandomAccessFile> open( |
352 {FileMode mode: FileMode.READ}) async { | 353 {FileMode mode: FileMode.READ}) async { |
353 DirectoryProxy rootDirectory = await _getRootDirectory(); | 354 _DirectoryProxy rootDirectory = await _getRootDirectory(); |
354 FileProxy file = new FileProxy.unbound(); | 355 _FileProxy file = new _FileProxy.unbound(); |
355 var response = await rootDirectory.responseOrError( | 356 var response = await rootDirectory.responseOrError( |
356 rootDirectory.openFile(_ensurePathIsRelative(path), | 357 rootDirectory.openFile(_ensurePathIsRelative(path), |
357 file, | 358 file.proxy, |
358 _openFlagsFromFileMode(mode))); | 359 _openFlagsFromFileMode(mode))); |
359 if (response.error != types.Error.ok) { | 360 if (response.error != types.Error.ok) { |
360 throw _OSErrorFromError(response.error); | 361 throw _OSErrorFromError(response.error); |
361 } | 362 } |
362 // We use the raw mojo handle as our fd. | 363 // We use the raw mojo handle as our fd. |
363 final int fd = file.ctrl.endpoint.handle.h; | 364 final int fd = file.proxy.ctrl.endpoint.handle.h; |
364 // Construct the RandomAccessFile using the original constructor. | 365 // Construct the RandomAccessFile using the original constructor. |
365 _RandomAccessFile raf = new _RandomAccessFile(fd, path); | 366 _RandomAccessFile raf = new _RandomAccessFile(fd, path); |
366 // Hook up our proxy. | 367 // Hook up our proxy. |
367 raf._proxy = file; | 368 raf._proxy = file; |
368 return raf; | 369 return raf; |
369 } | 370 } |
370 | 371 |
371 /* patch */ Future<int> length() async { | 372 /* patch */ Future<int> length() async { |
372 FileStat fileStat = await FileStat.stat(path); | 373 FileStat fileStat = await FileStat.stat(path); |
373 return fileStat.size; | 374 return fileStat.size; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 throw new UnimplementedError(); | 495 throw new UnimplementedError(); |
495 } | 496 } |
496 } | 497 } |
497 | 498 |
498 patch class FileStat { | 499 patch class FileStat { |
499 /* patch */ static FileStat statSync(String path) { | 500 /* patch */ static FileStat statSync(String path) { |
500 return _onSyncOperation(); | 501 return _onSyncOperation(); |
501 } | 502 } |
502 | 503 |
503 /* patch */ static Future<FileStat> stat(String path) async { | 504 /* patch */ static Future<FileStat> stat(String path) async { |
504 DirectoryProxy rootDirectory = await _getRootDirectory(); | 505 _DirectoryProxy rootDirectory = await _getRootDirectory(); |
505 int flags = types.kOpenFlagRead | types.kOpenFlagWrite; | 506 int flags = types.kOpenFlagRead | types.kOpenFlagWrite; |
506 DirectoryProxy directory = new DirectoryProxy.unbound(); | 507 _DirectoryProxy directory = new _DirectoryProxy.unbound(); |
507 var response = | 508 var response = |
508 await await rootDirectory.responseOrError( | 509 await await rootDirectory.responseOrError( |
509 rootDirectory.openDirectory(_ensurePathIsRelative(path), | 510 rootDirectory.openDirectory(_ensurePathIsRelative(path), |
510 directory, | 511 directory.proxy, |
511 flags)); | 512 flags)); |
512 if (response.error != types.Error.ok) { | 513 if (response.error != types.Error.ok) { |
513 throw _OSErrorFromError(response.error); | 514 throw _OSErrorFromError(response.error); |
514 } | 515 } |
515 var statResponse = await directory.responseOrError(directory.stat()); | 516 var statResponse = await directory.responseOrError(directory.stat()); |
516 // We are done with the directory now. | 517 // We are done with the directory now. |
517 directory.close(immediate: true); | 518 directory.close(immediate: true); |
518 if (statResponse.error != types.Error.ok) { | 519 if (statResponse.error != types.Error.ok) { |
519 throw _OSErrorFromError(response.error); | 520 throw _OSErrorFromError(response.error); |
520 } | 521 } |
(...skipping 22 matching lines...) Expand all Loading... |
543 /* patch */ Future<String> resolveSymbolicLinks() { | 544 /* patch */ Future<String> resolveSymbolicLinks() { |
544 // TODO(johnmccutchan): Canonicalize path before returning. | 545 // TODO(johnmccutchan): Canonicalize path before returning. |
545 return path; | 546 return path; |
546 } | 547 } |
547 | 548 |
548 /* patch */ String resolveSymbolicLinksSync() { | 549 /* patch */ String resolveSymbolicLinksSync() { |
549 return _onSyncOperation(); | 550 return _onSyncOperation(); |
550 } | 551 } |
551 | 552 |
552 /* patch */ Future<FileSystemEntity> delete({bool recursive: false}) async { | 553 /* patch */ Future<FileSystemEntity> delete({bool recursive: false}) async { |
553 DirectoryProxy rootDirectory = await _getRootDirectory(); | 554 _DirectoryProxy rootDirectory = await _getRootDirectory(); |
554 int flags = recursive ? types.kDeleteFlagRecursive : 0; | 555 int flags = recursive ? types.kDeleteFlagRecursive : 0; |
555 var response = await rootDirectory.responseOrError( | 556 var response = await rootDirectory.responseOrError( |
556 rootDirectory.delete(_ensurePathIsRelative(path), flags)); | 557 rootDirectory.delete(_ensurePathIsRelative(path), flags)); |
557 if (response.error != types.Error.ok) { | 558 if (response.error != types.Error.ok) { |
558 throw _OSErrorFromError(response.error); | 559 throw _OSErrorFromError(response.error); |
559 } | 560 } |
560 return this; | 561 return this; |
561 } | 562 } |
562 | 563 |
563 /* patch */ void deleteSync({bool recursive: false}) { | 564 /* patch */ void deleteSync({bool recursive: false}) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 => throw new UnimplementedError(); | 626 => throw new UnimplementedError(); |
626 position() => throw new UnimplementedError(); | 627 position() => throw new UnimplementedError(); |
627 setPosition(int position) => throw new UnimplementedError(); | 628 setPosition(int position) => throw new UnimplementedError(); |
628 truncate(int length) => throw new UnimplementedError(); | 629 truncate(int length) => throw new UnimplementedError(); |
629 length() => throw new UnimplementedError(); | 630 length() => throw new UnimplementedError(); |
630 flush() => throw new UnimplementedError(); | 631 flush() => throw new UnimplementedError(); |
631 lock(int lock, int start, int end) => throw new UnimplementedError(); | 632 lock(int lock, int start, int end) => throw new UnimplementedError(); |
632 } | 633 } |
633 | 634 |
634 patch class _RandomAccessFile { | 635 patch class _RandomAccessFile { |
635 FileProxy _proxy; | 636 _FileProxy _proxy; |
636 | 637 |
637 void _ensureProxy() { | 638 void _ensureProxy() { |
638 if (_proxy == null) { | 639 if (_proxy == null) { |
639 throw new StateError("_RandomAccessFile has a null proxy."); | 640 throw new StateError("_RandomAccessFile has a null proxy."); |
640 } | 641 } |
641 } | 642 } |
642 | 643 |
643 void _handleError(dynamic response) { | 644 void _handleError(dynamic response) { |
644 if (response.error != types.Error.ok) { | 645 if (response.error != types.Error.ok) { |
645 throw _OSErrorFromError(response.error); | 646 throw _OSErrorFromError(response.error); |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
847 | 848 |
848 /* patch */ void lockSync( | 849 /* patch */ void lockSync( |
849 [FileLock mode = FileLock.EXCLUSIVE, int start = 0, int end]) { | 850 [FileLock mode = FileLock.EXCLUSIVE, int start = 0, int end]) { |
850 _onSyncOperation(); | 851 _onSyncOperation(); |
851 } | 852 } |
852 | 853 |
853 /* patch */ void unlockSync([int start = 0, int end]) { | 854 /* patch */ void unlockSync([int start = 0, int end]) { |
854 _onSyncOperation(); | 855 _onSyncOperation(); |
855 } | 856 } |
856 } | 857 } |
OLD | NEW |