Index: runtime/bin/directory.cc |
diff --git a/runtime/bin/directory.cc b/runtime/bin/directory.cc |
index e31779c950da2766434e145e344e8ad120ac68ad..24e7d7ca3aca90d4e939a4d49dd59cc545b1610c 100644 |
--- a/runtime/bin/directory.cc |
+++ b/runtime/bin/directory.cc |
@@ -6,6 +6,7 @@ |
#include "bin/dartutils.h" |
#include "bin/log.h" |
+#include "bin/namespace.h" |
#include "include/dart_api.h" |
#include "platform/assert.h" |
@@ -15,7 +16,8 @@ namespace bin { |
char* Directory::system_temp_path_override_ = NULL; |
void FUNCTION_NAME(Directory_Current)(Dart_NativeArguments args) { |
- const char* current = Directory::Current(); |
+ Namespace* namespc = Namespace::GetNamespace(args, 0); |
+ const char* current = Directory::Current(namespc); |
if (current != NULL) { |
Dart_SetReturnValue(args, DartUtils::NewString(current)); |
} else { |
@@ -24,60 +26,62 @@ void FUNCTION_NAME(Directory_Current)(Dart_NativeArguments args) { |
} |
void FUNCTION_NAME(Directory_SetCurrent)(Dart_NativeArguments args) { |
- int argc = Dart_GetNativeArgumentCount(args); |
- Dart_Handle path; |
- if (argc == 1) { |
- path = Dart_GetNativeArgument(args, 0); |
- } |
- if ((argc != 1) || !Dart_IsString(path)) { |
+ Namespace* namespc = Namespace::GetNamespace(args, 0); |
+ Dart_Handle path = Dart_GetNativeArgument(args, 1); |
+ if (Dart_IsError(path) || !Dart_IsString(path)) { |
Dart_SetReturnValue(args, DartUtils::NewDartArgumentError(NULL)); |
+ return; |
+ } |
+ if (Directory::SetCurrent(namespc, DartUtils::GetStringValue(path))) { |
+ Dart_SetBooleanReturnValue(args, true); |
} else { |
- if (Directory::SetCurrent(DartUtils::GetStringValue(path))) { |
- Dart_SetReturnValue(args, Dart_True()); |
- } else { |
- Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
- } |
+ Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
} |
} |
void FUNCTION_NAME(Directory_Exists)(Dart_NativeArguments args) { |
static const int kExists = 1; |
static const int kDoesNotExist = 0; |
- Dart_Handle path = Dart_GetNativeArgument(args, 0); |
+ Namespace* namespc = Namespace::GetNamespace(args, 0); |
+ Dart_Handle path = Dart_GetNativeArgument(args, 1); |
Directory::ExistsResult result = |
- Directory::Exists(DartUtils::GetStringValue(path)); |
+ Directory::Exists(namespc, DartUtils::GetStringValue(path)); |
if (result == Directory::EXISTS) { |
- Dart_SetReturnValue(args, Dart_NewInteger(kExists)); |
+ Dart_SetIntegerReturnValue(args, kExists); |
} else if (result == Directory::DOES_NOT_EXIST) { |
- Dart_SetReturnValue(args, Dart_NewInteger(kDoesNotExist)); |
+ Dart_SetIntegerReturnValue(args, kDoesNotExist); |
} else { |
Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
} |
} |
void FUNCTION_NAME(Directory_Create)(Dart_NativeArguments args) { |
- Dart_Handle path = Dart_GetNativeArgument(args, 0); |
- if (Directory::Create(DartUtils::GetStringValue(path))) { |
- Dart_SetReturnValue(args, Dart_True()); |
+ Namespace* namespc = Namespace::GetNamespace(args, 0); |
+ Dart_Handle path = Dart_GetNativeArgument(args, 1); |
+ if (Directory::Create(namespc, DartUtils::GetStringValue(path))) { |
+ Dart_SetBooleanReturnValue(args, true); |
} else { |
Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
} |
} |
void FUNCTION_NAME(Directory_SystemTemp)(Dart_NativeArguments args) { |
- const char* result = Directory::SystemTemp(); |
+ Namespace* namespc = Namespace::GetNamespace(args, 0); |
+ const char* result = Directory::SystemTemp(namespc); |
Dart_SetReturnValue(args, DartUtils::NewString(result)); |
} |
void FUNCTION_NAME(Directory_CreateTemp)(Dart_NativeArguments args) { |
- Dart_Handle path = Dart_GetNativeArgument(args, 0); |
+ Namespace* namespc = Namespace::GetNamespace(args, 0); |
+ Dart_Handle path = Dart_GetNativeArgument(args, 1); |
if (!Dart_IsString(path)) { |
Dart_SetReturnValue( |
args, DartUtils::NewDartArgumentError( |
"Prefix argument of CreateSystemTempSync is not a String")); |
return; |
} |
- const char* result = Directory::CreateTemp(DartUtils::GetStringValue(path)); |
+ const char* result = |
+ Directory::CreateTemp(namespc, DartUtils::GetStringValue(path)); |
if (result != NULL) { |
Dart_SetReturnValue(args, DartUtils::NewString(result)); |
} else { |
@@ -86,22 +90,26 @@ void FUNCTION_NAME(Directory_CreateTemp)(Dart_NativeArguments args) { |
} |
void FUNCTION_NAME(Directory_Delete)(Dart_NativeArguments args) { |
- Dart_Handle path = Dart_GetNativeArgument(args, 0); |
- Dart_Handle recursive = Dart_GetNativeArgument(args, 1); |
- if (Directory::Delete(DartUtils::GetStringValue(path), |
+ Namespace* namespc = Namespace::GetNamespace(args, 0); |
+ Dart_Handle path = Dart_GetNativeArgument(args, 1); |
+ Dart_Handle recursive = Dart_GetNativeArgument(args, 2); |
+ if (Directory::Delete(namespc, |
+ DartUtils::GetStringValue(path), |
DartUtils::GetBooleanValue(recursive))) { |
- Dart_SetReturnValue(args, Dart_True()); |
+ Dart_SetBooleanReturnValue(args, true); |
} else { |
Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
} |
} |
void FUNCTION_NAME(Directory_Rename)(Dart_NativeArguments args) { |
- Dart_Handle path = Dart_GetNativeArgument(args, 0); |
- Dart_Handle newPath = Dart_GetNativeArgument(args, 1); |
- if (Directory::Rename(DartUtils::GetStringValue(path), |
+ Namespace* namespc = Namespace::GetNamespace(args, 0); |
+ Dart_Handle path = Dart_GetNativeArgument(args, 1); |
+ Dart_Handle newPath = Dart_GetNativeArgument(args, 2); |
+ if (Directory::Rename(namespc, |
+ DartUtils::GetStringValue(path), |
DartUtils::GetStringValue(newPath))) { |
- Dart_SetReturnValue(args, Dart_True()); |
+ Dart_SetBooleanReturnValue(args, true); |
} else { |
Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
} |
@@ -109,17 +117,19 @@ void FUNCTION_NAME(Directory_Rename)(Dart_NativeArguments args) { |
void FUNCTION_NAME(Directory_FillWithDirectoryListing)( |
Dart_NativeArguments args) { |
+ Namespace* namespc = Namespace::GetNamespace(args, 0); |
// The list that we should fill. |
- Dart_Handle results = Dart_GetNativeArgument(args, 0); |
- Dart_Handle path = Dart_GetNativeArgument(args, 1); |
- Dart_Handle recursive = Dart_GetNativeArgument(args, 2); |
- Dart_Handle follow_links = Dart_GetNativeArgument(args, 3); |
+ Dart_Handle results = Dart_GetNativeArgument(args, 1); |
+ Dart_Handle path = Dart_GetNativeArgument(args, 2); |
+ Dart_Handle recursive = Dart_GetNativeArgument(args, 3); |
+ Dart_Handle follow_links = Dart_GetNativeArgument(args, 4); |
Dart_Handle dart_error; |
{ |
// Pass the list that should hold the directory listing to the |
// SyncDirectoryListing object, which adds elements to it. |
- SyncDirectoryListing sync_listing(results, DartUtils::GetStringValue(path), |
+ SyncDirectoryListing sync_listing(results, namespc, |
+ DartUtils::GetStringValue(path), |
DartUtils::GetBooleanValue(recursive), |
DartUtils::GetBooleanValue(follow_links)); |
Directory::List(&sync_listing); |
@@ -187,61 +197,79 @@ void Directory::SetSystemTemp(const char* path) { |
} |
} |
+static Namespace* CObjectToNamespacePointer(CObject* cobject) { |
+ CObjectIntptr value(cobject); |
+ return reinterpret_cast<Namespace*>(value.Value()); |
+} |
+ |
CObject* Directory::CreateRequest(const CObjectArray& request) { |
- if ((request.Length() == 1) && request[0]->IsString()) { |
- CObjectString path(request[0]); |
- if (Directory::Create(path.CString())) { |
- return CObject::True(); |
- } else { |
- return CObject::NewOSError(); |
- } |
+ if ((request.Length() < 1) || !request[0]->IsIntptr()) { |
+ return CObject::IllegalArgumentError(); |
+ } |
+ Namespace* namespc = CObjectToNamespacePointer(request[0]); |
+ RefCntReleaseScope<Namespace> rs(namespc); |
+ if ((request.Length() != 2) || !request[1]->IsString()) { |
+ return CObject::IllegalArgumentError(); |
} |
- return CObject::IllegalArgumentError(); |
+ CObjectString path(request[1]); |
+ return Directory::Create(namespc, path.CString()) ? CObject::True() |
+ : CObject::NewOSError(); |
} |
CObject* Directory::DeleteRequest(const CObjectArray& request) { |
- if ((request.Length() == 2) && request[0]->IsString() && |
- request[1]->IsBool()) { |
- CObjectString path(request[0]); |
- CObjectBool recursive(request[1]); |
- if (Directory::Delete(path.CString(), recursive.Value())) { |
- return CObject::True(); |
- } else { |
- return CObject::NewOSError(); |
- } |
+ if ((request.Length() < 1) || !request[0]->IsIntptr()) { |
+ return CObject::IllegalArgumentError(); |
+ } |
+ Namespace* namespc = CObjectToNamespacePointer(request[0]); |
+ RefCntReleaseScope<Namespace> rs(namespc); |
+ if ((request.Length() != 3) || !request[1]->IsString() || |
+ !request[2]->IsBool()) { |
+ return CObject::IllegalArgumentError(); |
} |
- return CObject::IllegalArgumentError(); |
+ CObjectString path(request[1]); |
+ CObjectBool recursive(request[2]); |
+ return Directory::Delete(namespc, path.CString(), recursive.Value()) |
+ ? CObject::True() |
+ : CObject::NewOSError(); |
} |
CObject* Directory::ExistsRequest(const CObjectArray& request) { |
static const int kExists = 1; |
static const int kDoesNotExist = 0; |
- if ((request.Length() == 1) && request[0]->IsString()) { |
- CObjectString path(request[0]); |
- Directory::ExistsResult result = Directory::Exists(path.CString()); |
- if (result == Directory::EXISTS) { |
- return new CObjectInt32(CObject::NewInt32(kExists)); |
- } else if (result == Directory::DOES_NOT_EXIST) { |
- return new CObjectInt32(CObject::NewInt32(kDoesNotExist)); |
- } else { |
- return CObject::NewOSError(); |
- } |
+ if ((request.Length() < 1) || !request[0]->IsIntptr()) { |
+ return CObject::IllegalArgumentError(); |
+ } |
+ Namespace* namespc = CObjectToNamespacePointer(request[0]); |
+ RefCntReleaseScope<Namespace> rs(namespc); |
+ if ((request.Length() != 2) || !request[1]->IsString()) { |
+ return CObject::IllegalArgumentError(); |
+ } |
+ CObjectString path(request[1]); |
+ Directory::ExistsResult result = Directory::Exists(namespc, path.CString()); |
+ if (result == Directory::EXISTS) { |
+ return new CObjectInt32(CObject::NewInt32(kExists)); |
+ } else if (result == Directory::DOES_NOT_EXIST) { |
+ return new CObjectInt32(CObject::NewInt32(kDoesNotExist)); |
+ } else { |
+ return CObject::NewOSError(); |
} |
- return CObject::IllegalArgumentError(); |
} |
CObject* Directory::CreateTempRequest(const CObjectArray& request) { |
- if ((request.Length() == 1) && request[0]->IsString()) { |
- CObjectString path(request[0]); |
- const char* result = Directory::CreateTemp(path.CString()); |
- if (result != NULL) { |
- CObject* temp_dir = new CObjectString(CObject::NewString(result)); |
- return temp_dir; |
- } else { |
- return CObject::NewOSError(); |
- } |
+ if ((request.Length() < 1) || !request[0]->IsIntptr()) { |
+ return CObject::IllegalArgumentError(); |
+ } |
+ Namespace* namespc = CObjectToNamespacePointer(request[0]); |
+ RefCntReleaseScope<Namespace> rs(namespc); |
+ if ((request.Length() != 2) || !request[1]->IsString()) { |
+ return CObject::IllegalArgumentError(); |
} |
- return CObject::IllegalArgumentError(); |
+ CObjectString path(request[1]); |
+ const char* result = Directory::CreateTemp(namespc, path.CString()); |
+ if (result == NULL) { |
+ return CObject::NewOSError(); |
+ } |
+ return new CObjectString(CObject::NewString(result)); |
} |
static CObject* CreateIllegalArgumentError() { |
@@ -255,83 +283,91 @@ static CObject* CreateIllegalArgumentError() { |
} |
CObject* Directory::ListStartRequest(const CObjectArray& request) { |
- if ((request.Length() == 3) && request[0]->IsString() && |
- request[1]->IsBool() && request[2]->IsBool()) { |
- CObjectString path(request[0]); |
- CObjectBool recursive(request[1]); |
- CObjectBool follow_links(request[2]); |
- AsyncDirectoryListing* dir_listing = new AsyncDirectoryListing( |
- path.CString(), recursive.Value(), follow_links.Value()); |
- if (dir_listing->error()) { |
- // Report error now, so we capture the correct OSError. |
- CObject* err = CObject::NewOSError(); |
- dir_listing->Release(); |
- CObjectArray* error = new CObjectArray(CObject::NewArray(3)); |
- error->SetAt(0, new CObjectInt32(CObject::NewInt32( |
- AsyncDirectoryListing::kListError))); |
- error->SetAt(1, request[0]); |
- error->SetAt(2, err); |
- return error; |
- } |
- // TODO(ajohnsen): Consider returning the first few results. |
- return new CObjectIntptr( |
- CObject::NewIntptr(reinterpret_cast<intptr_t>(dir_listing))); |
+ if ((request.Length() < 1) || !request[0]->IsIntptr()) { |
+ return CreateIllegalArgumentError(); |
+ } |
+ Namespace* namespc = CObjectToNamespacePointer(request[0]); |
+ RefCntReleaseScope<Namespace> rs(namespc); |
+ if ((request.Length() != 4) || !request[1]->IsString() || |
+ !request[2]->IsBool() || !request[3]->IsBool()) { |
+ return CreateIllegalArgumentError(); |
+ } |
+ CObjectString path(request[1]); |
+ CObjectBool recursive(request[2]); |
+ CObjectBool follow_links(request[3]); |
+ AsyncDirectoryListing* dir_listing = new AsyncDirectoryListing( |
+ namespc, path.CString(), recursive.Value(), follow_links.Value()); |
+ if (dir_listing->error()) { |
+ // Report error now, so we capture the correct OSError. |
+ CObject* err = CObject::NewOSError(); |
+ dir_listing->Release(); |
+ CObjectArray* error = new CObjectArray(CObject::NewArray(3)); |
+ error->SetAt(0, new CObjectInt32(CObject::NewInt32( |
+ AsyncDirectoryListing::kListError))); |
+ error->SetAt(1, request[1]); |
+ error->SetAt(2, err); |
+ return error; |
} |
- return CreateIllegalArgumentError(); |
+ // TODO(ajohnsen): Consider returning the first few results. |
+ return new CObjectIntptr( |
+ CObject::NewIntptr(reinterpret_cast<intptr_t>(dir_listing))); |
} |
CObject* Directory::ListNextRequest(const CObjectArray& request) { |
- if ((request.Length() == 1) && request[0]->IsIntptr()) { |
- CObjectIntptr ptr(request[0]); |
- AsyncDirectoryListing* dir_listing = |
- reinterpret_cast<AsyncDirectoryListing*>(ptr.Value()); |
- RefCntReleaseScope<AsyncDirectoryListing> rs(dir_listing); |
- if (dir_listing->IsEmpty()) { |
- return new CObjectArray(CObject::NewArray(0)); |
- } |
- const int kArraySize = 128; |
- CObjectArray* response = new CObjectArray(CObject::NewArray(kArraySize)); |
- dir_listing->SetArray(response, kArraySize); |
- Directory::List(dir_listing); |
- // In case the listing ended before it hit the buffer length, we need to |
- // override the array length. |
- response->AsApiCObject()->value.as_array.length = dir_listing->index(); |
- return response; |
+ if ((request.Length() != 1) || !request[0]->IsIntptr()) { |
+ return CreateIllegalArgumentError(); |
+ } |
+ CObjectIntptr ptr(request[0]); |
+ AsyncDirectoryListing* dir_listing = |
+ reinterpret_cast<AsyncDirectoryListing*>(ptr.Value()); |
+ RefCntReleaseScope<AsyncDirectoryListing> rs(dir_listing); |
+ if (dir_listing->IsEmpty()) { |
+ return new CObjectArray(CObject::NewArray(0)); |
} |
- return CreateIllegalArgumentError(); |
+ const int kArraySize = 128; |
+ CObjectArray* response = new CObjectArray(CObject::NewArray(kArraySize)); |
+ dir_listing->SetArray(response, kArraySize); |
+ Directory::List(dir_listing); |
+ // In case the listing ended before it hit the buffer length, we need to |
+ // override the array length. |
+ response->AsApiCObject()->value.as_array.length = dir_listing->index(); |
+ return response; |
} |
CObject* Directory::ListStopRequest(const CObjectArray& request) { |
- if ((request.Length() == 1) && request[0]->IsIntptr()) { |
- CObjectIntptr ptr(request[0]); |
- AsyncDirectoryListing* dir_listing = |
- reinterpret_cast<AsyncDirectoryListing*>(ptr.Value()); |
- RefCntReleaseScope<AsyncDirectoryListing> rs(dir_listing); |
- |
- // We have retained a reference to the listing here. Therefore the listing's |
- // destructor can't be running. Since no further requests are dispatched by |
- // the Dart code after an async stop call, this PopAll() can't be racing |
- // with any other call on the listing. We don't do an extra Release(), and |
- // we don't delete the weak persistent handle. The file is closed here, but |
- // the memory for the listing will be cleaned up when the finalizer runs. |
- dir_listing->PopAll(); |
- return new CObjectBool(CObject::Bool(true)); |
+ if ((request.Length() != 1) || !request[0]->IsIntptr()) { |
+ return CreateIllegalArgumentError(); |
} |
- return CreateIllegalArgumentError(); |
+ CObjectIntptr ptr(request[0]); |
+ AsyncDirectoryListing* dir_listing = |
+ reinterpret_cast<AsyncDirectoryListing*>(ptr.Value()); |
+ RefCntReleaseScope<AsyncDirectoryListing> rs(dir_listing); |
+ |
+ // We have retained a reference to the listing here. Therefore the listing's |
+ // destructor can't be running. Since no further requests are dispatched by |
+ // the Dart code after an async stop call, this PopAll() can't be racing |
+ // with any other call on the listing. We don't do an extra Release(), and |
+ // we don't delete the weak persistent handle. The file is closed here, but |
+ // the memory for the listing will be cleaned up when the finalizer runs. |
+ dir_listing->PopAll(); |
+ return new CObjectBool(CObject::Bool(true)); |
} |
CObject* Directory::RenameRequest(const CObjectArray& request) { |
- if ((request.Length() == 2) && request[0]->IsString() && |
- request[1]->IsString()) { |
- CObjectString path(request[0]); |
- CObjectString new_path(request[1]); |
- bool completed = Directory::Rename(path.CString(), new_path.CString()); |
- if (completed) { |
- return CObject::True(); |
- } |
- return CObject::NewOSError(); |
+ if ((request.Length() < 1) || !request[0]->IsIntptr()) { |
+ return CObject::IllegalArgumentError(); |
} |
- return CObject::IllegalArgumentError(); |
+ Namespace* namespc = CObjectToNamespacePointer(request[0]); |
+ RefCntReleaseScope<Namespace> rs(namespc); |
+ if ((request.Length() != 3) || !request[1]->IsString() || |
+ !request[2]->IsString()) { |
+ return CObject::IllegalArgumentError(); |
+ } |
+ CObjectString path(request[1]); |
+ CObjectString new_path(request[2]); |
+ return Directory::Rename(namespc, path.CString(), new_path.CString()) |
+ ? CObject::True() |
+ : CObject::NewOSError(); |
} |
bool AsyncDirectoryListing::AddFileSystemEntityToResponse(Response type, |
@@ -463,5 +499,13 @@ void Directory::List(DirectoryListing* listing) { |
} |
} |
+const char* Directory::Current(Namespace* namespc) { |
+ return Namespace::GetCurrent(namespc); |
+} |
+ |
+bool Directory::SetCurrent(Namespace* namespc, const char* name) { |
+ return Namespace::SetCurrent(namespc, name); |
+} |
+ |
} // namespace bin |
} // namespace dart |