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 "include/dart_api.h" | 8 #include "include/dart_api.h" |
9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
10 | 10 |
11 | 11 |
12 namespace dart { | 12 namespace dart { |
13 namespace bin { | 13 namespace bin { |
14 | 14 |
15 void FUNCTION_NAME(Directory_Current)(Dart_NativeArguments args) { | 15 void FUNCTION_NAME(Directory_Current)(Dart_NativeArguments args) { |
16 char* current = Directory::Current(); | 16 const char* current = Directory::Current(); |
17 if (current != NULL) { | 17 if (current != NULL) { |
18 Dart_SetReturnValue(args, DartUtils::NewString(current)); | 18 Dart_SetReturnValue(args, DartUtils::NewString(current)); |
19 free(current); | |
20 } else { | 19 } else { |
21 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 20 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
22 } | 21 } |
23 } | 22 } |
24 | 23 |
25 | 24 |
26 void FUNCTION_NAME(Directory_SetCurrent)(Dart_NativeArguments args) { | 25 void FUNCTION_NAME(Directory_SetCurrent)(Dart_NativeArguments args) { |
27 int argc = Dart_GetNativeArgumentCount(args); | 26 int argc = Dart_GetNativeArgumentCount(args); |
28 Dart_Handle path; | 27 Dart_Handle path; |
29 if (argc == 1) { | 28 if (argc == 1) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 if (Directory::Create(DartUtils::GetStringValue(path))) { | 61 if (Directory::Create(DartUtils::GetStringValue(path))) { |
63 Dart_SetReturnValue(args, Dart_True()); | 62 Dart_SetReturnValue(args, Dart_True()); |
64 } else { | 63 } else { |
65 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 64 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
66 } | 65 } |
67 } | 66 } |
68 | 67 |
69 | 68 |
70 void FUNCTION_NAME(Directory_SystemTemp)( | 69 void FUNCTION_NAME(Directory_SystemTemp)( |
71 Dart_NativeArguments args) { | 70 Dart_NativeArguments args) { |
72 char* result = Directory::SystemTemp(); | 71 const char* result = Directory::SystemTemp(); |
73 Dart_SetReturnValue(args, DartUtils::NewString(result)); | 72 Dart_SetReturnValue(args, DartUtils::NewString(result)); |
74 free(result); | |
75 } | 73 } |
76 | 74 |
77 | 75 |
78 void FUNCTION_NAME(Directory_CreateTemp)(Dart_NativeArguments args) { | 76 void FUNCTION_NAME(Directory_CreateTemp)(Dart_NativeArguments args) { |
79 Dart_Handle path = Dart_GetNativeArgument(args, 0); | 77 Dart_Handle path = Dart_GetNativeArgument(args, 0); |
80 if (!Dart_IsString(path)) { | 78 if (!Dart_IsString(path)) { |
81 Dart_SetReturnValue(args, DartUtils::NewDartArgumentError( | 79 Dart_SetReturnValue(args, DartUtils::NewDartArgumentError( |
82 "Prefix argument of CreateSystemTempSync is not a String")); | 80 "Prefix argument of CreateSystemTempSync is not a String")); |
83 return; | 81 return; |
84 } | 82 } |
85 char* result = Directory::CreateTemp(DartUtils::GetStringValue(path)); | 83 const char* result = |
| 84 Directory::CreateTemp(DartUtils::GetStringValue(path)); |
86 if (result != NULL) { | 85 if (result != NULL) { |
87 Dart_SetReturnValue(args, DartUtils::NewString(result)); | 86 Dart_SetReturnValue(args, DartUtils::NewString(result)); |
88 free(result); | |
89 } else { | 87 } else { |
90 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 88 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
91 } | 89 } |
92 } | 90 } |
93 | 91 |
94 | 92 |
95 void FUNCTION_NAME(Directory_Delete)(Dart_NativeArguments args) { | 93 void FUNCTION_NAME(Directory_Delete)(Dart_NativeArguments args) { |
96 Dart_Handle path = Dart_GetNativeArgument(args, 0); | 94 Dart_Handle path = Dart_GetNativeArgument(args, 0); |
97 Dart_Handle recursive = Dart_GetNativeArgument(args, 1); | 95 Dart_Handle recursive = Dart_GetNativeArgument(args, 1); |
98 if (Directory::Delete(DartUtils::GetStringValue(path), | 96 if (Directory::Delete(DartUtils::GetStringValue(path), |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 return CObject::NewOSError(); | 178 return CObject::NewOSError(); |
181 } | 179 } |
182 } | 180 } |
183 return CObject::IllegalArgumentError(); | 181 return CObject::IllegalArgumentError(); |
184 } | 182 } |
185 | 183 |
186 | 184 |
187 CObject* Directory::CreateTempRequest(const CObjectArray& request) { | 185 CObject* Directory::CreateTempRequest(const CObjectArray& request) { |
188 if (request.Length() == 1 && request[0]->IsString()) { | 186 if (request.Length() == 1 && request[0]->IsString()) { |
189 CObjectString path(request[0]); | 187 CObjectString path(request[0]); |
190 char* result = Directory::CreateTemp(path.CString()); | 188 const char* result = Directory::CreateTemp(path.CString()); |
191 if (result != NULL) { | 189 if (result != NULL) { |
192 CObject* temp_dir = new CObjectString(CObject::NewString(result)); | 190 CObject* temp_dir = new CObjectString(CObject::NewString(result)); |
193 free(result); | |
194 return temp_dir; | 191 return temp_dir; |
195 } else { | 192 } else { |
196 return CObject::NewOSError(); | 193 return CObject::NewOSError(); |
197 } | 194 } |
198 } | 195 } |
199 return CObject::IllegalArgumentError(); | 196 return CObject::IllegalArgumentError(); |
200 } | 197 } |
201 | 198 |
202 | 199 |
203 static CObject* CreateIllegalArgumentError() { | 200 static CObject* CreateIllegalArgumentError() { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 } | 233 } |
237 // TODO(ajohnsen): Consider returning the first few results. | 234 // TODO(ajohnsen): Consider returning the first few results. |
238 return new CObjectIntptr(CObject::NewIntptr( | 235 return new CObjectIntptr(CObject::NewIntptr( |
239 reinterpret_cast<intptr_t>(dir_listing))); | 236 reinterpret_cast<intptr_t>(dir_listing))); |
240 } | 237 } |
241 return CreateIllegalArgumentError(); | 238 return CreateIllegalArgumentError(); |
242 } | 239 } |
243 | 240 |
244 | 241 |
245 CObject* Directory::ListNextRequest(const CObjectArray& request) { | 242 CObject* Directory::ListNextRequest(const CObjectArray& request) { |
246 if (request.Length() == 1 && | 243 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
247 request[0]->IsIntptr()) { | |
248 CObjectIntptr ptr(request[0]); | 244 CObjectIntptr ptr(request[0]); |
249 AsyncDirectoryListing* dir_listing = | 245 AsyncDirectoryListing* dir_listing = |
250 reinterpret_cast<AsyncDirectoryListing*>(ptr.Value()); | 246 reinterpret_cast<AsyncDirectoryListing*>(ptr.Value()); |
251 if (dir_listing->IsEmpty()) { | 247 if (dir_listing->IsEmpty()) { |
252 return new CObjectArray(CObject::NewArray(0)); | 248 return new CObjectArray(CObject::NewArray(0)); |
253 } | 249 } |
254 const int kArraySize = 128; | 250 const int kArraySize = 128; |
255 CObjectArray* response = new CObjectArray(CObject::NewArray(kArraySize)); | 251 CObjectArray* response = new CObjectArray(CObject::NewArray(kArraySize)); |
256 dir_listing->SetArray(response, kArraySize); | 252 dir_listing->SetArray(response, kArraySize); |
257 Directory::List(dir_listing); | 253 Directory::List(dir_listing); |
258 // In case the listing ended before it hit the buffer length, we need to | 254 // In case the listing ended before it hit the buffer length, we need to |
259 // override the array length. | 255 // override the array length. |
260 response->AsApiCObject()->value.as_array.length = dir_listing->index(); | 256 response->AsApiCObject()->value.as_array.length = dir_listing->index(); |
261 return response; | 257 return response; |
262 } | 258 } |
263 return CreateIllegalArgumentError(); | 259 return CreateIllegalArgumentError(); |
264 } | 260 } |
265 | 261 |
266 | 262 |
267 CObject* Directory::ListStopRequest(const CObjectArray& request) { | 263 CObject* Directory::ListStopRequest(const CObjectArray& request) { |
268 if (request.Length() == 1 && request[0]->IsIntptr()) { | 264 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
269 CObjectIntptr ptr(request[0]); | 265 CObjectIntptr ptr(request[0]); |
270 AsyncDirectoryListing* dir_listing = | 266 AsyncDirectoryListing* dir_listing = |
271 reinterpret_cast<AsyncDirectoryListing*>(ptr.Value()); | 267 reinterpret_cast<AsyncDirectoryListing*>(ptr.Value()); |
272 delete dir_listing; | 268 delete dir_listing; |
273 return new CObjectBool(CObject::Bool(true)); | 269 return new CObjectBool(CObject::Bool(true)); |
274 } | 270 } |
275 return CreateIllegalArgumentError(); | 271 return CreateIllegalArgumentError(); |
276 } | 272 } |
277 | 273 |
278 | 274 |
279 CObject* Directory::RenameRequest(const CObjectArray& request) { | 275 CObject* Directory::RenameRequest(const CObjectArray& request) { |
280 if (request.Length() == 2 && | 276 if (request.Length() == 2 && |
281 request[0]->IsString() && | 277 request[0]->IsString() && |
282 request[1]->IsString()) { | 278 request[1]->IsString()) { |
283 CObjectString path(request[0]); | 279 CObjectString path(request[0]); |
284 CObjectString new_path(request[1]); | 280 CObjectString new_path(request[1]); |
285 bool completed = Directory::Rename(path.CString(), new_path.CString()); | 281 bool completed = Directory::Rename(path.CString(), new_path.CString()); |
286 if (completed) return CObject::True(); | 282 if (completed) { |
| 283 return CObject::True(); |
| 284 } |
287 return CObject::NewOSError(); | 285 return CObject::NewOSError(); |
288 } | 286 } |
289 return CObject::IllegalArgumentError(); | 287 return CObject::IllegalArgumentError(); |
290 } | 288 } |
291 | 289 |
292 | 290 |
293 bool AsyncDirectoryListing::AddFileSystemEntityToResponse(Response type, | 291 bool AsyncDirectoryListing::AddFileSystemEntityToResponse(Response type, |
294 char* arg) { | 292 const char* arg) { |
295 array_->SetAt(index_++, new CObjectInt32(CObject::NewInt32(type))); | 293 array_->SetAt(index_++, new CObjectInt32(CObject::NewInt32(type))); |
296 if (arg != NULL) { | 294 if (arg != NULL) { |
297 array_->SetAt(index_++, new CObjectString(CObject::NewString(arg))); | 295 array_->SetAt(index_++, new CObjectString(CObject::NewString(arg))); |
298 } else { | 296 } else { |
299 array_->SetAt(index_++, CObject::Null()); | 297 array_->SetAt(index_++, CObject::Null()); |
300 } | 298 } |
301 return index_ < length_; | 299 return index_ < length_; |
302 } | 300 } |
303 | 301 |
304 | 302 |
305 bool AsyncDirectoryListing::HandleDirectory(char* dir_name) { | 303 bool AsyncDirectoryListing::HandleDirectory(const char* dir_name) { |
306 return AddFileSystemEntityToResponse(kListDirectory, dir_name); | 304 return AddFileSystemEntityToResponse(kListDirectory, dir_name); |
307 } | 305 } |
308 | 306 |
309 | 307 |
310 bool AsyncDirectoryListing::HandleFile(char* file_name) { | 308 bool AsyncDirectoryListing::HandleFile(const char* file_name) { |
311 return AddFileSystemEntityToResponse(kListFile, file_name); | 309 return AddFileSystemEntityToResponse(kListFile, file_name); |
312 } | 310 } |
313 | 311 |
314 | 312 |
315 bool AsyncDirectoryListing::HandleLink(char* link_name) { | 313 bool AsyncDirectoryListing::HandleLink(const char* link_name) { |
316 return AddFileSystemEntityToResponse(kListLink, link_name); | 314 return AddFileSystemEntityToResponse(kListLink, link_name); |
317 } | 315 } |
318 | 316 |
| 317 |
319 void AsyncDirectoryListing::HandleDone() { | 318 void AsyncDirectoryListing::HandleDone() { |
320 AddFileSystemEntityToResponse(kListDone, NULL); | 319 AddFileSystemEntityToResponse(kListDone, NULL); |
321 } | 320 } |
322 | 321 |
323 | 322 |
324 bool AsyncDirectoryListing::HandleError(const char* dir_name) { | 323 bool AsyncDirectoryListing::HandleError() { |
325 CObject* err = CObject::NewOSError(); | 324 CObject* err = CObject::NewOSError(); |
326 array_->SetAt(index_++, new CObjectInt32(CObject::NewInt32(kListError))); | 325 array_->SetAt(index_++, new CObjectInt32(CObject::NewInt32(kListError))); |
327 CObjectArray* response = new CObjectArray(CObject::NewArray(3)); | 326 CObjectArray* response = new CObjectArray(CObject::NewArray(3)); |
328 response->SetAt(0, new CObjectInt32(CObject::NewInt32(kListError))); | 327 response->SetAt(0, new CObjectInt32(CObject::NewInt32(kListError))); |
329 response->SetAt(1, new CObjectString(CObject::NewString(dir_name))); | 328 // Delay calling CurrentPath() until after CObject::NewOSError() in case |
| 329 // CurrentPath() pollutes the OS error code. |
| 330 response->SetAt(1, new CObjectString(CObject::NewString( |
| 331 error() ? "Invalid path" : CurrentPath()))); |
330 response->SetAt(2, err); | 332 response->SetAt(2, err); |
331 array_->SetAt(index_++, response); | 333 array_->SetAt(index_++, response); |
332 return index_ < length_; | 334 return index_ < length_; |
333 } | 335 } |
334 | 336 |
335 bool SyncDirectoryListing::HandleDirectory(char* dir_name) { | 337 |
| 338 bool SyncDirectoryListing::HandleDirectory(const char* dir_name) { |
336 Dart_Handle dir_name_dart = DartUtils::NewString(dir_name); | 339 Dart_Handle dir_name_dart = DartUtils::NewString(dir_name); |
337 Dart_Handle dir = | 340 Dart_Handle dir = |
338 Dart_New(directory_type_, Dart_Null(), 1, &dir_name_dart); | 341 Dart_New(directory_type_, Dart_Null(), 1, &dir_name_dart); |
339 Dart_Invoke(results_, add_string_, 1, &dir); | 342 Dart_Invoke(results_, add_string_, 1, &dir); |
340 return true; | 343 return true; |
341 } | 344 } |
342 | 345 |
343 bool SyncDirectoryListing::HandleLink(char* link_name) { | 346 |
| 347 bool SyncDirectoryListing::HandleLink(const char* link_name) { |
344 Dart_Handle link_name_dart = DartUtils::NewString(link_name); | 348 Dart_Handle link_name_dart = DartUtils::NewString(link_name); |
345 Dart_Handle link = | 349 Dart_Handle link = |
346 Dart_New(link_type_, Dart_Null(), 1, &link_name_dart); | 350 Dart_New(link_type_, Dart_Null(), 1, &link_name_dart); |
347 Dart_Invoke(results_, add_string_, 1, &link); | 351 Dart_Invoke(results_, add_string_, 1, &link); |
348 return true; | 352 return true; |
349 } | 353 } |
350 | 354 |
351 bool SyncDirectoryListing::HandleFile(char* file_name) { | 355 |
| 356 bool SyncDirectoryListing::HandleFile(const char* file_name) { |
352 Dart_Handle file_name_dart = DartUtils::NewString(file_name); | 357 Dart_Handle file_name_dart = DartUtils::NewString(file_name); |
353 Dart_Handle file = | 358 Dart_Handle file = |
354 Dart_New(file_type_, Dart_Null(), 1, &file_name_dart); | 359 Dart_New(file_type_, Dart_Null(), 1, &file_name_dart); |
355 Dart_Invoke(results_, add_string_, 1, &file); | 360 Dart_Invoke(results_, add_string_, 1, &file); |
356 return true; | 361 return true; |
357 } | 362 } |
358 | 363 |
359 bool SyncDirectoryListing::HandleError(const char* dir_name) { | 364 |
| 365 bool SyncDirectoryListing::HandleError() { |
360 Dart_Handle dart_os_error = DartUtils::NewDartOSError(); | 366 Dart_Handle dart_os_error = DartUtils::NewDartOSError(); |
361 Dart_Handle args[3]; | 367 Dart_Handle args[3]; |
362 args[0] = DartUtils::NewString("Directory listing failed"); | 368 args[0] = DartUtils::NewString("Directory listing failed"); |
363 args[1] = DartUtils::NewString(dir_name); | 369 args[1] = DartUtils::NewString(error() ? "Invalid path" : CurrentPath()); |
364 args[2] = dart_os_error; | 370 args[2] = dart_os_error; |
365 Dart_ThrowException(Dart_New( | 371 Dart_ThrowException(Dart_New( |
366 DartUtils::GetDartType(DartUtils::kIOLibURL, "FileSystemException"), | 372 DartUtils::GetDartType(DartUtils::kIOLibURL, "FileSystemException"), |
367 Dart_Null(), | 373 Dart_Null(), |
368 3, | 374 3, |
369 args)); | 375 args)); |
370 return true; | 376 return true; |
371 } | 377 } |
372 | 378 |
373 | 379 |
374 static bool ListNext(DirectoryListing* listing) { | 380 static bool ListNext(DirectoryListing* listing) { |
375 switch (listing->top()->Next(listing)) { | 381 switch (listing->top()->Next(listing)) { |
376 case kListFile: | 382 case kListFile: |
377 return listing->HandleFile(listing->CurrentPath()); | 383 return listing->HandleFile(listing->CurrentPath()); |
378 | 384 |
379 case kListLink: | 385 case kListLink: |
380 return listing->HandleLink(listing->CurrentPath()); | 386 return listing->HandleLink(listing->CurrentPath()); |
381 | 387 |
382 case kListDirectory: | 388 case kListDirectory: |
383 if (listing->recursive()) { | 389 if (listing->recursive()) { |
384 listing->Push(new DirectoryListingEntry(listing->top())); | 390 listing->Push(new DirectoryListingEntry(listing->top())); |
385 } | 391 } |
386 return listing->HandleDirectory(listing->CurrentPath()); | 392 return listing->HandleDirectory(listing->CurrentPath()); |
387 | 393 |
388 case kListError: | 394 case kListError: |
389 return listing->HandleError(listing->CurrentPath()); | 395 return listing->HandleError(); |
390 | 396 |
391 case kListDone: | 397 case kListDone: |
392 listing->Pop(); | 398 listing->Pop(); |
393 if (listing->IsEmpty()) { | 399 if (listing->IsEmpty()) { |
394 listing->HandleDone(); | 400 listing->HandleDone(); |
395 return false; | 401 return false; |
396 } else { | 402 } else { |
397 return true; | 403 return true; |
398 } | 404 } |
399 | 405 |
400 default: | 406 default: |
401 UNREACHABLE(); | 407 UNREACHABLE(); |
402 } | 408 } |
403 return false; | 409 return false; |
404 } | 410 } |
405 | 411 |
| 412 |
406 void Directory::List(DirectoryListing* listing) { | 413 void Directory::List(DirectoryListing* listing) { |
407 if (listing->error()) { | 414 if (listing->error()) { |
408 listing->HandleError("Invalid path"); | 415 listing->HandleError(); |
409 listing->HandleDone(); | 416 listing->HandleDone(); |
410 } else { | 417 } else { |
411 while (ListNext(listing)) {} | 418 while (ListNext(listing)) {} |
412 } | 419 } |
413 } | 420 } |
414 | 421 |
415 } // namespace bin | 422 } // namespace bin |
416 } // namespace dart | 423 } // namespace dart |
OLD | NEW |