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. | |
7 class _OSStatus { | |
8 int _errorCode; | |
9 String _errorMessage; | |
10 } | |
11 | |
12 | |
13 class _Directory implements Directory { | 6 class _Directory implements Directory { |
14 static final kCreateRequest = 0; | 7 static final kCreateRequest = 0; |
15 static final kDeleteRequest = 1; | 8 static final kDeleteRequest = 1; |
16 static final kExistsRequest = 2; | 9 static final kExistsRequest = 2; |
17 static final kCreateTempRequest = 3; | 10 static final kCreateTempRequest = 3; |
18 static final kListRequest = 4; | 11 static final kListRequest = 4; |
19 | 12 |
20 _Directory(String this._path); | 13 _Directory(String this._path); |
21 _Directory.current() : _path = _current(); | 14 _Directory.current() : _path = _current(); |
22 | 15 |
23 static String _current() native "Directory_Current"; | 16 static String _current() native "Directory_Current"; |
24 static String _createTemp(String template, | 17 static _createTemp(String template) native "Directory_CreateTemp"; |
25 _OSStatus status) native "Directory_CreateTemp"; | |
26 static int _exists(String path) native "Directory_Exists"; | 18 static int _exists(String path) native "Directory_Exists"; |
27 static bool _create(String path) native "Directory_Create"; | 19 static bool _create(String path) native "Directory_Create"; |
28 static bool _delete(String path, bool recursive) native "Directory_Delete"; | 20 static bool _delete(String path, bool recursive) native "Directory_Delete"; |
29 static SendPort _newServicePort() native "Directory_NewServicePort"; | 21 static SendPort _newServicePort() native "Directory_NewServicePort"; |
30 | 22 |
31 void exists(void callback(bool exists)) { | 23 void exists(void callback(bool exists)) { |
32 _ensureDirectoryService(); | 24 _ensureDirectoryService(); |
33 List request = new List(2); | 25 List request = new List(2); |
34 request[0] = kExistsRequest; | 26 request[0] = kExistsRequest; |
35 request[1] = _path; | 27 request[1] = _path; |
36 _directoryService.call(request).then((result) { | 28 _directoryService.call(request).then((result) { |
37 if (result < 0) { | 29 if (result < 0) { |
38 if (_onError != null) { | 30 if (_onError != null) { |
39 _onError("Diretory exists test failed: $_path"); | 31 _onError("Diretory exists test failed: $_path"); |
40 } | 32 } |
41 } else { | 33 } else { |
42 callback(result == 1); | 34 callback(result == 1); |
43 } | 35 } |
44 }); | 36 }); |
45 } | 37 } |
46 | 38 |
47 bool existsSync() { | 39 bool existsSync() { |
48 int exists = _exists(_path); | 40 int exists = _exists(_path); |
49 if (exists < 0) { | 41 if (exists < 0) { |
50 throw new DirectoryException("Diretory exists test failed: $_path"); | 42 throw new DirectoryIOException("Diretory exists test failed: $_path"); |
51 } | 43 } |
52 return (exists == 1); | 44 return (exists == 1); |
53 } | 45 } |
54 | 46 |
55 void create(void callback()) { | 47 void create(void callback()) { |
56 _ensureDirectoryService(); | 48 _ensureDirectoryService(); |
57 List request = new List(2); | 49 List request = new List(2); |
58 request[0] = kCreateRequest; | 50 request[0] = kCreateRequest; |
59 request[1] = _path; | 51 request[1] = _path; |
60 _directoryService.call(request).then((result) { | 52 _directoryService.call(request).then((result) { |
61 if (result) { | 53 if (result) { |
62 callback(); | 54 callback(); |
63 } else if (_onError != null) { | 55 } else if (_onError != null) { |
64 _onError("Directory creation failed: $_path"); | 56 _onError("Directory creation failed: $_path"); |
65 } | 57 } |
66 }); | 58 }); |
67 } | 59 } |
68 | 60 |
69 void createSync() { | 61 void createSync() { |
70 if (!_create(_path)) { | 62 if (!_create(_path)) { |
71 throw new DirectoryException("Directory creation failed: $_path"); | 63 throw new DirectoryIOException("Directory creation failed: $_path"); |
72 } | 64 } |
73 } | 65 } |
74 | 66 |
75 void createTemp(void callback()) { | 67 void createTemp(void callback()) { |
76 _ensureDirectoryService(); | 68 _ensureDirectoryService(); |
77 List request = new List(2); | 69 List request = new List(2); |
78 request[0] = kCreateTempRequest; | 70 request[0] = kCreateTempRequest; |
79 request[1] = _path; | 71 request[1] = _path; |
80 _directoryService.call(request).then((result) { | 72 _directoryService.call(request).then((result) { |
81 if (result is !List) { | 73 if (result is !List) { |
82 _path = result; | 74 _path = result; |
83 callback(); | 75 callback(); |
84 } else if (_onError != null) { | 76 } else if (_onError != null) { |
85 _onError("Could not create temporary directory [$_path]: " + | 77 _onError("Could not create temporary directory [$_path]: " + |
86 "${result[1]}"); | 78 "${result[1]}"); |
87 } | 79 } |
88 }); | 80 }); |
89 } | 81 } |
90 | 82 |
91 void createTempSync() { | 83 void createTempSync() { |
92 var status = new _OSStatus(); | 84 if (_path is !String) { |
93 var result = _createTemp(path, status); | 85 throw new IllegalArgumentException(); |
94 if (result != null) { | |
95 _path = result; | |
96 } else { | |
97 throw new DirectoryException( | |
98 "Could not create temporary directory [$_path]: " + | |
99 "${status._errorMessage}", | |
100 status._errorCode); | |
101 } | 86 } |
| 87 var result = _createTemp(path); |
| 88 if (result is OSError) { |
| 89 throw new DirectoryIOException("Could not create temporary directory", |
| 90 result); |
| 91 } |
| 92 _path = result; |
102 } | 93 } |
103 | 94 |
104 void _deleteHelper(bool recursive, String errorMsg, void callback()) { | 95 void _deleteHelper(bool recursive, String errorMsg, void callback()) { |
105 _ensureDirectoryService(); | 96 _ensureDirectoryService(); |
106 List request = new List(3); | 97 List request = new List(3); |
107 request[0] = kDeleteRequest; | 98 request[0] = kDeleteRequest; |
108 request[1] = _path; | 99 request[1] = _path; |
109 request[2] = recursive; | 100 request[2] = recursive; |
110 _directoryService.call(request).then((result) { | 101 _directoryService.call(request).then((result) { |
111 if (result) { | 102 if (result) { |
112 callback(); | 103 callback(); |
113 } else if (_onError != null) { | 104 } else if (_onError != null) { |
114 _onError("${errorMsg}: $_path"); | 105 _onError("${errorMsg}: $_path"); |
115 } | 106 } |
116 }); | 107 }); |
117 } | 108 } |
118 | 109 |
119 void delete(void callback()) { | 110 void delete(void callback()) { |
120 _deleteHelper(false, "Directory deletion failed", callback); | 111 _deleteHelper(false, "Directory deletion failed", callback); |
121 } | 112 } |
122 | 113 |
123 void deleteRecursively(void callback()) { | 114 void deleteRecursively(void callback()) { |
124 _deleteHelper(true, "Recursive directory deletion failed", callback); | 115 _deleteHelper(true, "Recursive directory deletion failed", callback); |
125 } | 116 } |
126 | 117 |
127 void deleteSync() { | 118 void deleteSync() { |
128 bool recursive = false; | 119 bool recursive = false; |
129 if (!_delete(_path, recursive)) { | 120 if (!_delete(_path, recursive)) { |
130 throw new DirectoryException("Directory deletion failed: $_path"); | 121 throw new DirectoryIOException("Directory deletion failed: $_path"); |
131 } | 122 } |
132 } | 123 } |
133 | 124 |
134 void deleteRecursivelySync() { | 125 void deleteRecursivelySync() { |
135 bool recursive = true; | 126 bool recursive = true; |
136 if (!_delete(_path, recursive)) { | 127 if (!_delete(_path, recursive)) { |
137 throw new DirectoryException( | 128 throw new DirectoryIOException( |
138 "Recursive directory deletion failed: $_path"); | 129 "Recursive directory deletion failed: $_path"); |
139 } | 130 } |
140 } | 131 } |
141 | 132 |
142 void list([bool recursive = false]) { | 133 void list([bool recursive = false]) { |
143 final int kListDirectory = 0; | 134 final int kListDirectory = 0; |
144 final int kListFile = 1; | 135 final int kListFile = 1; |
145 final int kListError = 2; | 136 final int kListError = 2; |
146 final int kListDone = 3; | 137 final int kListDone = 3; |
147 | 138 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 } | 193 } |
203 | 194 |
204 var _onDir; | 195 var _onDir; |
205 var _onFile; | 196 var _onFile; |
206 var _onDone; | 197 var _onDone; |
207 var _onError; | 198 var _onError; |
208 | 199 |
209 String _path; | 200 String _path; |
210 SendPort _directoryService; | 201 SendPort _directoryService; |
211 } | 202 } |
OLD | NEW |