| 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 part of dart.io; | 5 part of dart.io; | 
| 6 | 6 | 
| 7 class _Directory implements Directory { | 7 class _Directory implements Directory { | 
| 8   static const CREATE_REQUEST = 0; | 8   static const CREATE_REQUEST = 0; | 
| 9   static const DELETE_REQUEST = 1; | 9   static const DELETE_REQUEST = 1; | 
| 10   static const EXISTS_REQUEST = 2; | 10   static const EXISTS_REQUEST = 2; | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
| 24   external static _createTemp(String template); | 24   external static _createTemp(String template); | 
| 25   external static int _exists(String path); | 25   external static int _exists(String path); | 
| 26   external static _create(String path); | 26   external static _create(String path); | 
| 27   external static _delete(String path, bool recursive); | 27   external static _delete(String path, bool recursive); | 
| 28   external static _rename(String path, String newPath); | 28   external static _rename(String path, String newPath); | 
| 29   external static List _list(String path, bool recursive); | 29   external static List _list(String path, bool recursive); | 
| 30   external static SendPort _newServicePort(); | 30   external static SendPort _newServicePort(); | 
| 31 | 31 | 
| 32   Future<bool> exists() { | 32   Future<bool> exists() { | 
| 33     _ensureDirectoryService(); | 33     _ensureDirectoryService(); | 
| 34     List request = new List(2); | 34     List request = new List.fixedLength(2); | 
| 35     request[0] = EXISTS_REQUEST; | 35     request[0] = EXISTS_REQUEST; | 
| 36     request[1] = _path; | 36     request[1] = _path; | 
| 37     return _directoryService.call(request).transform((response) { | 37     return _directoryService.call(request).then((response) { | 
| 38       if (_isErrorResponse(response)) { | 38       if (_isErrorResponse(response)) { | 
| 39         throw _exceptionOrErrorFromResponse(response, "Exists failed"); | 39         throw _exceptionOrErrorFromResponse(response, "Exists failed"); | 
| 40       } | 40       } | 
| 41       return response == 1; | 41       return response == 1; | 
| 42     }); | 42     }); | 
| 43   } | 43   } | 
| 44 | 44 | 
| 45   bool existsSync() { | 45   bool existsSync() { | 
| 46     if (_path is !String) { | 46     if (_path is !String) { | 
| 47       throw new ArgumentError(); | 47       throw new ArgumentError(); | 
| 48     } | 48     } | 
| 49     var result = _exists(_path); | 49     var result = _exists(_path); | 
| 50     if (result is OSError) { | 50     if (result is OSError) { | 
| 51       throw new DirectoryIOException("Exists failed", _path, result); | 51       throw new DirectoryIOException("Exists failed", _path, result); | 
| 52     } | 52     } | 
| 53     return (result == 1); | 53     return (result == 1); | 
| 54   } | 54   } | 
| 55 | 55 | 
| 56   // Compute the index of the first directory in the list that exists. If | 56   // Compute the index of the first directory in the list that exists. If | 
| 57   // none of the directories exist dirsToCreate.length is returned. | 57   // none of the directories exist dirsToCreate.length is returned. | 
| 58   Future<int> _computeExistingIndex(List dirsToCreate) { | 58   Future<int> _computeExistingIndex(List dirsToCreate) { | 
| 59     var future; | 59     var future; | 
| 60     var notFound = dirsToCreate.length; | 60     var notFound = dirsToCreate.length; | 
| 61     for (var i = 0; i < dirsToCreate.length; i++) { | 61     for (var i = 0; i < dirsToCreate.length; i++) { | 
| 62       if (future == null) { | 62       if (future == null) { | 
| 63         future = dirsToCreate[i].exists().transform((e) => e ? i : notFound); | 63         future = dirsToCreate[i].exists().then((e) => e ? i : notFound); | 
| 64       } else { | 64       } else { | 
| 65         future = future.chain((index) { | 65         future = future.then((index) { | 
| 66           if (index != notFound) { | 66           if (index != notFound) { | 
| 67             return new Future.immediate(index); | 67             return new Future.immediate(index); | 
| 68           } | 68           } | 
| 69           return dirsToCreate[i].exists().transform((e) => e ? i : notFound); | 69           return dirsToCreate[i].exists().then((e) => e ? i : notFound); | 
| 70         }); | 70         }); | 
| 71       } | 71       } | 
| 72     } | 72     } | 
| 73     if (future == null) { | 73     if (future == null) { | 
| 74       return new Future.immediate(notFound); | 74       return new Future.immediate(notFound); | 
| 75     } else { | 75     } else { | 
| 76       return future; | 76       return future; | 
| 77     } | 77     } | 
| 78   } | 78   } | 
| 79 | 79 | 
| 80   Future<Directory> createRecursively() { | 80   Future<Directory> createRecursively() { | 
| 81     if (_path is !String) { | 81     if (_path is !String) { | 
| 82       throw new ArgumentError(); | 82       throw new ArgumentError(); | 
| 83     } | 83     } | 
| 84     var path = new Path.fromNative(_path); | 84     var path = new Path.fromNative(_path); | 
| 85     var dirsToCreate = []; | 85     var dirsToCreate = []; | 
| 86     var terminator = path.isAbsolute ? '/' : ''; | 86     var terminator = path.isAbsolute ? '/' : ''; | 
| 87     while (path.toString() != terminator) { | 87     while (path.toString() != terminator) { | 
| 88       dirsToCreate.add(new Directory.fromPath(path)); | 88       dirsToCreate.add(new Directory.fromPath(path)); | 
| 89       path = path.directoryPath; | 89       path = path.directoryPath; | 
| 90     } | 90     } | 
| 91     return _computeExistingIndex(dirsToCreate).chain((index) { | 91     return _computeExistingIndex(dirsToCreate).then((index) { | 
| 92       var future; | 92       var future; | 
| 93       for (var i = index - 1; i >= 0 ; i--) { | 93       for (var i = index - 1; i >= 0 ; i--) { | 
| 94         if (future == null) { | 94         if (future == null) { | 
| 95           future = dirsToCreate[i].create(); | 95           future = dirsToCreate[i].create(); | 
| 96         } else { | 96         } else { | 
| 97           future = future.chain((_) { | 97           future = future.then((_) { | 
| 98             return dirsToCreate[i].create(); | 98             return dirsToCreate[i].create(); | 
| 99           }); | 99           }); | 
| 100         } | 100         } | 
| 101       } | 101       } | 
| 102       if (future == null) { | 102       if (future == null) { | 
| 103         return new Future.immediate(this); | 103         return new Future.immediate(this); | 
| 104       } else { | 104       } else { | 
| 105         return future.transform((_) => this); | 105         return future.then((_) => this); | 
| 106       } | 106       } | 
| 107     }); | 107     }); | 
| 108   } | 108   } | 
| 109 | 109 | 
| 110   Future<Directory> create({recursive: false}) { | 110   Future<Directory> create({recursive: false}) { | 
| 111     if (recursive) return createRecursively(); | 111     if (recursive) return createRecursively(); | 
| 112     _ensureDirectoryService(); | 112     _ensureDirectoryService(); | 
| 113     List request = new List(2); | 113     List request = new List.fixedLength(2); | 
| 114     request[0] = CREATE_REQUEST; | 114     request[0] = CREATE_REQUEST; | 
| 115     request[1] = _path; | 115     request[1] = _path; | 
| 116     return _directoryService.call(request).transform((response) { | 116     return _directoryService.call(request).then((response) { | 
| 117       if (_isErrorResponse(response)) { | 117       if (_isErrorResponse(response)) { | 
| 118         throw _exceptionOrErrorFromResponse(response, "Creation failed"); | 118         throw _exceptionOrErrorFromResponse(response, "Creation failed"); | 
| 119       } | 119       } | 
| 120       return this; | 120       return this; | 
| 121     }); | 121     }); | 
| 122   } | 122   } | 
| 123 | 123 | 
| 124   void createRecursivelySync() { | 124   void createRecursivelySync() { | 
| 125     var path = new Path.fromNative(_path); | 125     var path = new Path.fromNative(_path); | 
| 126     var dirsToCreate = []; | 126     var dirsToCreate = []; | 
| (...skipping 15 matching lines...) Expand all  Loading... | 
| 142     } | 142     } | 
| 143     if (recursive) return createRecursivelySync(); | 143     if (recursive) return createRecursivelySync(); | 
| 144     var result = _create(_path); | 144     var result = _create(_path); | 
| 145     if (result is OSError) { | 145     if (result is OSError) { | 
| 146       throw new DirectoryIOException("Creation failed", _path, result); | 146       throw new DirectoryIOException("Creation failed", _path, result); | 
| 147     } | 147     } | 
| 148   } | 148   } | 
| 149 | 149 | 
| 150   Future<Directory> createTemp() { | 150   Future<Directory> createTemp() { | 
| 151     _ensureDirectoryService(); | 151     _ensureDirectoryService(); | 
| 152     List request = new List(2); | 152     List request = new List.fixedLength(2); | 
| 153     request[0] = CREATE_TEMP_REQUEST; | 153     request[0] = CREATE_TEMP_REQUEST; | 
| 154     request[1] = _path; | 154     request[1] = _path; | 
| 155     return _directoryService.call(request).transform((response) { | 155     return _directoryService.call(request).then((response) { | 
| 156       if (_isErrorResponse(response)) { | 156       if (_isErrorResponse(response)) { | 
| 157         throw _exceptionOrErrorFromResponse(response, | 157         throw _exceptionOrErrorFromResponse(response, | 
| 158                                       "Creation of temporary directory failed"); | 158                                       "Creation of temporary directory failed"); | 
| 159       } | 159       } | 
| 160       return new Directory(response); | 160       return new Directory(response); | 
| 161     }); | 161     }); | 
| 162   } | 162   } | 
| 163 | 163 | 
| 164   Directory createTempSync() { | 164   Directory createTempSync() { | 
| 165     if (_path is !String) { | 165     if (_path is !String) { | 
| 166       throw new ArgumentError(); | 166       throw new ArgumentError(); | 
| 167     } | 167     } | 
| 168     var result = _createTemp(path); | 168     var result = _createTemp(path); | 
| 169     if (result is OSError) { | 169     if (result is OSError) { | 
| 170       throw new DirectoryIOException("Creation of temporary directory failed", | 170       throw new DirectoryIOException("Creation of temporary directory failed", | 
| 171                                      _path, | 171                                      _path, | 
| 172                                      result); | 172                                      result); | 
| 173     } | 173     } | 
| 174     return new Directory(result); | 174     return new Directory(result); | 
| 175   } | 175   } | 
| 176 | 176 | 
| 177   Future<Directory> _deleteHelper(bool recursive, String errorMsg) { | 177   Future<Directory> _deleteHelper(bool recursive, String errorMsg) { | 
| 178   } | 178   } | 
| 179 | 179 | 
| 180   Future<Directory> delete({recursive: false}) { | 180   Future<Directory> delete({recursive: false}) { | 
| 181     _ensureDirectoryService(); | 181     _ensureDirectoryService(); | 
| 182     List request = new List(3); | 182     List request = new List.fixedLength(3); | 
| 183     request[0] = DELETE_REQUEST; | 183     request[0] = DELETE_REQUEST; | 
| 184     request[1] = _path; | 184     request[1] = _path; | 
| 185     request[2] = recursive; | 185     request[2] = recursive; | 
| 186     return _directoryService.call(request).transform((response) { | 186     return _directoryService.call(request).then((response) { | 
| 187       if (_isErrorResponse(response)) { | 187       if (_isErrorResponse(response)) { | 
| 188         throw _exceptionOrErrorFromResponse(response, "Deletion failed"); | 188         throw _exceptionOrErrorFromResponse(response, "Deletion failed"); | 
| 189       } | 189       } | 
| 190       return this; | 190       return this; | 
| 191     }); | 191     }); | 
| 192   } | 192   } | 
| 193 | 193 | 
| 194   void deleteSync({recursive: false}) { | 194   void deleteSync({recursive: false}) { | 
| 195     if (_path is !String) { | 195     if (_path is !String) { | 
| 196       throw new ArgumentError(); | 196       throw new ArgumentError(); | 
| 197     } | 197     } | 
| 198     var result = _delete(_path, recursive); | 198     var result = _delete(_path, recursive); | 
| 199     if (result is OSError) { | 199     if (result is OSError) { | 
| 200       throw new DirectoryIOException("Deletion failed", _path, result); | 200       throw new DirectoryIOException("Deletion failed", _path, result); | 
| 201     } | 201     } | 
| 202   } | 202   } | 
| 203 | 203 | 
| 204   Future<Directory> rename(String newPath) { | 204   Future<Directory> rename(String newPath) { | 
| 205     _ensureDirectoryService(); | 205     _ensureDirectoryService(); | 
| 206     List request = new List(3); | 206     List request = new List.fixedLength(3); | 
| 207     request[0] = RENAME_REQUEST; | 207     request[0] = RENAME_REQUEST; | 
| 208     request[1] = _path; | 208     request[1] = _path; | 
| 209     request[2] = newPath; | 209     request[2] = newPath; | 
| 210     return _directoryService.call(request).transform((response) { | 210     return _directoryService.call(request).then((response) { | 
| 211       if (_isErrorResponse(response)) { | 211       if (_isErrorResponse(response)) { | 
| 212         throw _exceptionOrErrorFromResponse(response, "Rename failed"); | 212         throw _exceptionOrErrorFromResponse(response, "Rename failed"); | 
| 213       } | 213       } | 
| 214       return new Directory(newPath); | 214       return new Directory(newPath); | 
| 215     }); | 215     }); | 
| 216   } | 216   } | 
| 217 | 217 | 
| 218   Directory renameSync(String newPath) { | 218   Directory renameSync(String newPath) { | 
| 219     if (_path is !String || newPath is !String) { | 219     if (_path is !String || newPath is !String) { | 
| 220       throw new ArgumentError(); | 220       throw new ArgumentError(); | 
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 274     const int LIST_DIRECTORY = 0; | 274     const int LIST_DIRECTORY = 0; | 
| 275     const int LIST_FILE = 1; | 275     const int LIST_FILE = 1; | 
| 276     const int LIST_ERROR = 2; | 276     const int LIST_ERROR = 2; | 
| 277     const int LIST_DONE = 3; | 277     const int LIST_DONE = 3; | 
| 278 | 278 | 
| 279     final int RESPONSE_TYPE = 0; | 279     final int RESPONSE_TYPE = 0; | 
| 280     final int RESPONSE_PATH = 1; | 280     final int RESPONSE_PATH = 1; | 
| 281     final int RESPONSE_COMPLETE = 1; | 281     final int RESPONSE_COMPLETE = 1; | 
| 282     final int RESPONSE_ERROR = 2; | 282     final int RESPONSE_ERROR = 2; | 
| 283 | 283 | 
| 284     List request = new List(3); | 284     List request = new List.fixedLength(3); | 
| 285     request[0] = _Directory.LIST_REQUEST; | 285     request[0] = _Directory.LIST_REQUEST; | 
| 286     request[1] = path; | 286     request[1] = path; | 
| 287     request[2] = recursive; | 287     request[2] = recursive; | 
| 288     ReceivePort responsePort = new ReceivePort(); | 288     ReceivePort responsePort = new ReceivePort(); | 
| 289     // Use a separate directory service port for each listing as | 289     // Use a separate directory service port for each listing as | 
| 290     // listing operations on the same directory can run in parallel. | 290     // listing operations on the same directory can run in parallel. | 
| 291     _Directory._newServicePort().send(request, responsePort.toSendPort()); | 291     _Directory._newServicePort().send(request, responsePort.toSendPort()); | 
| 292     responsePort.receive((message, replyTo) { | 292     responsePort.receive((message, replyTo) { | 
| 293       if (message is !List || message[RESPONSE_TYPE] is !int) { | 293       if (message is !List || message[RESPONSE_TYPE] is !int) { | 
| 294         responsePort.close(); | 294         responsePort.close(); | 
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 351     } else { | 351     } else { | 
| 352       throw e; | 352       throw e; | 
| 353     } | 353     } | 
| 354   } | 354   } | 
| 355 | 355 | 
| 356   Function _onDir; | 356   Function _onDir; | 
| 357   Function _onFile; | 357   Function _onFile; | 
| 358   Function _onDone; | 358   Function _onDone; | 
| 359   Function _onError; | 359   Function _onError; | 
| 360 } | 360 } | 
| OLD | NEW | 
|---|