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 "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
8 #include "bin/log.h" | 8 #include "bin/log.h" |
| 9 #include "bin/namespace.h" |
9 #include "include/dart_api.h" | 10 #include "include/dart_api.h" |
10 #include "platform/assert.h" | 11 #include "platform/assert.h" |
11 | 12 |
12 namespace dart { | 13 namespace dart { |
13 namespace bin { | 14 namespace bin { |
14 | 15 |
15 char* Directory::system_temp_path_override_ = NULL; | 16 char* Directory::system_temp_path_override_ = NULL; |
16 | 17 |
17 void FUNCTION_NAME(Directory_Current)(Dart_NativeArguments args) { | 18 void FUNCTION_NAME(Directory_Current)(Dart_NativeArguments args) { |
18 const char* current = Directory::Current(); | 19 Namespace* namespc = Namespace::GetNamespace(args, 0); |
| 20 const char* current = Directory::Current(namespc); |
19 if (current != NULL) { | 21 if (current != NULL) { |
20 Dart_SetReturnValue(args, DartUtils::NewString(current)); | 22 Dart_SetReturnValue(args, DartUtils::NewString(current)); |
21 } else { | 23 } else { |
22 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 24 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
23 } | 25 } |
24 } | 26 } |
25 | 27 |
26 void FUNCTION_NAME(Directory_SetCurrent)(Dart_NativeArguments args) { | 28 void FUNCTION_NAME(Directory_SetCurrent)(Dart_NativeArguments args) { |
27 int argc = Dart_GetNativeArgumentCount(args); | 29 Namespace* namespc = Namespace::GetNamespace(args, 0); |
28 Dart_Handle path; | 30 Dart_Handle path = Dart_GetNativeArgument(args, 1); |
29 if (argc == 1) { | 31 if (Dart_IsError(path) || !Dart_IsString(path)) { |
30 path = Dart_GetNativeArgument(args, 0); | 32 Dart_SetReturnValue(args, DartUtils::NewDartArgumentError(NULL)); |
| 33 return; |
31 } | 34 } |
32 if ((argc != 1) || !Dart_IsString(path)) { | 35 if (Directory::SetCurrent(namespc, DartUtils::GetStringValue(path))) { |
33 Dart_SetReturnValue(args, DartUtils::NewDartArgumentError(NULL)); | 36 Dart_SetBooleanReturnValue(args, true); |
34 } else { | 37 } else { |
35 if (Directory::SetCurrent(DartUtils::GetStringValue(path))) { | 38 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
36 Dart_SetReturnValue(args, Dart_True()); | |
37 } else { | |
38 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
39 } | |
40 } | 39 } |
41 } | 40 } |
42 | 41 |
43 void FUNCTION_NAME(Directory_Exists)(Dart_NativeArguments args) { | 42 void FUNCTION_NAME(Directory_Exists)(Dart_NativeArguments args) { |
44 static const int kExists = 1; | 43 static const int kExists = 1; |
45 static const int kDoesNotExist = 0; | 44 static const int kDoesNotExist = 0; |
46 Dart_Handle path = Dart_GetNativeArgument(args, 0); | 45 Namespace* namespc = Namespace::GetNamespace(args, 0); |
| 46 Dart_Handle path = Dart_GetNativeArgument(args, 1); |
47 Directory::ExistsResult result = | 47 Directory::ExistsResult result = |
48 Directory::Exists(DartUtils::GetStringValue(path)); | 48 Directory::Exists(namespc, DartUtils::GetStringValue(path)); |
49 if (result == Directory::EXISTS) { | 49 if (result == Directory::EXISTS) { |
50 Dart_SetReturnValue(args, Dart_NewInteger(kExists)); | 50 Dart_SetIntegerReturnValue(args, kExists); |
51 } else if (result == Directory::DOES_NOT_EXIST) { | 51 } else if (result == Directory::DOES_NOT_EXIST) { |
52 Dart_SetReturnValue(args, Dart_NewInteger(kDoesNotExist)); | 52 Dart_SetIntegerReturnValue(args, kDoesNotExist); |
53 } else { | 53 } else { |
54 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 54 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 void FUNCTION_NAME(Directory_Create)(Dart_NativeArguments args) { | 58 void FUNCTION_NAME(Directory_Create)(Dart_NativeArguments args) { |
59 Dart_Handle path = Dart_GetNativeArgument(args, 0); | 59 Namespace* namespc = Namespace::GetNamespace(args, 0); |
60 if (Directory::Create(DartUtils::GetStringValue(path))) { | 60 Dart_Handle path = Dart_GetNativeArgument(args, 1); |
61 Dart_SetReturnValue(args, Dart_True()); | 61 if (Directory::Create(namespc, DartUtils::GetStringValue(path))) { |
| 62 Dart_SetBooleanReturnValue(args, true); |
62 } else { | 63 } else { |
63 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 64 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
64 } | 65 } |
65 } | 66 } |
66 | 67 |
67 void FUNCTION_NAME(Directory_SystemTemp)(Dart_NativeArguments args) { | 68 void FUNCTION_NAME(Directory_SystemTemp)(Dart_NativeArguments args) { |
68 const char* result = Directory::SystemTemp(); | 69 Namespace* namespc = Namespace::GetNamespace(args, 0); |
| 70 const char* result = Directory::SystemTemp(namespc); |
69 Dart_SetReturnValue(args, DartUtils::NewString(result)); | 71 Dart_SetReturnValue(args, DartUtils::NewString(result)); |
70 } | 72 } |
71 | 73 |
72 void FUNCTION_NAME(Directory_CreateTemp)(Dart_NativeArguments args) { | 74 void FUNCTION_NAME(Directory_CreateTemp)(Dart_NativeArguments args) { |
73 Dart_Handle path = Dart_GetNativeArgument(args, 0); | 75 Namespace* namespc = Namespace::GetNamespace(args, 0); |
| 76 Dart_Handle path = Dart_GetNativeArgument(args, 1); |
74 if (!Dart_IsString(path)) { | 77 if (!Dart_IsString(path)) { |
75 Dart_SetReturnValue( | 78 Dart_SetReturnValue( |
76 args, DartUtils::NewDartArgumentError( | 79 args, DartUtils::NewDartArgumentError( |
77 "Prefix argument of CreateSystemTempSync is not a String")); | 80 "Prefix argument of CreateSystemTempSync is not a String")); |
78 return; | 81 return; |
79 } | 82 } |
80 const char* result = Directory::CreateTemp(DartUtils::GetStringValue(path)); | 83 const char* result = |
| 84 Directory::CreateTemp(namespc, DartUtils::GetStringValue(path)); |
81 if (result != NULL) { | 85 if (result != NULL) { |
82 Dart_SetReturnValue(args, DartUtils::NewString(result)); | 86 Dart_SetReturnValue(args, DartUtils::NewString(result)); |
83 } else { | 87 } else { |
84 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 88 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
85 } | 89 } |
86 } | 90 } |
87 | 91 |
88 void FUNCTION_NAME(Directory_Delete)(Dart_NativeArguments args) { | 92 void FUNCTION_NAME(Directory_Delete)(Dart_NativeArguments args) { |
89 Dart_Handle path = Dart_GetNativeArgument(args, 0); | 93 Namespace* namespc = Namespace::GetNamespace(args, 0); |
90 Dart_Handle recursive = Dart_GetNativeArgument(args, 1); | 94 Dart_Handle path = Dart_GetNativeArgument(args, 1); |
91 if (Directory::Delete(DartUtils::GetStringValue(path), | 95 Dart_Handle recursive = Dart_GetNativeArgument(args, 2); |
| 96 if (Directory::Delete(namespc, |
| 97 DartUtils::GetStringValue(path), |
92 DartUtils::GetBooleanValue(recursive))) { | 98 DartUtils::GetBooleanValue(recursive))) { |
93 Dart_SetReturnValue(args, Dart_True()); | 99 Dart_SetBooleanReturnValue(args, true); |
94 } else { | 100 } else { |
95 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 101 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
96 } | 102 } |
97 } | 103 } |
98 | 104 |
99 void FUNCTION_NAME(Directory_Rename)(Dart_NativeArguments args) { | 105 void FUNCTION_NAME(Directory_Rename)(Dart_NativeArguments args) { |
100 Dart_Handle path = Dart_GetNativeArgument(args, 0); | 106 Namespace* namespc = Namespace::GetNamespace(args, 0); |
101 Dart_Handle newPath = Dart_GetNativeArgument(args, 1); | 107 Dart_Handle path = Dart_GetNativeArgument(args, 1); |
102 if (Directory::Rename(DartUtils::GetStringValue(path), | 108 Dart_Handle newPath = Dart_GetNativeArgument(args, 2); |
| 109 if (Directory::Rename(namespc, |
| 110 DartUtils::GetStringValue(path), |
103 DartUtils::GetStringValue(newPath))) { | 111 DartUtils::GetStringValue(newPath))) { |
104 Dart_SetReturnValue(args, Dart_True()); | 112 Dart_SetBooleanReturnValue(args, true); |
105 } else { | 113 } else { |
106 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 114 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
107 } | 115 } |
108 } | 116 } |
109 | 117 |
110 void FUNCTION_NAME(Directory_FillWithDirectoryListing)( | 118 void FUNCTION_NAME(Directory_FillWithDirectoryListing)( |
111 Dart_NativeArguments args) { | 119 Dart_NativeArguments args) { |
| 120 Namespace* namespc = Namespace::GetNamespace(args, 0); |
112 // The list that we should fill. | 121 // The list that we should fill. |
113 Dart_Handle results = Dart_GetNativeArgument(args, 0); | 122 Dart_Handle results = Dart_GetNativeArgument(args, 1); |
114 Dart_Handle path = Dart_GetNativeArgument(args, 1); | 123 Dart_Handle path = Dart_GetNativeArgument(args, 2); |
115 Dart_Handle recursive = Dart_GetNativeArgument(args, 2); | 124 Dart_Handle recursive = Dart_GetNativeArgument(args, 3); |
116 Dart_Handle follow_links = Dart_GetNativeArgument(args, 3); | 125 Dart_Handle follow_links = Dart_GetNativeArgument(args, 4); |
117 | 126 |
118 Dart_Handle dart_error; | 127 Dart_Handle dart_error; |
119 { | 128 { |
120 // Pass the list that should hold the directory listing to the | 129 // Pass the list that should hold the directory listing to the |
121 // SyncDirectoryListing object, which adds elements to it. | 130 // SyncDirectoryListing object, which adds elements to it. |
122 SyncDirectoryListing sync_listing(results, DartUtils::GetStringValue(path), | 131 SyncDirectoryListing sync_listing(results, namespc, |
| 132 DartUtils::GetStringValue(path), |
123 DartUtils::GetBooleanValue(recursive), | 133 DartUtils::GetBooleanValue(recursive), |
124 DartUtils::GetBooleanValue(follow_links)); | 134 DartUtils::GetBooleanValue(follow_links)); |
125 Directory::List(&sync_listing); | 135 Directory::List(&sync_listing); |
126 dart_error = sync_listing.dart_error(); | 136 dart_error = sync_listing.dart_error(); |
127 } | 137 } |
128 if (Dart_IsError(dart_error)) { | 138 if (Dart_IsError(dart_error)) { |
129 Dart_PropagateError(dart_error); | 139 Dart_PropagateError(dart_error); |
130 } else if (!Dart_IsNull(dart_error)) { | 140 } else if (!Dart_IsNull(dart_error)) { |
131 Dart_ThrowException(dart_error); | 141 Dart_ThrowException(dart_error); |
132 } | 142 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 void Directory::SetSystemTemp(const char* path) { | 190 void Directory::SetSystemTemp(const char* path) { |
181 if (system_temp_path_override_ != NULL) { | 191 if (system_temp_path_override_ != NULL) { |
182 free(system_temp_path_override_); | 192 free(system_temp_path_override_); |
183 system_temp_path_override_ = NULL; | 193 system_temp_path_override_ = NULL; |
184 } | 194 } |
185 if (path != NULL) { | 195 if (path != NULL) { |
186 system_temp_path_override_ = strdup(path); | 196 system_temp_path_override_ = strdup(path); |
187 } | 197 } |
188 } | 198 } |
189 | 199 |
| 200 static Namespace* CObjectToNamespacePointer(CObject* cobject) { |
| 201 CObjectIntptr value(cobject); |
| 202 return reinterpret_cast<Namespace*>(value.Value()); |
| 203 } |
| 204 |
190 CObject* Directory::CreateRequest(const CObjectArray& request) { | 205 CObject* Directory::CreateRequest(const CObjectArray& request) { |
191 if ((request.Length() == 1) && request[0]->IsString()) { | 206 if ((request.Length() < 1) || !request[0]->IsIntptr()) { |
192 CObjectString path(request[0]); | 207 return CObject::IllegalArgumentError(); |
193 if (Directory::Create(path.CString())) { | |
194 return CObject::True(); | |
195 } else { | |
196 return CObject::NewOSError(); | |
197 } | |
198 } | 208 } |
199 return CObject::IllegalArgumentError(); | 209 Namespace* namespc = CObjectToNamespacePointer(request[0]); |
| 210 RefCntReleaseScope<Namespace> rs(namespc); |
| 211 if ((request.Length() != 2) || !request[1]->IsString()) { |
| 212 return CObject::IllegalArgumentError(); |
| 213 } |
| 214 CObjectString path(request[1]); |
| 215 return Directory::Create(namespc, path.CString()) ? CObject::True() |
| 216 : CObject::NewOSError(); |
200 } | 217 } |
201 | 218 |
202 CObject* Directory::DeleteRequest(const CObjectArray& request) { | 219 CObject* Directory::DeleteRequest(const CObjectArray& request) { |
203 if ((request.Length() == 2) && request[0]->IsString() && | 220 if ((request.Length() < 1) || !request[0]->IsIntptr()) { |
204 request[1]->IsBool()) { | 221 return CObject::IllegalArgumentError(); |
205 CObjectString path(request[0]); | |
206 CObjectBool recursive(request[1]); | |
207 if (Directory::Delete(path.CString(), recursive.Value())) { | |
208 return CObject::True(); | |
209 } else { | |
210 return CObject::NewOSError(); | |
211 } | |
212 } | 222 } |
213 return CObject::IllegalArgumentError(); | 223 Namespace* namespc = CObjectToNamespacePointer(request[0]); |
| 224 RefCntReleaseScope<Namespace> rs(namespc); |
| 225 if ((request.Length() != 3) || !request[1]->IsString() || |
| 226 !request[2]->IsBool()) { |
| 227 return CObject::IllegalArgumentError(); |
| 228 } |
| 229 CObjectString path(request[1]); |
| 230 CObjectBool recursive(request[2]); |
| 231 return Directory::Delete(namespc, path.CString(), recursive.Value()) |
| 232 ? CObject::True() |
| 233 : CObject::NewOSError(); |
214 } | 234 } |
215 | 235 |
216 CObject* Directory::ExistsRequest(const CObjectArray& request) { | 236 CObject* Directory::ExistsRequest(const CObjectArray& request) { |
217 static const int kExists = 1; | 237 static const int kExists = 1; |
218 static const int kDoesNotExist = 0; | 238 static const int kDoesNotExist = 0; |
219 if ((request.Length() == 1) && request[0]->IsString()) { | 239 if ((request.Length() < 1) || !request[0]->IsIntptr()) { |
220 CObjectString path(request[0]); | 240 return CObject::IllegalArgumentError(); |
221 Directory::ExistsResult result = Directory::Exists(path.CString()); | |
222 if (result == Directory::EXISTS) { | |
223 return new CObjectInt32(CObject::NewInt32(kExists)); | |
224 } else if (result == Directory::DOES_NOT_EXIST) { | |
225 return new CObjectInt32(CObject::NewInt32(kDoesNotExist)); | |
226 } else { | |
227 return CObject::NewOSError(); | |
228 } | |
229 } | 241 } |
230 return CObject::IllegalArgumentError(); | 242 Namespace* namespc = CObjectToNamespacePointer(request[0]); |
| 243 RefCntReleaseScope<Namespace> rs(namespc); |
| 244 if ((request.Length() != 2) || !request[1]->IsString()) { |
| 245 return CObject::IllegalArgumentError(); |
| 246 } |
| 247 CObjectString path(request[1]); |
| 248 Directory::ExistsResult result = Directory::Exists(namespc, path.CString()); |
| 249 if (result == Directory::EXISTS) { |
| 250 return new CObjectInt32(CObject::NewInt32(kExists)); |
| 251 } else if (result == Directory::DOES_NOT_EXIST) { |
| 252 return new CObjectInt32(CObject::NewInt32(kDoesNotExist)); |
| 253 } else { |
| 254 return CObject::NewOSError(); |
| 255 } |
231 } | 256 } |
232 | 257 |
233 CObject* Directory::CreateTempRequest(const CObjectArray& request) { | 258 CObject* Directory::CreateTempRequest(const CObjectArray& request) { |
234 if ((request.Length() == 1) && request[0]->IsString()) { | 259 if ((request.Length() < 1) || !request[0]->IsIntptr()) { |
235 CObjectString path(request[0]); | 260 return CObject::IllegalArgumentError(); |
236 const char* result = Directory::CreateTemp(path.CString()); | |
237 if (result != NULL) { | |
238 CObject* temp_dir = new CObjectString(CObject::NewString(result)); | |
239 return temp_dir; | |
240 } else { | |
241 return CObject::NewOSError(); | |
242 } | |
243 } | 261 } |
244 return CObject::IllegalArgumentError(); | 262 Namespace* namespc = CObjectToNamespacePointer(request[0]); |
| 263 RefCntReleaseScope<Namespace> rs(namespc); |
| 264 if ((request.Length() != 2) || !request[1]->IsString()) { |
| 265 return CObject::IllegalArgumentError(); |
| 266 } |
| 267 CObjectString path(request[1]); |
| 268 const char* result = Directory::CreateTemp(namespc, path.CString()); |
| 269 if (result == NULL) { |
| 270 return CObject::NewOSError(); |
| 271 } |
| 272 return new CObjectString(CObject::NewString(result)); |
245 } | 273 } |
246 | 274 |
247 static CObject* CreateIllegalArgumentError() { | 275 static CObject* CreateIllegalArgumentError() { |
248 // Respond with an illegal argument list error message. | 276 // Respond with an illegal argument list error message. |
249 CObjectArray* error = new CObjectArray(CObject::NewArray(3)); | 277 CObjectArray* error = new CObjectArray(CObject::NewArray(3)); |
250 error->SetAt(0, new CObjectInt32( | 278 error->SetAt(0, new CObjectInt32( |
251 CObject::NewInt32(AsyncDirectoryListing::kListError))); | 279 CObject::NewInt32(AsyncDirectoryListing::kListError))); |
252 error->SetAt(1, CObject::Null()); | 280 error->SetAt(1, CObject::Null()); |
253 error->SetAt(2, CObject::IllegalArgumentError()); | 281 error->SetAt(2, CObject::IllegalArgumentError()); |
254 return error; | 282 return error; |
255 } | 283 } |
256 | 284 |
257 CObject* Directory::ListStartRequest(const CObjectArray& request) { | 285 CObject* Directory::ListStartRequest(const CObjectArray& request) { |
258 if ((request.Length() == 3) && request[0]->IsString() && | 286 if ((request.Length() < 1) || !request[0]->IsIntptr()) { |
259 request[1]->IsBool() && request[2]->IsBool()) { | 287 return CreateIllegalArgumentError(); |
260 CObjectString path(request[0]); | |
261 CObjectBool recursive(request[1]); | |
262 CObjectBool follow_links(request[2]); | |
263 AsyncDirectoryListing* dir_listing = new AsyncDirectoryListing( | |
264 path.CString(), recursive.Value(), follow_links.Value()); | |
265 if (dir_listing->error()) { | |
266 // Report error now, so we capture the correct OSError. | |
267 CObject* err = CObject::NewOSError(); | |
268 dir_listing->Release(); | |
269 CObjectArray* error = new CObjectArray(CObject::NewArray(3)); | |
270 error->SetAt(0, new CObjectInt32(CObject::NewInt32( | |
271 AsyncDirectoryListing::kListError))); | |
272 error->SetAt(1, request[0]); | |
273 error->SetAt(2, err); | |
274 return error; | |
275 } | |
276 // TODO(ajohnsen): Consider returning the first few results. | |
277 return new CObjectIntptr( | |
278 CObject::NewIntptr(reinterpret_cast<intptr_t>(dir_listing))); | |
279 } | 288 } |
280 return CreateIllegalArgumentError(); | 289 Namespace* namespc = CObjectToNamespacePointer(request[0]); |
| 290 RefCntReleaseScope<Namespace> rs(namespc); |
| 291 if ((request.Length() != 4) || !request[1]->IsString() || |
| 292 !request[2]->IsBool() || !request[3]->IsBool()) { |
| 293 return CreateIllegalArgumentError(); |
| 294 } |
| 295 CObjectString path(request[1]); |
| 296 CObjectBool recursive(request[2]); |
| 297 CObjectBool follow_links(request[3]); |
| 298 AsyncDirectoryListing* dir_listing = new AsyncDirectoryListing( |
| 299 namespc, path.CString(), recursive.Value(), follow_links.Value()); |
| 300 if (dir_listing->error()) { |
| 301 // Report error now, so we capture the correct OSError. |
| 302 CObject* err = CObject::NewOSError(); |
| 303 dir_listing->Release(); |
| 304 CObjectArray* error = new CObjectArray(CObject::NewArray(3)); |
| 305 error->SetAt(0, new CObjectInt32(CObject::NewInt32( |
| 306 AsyncDirectoryListing::kListError))); |
| 307 error->SetAt(1, request[1]); |
| 308 error->SetAt(2, err); |
| 309 return error; |
| 310 } |
| 311 // TODO(ajohnsen): Consider returning the first few results. |
| 312 return new CObjectIntptr( |
| 313 CObject::NewIntptr(reinterpret_cast<intptr_t>(dir_listing))); |
281 } | 314 } |
282 | 315 |
283 CObject* Directory::ListNextRequest(const CObjectArray& request) { | 316 CObject* Directory::ListNextRequest(const CObjectArray& request) { |
284 if ((request.Length() == 1) && request[0]->IsIntptr()) { | 317 if ((request.Length() != 1) || !request[0]->IsIntptr()) { |
285 CObjectIntptr ptr(request[0]); | 318 return CreateIllegalArgumentError(); |
286 AsyncDirectoryListing* dir_listing = | |
287 reinterpret_cast<AsyncDirectoryListing*>(ptr.Value()); | |
288 RefCntReleaseScope<AsyncDirectoryListing> rs(dir_listing); | |
289 if (dir_listing->IsEmpty()) { | |
290 return new CObjectArray(CObject::NewArray(0)); | |
291 } | |
292 const int kArraySize = 128; | |
293 CObjectArray* response = new CObjectArray(CObject::NewArray(kArraySize)); | |
294 dir_listing->SetArray(response, kArraySize); | |
295 Directory::List(dir_listing); | |
296 // In case the listing ended before it hit the buffer length, we need to | |
297 // override the array length. | |
298 response->AsApiCObject()->value.as_array.length = dir_listing->index(); | |
299 return response; | |
300 } | 319 } |
301 return CreateIllegalArgumentError(); | 320 CObjectIntptr ptr(request[0]); |
| 321 AsyncDirectoryListing* dir_listing = |
| 322 reinterpret_cast<AsyncDirectoryListing*>(ptr.Value()); |
| 323 RefCntReleaseScope<AsyncDirectoryListing> rs(dir_listing); |
| 324 if (dir_listing->IsEmpty()) { |
| 325 return new CObjectArray(CObject::NewArray(0)); |
| 326 } |
| 327 const int kArraySize = 128; |
| 328 CObjectArray* response = new CObjectArray(CObject::NewArray(kArraySize)); |
| 329 dir_listing->SetArray(response, kArraySize); |
| 330 Directory::List(dir_listing); |
| 331 // In case the listing ended before it hit the buffer length, we need to |
| 332 // override the array length. |
| 333 response->AsApiCObject()->value.as_array.length = dir_listing->index(); |
| 334 return response; |
302 } | 335 } |
303 | 336 |
304 CObject* Directory::ListStopRequest(const CObjectArray& request) { | 337 CObject* Directory::ListStopRequest(const CObjectArray& request) { |
305 if ((request.Length() == 1) && request[0]->IsIntptr()) { | 338 if ((request.Length() != 1) || !request[0]->IsIntptr()) { |
306 CObjectIntptr ptr(request[0]); | 339 return CreateIllegalArgumentError(); |
307 AsyncDirectoryListing* dir_listing = | 340 } |
308 reinterpret_cast<AsyncDirectoryListing*>(ptr.Value()); | 341 CObjectIntptr ptr(request[0]); |
309 RefCntReleaseScope<AsyncDirectoryListing> rs(dir_listing); | 342 AsyncDirectoryListing* dir_listing = |
| 343 reinterpret_cast<AsyncDirectoryListing*>(ptr.Value()); |
| 344 RefCntReleaseScope<AsyncDirectoryListing> rs(dir_listing); |
310 | 345 |
311 // We have retained a reference to the listing here. Therefore the listing's | 346 // We have retained a reference to the listing here. Therefore the listing's |
312 // destructor can't be running. Since no further requests are dispatched by | 347 // destructor can't be running. Since no further requests are dispatched by |
313 // the Dart code after an async stop call, this PopAll() can't be racing | 348 // the Dart code after an async stop call, this PopAll() can't be racing |
314 // with any other call on the listing. We don't do an extra Release(), and | 349 // with any other call on the listing. We don't do an extra Release(), and |
315 // we don't delete the weak persistent handle. The file is closed here, but | 350 // we don't delete the weak persistent handle. The file is closed here, but |
316 // the memory for the listing will be cleaned up when the finalizer runs. | 351 // the memory for the listing will be cleaned up when the finalizer runs. |
317 dir_listing->PopAll(); | 352 dir_listing->PopAll(); |
318 return new CObjectBool(CObject::Bool(true)); | 353 return new CObjectBool(CObject::Bool(true)); |
319 } | |
320 return CreateIllegalArgumentError(); | |
321 } | 354 } |
322 | 355 |
323 CObject* Directory::RenameRequest(const CObjectArray& request) { | 356 CObject* Directory::RenameRequest(const CObjectArray& request) { |
324 if ((request.Length() == 2) && request[0]->IsString() && | 357 if ((request.Length() < 1) || !request[0]->IsIntptr()) { |
325 request[1]->IsString()) { | 358 return CObject::IllegalArgumentError(); |
326 CObjectString path(request[0]); | |
327 CObjectString new_path(request[1]); | |
328 bool completed = Directory::Rename(path.CString(), new_path.CString()); | |
329 if (completed) { | |
330 return CObject::True(); | |
331 } | |
332 return CObject::NewOSError(); | |
333 } | 359 } |
334 return CObject::IllegalArgumentError(); | 360 Namespace* namespc = CObjectToNamespacePointer(request[0]); |
| 361 RefCntReleaseScope<Namespace> rs(namespc); |
| 362 if ((request.Length() != 3) || !request[1]->IsString() || |
| 363 !request[2]->IsString()) { |
| 364 return CObject::IllegalArgumentError(); |
| 365 } |
| 366 CObjectString path(request[1]); |
| 367 CObjectString new_path(request[2]); |
| 368 return Directory::Rename(namespc, path.CString(), new_path.CString()) |
| 369 ? CObject::True() |
| 370 : CObject::NewOSError(); |
335 } | 371 } |
336 | 372 |
337 bool AsyncDirectoryListing::AddFileSystemEntityToResponse(Response type, | 373 bool AsyncDirectoryListing::AddFileSystemEntityToResponse(Response type, |
338 const char* arg) { | 374 const char* arg) { |
339 array_->SetAt(index_++, new CObjectInt32(CObject::NewInt32(type))); | 375 array_->SetAt(index_++, new CObjectInt32(CObject::NewInt32(type))); |
340 if (arg != NULL) { | 376 if (arg != NULL) { |
341 array_->SetAt(index_++, new CObjectString(CObject::NewString(arg))); | 377 array_->SetAt(index_++, new CObjectString(CObject::NewString(arg))); |
342 } else { | 378 } else { |
343 array_->SetAt(index_++, CObject::Null()); | 379 array_->SetAt(index_++, CObject::Null()); |
344 } | 380 } |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 void Directory::List(DirectoryListing* listing) { | 492 void Directory::List(DirectoryListing* listing) { |
457 if (listing->error()) { | 493 if (listing->error()) { |
458 listing->HandleError(); | 494 listing->HandleError(); |
459 listing->HandleDone(); | 495 listing->HandleDone(); |
460 } else { | 496 } else { |
461 while (ListNext(listing)) { | 497 while (ListNext(listing)) { |
462 } | 498 } |
463 } | 499 } |
464 } | 500 } |
465 | 501 |
| 502 const char* Directory::Current(Namespace* namespc) { |
| 503 return Namespace::GetCurrent(namespc); |
| 504 } |
| 505 |
| 506 bool Directory::SetCurrent(Namespace* namespc, const char* name) { |
| 507 return Namespace::SetCurrent(namespc, name); |
| 508 } |
| 509 |
466 } // namespace bin | 510 } // namespace bin |
467 } // namespace dart | 511 } // namespace dart |
OLD | NEW |