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 const CREATE_REQUEST = 0; | 7 static const CREATE_REQUEST = 0; |
8 static const DELETE_REQUEST = 1; | 8 static const DELETE_REQUEST = 1; |
9 static const EXISTS_REQUEST = 2; | 9 static const EXISTS_REQUEST = 2; |
10 static const CREATE_TEMP_REQUEST = 3; | 10 static const CREATE_TEMP_REQUEST = 3; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 if (_path is !String) { | 44 if (_path is !String) { |
45 throw new ArgumentError(); | 45 throw new ArgumentError(); |
46 } | 46 } |
47 var result = _exists(_path); | 47 var result = _exists(_path); |
48 if (result is OSError) { | 48 if (result is OSError) { |
49 throw new DirectoryIOException("Exists failed", _path, result); | 49 throw new DirectoryIOException("Exists failed", _path, result); |
50 } | 50 } |
51 return (result == 1); | 51 return (result == 1); |
52 } | 52 } |
53 | 53 |
| 54 Future<int> _computeExistingIndex(List dirsToCreate) { |
| 55 var future; |
| 56 for (var i = 0; i < dirsToCreate.length; i++) { |
| 57 if (future == null) { |
| 58 future = dirsToCreate[i].exists().transform((e) => e ? i : -1); |
| 59 } else { |
| 60 future = future.chain((index) { |
| 61 if (index != -1) { |
| 62 return new Future.immediate(index); |
| 63 } |
| 64 return dirsToCreate[i].exists().transform((e) => e ? i : -1); |
| 65 }); |
| 66 } |
| 67 } |
| 68 if (future == null) { |
| 69 return new Future.immedidate(-1); |
| 70 } else { |
| 71 return future; |
| 72 } |
| 73 } |
| 74 |
54 Future<Directory> createRecursively() { | 75 Future<Directory> createRecursively() { |
55 if (_path is !String) { | 76 if (_path is !String) { |
56 throw new ArgumentError(); | 77 throw new ArgumentError(); |
57 } | 78 } |
58 var path = new Path.fromNative(_path); | 79 var path = new Path.fromNative(_path); |
59 var current = new Path(path.isAbsolute ? '/' : ''); | 80 var dirsToCreate = []; |
60 var future = null; | 81 var terminator = path.isAbsolute ? '/' : ''; |
61 for (var segment in path.segments()) { | 82 while (path.toString() != terminator) { |
62 var next = current.append(segment); | 83 dirsToCreate.add(new Directory.fromPath(path)); |
| 84 path = path.directoryPath; |
| 85 } |
| 86 return _computeExistingIndex(dirsToCreate).chain((index) { |
| 87 var future; |
| 88 for (var i = index - 1; i >= 0 ; i--) { |
| 89 if (future == null) { |
| 90 future = dirsToCreate[i].create(); |
| 91 } else { |
| 92 future = future.chain((_) { |
| 93 return dirsToCreate[i].create(); |
| 94 }); |
| 95 } |
| 96 } |
63 if (future == null) { | 97 if (future == null) { |
64 future = new Directory.fromPath(current).create(); | 98 return new Future.immediate(this); |
65 } else { | 99 } else { |
66 future = future.chain((_) => new Directory.fromPath(next).create()); | 100 return future.transform((_) => this); |
67 } | 101 } |
68 current = next; | 102 }); |
69 } | |
70 if (future == null) { | |
71 return new Future.immediate(this); | |
72 } else { | |
73 return future.transform((result) => this); | |
74 } | |
75 } | 103 } |
76 | 104 |
77 Future<Directory> create({recursive: false}) { | 105 Future<Directory> create({recursive: false}) { |
78 if (recursive) return createRecursively(); | 106 if (recursive) return createRecursively(); |
79 _ensureDirectoryService(); | 107 _ensureDirectoryService(); |
80 List request = new List(2); | 108 List request = new List(2); |
81 request[0] = CREATE_REQUEST; | 109 request[0] = CREATE_REQUEST; |
82 request[1] = _path; | 110 request[1] = _path; |
83 return _directoryService.call(request).transform((response) { | 111 return _directoryService.call(request).transform((response) { |
84 if (_isErrorResponse(response)) { | 112 if (_isErrorResponse(response)) { |
85 throw _exceptionOrErrorFromResponse(response, "Creation failed"); | 113 throw _exceptionOrErrorFromResponse(response, "Creation failed"); |
86 } | 114 } |
87 return this; | 115 return this; |
88 }); | 116 }); |
89 } | 117 } |
90 | 118 |
91 void createRecursivelySync() { | 119 void createRecursivelySync() { |
92 var path = new Path.fromNative(_path); | 120 var path = new Path.fromNative(_path); |
93 var current = new Path(path.isAbsolute ? '/' : ''); | 121 var dirsToCreate = []; |
94 for (var segment in path.segments()) { | 122 var terminator = path.isAbsolute ? '/' : ''; |
95 current = current.append(segment); | 123 while (path.toString() != terminator) { |
96 new Directory.fromPath(current).createSync(); | 124 var dir = new Directory.fromPath(path); |
| 125 if (dir.existsSync()) break; |
| 126 dirsToCreate.add(dir); |
| 127 path = path.directoryPath; |
| 128 } |
| 129 for (var i = dirsToCreate.length - 1; i >= 0; i--) { |
| 130 dirsToCreate[i].createSync(); |
97 } | 131 } |
98 } | 132 } |
99 | 133 |
100 void createSync({recursive: false}) { | 134 void createSync({recursive: false}) { |
101 if (_path is !String) { | 135 if (_path is !String) { |
102 throw new ArgumentError(); | 136 throw new ArgumentError(); |
103 } | 137 } |
104 if (recursive) return createRecursivelySync(); | 138 if (recursive) return createRecursivelySync(); |
105 var result = _create(_path); | 139 var result = _create(_path); |
106 if (result is OSError) { | 140 if (result is OSError) { |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 } else { | 337 } else { |
304 throw e; | 338 throw e; |
305 } | 339 } |
306 } | 340 } |
307 | 341 |
308 Function _onDir; | 342 Function _onDir; |
309 Function _onFile; | 343 Function _onFile; |
310 Function _onDone; | 344 Function _onDone; |
311 Function _onError; | 345 Function _onError; |
312 } | 346 } |
OLD | NEW |