OLD | NEW |
| (Empty) |
1 // Copyright 2013 Google Inc. All Rights Reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 library quiver.io; | |
16 | |
17 import 'dart:async'; | |
18 import 'dart:convert'; | |
19 import 'dart:io'; | |
20 | |
21 import 'package:quiver/async.dart'; | |
22 | |
23 /** | |
24 * Converts a [Stream] of byte lists to a [String]. | |
25 */ | |
26 Future<String> byteStreamToString(Stream<List<int>> stream, | |
27 {Encoding encoding: UTF8}) { | |
28 return stream.transform(encoding.decoder).join(); | |
29 } | |
30 | |
31 /** | |
32 * Gets the full path of [path] by using [File.fullPathSync]. | |
33 */ | |
34 String getFullPath(path) => new File(path).resolveSymbolicLinksSync(); | |
35 | |
36 /** | |
37 * Lists the sub-directories and files of this Directory, optionally recursing | |
38 * into sub-directories based on the return value of [visit]. | |
39 * | |
40 * [visit] is called with a [File], [Directory] or [Link] to a directory, | |
41 * never a Symlink to a File. If [visit] returns true, then it's argument is | |
42 * listed recursively. | |
43 */ | |
44 Future visitDirectory(Directory dir, Future<bool> visit(FileSystemEntity f)) { | |
45 var futureGroup = new FutureGroup(); | |
46 | |
47 void _list(Directory dir) { | |
48 var completer = new Completer(); | |
49 futureGroup.add(completer.future); | |
50 dir.list(followLinks: false).listen((FileSystemEntity entity) { | |
51 var future = visit(entity); | |
52 if (future != null) { | |
53 futureGroup.add(future.then((bool recurse) { | |
54 // recurse on directories, but not cyclic symlinks | |
55 if (entity is! File && recurse == true) { | |
56 if (entity is Link) { | |
57 if (FileSystemEntity.typeSync(entity.path, followLinks: true) == | |
58 FileSystemEntityType.DIRECTORY) { | |
59 var fullPath = getFullPath(entity.path).toString(); | |
60 var dirFullPath = getFullPath(dir.path).toString(); | |
61 if (!dirFullPath.startsWith(fullPath)) { | |
62 _list(new Directory(entity.path)); | |
63 } | |
64 } | |
65 } else { | |
66 _list(entity); | |
67 } | |
68 } | |
69 })); | |
70 } | |
71 }, onDone: () { | |
72 completer.complete(null); | |
73 }, cancelOnError: true); | |
74 } | |
75 _list(dir); | |
76 | |
77 return futureGroup.future; | |
78 } | |
OLD | NEW |