Skip to content

Commit 757479c

Browse files
committed
Added default file buffer size + function to change it by user
1 parent 4cf8881 commit 757479c

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ size_t File::size() const
130130
return _p->size();
131131
}
132132

133+
bool File::setBufferSize(size_t size)
134+
{
135+
if (!*this) {
136+
return 0;
137+
}
138+
139+
return _p->setBufferSize(size);
140+
}
141+
133142
void File::close()
134143
{
135144
if (_p) {

Diff for: libraries/FS/src/FS.h

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class File : public Stream
7070
}
7171
size_t position() const;
7272
size_t size() const;
73+
bool setBufferSize(size_t size);
7374
void close();
7475
operator bool() const;
7576
time_t getLastWrite();

Diff for: libraries/FS/src/FSImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class FileImpl
3636
virtual bool seek(uint32_t pos, SeekMode mode) = 0;
3737
virtual size_t position() const = 0;
3838
virtual size_t size() const = 0;
39+
virtual bool setBufferSize(size_t size) = 0;
3940
virtual void close() = 0;
4041
virtual time_t getLastWrite() = 0;
4142
virtual const char* path() const = 0;

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

+23
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
using namespace fs;
1818

19+
#define DEFAULT_FILE_BUFFER_SIZE 4096
20+
1921
FileImplPtr VFSImpl::open(const char* fpath, const char* mode, const bool create)
2022
{
2123
if(!_mountpoint) {
@@ -280,6 +282,10 @@ VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* fpath, const char* mode)
280282
if(!_f) {
281283
log_e("fopen(%s) failed", temp);
282284
}
285+
if(_f && (_stat.st_blksize == 0))
286+
{
287+
setvbuf(_f,NULL,_IOFBF,DEFAULT_FILE_BUFFER_SIZE);
288+
}
283289
} else if(S_ISDIR(_stat.st_mode)) {
284290
_isDirectory = true;
285291
_d = opendir(temp);
@@ -307,6 +313,10 @@ VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* fpath, const char* mode)
307313
if(!_f) {
308314
log_e("fopen(%s) failed", temp);
309315
}
316+
if(_f && (_stat.st_blksize == 0))
317+
{
318+
setvbuf(_f,NULL,_IOFBF,DEFAULT_FILE_BUFFER_SIZE);
319+
}
310320
}
311321
}
312322
free(temp);
@@ -415,6 +425,19 @@ size_t VFSFileImpl::size() const
415425
return _stat.st_size;
416426
}
417427

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+
418441
const char* VFSFileImpl::path() const
419442
{
420443
return (const char*) _path;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ class VFSFileImpl : public FileImpl
6565
bool seek(uint32_t pos, SeekMode mode) override;
6666
size_t position() const override;
6767
size_t size() const override;
68+
bool setBufferSize(size_t size);
6869
void close() override;
6970
const char* path() const override;
7071
const char* name() const override;
71-
time_t getLastWrite() override;
72+
time_t getLastWrite() override;
7273
boolean isDirectory(void) override;
7374
FileImplPtr openNextFile(const char* mode) override;
7475
void rewindDirectory(void) override;

0 commit comments

Comments
 (0)