Skip to content

Commit c8acb84

Browse files
Add ::truncate to File interface
Fixes esp8266#3846
1 parent ef2374c commit c8acb84

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed

cores/esp8266/FS.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ File::operator bool() const {
114114
return !!_p;
115115
}
116116

117+
bool File::truncate(uint32_t size) {
118+
if (!_p)
119+
return false;
120+
121+
return _p->truncate(size);
122+
}
123+
117124
const char* File::name() const {
118125
if (!_p)
119126
return nullptr;

cores/esp8266/FS.h

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class File : public Stream
7474
operator bool() const;
7575
const char* name() const;
7676
const char* fullName() const; // Includes path
77+
bool truncate(uint32_t size);
7778

7879
bool isFile() const;
7980
bool isDirectory() const;

cores/esp8266/FSImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class FileImpl {
3434
virtual bool seek(uint32_t pos, SeekMode mode) = 0;
3535
virtual size_t position() const = 0;
3636
virtual size_t size() const = 0;
37+
virtual bool truncate(uint32_t size) = 0;
3738
virtual void close() = 0;
3839
virtual const char* name() const = 0;
3940
virtual const char* fullName() const = 0;

cores/esp8266/spiffs_api.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
#undef max
3030
#undef min
3131
#include "FSImpl.h"
32-
#include "spiffs/spiffs.h"
32+
extern "C" {
33+
#include "spiffs/spiffs.h"
34+
#include "spiffs/spiffs_nucleus.h"
35+
};
3336
#include "debug.h"
3437
#include "flash_utils.h"
3538

@@ -406,6 +409,17 @@ class SPIFFSFileImpl : public FileImpl
406409
return _stat.size;
407410
}
408411

412+
bool truncate(uint32_t size) override
413+
{
414+
CHECKFD();
415+
spiffs_fd *sfd;
416+
if (spiffs_fd_get(_fs->getFs(), _fd, &sfd) == SPIFFS_OK) {
417+
return SPIFFS_OK == spiffs_object_truncate(sfd, size, 0);
418+
} else {
419+
return false;
420+
}
421+
}
422+
409423
bool isFile() const override
410424
{
411425
// No such thing as directories on SPIFFS

libraries/SDFS/src/SDFS.h

+8
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ class SDFSFileImpl : public FileImpl
251251
return _opened ? _fd->fileSize() : 0;
252252
}
253253

254+
bool truncate(uint32_t size) override
255+
{
256+
if (!_opened) {
257+
return false;
258+
}
259+
return _fd->truncate(size);
260+
}
261+
254262
void close() override
255263
{
256264
if (_opened) {

tests/host/fs/test_fs.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,18 @@ TEST_CASE("#1819 Can list all files with openDir(\"\")", "[fs][bugreport]")
219219
REQUIRE(files.size() == 4);
220220
}
221221

222+
TEST_CASE("truncate", "[fs][spiffs]")
223+
{
224+
SPIFFS_MOCK_DECLARE(64, 8, 512, "");
225+
REQUIRE(SPIFFS.begin());
226+
createFile("/file1", "some text");
227+
auto f = SPIFFS.open("/file1", "r");
228+
f.truncate(4);
229+
f.close();
230+
String s = readFile("/file1");
231+
REQUIRE( s == "some" );
232+
}
233+
222234
TEST_CASE("SDFS", "[sdfs]")
223235
{
224236
SDFS_MOCK_DECLARE();
@@ -320,3 +332,4 @@ TEST_CASE("Listfiles.ino example", "[sd]")
320332
REQUIRE(readFileSD("dir1/file3") == "nihao");
321333
REQUIRE(readFileSD("/dir2/dir3/file4") == "bonjour");
322334
}
335+

0 commit comments

Comments
 (0)