Skip to content

Commit

Permalink
Manually follow-up the last esp_lottlefs merge
Browse files Browse the repository at this point in the history
  • Loading branch information
lorol committed Oct 6, 2020
1 parent f6f365f commit 81d35bd
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/LITTLEFS_PlatformIO/README.md
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
# How to run on PlatformIO IDE

- Download and extract here the **mklittlefs** executable for your OS from a zipped binary [here](https://github.com/earlephilhower/mklittlefs/releases)
- Download and extract to project root a **mklittlefs** executable for your OS from a zipped binary [here](https://github.com/earlephilhower/mklittlefs/releases)
- Open **LITTLEFS_PlatformIO** folder
- Run PlatformIO project task: **Upload Filesystem Image**
- Run PlatformIO project task: **Upload and Monitor**
Expand Down
8 changes: 6 additions & 2 deletions examples/LITTLEFS_PlatformIO/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 15,15 @@ default_envs = esp32
framework = arduino

[env:esp32]
platform = https://github.com/platformio/platform-espressif32.git
platform = espressif32
;platform = https://github.com/platformio/platform-espressif32.git
;board_build.mcu = esp32
platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git

build_flags =
${env.build_flags}
-D=${PIOENV}
-DCONFIG_LITTLEFS_FOR_IDF_3_2
;-D CONFIG_LITTLEFS_FOR_IDF_3_2

lib_deps = https://github.com/lorol/LITTLEFS.git

Expand Down
24 changes: 21 additions & 3 deletions examples/LITTLEFS_PlatformIO/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,10 @@
#include "FS.h"
#include <LITTLEFS.h>

#ifndef CONFIG_LITTLEFS_FOR_IDF_3_2
#include <time.h>
#endif

/* You only need to format LITTLEFS the first time you run a
test or else use the LITTLEFS plugin to create a partition
https://github.com/lorol/arduino-esp32littlefs-plugin */
Expand All @@ -25,15 29,29 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
Serial.print (file.name());

#ifndef CONFIG_LITTLEFS_FOR_IDF_3_2
time_t t= file.getLastWrite();
struct tm * tmstruct = localtime(&t);
Serial.printf(" LAST WRITE: %d-d-d d:d:d\n",(tmstruct->tm_year) 1900,( tmstruct->tm_mon) 1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
#endif

if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print("\tSIZE: ");
Serial.println(file.size());
Serial.print(" SIZE: ");
Serial.print(file.size());

#ifndef CONFIG_LITTLEFS_FOR_IDF_3_2
time_t t= file.getLastWrite();
struct tm * tmstruct = localtime(&t);
Serial.printf(" LAST WRITE: %d-d-d d:d:d\n",(tmstruct->tm_year) 1900,( tmstruct->tm_mon) 1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
#endif

}
file = root.openNextFile();
}
Expand Down
118 changes: 118 additions & 0 deletions src/esp_littlefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 79,8 @@ typedef struct {
static int vfs_littlefs_open(void* ctx, const char * path, int flags, int mode);
static ssize_t vfs_littlefs_write(void* ctx, int fd, const void * data, size_t size);
static ssize_t vfs_littlefs_read(void* ctx, int fd, void * dst, size_t size);
static ssize_t vfs_littlefs_pwrite(void *ctx, int fd, const void *src, size_t size, off_t offset);
static ssize_t vfs_littlefs_pread(void *ctx, int fd, void *dst, size_t size, off_t offset);
static int vfs_littlefs_close(void* ctx, int fd);
static off_t vfs_littlefs_lseek(void* ctx, int fd, off_t offset, int mode);
static int vfs_littlefs_stat(void* ctx, const char * path, struct stat * st);
Expand Down Expand Up @@ -171,8 173,10 @@ esp_err_t esp_vfs_littlefs_register(const esp_vfs_littlefs_conf_t * conf)
const esp_vfs_t vfs = {
.flags = ESP_VFS_FLAG_CONTEXT_PTR,
.write_p = &vfs_littlefs_write,
.pwrite_p = &vfs_littlefs_pwrite,
.lseek_p = &vfs_littlefs_lseek,
.read_p = &vfs_littlefs_read,
.pread_p = &vfs_littlefs_pread,
.open_p = &vfs_littlefs_open,
.close_p = &vfs_littlefs_close,
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
Expand Down Expand Up @@ -986,6 990,120 @@ static ssize_t vfs_littlefs_read(void* ctx, int fd, void * dst, size_t size) {
return res;
}

static ssize_t vfs_littlefs_pwrite(void *ctx, int fd, const void *src, size_t size, off_t offset)
{
esp_littlefs_t *efs = (esp_littlefs_t *)ctx;
ssize_t res, save_res;
vfs_littlefs_file_t *file = NULL;

sem_take(efs);
if ((uint32_t)fd > efs->cache_size)
{
sem_give(efs);
ESP_LOGE(TAG, "FD %d must be <%d.", fd, efs->cache_size);
return LFS_ERR_BADF;
}
file = efs->cache[fd];

off_t old_offset = lfs_file_seek(efs->fs, &file->file, 0, SEEK_CUR);
if (old_offset < (off_t)0)
{
res = old_offset;
goto exit;
}

/* Set to wanted position. */
res = lfs_file_seek(efs->fs, &file->file, offset, SEEK_SET);
if (res < (off_t)0)
goto exit;

/* Write out the data. */
res = lfs_file_write(efs->fs, &file->file, src, size);

/* Now we have to restore the position. If this fails we have to
return this as an error. But if the writing also failed we
return writing error. */
save_res = lfs_file_seek(efs->fs, &file->file, old_offset, SEEK_SET);
if (res >= (ssize_t)0 && save_res < (off_t)0)
{
res = save_res;
}
sem_give(efs);

exit:
if (res < 0)
{
errno = -res;
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(TAG, "Failed to write FD %d; path \"%s\". Error %s (%d)",
fd, file->path, esp_littlefs_errno(res), res);
#else
ESP_LOGV(TAG, "Failed to write FD %d. Error %s (%d)",
fd, esp_littlefs_errno(res), res);
#endif
return -1;
}

return res;
}

static ssize_t vfs_littlefs_pread(void *ctx, int fd, void *dst, size_t size, off_t offset)
{
esp_littlefs_t *efs = (esp_littlefs_t *)ctx;
ssize_t res, save_res;
vfs_littlefs_file_t *file = NULL;

sem_take(efs);
if ((uint32_t)fd > efs->cache_size)
{
sem_give(efs);
ESP_LOGE(TAG, "FD %d must be <%d.", fd, efs->cache_size);
return LFS_ERR_BADF;
}
file = efs->cache[fd];

off_t old_offset = lfs_file_seek(efs->fs, &file->file, 0, SEEK_CUR);
if (old_offset < (off_t)0)
{
res = old_offset;
goto exit;
}

/* Set to wanted position. */
res = lfs_file_seek(efs->fs, &file->file, offset, SEEK_SET);
if (res < (off_t)0)
goto exit;

/* Read the data. */
res = lfs_file_read(efs->fs, &file->file, dst, size);

/* Now we have to restore the position. If this fails we have to
return this as an error. But if the reading also failed we
return reading error. */
save_res = lfs_file_seek(efs->fs, &file->file, old_offset, SEEK_SET);
if (res >= (ssize_t)0 && save_res < (off_t)0)
{
res = save_res;
}
sem_give(efs);

exit:
if (res < 0)
{
errno = -res;
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(TAG, "Failed to read file \"%s\". Error %s (%d)",
file->path, esp_littlefs_errno(res), res);
#else
ESP_LOGV(TAG, "Failed to read FD %d. Error %s (%d)",
fd, esp_littlefs_errno(res), res);
#endif
return -1;
}

return res;
}

static int vfs_littlefs_close(void* ctx, int fd) {
// TODO update mtime on close? SPIFFS doesn't do this
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
Expand Down

0 comments on commit 81d35bd

Please sign in to comment.