| 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 // Used for holding error code and error message for failed OS system calls. | 6 // Used for holding error code and error message for failed OS system calls. |
| 7 class _OSStatus { | 7 class _OSStatus { |
| 8 int _errorCode; | 8 int _errorCode; |
| 9 String _errorMessage; | 9 String _errorMessage; |
| 10 } | 10 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 static int _exists(String path) native "Directory_Exists"; | 26 static int _exists(String path) native "Directory_Exists"; |
| 27 static bool _create(String path) native "Directory_Create"; | 27 static bool _create(String path) native "Directory_Create"; |
| 28 static bool _delete(String path, bool recursive) native "Directory_Delete"; | 28 static bool _delete(String path, bool recursive) native "Directory_Delete"; |
| 29 static SendPort _newServicePort() native "Directory_NewServicePort"; | 29 static SendPort _newServicePort() native "Directory_NewServicePort"; |
| 30 | 30 |
| 31 void exists(void callback(bool exists)) { | 31 void exists(void callback(bool exists)) { |
| 32 _ensureDirectoryService(); | 32 _ensureDirectoryService(); |
| 33 List request = new List(2); | 33 List request = new List(2); |
| 34 request[0] = kExistsRequest; | 34 request[0] = kExistsRequest; |
| 35 request[1] = _path; | 35 request[1] = _path; |
| 36 _directoryService.call(request).receive((result, replyTo) { | 36 _directoryService.call(request).then((result) { |
| 37 if (result < 0) { | 37 if (result < 0) { |
| 38 if (_onError != null) { | 38 if (_onError != null) { |
| 39 _onError("Diretory exists test failed: $_path"); | 39 _onError("Diretory exists test failed: $_path"); |
| 40 } | 40 } |
| 41 } else { | 41 } else { |
| 42 callback(result == 1); | 42 callback(result == 1); |
| 43 } | 43 } |
| 44 }); | 44 }); |
| 45 } | 45 } |
| 46 | 46 |
| 47 bool existsSync() { | 47 bool existsSync() { |
| 48 int exists = _exists(_path); | 48 int exists = _exists(_path); |
| 49 if (exists < 0) { | 49 if (exists < 0) { |
| 50 throw new DirectoryException("Diretory exists test failed: $_path"); | 50 throw new DirectoryException("Diretory exists test failed: $_path"); |
| 51 } | 51 } |
| 52 return (exists == 1); | 52 return (exists == 1); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void create(void callback()) { | 55 void create(void callback()) { |
| 56 _ensureDirectoryService(); | 56 _ensureDirectoryService(); |
| 57 List request = new List(2); | 57 List request = new List(2); |
| 58 request[0] = kCreateRequest; | 58 request[0] = kCreateRequest; |
| 59 request[1] = _path; | 59 request[1] = _path; |
| 60 _directoryService.call(request).receive((result, replyTo) { | 60 _directoryService.call(request).then((result) { |
| 61 if (result) { | 61 if (result) { |
| 62 callback(); | 62 callback(); |
| 63 } else if (_onError != null) { | 63 } else if (_onError != null) { |
| 64 _onError("Directory creation failed: $_path"); | 64 _onError("Directory creation failed: $_path"); |
| 65 } | 65 } |
| 66 }); | 66 }); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void createSync() { | 69 void createSync() { |
| 70 if (!_create(_path)) { | 70 if (!_create(_path)) { |
| 71 throw new DirectoryException("Directory creation failed: $_path"); | 71 throw new DirectoryException("Directory creation failed: $_path"); |
| 72 } | 72 } |
| 73 } | 73 } |
| 74 | 74 |
| 75 void createTemp(void callback()) { | 75 void createTemp(void callback()) { |
| 76 _ensureDirectoryService(); | 76 _ensureDirectoryService(); |
| 77 List request = new List(2); | 77 List request = new List(2); |
| 78 request[0] = kCreateTempRequest; | 78 request[0] = kCreateTempRequest; |
| 79 request[1] = _path; | 79 request[1] = _path; |
| 80 _directoryService.call(request).receive((result, replyTo) { | 80 _directoryService.call(request).then((result) { |
| 81 if (result is !List) { | 81 if (result is !List) { |
| 82 _path = result; | 82 _path = result; |
| 83 callback(); | 83 callback(); |
| 84 } else if (_onError != null) { | 84 } else if (_onError != null) { |
| 85 _onError("Could not create temporary directory [$_path]: " + | 85 _onError("Could not create temporary directory [$_path]: " + |
| 86 "${result[1]}"); | 86 "${result[1]}"); |
| 87 } | 87 } |
| 88 }); | 88 }); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void createTempSync() { | 91 void createTempSync() { |
| 92 var status = new _OSStatus(); | 92 var status = new _OSStatus(); |
| 93 var result = _createTemp(path, status); | 93 var result = _createTemp(path, status); |
| 94 if (result != null) { | 94 if (result != null) { |
| 95 _path = result; | 95 _path = result; |
| 96 } else { | 96 } else { |
| 97 throw new DirectoryException( | 97 throw new DirectoryException( |
| 98 "Could not create temporary directory [$_path]: " + | 98 "Could not create temporary directory [$_path]: " + |
| 99 "${status._errorMessage}", | 99 "${status._errorMessage}", |
| 100 status._errorCode); | 100 status._errorCode); |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 | 103 |
| 104 void _deleteHelper(bool recursive, String errorMsg, void callback()) { | 104 void _deleteHelper(bool recursive, String errorMsg, void callback()) { |
| 105 _ensureDirectoryService(); | 105 _ensureDirectoryService(); |
| 106 List request = new List(3); | 106 List request = new List(3); |
| 107 request[0] = kDeleteRequest; | 107 request[0] = kDeleteRequest; |
| 108 request[1] = _path; | 108 request[1] = _path; |
| 109 request[2] = recursive; | 109 request[2] = recursive; |
| 110 _directoryService.call(request).receive((result, replyTo) { | 110 _directoryService.call(request).then((result) { |
| 111 if (result) { | 111 if (result) { |
| 112 callback(); | 112 callback(); |
| 113 } else if (_onError != null) { | 113 } else if (_onError != null) { |
| 114 _onError("${errorMsg}: $_path"); | 114 _onError("${errorMsg}: $_path"); |
| 115 } | 115 } |
| 116 }); | 116 }); |
| 117 } | 117 } |
| 118 | 118 |
| 119 void delete(void callback()) { | 119 void delete(void callback()) { |
| 120 _deleteHelper(false, "Directory deletion failed", callback); | 120 _deleteHelper(false, "Directory deletion failed", callback); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 } | 202 } |
| 203 | 203 |
| 204 var _onDir; | 204 var _onDir; |
| 205 var _onFile; | 205 var _onFile; |
| 206 var _onDone; | 206 var _onDone; |
| 207 var _onError; | 207 var _onError; |
| 208 | 208 |
| 209 String _path; | 209 String _path; |
| 210 SendPort _directoryService; | 210 SendPort _directoryService; |
| 211 } | 211 } |
| OLD | NEW |