|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | #include "vfs_api.h"
|
16 |
| -#include <stdio_ext.h> |
17 | 16 |
|
18 | 17 | using namespace fs;
|
19 | 18 |
|
20 |
| -#define READ_SIZE_SWITCH 128 //swithc to read func when read size > 128bytes |
| 19 | +#define DEFAULT_FILE_BUFFER_SIZE 4096 |
21 | 20 |
|
22 | 21 | FileImplPtr VFSImpl::open(const char* fpath, const char* mode, const bool create)
|
23 | 22 | {
|
@@ -283,6 +282,10 @@ VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* fpath, const char* mode)
|
283 | 282 | if(!_f) {
|
284 | 283 | log_e("fopen(%s) failed", temp);
|
285 | 284 | }
|
| 285 | + if(_f && (_stat.st_blksize == 0)) |
| 286 | + { |
| 287 | + setvbuf(_f,NULL,_IOFBF,DEFAULT_FILE_BUFFER_SIZE); |
| 288 | + } |
286 | 289 | } else if(S_ISDIR(_stat.st_mode)) {
|
287 | 290 | _isDirectory = true;
|
288 | 291 | _d = opendir(temp);
|
@@ -310,6 +313,10 @@ VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* fpath, const char* mode)
|
310 | 313 | if(!_f) {
|
311 | 314 | log_e("fopen(%s) failed", temp);
|
312 | 315 | }
|
| 316 | + if(_f && (_stat.st_blksize == 0)) |
| 317 | + { |
| 318 | + setvbuf(_f,NULL,_IOFBF,DEFAULT_FILE_BUFFER_SIZE); |
| 319 | + } |
313 | 320 | }
|
314 | 321 | }
|
315 | 322 | free(temp);
|
@@ -377,28 +384,7 @@ size_t VFSFileImpl::read(uint8_t* buf, size_t size)
|
377 | 384 | return 0;
|
378 | 385 | }
|
379 | 386 |
|
380 |
| - //ERASE BYTEBUFFER and use read when size > READ_SIZE_SWITCH always |
381 |
| - if(size > READ_SIZE_SWITCH) |
382 |
| - { |
383 |
| - //check some data in buffer exists –> clear buffer and move pointer to deleted data |
384 |
| - size_t bytesinbuf = __fpending(_f); |
385 |
| - if (bytesinbuf && (bytesinbuf != 128)) //buffer lenght is 128 bytes |
386 |
| - { |
387 |
| - fpurge(_f); |
388 |
| - lseek(fileno(_f),(-128+bytesinbuf),SEEK_CUR); |
389 |
| - } |
390 |
| - |
391 |
| - int res = ::read(fileno(_f), buf, size); |
392 |
| - if (res < 0) { |
393 |
| - // an error occurred |
394 |
| - return 0; |
395 |
| - } |
396 |
| - return res; |
397 |
| - } |
398 |
| - else |
399 |
| - { |
400 |
| - return fread(buf, 1, size, _f); |
401 |
| - } |
| 387 | + return fread(buf, 1, size, _f); |
402 | 388 | }
|
403 | 389 |
|
404 | 390 | void VFSFileImpl::flush()
|
@@ -439,6 +425,19 @@ size_t VFSFileImpl::size() const
|
439 | 425 | return _stat.st_size;
|
440 | 426 | }
|
441 | 427 |
|
| 428 | +/* |
| 429 | +* Change size of files internal buffer used for read / write operations. |
| 430 | +* Need to be called right after opening file before any other operation! |
| 431 | +*/ |
| 432 | +bool VFSFileImpl::setBufferSize(size_t size) |
| 433 | +{ |
| 434 | + if(_isDirectory || !_f) { |
| 435 | + return 0; |
| 436 | + } |
| 437 | + int res = setvbuf(_f,NULL,_IOFBF,size); |
| 438 | + return res == 0; |
| 439 | +} |
| 440 | + |
442 | 441 | const char* VFSFileImpl::path() const
|
443 | 442 | {
|
444 | 443 | return (const char*) _path;
|
|
0 commit comments