OLD | NEW |
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 #include "handlers.h" | 6 #include "handlers.h" |
7 | 7 |
8 #include <assert.h> | 8 #include <assert.h> |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <stdlib.h> | 10 #include <stdlib.h> |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 data_len = strlen(data); | 157 data_len = strlen(data); |
158 | 158 |
159 if (!file) { | 159 if (!file) { |
160 *output = PrintfToNewString("Error: Unknown file handle %s.", | 160 *output = PrintfToNewString("Error: Unknown file handle %s.", |
161 file_index_string); | 161 file_index_string); |
162 return 2; | 162 return 2; |
163 } | 163 } |
164 | 164 |
165 bytes_written = fwrite(data, 1, data_len, file); | 165 bytes_written = fwrite(data, 1, data_len, file); |
166 | 166 |
| 167 if (ferror(file)) { |
| 168 *output = PrintfToNewString( |
| 169 "Error: Wrote %d bytes, but ferror() returns true.", bytes_written); |
| 170 return 3; |
| 171 } |
| 172 |
167 *output = PrintfToNewString("fwrite\1%s\1%d", file_index_string, | 173 *output = PrintfToNewString("fwrite\1%s\1%d", file_index_string, |
168 bytes_written); | 174 bytes_written); |
169 return 0; | 175 return 0; |
170 } | 176 } |
171 | 177 |
172 /** | 178 /** |
173 * Handle a call to fread() made by JavaScript. | 179 * Handle a call to fread() made by JavaScript. |
174 * | 180 * |
175 * fread expects 2 parameters: | 181 * fread expects 2 parameters: |
176 * 0: The index of the file (which is mapped to a FILE*) | 182 * 0: The index of the file (which is mapped to a FILE*) |
(...skipping 27 matching lines...) Expand all Loading... |
204 if (!file) { | 210 if (!file) { |
205 *output = PrintfToNewString("Error: Unknown file handle %s.", | 211 *output = PrintfToNewString("Error: Unknown file handle %s.", |
206 file_index_string); | 212 file_index_string); |
207 return 2; | 213 return 2; |
208 } | 214 } |
209 | 215 |
210 buffer = (char*)malloc(data_len + 1); | 216 buffer = (char*)malloc(data_len + 1); |
211 bytes_read = fread(buffer, 1, data_len, file); | 217 bytes_read = fread(buffer, 1, data_len, file); |
212 buffer[bytes_read] = 0; | 218 buffer[bytes_read] = 0; |
213 | 219 |
| 220 if (ferror(file)) { |
| 221 *output = PrintfToNewString( |
| 222 "Error: Read %d bytes, but ferror() returns true.", bytes_read); |
| 223 return 3; |
| 224 } |
| 225 |
214 *output = PrintfToNewString("fread\1%s\1%s", file_index_string, buffer); | 226 *output = PrintfToNewString("fread\1%s\1%s", file_index_string, buffer); |
215 free(buffer); | 227 free(buffer); |
216 return 0; | 228 return 0; |
217 } | 229 } |
218 | 230 |
219 /** | 231 /** |
220 * Handle a call to fseek() made by JavaScript. | 232 * Handle a call to fseek() made by JavaScript. |
221 * | 233 * |
222 * fseek expects 3 parameters: | 234 * fseek expects 3 parameters: |
223 * 0: The index of the file (which is mapped to a FILE*) | 235 * 0: The index of the file (which is mapped to a FILE*) |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 if (result) { | 325 if (result) { |
314 *output = PrintfToNewString("Error: fclose returned error %d.", result); | 326 *output = PrintfToNewString("Error: fclose returned error %d.", result); |
315 return 3; | 327 return 3; |
316 } | 328 } |
317 | 329 |
318 RemoveFileFromMap(file_index); | 330 RemoveFileFromMap(file_index); |
319 | 331 |
320 *output = PrintfToNewString("fclose\1%s", file_index_string); | 332 *output = PrintfToNewString("fclose\1%s", file_index_string); |
321 return 0; | 333 return 0; |
322 } | 334 } |
OLD | NEW |