| 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<Directory> create() { | 54 Future<Directory> createRecursively() { |
| 55 if (_path is !String) { |
| 56 throw new ArgumentError(); |
| 57 } |
| 58 var path = new Path.fromNative(_path); |
| 59 var current = new Path(path.isAbsolute ? '/' : ''); |
| 60 var future = null; |
| 61 for (var segment in path.segments()) { |
| 62 var next = current.append(segment); |
| 63 if (future == null) { |
| 64 future = new Directory.fromPath(current).create(); |
| 65 } else { |
| 66 future = future.chain((_) => new Directory.fromPath(next).create()); |
| 67 } |
| 68 current = next; |
| 69 } |
| 70 if (future == null) { |
| 71 return new Future.immediate(this); |
| 72 } else { |
| 73 return future.transform((result) => this); |
| 74 } |
| 75 } |
| 76 |
| 77 Future<Directory> create({recursive: false}) { |
| 78 if (recursive) return createRecursively(); |
| 55 _ensureDirectoryService(); | 79 _ensureDirectoryService(); |
| 56 List request = new List(2); | 80 List request = new List(2); |
| 57 request[0] = CREATE_REQUEST; | 81 request[0] = CREATE_REQUEST; |
| 58 request[1] = _path; | 82 request[1] = _path; |
| 59 return _directoryService.call(request).transform((response) { | 83 return _directoryService.call(request).transform((response) { |
| 60 if (_isErrorResponse(response)) { | 84 if (_isErrorResponse(response)) { |
| 61 throw _exceptionOrErrorFromResponse(response, "Creation failed"); | 85 throw _exceptionOrErrorFromResponse(response, "Creation failed"); |
| 62 } | 86 } |
| 63 return this; | 87 return this; |
| 64 }); | 88 }); |
| 65 } | 89 } |
| 66 | 90 |
| 67 void createSync() { | 91 void createRecursivelySync() { |
| 92 var path = new Path.fromNative(_path); |
| 93 var current = new Path(path.isAbsolute ? '/' : ''); |
| 94 for (var segment in path.segments()) { |
| 95 current = current.append(segment); |
| 96 new Directory.fromPath(current).createSync(); |
| 97 } |
| 98 } |
| 99 |
| 100 void createSync({recursive: false}) { |
| 68 if (_path is !String) { | 101 if (_path is !String) { |
| 69 throw new ArgumentError(); | 102 throw new ArgumentError(); |
| 70 } | 103 } |
| 104 if (recursive) return createRecursivelySync(); |
| 71 var result = _create(_path); | 105 var result = _create(_path); |
| 72 if (result is OSError) { | 106 if (result is OSError) { |
| 73 throw new DirectoryIOException("Creation failed", _path, result); | 107 throw new DirectoryIOException("Creation failed", _path, result); |
| 74 } | 108 } |
| 75 } | 109 } |
| 76 | 110 |
| 77 Future<Directory> createTemp() { | 111 Future<Directory> createTemp() { |
| 78 _ensureDirectoryService(); | 112 _ensureDirectoryService(); |
| 79 List request = new List(2); | 113 List request = new List(2); |
| 80 request[0] = CREATE_TEMP_REQUEST; | 114 request[0] = CREATE_TEMP_REQUEST; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 95 var result = _createTemp(path); | 129 var result = _createTemp(path); |
| 96 if (result is OSError) { | 130 if (result is OSError) { |
| 97 throw new DirectoryIOException("Creation of temporary directory failed", | 131 throw new DirectoryIOException("Creation of temporary directory failed", |
| 98 _path, | 132 _path, |
| 99 result); | 133 result); |
| 100 } | 134 } |
| 101 return new Directory(result); | 135 return new Directory(result); |
| 102 } | 136 } |
| 103 | 137 |
| 104 Future<Directory> _deleteHelper(bool recursive, String errorMsg) { | 138 Future<Directory> _deleteHelper(bool recursive, String errorMsg) { |
| 139 } |
| 140 |
| 141 Future<Directory> delete({recursive: false}) { |
| 105 _ensureDirectoryService(); | 142 _ensureDirectoryService(); |
| 106 List request = new List(3); | 143 List request = new List(3); |
| 107 request[0] = DELETE_REQUEST; | 144 request[0] = DELETE_REQUEST; |
| 108 request[1] = _path; | 145 request[1] = _path; |
| 109 request[2] = recursive; | 146 request[2] = recursive; |
| 110 return _directoryService.call(request).transform((response) { | 147 return _directoryService.call(request).transform((response) { |
| 111 if (_isErrorResponse(response)) { | 148 if (_isErrorResponse(response)) { |
| 112 throw _exceptionOrErrorFromResponse(response, errorMsg); | 149 throw _exceptionOrErrorFromResponse(response, "Deletion failed"); |
| 113 } | 150 } |
| 114 return this; | 151 return this; |
| 115 }); | 152 }); |
| 116 } | 153 } |
| 117 | 154 |
| 118 Future<Directory> delete() { | 155 void deleteSync({recursive: false}) { |
| 119 return _deleteHelper(false, "Deletion failed"); | |
| 120 } | |
| 121 | |
| 122 void deleteSync() { | |
| 123 if (_path is !String) { | 156 if (_path is !String) { |
| 124 throw new ArgumentError(); | 157 throw new ArgumentError(); |
| 125 } | 158 } |
| 126 var result = _delete(_path, false); | 159 var result = _delete(_path, recursive); |
| 127 if (result is OSError) { | 160 if (result is OSError) { |
| 128 throw new DirectoryIOException("Deletion failed", _path, result); | 161 throw new DirectoryIOException("Deletion failed", _path, result); |
| 129 } | 162 } |
| 130 } | |
| 131 | |
| 132 Future<Directory> deleteRecursively() { | |
| 133 return _deleteHelper(true, "Deletion failed"); | |
| 134 } | |
| 135 | |
| 136 void deleteRecursivelySync() { | |
| 137 if (_path is !String) { | |
| 138 throw new ArgumentError(); | |
| 139 } | |
| 140 var result = _delete(_path, true); | |
| 141 if (result is OSError) { | |
| 142 throw new DirectoryIOException("Deletion failed", _path, result); | |
| 143 } | |
| 144 } | 163 } |
| 145 | 164 |
| 146 Future<Directory> rename(String newPath) { | 165 Future<Directory> rename(String newPath) { |
| 147 _ensureDirectoryService(); | 166 _ensureDirectoryService(); |
| 148 List request = new List(3); | 167 List request = new List(3); |
| 149 request[0] = RENAME_REQUEST; | 168 request[0] = RENAME_REQUEST; |
| 150 request[1] = _path; | 169 request[1] = _path; |
| 151 request[2] = newPath; | 170 request[2] = newPath; |
| 152 return _directoryService.call(request).transform((response) { | 171 return _directoryService.call(request).transform((response) { |
| 153 if (_isErrorResponse(response)) { | 172 if (_isErrorResponse(response)) { |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 } else { | 303 } else { |
| 285 throw e; | 304 throw e; |
| 286 } | 305 } |
| 287 } | 306 } |
| 288 | 307 |
| 289 Function _onDir; | 308 Function _onDir; |
| 290 Function _onFile; | 309 Function _onFile; |
| 291 Function _onDone; | 310 Function _onDone; |
| 292 Function _onError; | 311 Function _onError; |
| 293 } | 312 } |
| OLD | NEW |