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 #include "bin/directory.h" | 5 #include "bin/directory.h" |
6 | 6 |
7 #include "include/dart_api.h" | |
7 #include "bin/dartutils.h" | 8 #include "bin/dartutils.h" |
8 #include "include/dart_api.h" | |
9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
10 | 10 |
11 static intptr_t GetHandlerPort(Dart_Handle handle) { | |
12 if (Dart_IsNull(handle)) { | |
13 return 0; | |
14 } | |
15 return DartUtils::GetIntegerInstanceField(handle, DartUtils::kIdFieldName); | |
16 } | |
17 | |
18 | |
19 void FUNCTION_NAME(Directory_List)(Dart_NativeArguments args) { | |
20 Dart_EnterScope(); | |
21 Dart_Handle path = Dart_GetNativeArgument(args, 1); | |
22 Dart_Handle recursive = Dart_GetNativeArgument(args, 2); | |
23 Dart_Port dir_port = GetHandlerPort(Dart_GetNativeArgument(args, 3)); | |
24 Dart_Port file_port = GetHandlerPort(Dart_GetNativeArgument(args, 4)); | |
25 Dart_Port done_port = GetHandlerPort(Dart_GetNativeArgument(args, 5)); | |
26 Dart_Port error_port = | |
27 GetHandlerPort(Dart_GetNativeArgument(args, 6)); | |
28 if (!Dart_IsString(path) || !Dart_IsBoolean(recursive)) { | |
29 Dart_SetReturnValue(args, Dart_NewBoolean(false)); | |
30 } else { | |
31 Directory::List(DartUtils::GetStringValue(path), | |
32 DartUtils::GetBooleanValue(recursive), | |
33 dir_port, | |
34 file_port, | |
35 done_port, | |
36 error_port); | |
37 Dart_SetReturnValue(args, Dart_NewBoolean(true)); | |
38 } | |
39 Dart_ExitScope(); | |
40 } | |
41 | |
42 | |
43 void FUNCTION_NAME(Directory_Exists)(Dart_NativeArguments args) { | 11 void FUNCTION_NAME(Directory_Exists)(Dart_NativeArguments args) { |
44 static const int kError = -1; | 12 static const int kError = -1; |
45 static const int kExists = 1; | 13 static const int kExists = 1; |
46 static const int kDoesNotExist = 0; | 14 static const int kDoesNotExist = 0; |
47 Dart_EnterScope(); | 15 Dart_EnterScope(); |
48 Dart_Handle path = Dart_GetNativeArgument(args, 0); | 16 Dart_Handle path = Dart_GetNativeArgument(args, 0); |
49 if (Dart_IsString(path)) { | 17 if (Dart_IsString(path)) { |
50 Directory::ExistsResult result = | 18 Directory::ExistsResult result = |
51 Directory::Exists(DartUtils::GetStringValue(path)); | 19 Directory::Exists(DartUtils::GetStringValue(path)); |
52 int return_value = kError; | 20 int return_value = kError; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 Dart_Handle recursive = Dart_GetNativeArgument(args, 1); | 92 Dart_Handle recursive = Dart_GetNativeArgument(args, 1); |
125 if (Dart_IsString(path) && Dart_IsBoolean(recursive)) { | 93 if (Dart_IsString(path) && Dart_IsBoolean(recursive)) { |
126 bool deleted = Directory::Delete(DartUtils::GetStringValue(path), | 94 bool deleted = Directory::Delete(DartUtils::GetStringValue(path), |
127 DartUtils::GetBooleanValue(recursive)); | 95 DartUtils::GetBooleanValue(recursive)); |
128 Dart_SetReturnValue(args, Dart_NewBoolean(deleted)); | 96 Dart_SetReturnValue(args, Dart_NewBoolean(deleted)); |
129 } else { | 97 } else { |
130 Dart_SetReturnValue(args, Dart_NewBoolean(false)); | 98 Dart_SetReturnValue(args, Dart_NewBoolean(false)); |
131 } | 99 } |
132 Dart_ExitScope(); | 100 Dart_ExitScope(); |
133 } | 101 } |
102 | |
103 | |
104 static CObject* DirectoryCreateRequest(const CObjectArray& request) { | |
105 if (request.Length() == 2 && request[1]->IsString()) { | |
106 CObjectString path(request[1]); | |
107 bool created = Directory::Create(path.CString()); | |
108 return CObject::Bool(created); | |
109 } | |
110 return CObject::False(); | |
111 } | |
112 | |
113 | |
114 static CObject* DirectoryDeleteRequest(const CObjectArray& request) { | |
115 if (request.Length() == 3 && request[1]->IsString() && request[2]->IsBool()) { | |
116 CObjectString path(request[1]); | |
117 CObjectBool recursive(request[2]); | |
118 bool deleted = Directory::Delete(path.CString(), recursive.Value()); | |
119 return CObject::Bool(deleted); | |
120 } | |
121 return CObject::False(); | |
122 } | |
123 | |
124 | |
125 static CObject* DirectoryExistsRequest(const CObjectArray& request) { | |
126 static const int kError = -1; | |
127 static const int kExists = 1; | |
128 static const int kDoesNotExist = 0; | |
129 if (request.Length() == 2 && request[1]->IsString()) { | |
130 CObjectString path(request[1]); | |
131 Directory::ExistsResult result = Directory::Exists(path.CString()); | |
132 int return_value = kError; | |
133 if (result == Directory::EXISTS) { | |
134 return_value = kExists; | |
135 } | |
136 if (result == Directory::DOES_NOT_EXIST) { | |
137 return_value = kDoesNotExist; | |
138 } | |
139 return new CObjectInt32(CObject::NewInt32(return_value)); | |
140 } | |
141 return new CObjectInt32(CObject::NewInt32(kDoesNotExist)); | |
142 } | |
143 | |
144 | |
145 static CObject* DirectoryCreateTempRequest(const CObjectArray& request) { | |
146 if (request.Length() == 2 && request[1]->IsString()) { | |
147 CObjectString path(request[1]); | |
148 | |
149 static const int kMaxChildOsErrorMessageLength = 256; | |
150 char os_error_message[kMaxChildOsErrorMessageLength]; | |
151 char* result = NULL; | |
152 int error_code = Directory::CreateTemp(path.CString(), | |
153 &result, | |
154 os_error_message, | |
155 kMaxChildOsErrorMessageLength); | |
156 if (error_code == 0) { | |
157 CObject* temp_dir = new CObjectString(CObject::NewString(result)); | |
158 free(result); | |
159 return temp_dir; | |
160 } else { | |
161 ASSERT(result == NULL); | |
162 CObjectArray* error_response = new CObjectArray(CObject::NewArray(2)); | |
163 if (error_code == -1) { | |
164 error_response->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); | |
165 error_response->SetAt( | |
166 1, new CObjectString(CObject::NewString("Invalid arguments"))); | |
167 } else { | |
168 error_response->SetAt( | |
169 0, new CObjectInt32(CObject::NewInt32(error_code))); | |
170 error_response->SetAt( | |
171 1, new CObjectString(CObject::NewString(os_error_message))); | |
172 } | |
173 return error_response; | |
174 } | |
175 } | |
176 return CObject::False(); | |
177 } | |
178 | |
179 | |
180 static CObject* DirectoryListRequest(const CObjectArray& request, | |
181 Dart_Port response_port) { | |
182 if (request.Length() == 3 && request[1]->IsString() && request[2]->IsBool()) { | |
183 DirectoryListing* dir_listing = new DirectoryListing(response_port); | |
184 CObjectString path(request[1]); | |
185 CObjectBool recursive(request[2]); | |
186 bool completed = Directory::List( | |
187 path.CString(), recursive.Value(), dir_listing); | |
188 delete dir_listing; | |
189 CObjectArray* response = new CObjectArray(CObject::NewArray(2)); | |
190 response->SetAt( | |
191 0, new CObjectInt32(CObject::NewInt32(DirectoryListing::kListDone))); | |
192 response->SetAt(1, CObject::Bool(completed)); | |
193 return response; | |
194 } | |
195 return CObject::False(); | |
196 } | |
197 | |
198 | |
199 void DirectoryService(Dart_Port dest_port_id, | |
200 Dart_Port reply_port_id, | |
201 Dart_CObject* message) { | |
202 CObject* response = CObject::False(); | |
203 CObjectArray request(message); | |
204 if (message->type == Dart_CObject::kArray) { | |
205 if (request.Length() > 1 && request[0]->IsInt32()) { | |
206 CObjectInt32 requestType(request[0]); | |
207 switch (requestType.Value()) { | |
208 case Directory::kCreateRequest: | |
209 response = DirectoryCreateRequest(request); | |
210 break; | |
211 case Directory::kDeleteRequest: | |
212 response = DirectoryDeleteRequest(request); | |
213 break; | |
214 case Directory::kExistsRequest: | |
215 response = DirectoryExistsRequest(request); | |
216 break; | |
217 case Directory::kCreateTempRequest: | |
218 response = DirectoryCreateTempRequest(request); | |
219 break; | |
220 case Directory::kListRequest: | |
221 response = DirectoryListRequest(request, reply_port_id); | |
222 break; | |
223 default: | |
224 UNREACHABLE(); | |
225 } | |
226 } | |
227 } | |
228 | |
229 Dart_PostCObject(reply_port_id, response->AsApiCObject()); | |
230 } | |
231 | |
232 | |
233 void FUNCTION_NAME(Directory_NewServicePort)(Dart_NativeArguments args) { | |
234 Dart_EnterScope(); | |
235 Dart_SetReturnValue(args, Dart_Null()); | |
236 Dart_Port service_port = kIllegalPort; | |
237 service_port = Dart_NewNativePort("DirectoryService", | |
Mads Ager (google)
2012/02/20 12:56:38
Are there two spaces after the '=' here?
Søren Gjesse
2012/02/21 11:23:24
Yes, removed one.
| |
238 DirectoryService, | |
239 true); | |
240 if (service_port != kIllegalPort) { | |
241 // Return a send port for the service port. | |
242 Dart_Handle send_port = Dart_NewSendPort(service_port); | |
243 Dart_SetReturnValue(args, send_port); | |
244 } | |
245 Dart_ExitScope(); | |
246 } | |
247 | |
248 | |
249 CObjectArray* DirectoryListing::NewResponse(Response type, char* arg) { | |
250 CObjectArray* response = new CObjectArray(CObject::NewArray(2)); | |
251 response->SetAt(0, new CObjectInt32(CObject::NewInt32(type))); | |
252 response->SetAt(1, new CObjectString(CObject::NewString(arg))); | |
253 return response; | |
254 } | |
255 | |
256 | |
257 bool DirectoryListing::HandleDirectory(char* dir_name) { | |
258 // TODO(sgjesse): Pass flags to indicate whether directory | |
259 // responses are needed. | |
260 CObjectArray* response = NewResponse(kListDirectory, dir_name); | |
261 return Dart_PostCObject(response_port_, response->AsApiCObject()); | |
262 } | |
263 | |
264 | |
265 bool DirectoryListing::HandleFile(char* file_name) { | |
266 // TODO(sgjesse): Pass flags to indicate whether file | |
267 // responses are needed. | |
268 CObjectArray* response = NewResponse(kListFile, file_name); | |
269 return Dart_PostCObject(response_port_, response->AsApiCObject()); | |
270 } | |
271 | |
272 | |
273 bool DirectoryListing::HandleError(char* message) { | |
274 // TODO(sgjesse): Pass flags to indicate whether error | |
275 // responses are needed. | |
276 CObjectArray* response = NewResponse(kListError, message); | |
277 return Dart_PostCObject(response_port_, response->AsApiCObject()); | |
278 } | |
OLD | NEW |