Skip to content

Commit 7b89b39

Browse files
authored
Edited VFSFileImpl::read to use both read/fread (#6456)
* Edited VFSFileImpl::read to use both read/fread * Added missing include
1 parent 905f8f2 commit 7b89b39

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

Diff for: libraries/FS/src/vfs_api.cpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
// limitations under the License.
1414

1515
#include "vfs_api.h"
16+
#include <stdio_ext.h>
1617

1718
using namespace fs;
1819

20+
#define READ_SIZE_SWITCH 128 //swithc to read func when read size > 128bytes
21+
1922
FileImplPtr VFSImpl::open(const char* fpath, const char* mode, const bool create)
2023
{
2124
if(!_mountpoint) {
@@ -374,7 +377,28 @@ size_t VFSFileImpl::read(uint8_t* buf, size_t size)
374377
return 0;
375378
}
376379

377-
return fread(buf, 1, size, _f);
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+
}
378402
}
379403

380404
void VFSFileImpl::flush()

0 commit comments

Comments
 (0)