| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
| 6 | 6 |
| 7 #include "bin/file.h" | 7 #include "bin/file.h" |
| 8 | 8 |
| 9 #include "bin/builtin.h" | 9 #include "bin/builtin.h" |
| 10 #include "bin/dartutils.h" | 10 #include "bin/dartutils.h" |
| (...skipping 770 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 781 } | 781 } |
| 782 } else { | 782 } else { |
| 783 return CObject::FileClosedError(); | 783 return CObject::FileClosedError(); |
| 784 } | 784 } |
| 785 } | 785 } |
| 786 return CObject::IllegalArgumentError(); | 786 return CObject::IllegalArgumentError(); |
| 787 } | 787 } |
| 788 | 788 |
| 789 | 789 |
| 790 CObject* File::SetPositionRequest(const CObjectArray& request) { | 790 CObject* File::SetPositionRequest(const CObjectArray& request) { |
| 791 if ((request.Length() == 2) && | 791 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
| 792 request[0]->IsIntptr() && | |
| 793 request[1]->IsInt32OrInt64()) { | |
| 794 File* file = CObjectToFilePointer(request[0]); | 792 File* file = CObjectToFilePointer(request[0]); |
| 795 RefCntReleaseScope<File> rs(file); | 793 RefCntReleaseScope<File> rs(file); |
| 796 if (!file->IsClosed()) { | 794 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { |
| 797 int64_t position = CObjectInt32OrInt64ToInt64(request[1]); | 795 if (!file->IsClosed()) { |
| 798 if (file->SetPosition(position)) { | 796 int64_t position = CObjectInt32OrInt64ToInt64(request[1]); |
| 799 return CObject::True(); | 797 if (file->SetPosition(position)) { |
| 798 return CObject::True(); |
| 799 } else { |
| 800 return CObject::NewOSError(); |
| 801 } |
| 800 } else { | 802 } else { |
| 801 return CObject::NewOSError(); | 803 return CObject::FileClosedError(); |
| 802 } | 804 } |
| 803 } else { | 805 } else { |
| 804 return CObject::FileClosedError(); | 806 return CObject::IllegalArgumentError(); |
| 805 } | 807 } |
| 806 } | 808 } |
| 807 return CObject::IllegalArgumentError(); | 809 return CObject::IllegalArgumentError(); |
| 808 } | 810 } |
| 809 | 811 |
| 810 | 812 |
| 811 CObject* File::TruncateRequest(const CObjectArray& request) { | 813 CObject* File::TruncateRequest(const CObjectArray& request) { |
| 812 if ((request.Length() == 2) && | 814 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
| 813 request[0]->IsIntptr() && | |
| 814 request[1]->IsInt32OrInt64()) { | |
| 815 File* file = CObjectToFilePointer(request[0]); | 815 File* file = CObjectToFilePointer(request[0]); |
| 816 RefCntReleaseScope<File> rs(file); | 816 RefCntReleaseScope<File> rs(file); |
| 817 if (!file->IsClosed()) { | 817 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { |
| 818 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); | 818 if (!file->IsClosed()) { |
| 819 if (file->Truncate(length)) { | 819 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); |
| 820 return CObject::True(); | 820 if (file->Truncate(length)) { |
| 821 return CObject::True(); |
| 822 } else { |
| 823 return CObject::NewOSError(); |
| 824 } |
| 821 } else { | 825 } else { |
| 822 return CObject::NewOSError(); | 826 return CObject::FileClosedError(); |
| 823 } | 827 } |
| 824 } else { | 828 } else { |
| 825 return CObject::FileClosedError(); | 829 return CObject::IllegalArgumentError(); |
| 826 } | 830 } |
| 827 } | 831 } |
| 828 return CObject::IllegalArgumentError(); | 832 return CObject::IllegalArgumentError(); |
| 829 } | 833 } |
| 830 | 834 |
| 831 | 835 |
| 832 CObject* File::LengthRequest(const CObjectArray& request) { | 836 CObject* File::LengthRequest(const CObjectArray& request) { |
| 833 if ((request.Length() == 1) && request[0]->IsIntptr()) { | 837 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
| 834 File* file = CObjectToFilePointer(request[0]); | 838 File* file = CObjectToFilePointer(request[0]); |
| 835 RefCntReleaseScope<File> rs(file); | 839 RefCntReleaseScope<File> rs(file); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 910 } | 914 } |
| 911 } else { | 915 } else { |
| 912 return CObject::FileClosedError(); | 916 return CObject::FileClosedError(); |
| 913 } | 917 } |
| 914 } | 918 } |
| 915 return CObject::IllegalArgumentError(); | 919 return CObject::IllegalArgumentError(); |
| 916 } | 920 } |
| 917 | 921 |
| 918 | 922 |
| 919 CObject* File::WriteByteRequest(const CObjectArray& request) { | 923 CObject* File::WriteByteRequest(const CObjectArray& request) { |
| 920 if ((request.Length() == 2) && | 924 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
| 921 request[0]->IsIntptr() && | |
| 922 request[1]->IsInt32OrInt64()) { | |
| 923 File* file = CObjectToFilePointer(request[0]); | 925 File* file = CObjectToFilePointer(request[0]); |
| 924 RefCntReleaseScope<File> rs(file); | 926 RefCntReleaseScope<File> rs(file); |
| 925 if (!file->IsClosed()) { | 927 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { |
| 926 int64_t byte = CObjectInt32OrInt64ToInt64(request[1]); | 928 if (!file->IsClosed()) { |
| 927 uint8_t buffer = static_cast<uint8_t>(byte & 0xff); | 929 int64_t byte = CObjectInt32OrInt64ToInt64(request[1]); |
| 928 bool success = file->WriteFully(reinterpret_cast<void*>(&buffer), 1); | 930 uint8_t buffer = static_cast<uint8_t>(byte & 0xff); |
| 929 if (success) { | 931 bool success = file->WriteFully(reinterpret_cast<void*>(&buffer), 1); |
| 930 return new CObjectInt64(CObject::NewInt64(1)); | 932 if (success) { |
| 933 return new CObjectInt64(CObject::NewInt64(1)); |
| 934 } else { |
| 935 return CObject::NewOSError(); |
| 936 } |
| 931 } else { | 937 } else { |
| 932 return CObject::NewOSError(); | 938 return CObject::FileClosedError(); |
| 933 } | 939 } |
| 934 } else { | 940 } else { |
| 935 return CObject::FileClosedError(); | 941 return CObject::IllegalArgumentError(); |
| 936 } | 942 } |
| 937 } | 943 } |
| 938 return CObject::IllegalArgumentError(); | 944 return CObject::IllegalArgumentError(); |
| 939 } | 945 } |
| 940 | 946 |
| 941 | 947 |
| 942 CObject* File::ReadRequest(const CObjectArray& request) { | 948 CObject* File::ReadRequest(const CObjectArray& request) { |
| 943 if ((request.Length() == 2) && | 949 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
| 944 request[0]->IsIntptr() && | |
| 945 request[1]->IsInt32OrInt64()) { | |
| 946 File* file = CObjectToFilePointer(request[0]); | 950 File* file = CObjectToFilePointer(request[0]); |
| 947 RefCntReleaseScope<File> rs(file); | 951 RefCntReleaseScope<File> rs(file); |
| 948 if (!file->IsClosed()) { | 952 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { |
| 949 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); | 953 if (!file->IsClosed()) { |
| 950 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); | 954 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); |
| 951 ASSERT(io_buffer != NULL); | 955 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); |
| 952 uint8_t* data = io_buffer->value.as_external_typed_data.data; | 956 ASSERT(io_buffer != NULL); |
| 953 int64_t bytes_read = file->Read(data, length); | 957 uint8_t* data = io_buffer->value.as_external_typed_data.data; |
| 954 if (bytes_read >= 0) { | 958 int64_t bytes_read = file->Read(data, length); |
| 955 CObjectExternalUint8Array* external_array = | 959 if (bytes_read >= 0) { |
| 956 new CObjectExternalUint8Array(io_buffer); | 960 CObjectExternalUint8Array* external_array = |
| 957 external_array->SetLength(bytes_read); | 961 new CObjectExternalUint8Array(io_buffer); |
| 958 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); | 962 external_array->SetLength(bytes_read); |
| 959 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); | 963 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); |
| 960 result->SetAt(1, external_array); | 964 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); |
| 961 return result; | 965 result->SetAt(1, external_array); |
| 966 return result; |
| 967 } else { |
| 968 CObject::FreeIOBufferData(io_buffer); |
| 969 return CObject::NewOSError(); |
| 970 } |
| 962 } else { | 971 } else { |
| 963 CObject::FreeIOBufferData(io_buffer); | 972 return CObject::FileClosedError(); |
| 964 return CObject::NewOSError(); | |
| 965 } | 973 } |
| 966 } else { | 974 } else { |
| 967 return CObject::FileClosedError(); | 975 return CObject::IllegalArgumentError(); |
| 968 } | 976 } |
| 969 } | 977 } |
| 970 return CObject::IllegalArgumentError(); | 978 return CObject::IllegalArgumentError(); |
| 971 } | 979 } |
| 972 | 980 |
| 973 | 981 |
| 974 CObject* File::ReadIntoRequest(const CObjectArray& request) { | 982 CObject* File::ReadIntoRequest(const CObjectArray& request) { |
| 975 if ((request.Length() == 2) && | 983 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
| 976 request[0]->IsIntptr() && | |
| 977 request[1]->IsInt32OrInt64()) { | |
| 978 File* file = CObjectToFilePointer(request[0]); | 984 File* file = CObjectToFilePointer(request[0]); |
| 979 RefCntReleaseScope<File> rs(file); | 985 RefCntReleaseScope<File> rs(file); |
| 980 if (!file->IsClosed()) { | 986 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { |
| 981 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); | 987 if (!file->IsClosed()) { |
| 982 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); | 988 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); |
| 983 ASSERT(io_buffer != NULL); | 989 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); |
| 984 uint8_t* data = io_buffer->value.as_external_typed_data.data; | 990 ASSERT(io_buffer != NULL); |
| 985 int64_t bytes_read = file->Read(data, length); | 991 uint8_t* data = io_buffer->value.as_external_typed_data.data; |
| 986 if (bytes_read >= 0) { | 992 int64_t bytes_read = file->Read(data, length); |
| 987 CObjectExternalUint8Array* external_array = | 993 if (bytes_read >= 0) { |
| 988 new CObjectExternalUint8Array(io_buffer); | 994 CObjectExternalUint8Array* external_array = |
| 989 external_array->SetLength(bytes_read); | 995 new CObjectExternalUint8Array(io_buffer); |
| 990 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 996 external_array->SetLength(bytes_read); |
| 991 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); | 997 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 992 result->SetAt(1, new CObjectInt64(CObject::NewInt64(bytes_read))); | 998 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); |
| 993 result->SetAt(2, external_array); | 999 result->SetAt(1, new CObjectInt64(CObject::NewInt64(bytes_read))); |
| 994 return result; | 1000 result->SetAt(2, external_array); |
| 1001 return result; |
| 1002 } else { |
| 1003 CObject::FreeIOBufferData(io_buffer); |
| 1004 return CObject::NewOSError(); |
| 1005 } |
| 995 } else { | 1006 } else { |
| 996 CObject::FreeIOBufferData(io_buffer); | 1007 return CObject::FileClosedError(); |
| 997 return CObject::NewOSError(); | |
| 998 } | 1008 } |
| 999 } else { | 1009 } else { |
| 1000 return CObject::FileClosedError(); | 1010 return CObject::IllegalArgumentError(); |
| 1001 } | 1011 } |
| 1002 } | 1012 } |
| 1003 return CObject::IllegalArgumentError(); | 1013 return CObject::IllegalArgumentError(); |
| 1004 } | 1014 } |
| 1005 | 1015 |
| 1006 | 1016 |
| 1007 static int SizeInBytes(Dart_TypedData_Type type) { | 1017 static int SizeInBytes(Dart_TypedData_Type type) { |
| 1008 switch (type) { | 1018 switch (type) { |
| 1009 case Dart_TypedData_kInt8: | 1019 case Dart_TypedData_kInt8: |
| 1010 case Dart_TypedData_kUint8: | 1020 case Dart_TypedData_kUint8: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1023 return 8; | 1033 return 8; |
| 1024 default: | 1034 default: |
| 1025 break; | 1035 break; |
| 1026 } | 1036 } |
| 1027 UNREACHABLE(); | 1037 UNREACHABLE(); |
| 1028 return -1; | 1038 return -1; |
| 1029 } | 1039 } |
| 1030 | 1040 |
| 1031 | 1041 |
| 1032 CObject* File::WriteFromRequest(const CObjectArray& request) { | 1042 CObject* File::WriteFromRequest(const CObjectArray& request) { |
| 1033 if ((request.Length() == 4) && | 1043 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
| 1034 request[0]->IsIntptr() && | |
| 1035 (request[1]->IsTypedData() || request[1]->IsArray()) && | |
| 1036 request[2]->IsInt32OrInt64() && | |
| 1037 request[3]->IsInt32OrInt64()) { | |
| 1038 File* file = CObjectToFilePointer(request[0]); | 1044 File* file = CObjectToFilePointer(request[0]); |
| 1039 RefCntReleaseScope<File> rs(file); | 1045 RefCntReleaseScope<File> rs(file); |
| 1040 if (!file->IsClosed()) { | 1046 if ((request.Length() == 4) && |
| 1041 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); | 1047 (request[1]->IsTypedData() || request[1]->IsArray()) && |
| 1042 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); | 1048 request[2]->IsInt32OrInt64() && |
| 1043 int64_t length = end - start; | 1049 request[3]->IsInt32OrInt64()) { |
| 1044 uint8_t* buffer_start; | 1050 if (!file->IsClosed()) { |
| 1045 if (request[1]->IsTypedData()) { | 1051 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); |
| 1046 CObjectTypedData typed_data(request[1]); | 1052 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); |
| 1047 start = start * SizeInBytes(typed_data.Type()); | 1053 int64_t length = end - start; |
| 1048 length = length * SizeInBytes(typed_data.Type()); | 1054 uint8_t* buffer_start; |
| 1049 buffer_start = typed_data.Buffer() + start; | 1055 if (request[1]->IsTypedData()) { |
| 1056 CObjectTypedData typed_data(request[1]); |
| 1057 start = start * SizeInBytes(typed_data.Type()); |
| 1058 length = length * SizeInBytes(typed_data.Type()); |
| 1059 buffer_start = typed_data.Buffer() + start; |
| 1060 } else { |
| 1061 CObjectArray array(request[1]); |
| 1062 buffer_start = Dart_ScopeAllocate(length); |
| 1063 for (int i = 0; i < length; i++) { |
| 1064 if (array[i + start]->IsInt32OrInt64()) { |
| 1065 int64_t value = CObjectInt32OrInt64ToInt64(array[i + start]); |
| 1066 buffer_start[i] = static_cast<uint8_t>(value & 0xFF); |
| 1067 } else { |
| 1068 // Unsupported type. |
| 1069 return CObject::IllegalArgumentError(); |
| 1070 } |
| 1071 } |
| 1072 start = 0; |
| 1073 } |
| 1074 bool success = |
| 1075 file->WriteFully(reinterpret_cast<void*>(buffer_start), length); |
| 1076 if (success) { |
| 1077 return new CObjectInt64(CObject::NewInt64(length)); |
| 1078 } else { |
| 1079 return CObject::NewOSError(); |
| 1080 } |
| 1050 } else { | 1081 } else { |
| 1051 CObjectArray array(request[1]); | 1082 return CObject::FileClosedError(); |
| 1052 buffer_start = Dart_ScopeAllocate(length); | |
| 1053 for (int i = 0; i < length; i++) { | |
| 1054 if (array[i + start]->IsInt32OrInt64()) { | |
| 1055 int64_t value = CObjectInt32OrInt64ToInt64(array[i + start]); | |
| 1056 buffer_start[i] = static_cast<uint8_t>(value & 0xFF); | |
| 1057 } else { | |
| 1058 // Unsupported type. | |
| 1059 return CObject::IllegalArgumentError(); | |
| 1060 } | |
| 1061 } | |
| 1062 start = 0; | |
| 1063 } | |
| 1064 bool success = | |
| 1065 file->WriteFully(reinterpret_cast<void*>(buffer_start), length); | |
| 1066 if (success) { | |
| 1067 return new CObjectInt64(CObject::NewInt64(length)); | |
| 1068 } else { | |
| 1069 return CObject::NewOSError(); | |
| 1070 } | 1083 } |
| 1071 } else { | 1084 } else { |
| 1072 return CObject::FileClosedError(); | 1085 return CObject::IllegalArgumentError(); |
| 1073 } | 1086 } |
| 1074 } | 1087 } |
| 1075 return CObject::IllegalArgumentError(); | 1088 return CObject::IllegalArgumentError(); |
| 1076 } | 1089 } |
| 1077 | 1090 |
| 1078 | 1091 |
| 1079 CObject* File::CreateLinkRequest(const CObjectArray& request) { | 1092 CObject* File::CreateLinkRequest(const CObjectArray& request) { |
| 1080 if ((request.Length() != 2) || | 1093 if ((request.Length() != 2) || |
| 1081 !request[0]->IsString() || | 1094 !request[0]->IsString() || |
| 1082 !request[1]->IsString()) { | 1095 !request[1]->IsString()) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1186 CObjectArray* wrapper = new CObjectArray(CObject::NewArray(2)); | 1199 CObjectArray* wrapper = new CObjectArray(CObject::NewArray(2)); |
| 1187 wrapper->SetAt(0, new CObjectInt32(CObject::NewInt32(CObject::kSuccess))); | 1200 wrapper->SetAt(0, new CObjectInt32(CObject::NewInt32(CObject::kSuccess))); |
| 1188 wrapper->SetAt(1, result); | 1201 wrapper->SetAt(1, result); |
| 1189 return wrapper; | 1202 return wrapper; |
| 1190 } | 1203 } |
| 1191 return CObject::IllegalArgumentError(); | 1204 return CObject::IllegalArgumentError(); |
| 1192 } | 1205 } |
| 1193 | 1206 |
| 1194 | 1207 |
| 1195 CObject* File::LockRequest(const CObjectArray& request) { | 1208 CObject* File::LockRequest(const CObjectArray& request) { |
| 1196 if ((request.Length() == 4) && | 1209 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
| 1197 request[0]->IsIntptr() && | |
| 1198 request[1]->IsInt32OrInt64() && | |
| 1199 request[2]->IsInt32OrInt64() && | |
| 1200 request[3]->IsInt32OrInt64()) { | |
| 1201 File* file = CObjectToFilePointer(request[0]); | 1210 File* file = CObjectToFilePointer(request[0]); |
| 1202 RefCntReleaseScope<File> rs(file); | 1211 RefCntReleaseScope<File> rs(file); |
| 1203 if (!file->IsClosed()) { | 1212 if ((request.Length() == 4) && |
| 1204 int64_t lock = CObjectInt32OrInt64ToInt64(request[1]); | 1213 request[1]->IsInt32OrInt64() && |
| 1205 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); | 1214 request[2]->IsInt32OrInt64() && |
| 1206 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); | 1215 request[3]->IsInt32OrInt64()) { |
| 1207 if (file->Lock(static_cast<File::LockType>(lock), start, end)) { | 1216 if (!file->IsClosed()) { |
| 1208 return CObject::True(); | 1217 int64_t lock = CObjectInt32OrInt64ToInt64(request[1]); |
| 1218 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); |
| 1219 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); |
| 1220 if (file->Lock(static_cast<File::LockType>(lock), start, end)) { |
| 1221 return CObject::True(); |
| 1222 } else { |
| 1223 return CObject::NewOSError(); |
| 1224 } |
| 1209 } else { | 1225 } else { |
| 1210 return CObject::NewOSError(); | 1226 return CObject::FileClosedError(); |
| 1211 } | 1227 } |
| 1212 } else { | 1228 } else { |
| 1213 return CObject::FileClosedError(); | 1229 return CObject::IllegalArgumentError(); |
| 1214 } | 1230 } |
| 1215 } | 1231 } |
| 1216 return CObject::IllegalArgumentError(); | 1232 return CObject::IllegalArgumentError(); |
| 1217 } | 1233 } |
| 1218 | 1234 |
| 1219 } // namespace bin | 1235 } // namespace bin |
| 1220 } // namespace dart | 1236 } // namespace dart |
| 1221 | 1237 |
| 1222 #endif // !defined(DART_IO_DISABLED) | 1238 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |