Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(570)

Side by Side Diff: runtime/bin/file.cc

Issue 1015763002: Make sure File_WriteFrom write all data unless an error occour (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "bin/file.h" 5 #include "bin/file.h"
6 6
7 #include "bin/builtin.h" 7 #include "bin/builtin.h"
8 #include "bin/dartutils.h" 8 #include "bin/dartutils.h"
9 #include "bin/io_buffer.h" 9 #include "bin/io_buffer.h"
10 #include "bin/utils.h" 10 #include "bin/utils.h"
(...skipping 27 matching lines...) Expand all
38 current_buffer += bytes_read; // Move the buffer forward. 38 current_buffer += bytes_read; // Move the buffer forward.
39 } 39 }
40 return true; 40 return true;
41 } 41 }
42 42
43 43
44 bool File::WriteFully(const void* buffer, int64_t num_bytes) { 44 bool File::WriteFully(const void* buffer, int64_t num_bytes) {
45 int64_t remaining = num_bytes; 45 int64_t remaining = num_bytes;
46 const char* current_buffer = reinterpret_cast<const char*>(buffer); 46 const char* current_buffer = reinterpret_cast<const char*>(buffer);
47 while (remaining > 0) { 47 while (remaining > 0) {
48 int64_t bytes_read = Write(current_buffer, remaining); 48 int64_t bytes_written = Write(current_buffer, remaining);
49 if (bytes_read < 0) { 49 if (bytes_written < 0) {
50 return false; 50 return false;
51 } 51 }
52 remaining -= bytes_read; // Reduce the number of remaining bytes. 52 remaining -= bytes_written; // Reduce the number of remaining bytes.
53 current_buffer += bytes_read; // Move the buffer forward. 53 current_buffer += bytes_written; // Move the buffer forward.
54 } 54 }
55 return true; 55 return true;
56 } 56 }
57 57
58 58
59 File::FileOpenMode File::DartModeToFileMode(DartFileOpenMode mode) { 59 File::FileOpenMode File::DartModeToFileMode(DartFileOpenMode mode) {
60 ASSERT(mode == File::kDartRead || 60 ASSERT(mode == File::kDartRead ||
61 mode == File::kDartWrite || 61 mode == File::kDartWrite ||
62 mode == File::kDartAppend); 62 mode == File::kDartAppend);
63 if (mode == File::kDartWrite) { 63 if (mode == File::kDartWrite) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 } 133 }
134 } 134 }
135 135
136 136
137 void FUNCTION_NAME(File_WriteByte)(Dart_NativeArguments args) { 137 void FUNCTION_NAME(File_WriteByte)(Dart_NativeArguments args) {
138 File* file = GetFilePointer(Dart_GetNativeArgument(args, 0)); 138 File* file = GetFilePointer(Dart_GetNativeArgument(args, 0));
139 ASSERT(file != NULL); 139 ASSERT(file != NULL);
140 int64_t byte = 0; 140 int64_t byte = 0;
141 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &byte)) { 141 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &byte)) {
142 uint8_t buffer = static_cast<uint8_t>(byte & 0xff); 142 uint8_t buffer = static_cast<uint8_t>(byte & 0xff);
143 int64_t bytes_written = file->Write(reinterpret_cast<void*>(&buffer), 1); 143 bool success = file->WriteFully(reinterpret_cast<void*>(&buffer), 1);
144 if (bytes_written >= 0) { 144 if (success) {
145 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); 145 Dart_SetReturnValue(args, Dart_NewInteger(1));
146 } else { 146 } else {
147 Dart_Handle err = DartUtils::NewDartOSError(); 147 Dart_Handle err = DartUtils::NewDartOSError();
148 if (Dart_IsError(err)) Dart_PropagateError(err); 148 if (Dart_IsError(err)) Dart_PropagateError(err);
149 Dart_SetReturnValue(args, err); 149 Dart_SetReturnValue(args, err);
150 } 150 }
151 } else { 151 } else {
152 OSError os_error(-1, "Invalid argument", OSError::kUnknown); 152 OSError os_error(-1, "Invalid argument", OSError::kUnknown);
153 Dart_Handle err = DartUtils::NewDartOSError(&os_error); 153 Dart_Handle err = DartUtils::NewDartOSError(&os_error);
154 if (Dart_IsError(err)) Dart_PropagateError(err); 154 if (Dart_IsError(err)) Dart_PropagateError(err);
155 Dart_SetReturnValue(args, err); 155 Dart_SetReturnValue(args, err);
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 intptr_t buffer_len = 0; 259 intptr_t buffer_len = 0;
260 void* buffer = NULL; 260 void* buffer = NULL;
261 Dart_Handle result = 261 Dart_Handle result =
262 Dart_TypedDataAcquireData(buffer_obj, &type, &buffer, &buffer_len); 262 Dart_TypedDataAcquireData(buffer_obj, &type, &buffer, &buffer_len);
263 if (Dart_IsError(result)) Dart_PropagateError(result); 263 if (Dart_IsError(result)) Dart_PropagateError(result);
264 264
265 ASSERT(type == Dart_TypedData_kUint8 || type == Dart_TypedData_kInt8); 265 ASSERT(type == Dart_TypedData_kUint8 || type == Dart_TypedData_kInt8);
266 ASSERT(end <= buffer_len); 266 ASSERT(end <= buffer_len);
267 ASSERT(buffer != NULL); 267 ASSERT(buffer != NULL);
268 268
269 // Write the data out into the file. 269 // Write all the data out into the file.
270 int64_t bytes_written = file->Write(buffer, length); 270 bool success = file->WriteFully(buffer, length);
271
271 // Release the direct pointer acquired above. 272 // Release the direct pointer acquired above.
272 result = Dart_TypedDataReleaseData(buffer_obj); 273 result = Dart_TypedDataReleaseData(buffer_obj);
273 if (Dart_IsError(result)) Dart_PropagateError(result); 274 if (Dart_IsError(result)) Dart_PropagateError(result);
274 275
275 if (bytes_written != length) { 276 if (!success) {
276 Dart_Handle err = DartUtils::NewDartOSError(); 277 Dart_Handle err = DartUtils::NewDartOSError();
277 if (Dart_IsError(err)) Dart_PropagateError(err); 278 if (Dart_IsError(err)) Dart_PropagateError(err);
278 Dart_SetReturnValue(args, err); 279 Dart_SetReturnValue(args, err);
280 } else {
281 Dart_SetReturnValue(args, Dart_Null());
279 } 282 }
280 } 283 }
281 284
282 285
283 void FUNCTION_NAME(File_Position)(Dart_NativeArguments args) { 286 void FUNCTION_NAME(File_Position)(Dart_NativeArguments args) {
284 File* file = GetFilePointer(Dart_GetNativeArgument(args, 0)); 287 File* file = GetFilePointer(Dart_GetNativeArgument(args, 0));
285 ASSERT(file != NULL); 288 ASSERT(file != NULL);
286 intptr_t return_value = file->Position(); 289 intptr_t return_value = file->Position();
287 if (return_value >= 0) { 290 if (return_value >= 0) {
288 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); 291 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
(...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after
948 951
949 CObject* File::WriteByteRequest(const CObjectArray& request) { 952 CObject* File::WriteByteRequest(const CObjectArray& request) {
950 if (request.Length() == 2 && 953 if (request.Length() == 2 &&
951 request[0]->IsIntptr() && 954 request[0]->IsIntptr() &&
952 request[1]->IsInt32OrInt64()) { 955 request[1]->IsInt32OrInt64()) {
953 File* file = CObjectToFilePointer(request[0]); 956 File* file = CObjectToFilePointer(request[0]);
954 ASSERT(file != NULL); 957 ASSERT(file != NULL);
955 if (!file->IsClosed()) { 958 if (!file->IsClosed()) {
956 int64_t byte = CObjectInt32OrInt64ToInt64(request[1]); 959 int64_t byte = CObjectInt32OrInt64ToInt64(request[1]);
957 uint8_t buffer = static_cast<uint8_t>(byte & 0xff); 960 uint8_t buffer = static_cast<uint8_t>(byte & 0xff);
958 int64_t bytes_written = file->Write(reinterpret_cast<void*>(&buffer), 1); 961 bool success = file->WriteFully(reinterpret_cast<void*>(&buffer), 1);
959 if (bytes_written > 0) { 962 if (success) {
960 return new CObjectInt64(CObject::NewInt64(bytes_written)); 963 return new CObjectInt64(CObject::NewInt64(1));
961 } else { 964 } else {
962 return CObject::NewOSError(); 965 return CObject::NewOSError();
963 } 966 }
964 } else { 967 } else {
965 return CObject::FileClosedError(); 968 return CObject::FileClosedError();
966 } 969 }
967 } 970 }
968 return CObject::IllegalArgumentError(); 971 return CObject::IllegalArgumentError();
969 } 972 }
970 973
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
1085 int64_t value = CObjectInt32OrInt64ToInt64(array[i + start]); 1088 int64_t value = CObjectInt32OrInt64ToInt64(array[i + start]);
1086 buffer_start[i] = static_cast<uint8_t>(value & 0xFF); 1089 buffer_start[i] = static_cast<uint8_t>(value & 0xFF);
1087 } else { 1090 } else {
1088 // Unsupported type. 1091 // Unsupported type.
1089 delete[] buffer_start; 1092 delete[] buffer_start;
1090 return CObject::IllegalArgumentError(); 1093 return CObject::IllegalArgumentError();
1091 } 1094 }
1092 } 1095 }
1093 start = 0; 1096 start = 0;
1094 } 1097 }
1095 int64_t bytes_written = 1098 bool success =
1096 file->Write(reinterpret_cast<void*>(buffer_start), length); 1099 file->WriteFully(reinterpret_cast<void*>(buffer_start), length);
1097 if (!request[1]->IsTypedData()) { 1100 if (!request[1]->IsTypedData()) {
1098 delete[] buffer_start; 1101 delete[] buffer_start;
1099 } 1102 }
1100 if (bytes_written >= 0) { 1103 if (success) {
1101 return new CObjectInt64(CObject::NewInt64(bytes_written)); 1104 return new CObjectInt64(CObject::NewInt64(length));
1102 } else { 1105 } else {
1103 return CObject::NewOSError(); 1106 return CObject::NewOSError();
1104 } 1107 }
1105 } else { 1108 } else {
1106 return CObject::FileClosedError(); 1109 return CObject::FileClosedError();
1107 } 1110 }
1108 } 1111 }
1109 return CObject::IllegalArgumentError(); 1112 return CObject::IllegalArgumentError();
1110 } 1113 }
1111 1114
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
1245 } 1248 }
1246 } else { 1249 } else {
1247 return CObject::FileClosedError(); 1250 return CObject::FileClosedError();
1248 } 1251 }
1249 } 1252 }
1250 return CObject::IllegalArgumentError(); 1253 return CObject::IllegalArgumentError();
1251 } 1254 }
1252 1255
1253 } // namespace bin 1256 } // namespace bin
1254 } // namespace dart 1257 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698