diff --git a/components/esp_partition/CMakeLists.txt b/components/esp_partition/CMakeLists.txt index 977f7cc679..5dc13db76f 100644 --- a/components/esp_partition/CMakeLists.txt +++ b/components/esp_partition/CMakeLists.txt @@ -56,6 +56,7 @@ else() if(${target} STREQUAL "linux") # set BUILD_DIR because partition_linux.c uses a file created in the build directory target_compile_definitions(${COMPONENT_LIB} PRIVATE "BUILD_DIR=\"${build_dir}\"") + target_link_libraries(${COMPONENT_LIB} PRIVATE dl) endif() if(CMAKE_C_COMPILER_ID MATCHES "GNU") diff --git a/components/esp_partition/partition_linux.c b/components/esp_partition/partition_linux.c index 5d18a0d5b6..7dd8aab9c9 100644 --- a/components/esp_partition/partition_linux.c +++ b/components/esp_partition/partition_linux.c @@ -21,9 +21,22 @@ #include "esp_private/partition_linux.h" #include "esp_log.h" #include "spi_flash_mmap.h" +#include ESP_LOG_ATTR_TAG(TAG, "linux_spiflash"); +typedef int (*ftruncate_func_t)(int fd, off_t length); +static ftruncate_func_t __orig_ftruncate = NULL; + +void __attribute__((constructor)) init_orig_funcs(void) +{ + __orig_ftruncate = (ftruncate_func_t) dlsym(RTLD_NEXT, "ftruncate"); + if (__orig_ftruncate == NULL) { + ESP_LOGE(TAG, "Failed to load original ftruncate function: %s", dlerror()); + abort(); + } +} + static void *s_spiflash_mem_file_buf = NULL; static int s_spiflash_mem_file_fd = -1; static const esp_partition_mmap_handle_t s_default_partition_mmap_handle = 0; @@ -268,7 +281,7 @@ esp_err_t esp_partition_file_mmap(const uint8_t **part_desc_addr_start) do { // resize file - if (ftruncate(s_spiflash_mem_file_fd, s_esp_partition_file_mmap_ctrl_act.flash_file_size) != 0) { + if (__orig_ftruncate && __orig_ftruncate(s_spiflash_mem_file_fd, s_esp_partition_file_mmap_ctrl_act.flash_file_size) != 0) { ESP_LOGE(TAG, "Failed to set size of SPI FLASH memory emulation file %s: %s", s_esp_partition_file_mmap_ctrl_act.flash_file_name, strerror(errno)); ret = ESP_ERR_INVALID_SIZE; break; diff --git a/components/fatfs/CMakeLists.txt b/components/fatfs/CMakeLists.txt index dddcb8905f..67423287af 100644 --- a/components/fatfs/CMakeLists.txt +++ b/components/fatfs/CMakeLists.txt @@ -10,9 +10,12 @@ set(include_dirs "diskio" "src") set(requires "wear_levelling") -# for linux, we do not have support for vfs and sdmmc, for real targets, add respective sources +# for linux, we do not have support for sdmmc, for real targets, add respective sources if(${target} STREQUAL "linux") - list(APPEND srcs "port/linux/ffsystem.c") + list(APPEND srcs "port/linux/ffsystem.c" + "vfs/vfs_fat.c") + list(APPEND include_dirs "vfs") + list(APPEND priv_requires "vfs" "linux") else() list(APPEND srcs "port/freertos/ffsystem.c" "diskio/diskio_sdmmc.c" diff --git a/components/fatfs/diskio/diskio_impl.h b/components/fatfs/diskio/diskio_impl.h index 9d7e012481..12dbcb31d8 100644 --- a/components/fatfs/diskio/diskio_impl.h +++ b/components/fatfs/diskio/diskio_impl.h @@ -1,16 +1,8 @@ -// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once @@ -19,9 +11,7 @@ extern "C" { #endif #include -typedef unsigned int UINT; -typedef unsigned char BYTE; -typedef uint32_t DWORD; +#include "ff.h" #define FF_DRV_NOT_USED 0xFF @@ -36,8 +26,8 @@ typedef uint32_t DWORD; typedef struct { DSTATUS (*init) (unsigned char pdrv); /*!< disk initialization function */ DSTATUS (*status) (unsigned char pdrv); /*!< disk status check function */ - DRESULT (*read) (unsigned char pdrv, unsigned char* buff, uint32_t sector, unsigned count); /*!< sector read function */ - DRESULT (*write) (unsigned char pdrv, const unsigned char* buff, uint32_t sector, unsigned count); /*!< sector write function */ + DRESULT (*read) (unsigned char pdrv, unsigned char* buff, uint32_t sector, UINT count); /*!< sector read function */ + DRESULT (*write) (unsigned char pdrv, const unsigned char* buff, uint32_t sector, UINT count); /*!< sector write function */ DRESULT (*ioctl) (unsigned char pdrv, unsigned char cmd, void* buff); /*!< function to get info about disk and do some misc operations */ } ff_diskio_impl_t; diff --git a/components/fatfs/host_test/main/CMakeLists.txt b/components/fatfs/host_test/main/CMakeLists.txt index 046433c130..bc1b10714c 100644 --- a/components/fatfs/host_test/main/CMakeLists.txt +++ b/components/fatfs/host_test/main/CMakeLists.txt @@ -1,5 +1,5 @@ -idf_component_register(SRCS "test_fatfs.cpp" - REQUIRES fatfs +idf_component_register(SRCS "test_fatfs.cpp" "test_fatfs_vfs.cpp" + REQUIRES fatfs vfs WHOLE_ARCHIVE ) diff --git a/components/fatfs/host_test/main/test_fatfs_vfs.cpp b/components/fatfs/host_test/main/test_fatfs_vfs.cpp new file mode 100644 index 0000000000..516a2864b8 --- /dev/null +++ b/components/fatfs/host_test/main/test_fatfs_vfs.cpp @@ -0,0 +1,579 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include +#include +#include + +#include "ff.h" +#include "esp_partition.h" +#include "wear_levelling.h" +#include "diskio_impl.h" +#include "diskio_wl.h" +#include "esp_vfs_fat.h" +#include "esp_vfs.h" + +#include + +static const char* TAG = "linux_vfs"; + +static BYTE pdrv; +static wl_handle_t wl_handle; +static char drv[3]; + +static void test_setup(void) +{ + FATFS *fs; + esp_err_t ret = ESP_OK; + FRESULT fr_result; + + esp_err_t esp_result; + + const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "storage"); + REQUIRE(partition != NULL); + + // Mount wear-levelled partition + esp_result = wl_mount(partition, &wl_handle); + REQUIRE(esp_result == ESP_OK); + + // Get a physical drive + esp_result = ff_diskio_get_drive(&pdrv); + REQUIRE(esp_result == ESP_OK); + + // Register physical drive as wear-levelled partition + esp_result = ff_diskio_register_wl_partition(pdrv, wl_handle); + + char temp_drv[3] = {(char)('0' + pdrv), ':', 0}; + strncpy(drv, temp_drv, 3); + + // Create FAT volume on the entire disk + LBA_t part_list[] = {100, 0, 0, 0}; + BYTE workbuf[FF_MAX_SS]; + const size_t workbuf_size = sizeof(workbuf); + + fr_result = f_fdisk(pdrv, part_list, workbuf); + REQUIRE(fr_result == FR_OK); + const MKFS_PARM opt = {(BYTE)FM_ANY, 0, 0, 0, 0}; + fr_result = f_mkfs("", &opt, workbuf, workbuf_size); // Use default volume + REQUIRE(fr_result == FR_OK); + + esp_vfs_fat_conf_t conf = { + .base_path = "/linux", + .fat_drive = drv, + .max_files = 5, + }; + ret = esp_vfs_fat_register_cfg(&conf, &fs); + if (ret == ESP_ERR_INVALID_STATE) { + // it's okay, already registered with VFS + } else if (ret != ESP_OK) { + ESP_LOGD(TAG, "esp_vfs_fat_register_cfg failed 0x(%x)", ret); + } + REQUIRE(ret == ESP_OK); + + //Mount the volume + fr_result = f_mount(fs, "", 0); + REQUIRE(fr_result == FR_OK); +} + +static void test_teardown(void) +{ + esp_err_t esp_result; + FRESULT fr_result; + // Unmount default volume + fr_result = f_mount(0, drv, 0); + REQUIRE(fr_result == FR_OK); + + ff_diskio_unregister(pdrv); + ff_diskio_clear_pdrv_wl(wl_handle); + esp_result = wl_unmount(wl_handle); + esp_vfs_fat_unregister_path("/linux"); + REQUIRE(esp_result == ESP_OK); +} + +static void test_fatfs_create_file_with_text(const char* name, const char* text) +{ + int fd = -1; + fd = open(name, O_CREAT|O_RDWR, 0777); + REQUIRE(fd != -1); + + ssize_t sz = write(fd, text, strlen(text)); + REQUIRE(sz == strlen(text)); + + REQUIRE(0 == close(fd)); +} + +static void test_open_read_write_file() +{ + constexpr const char *test_str = "0123456789\n"; + const char *filename = "/linux/test.txt"; + test_fatfs_create_file_with_text(filename, test_str); + + int fd = open(filename, O_RDWR); + + char data[strlen(test_str)]; + size_t sz = read(fd, data, strlen(test_str)); + REQUIRE(sz == strlen(test_str)); + REQUIRE(0 == strncmp(data, test_str, strlen(test_str))); + + close(fd); +} + +TEST_CASE("open file, write and read back data via VFS", "[fatfs]") +{ + test_setup(); + test_open_read_write_file(); + test_teardown(); +} + +static void test_lseek() +{ + const char *test_str = "0123456789\n"; + const char *filename = "/linux/lseek.txt"; + test_fatfs_create_file_with_text(filename, test_str); + + int fd = open(filename, O_RDWR); + + off_t off = lseek(fd, 6, SEEK_CUR); + REQUIRE(off == 6); + off = lseek(fd, 3, SEEK_SET); + REQUIRE(off == 3); + off = lseek(fd, -9, SEEK_END); + REQUIRE(off == 2); + + close(fd); +} + +TEST_CASE("lseek via VFS", "[fatfs]") +{ + test_setup(); + test_lseek(); + test_teardown(); +} + +static void test_pread() +{ + char buf[32] = { 0 }; + const char *test_str = "0123456789\n"; + const char *filename = "/linux/pread.txt"; + test_fatfs_create_file_with_text(filename, test_str); + + int fd = open(filename, O_RDWR); + + int r = pread(fd, buf, sizeof(buf), 0); // it is a regular read() with offset==0 + REQUIRE(0 == strcmp(test_str, buf)); + REQUIRE(strlen(test_str) == r); + + memset(buf, 0, sizeof(buf)); + r = pread(fd, buf, sizeof(buf), 1); // offset==1 + REQUIRE(0 == strcmp(test_str + 1, buf)); + REQUIRE(strlen(test_str) - 1 == r); + + memset(buf, 0, sizeof(buf)); + r = pread(fd, buf, sizeof(buf), 5); // offset==5 + REQUIRE(0 == strcmp(test_str + 5, buf)); + REQUIRE(strlen(test_str) - 5 == r); + + close(fd); +} + +TEST_CASE("can read file with pread", "[fatfs]") +{ + test_setup(); + test_pread(); + test_teardown(); +} + +static void test_pwrite() +{ + const char *test_str = "0123456789\n"; + const char *msg = "ESP"; + const char *filename = "/linux/pwrite.txt"; + test_fatfs_create_file_with_text(filename, test_str); + + int fd = open(filename, O_RDWR); + + const off_t current_pos = lseek(fd, 0, SEEK_END); // O_APPEND is not the same - jumps to the end only before write() + + const int r = pwrite(fd, msg, strlen(msg), 0); + REQUIRE(strlen(msg) == r); + + REQUIRE(current_pos == lseek(fd, 0, SEEK_CUR)); // pwrite should not move the pointer + + close(fd); +} + +TEST_CASE("pwrite() works well", "[fatfs]") +{ + test_setup(); + test_pwrite(); + test_teardown(); +} + +static void test_link_unlink_rename() +{ + const char *filename_prefix = "/linux/link"; + + char name_copy[64]; + char name_dst[64]; + char name_src[64]; + snprintf(name_copy, sizeof(name_copy), "%s_cpy.txt", filename_prefix); + snprintf(name_dst, sizeof(name_dst), "%s_dst.txt", filename_prefix); + snprintf(name_src, sizeof(name_src), "%s_src.txt", filename_prefix); + + unlink(name_copy); + unlink(name_dst); + unlink(name_src); + + const char *test_str = "0123456789\n"; + test_fatfs_create_file_with_text(name_src, test_str); + + REQUIRE(0 == link(name_src, name_copy)); + REQUIRE(-1 != open(name_copy, O_RDONLY)); + + test_fatfs_create_file_with_text(name_copy, test_str); + rename(name_copy, name_dst); + REQUIRE(-1 == open(name_copy, O_RDONLY)); + REQUIRE(-1 != open(name_dst, O_RDONLY)); + + unlink(name_dst); + unlink(name_src); + + REQUIRE(-1 == open(name_src, O_RDONLY)); + REQUIRE(-1 == open(name_dst, O_RDONLY)); +} + +TEST_CASE("link copies a file, unlink removes file, rename moves a file", "[fatfs]") +{ + test_setup(); + test_link_unlink_rename(); + test_teardown(); +} + +static void test_opendir_closedir_readdir() +{ + const char *dirname = "/linux/dir"; + + REQUIRE(0 == mkdir(dirname, 0755)); + + char name_dir_file[64]; + const char * file_name = "test_opd.txt"; + snprintf(name_dir_file, sizeof(name_dir_file), "%s/%s", dirname, file_name); + unlink(name_dir_file); + test_fatfs_create_file_with_text(name_dir_file, "test_opendir\n"); + DIR* dir = opendir(dirname); + REQUIRE(dir != NULL); + bool found = false; + while (true) { + struct dirent* de = readdir(dir); + if (!de) { + break; + } + if (strcasecmp(de->d_name, file_name) == 0) { + found = true; + break; + } + } + REQUIRE(found == true); + REQUIRE(0 == closedir(dir)); + unlink(name_dir_file); +} + +void test_fatfs_opendir_readdir_rewinddir(const char* dir_prefix) +{ + char name_dir_inner_file[64]; + char name_dir_inner[64]; + char name_dir_file3[64]; + char name_dir_file2[64]; + char name_dir_file1[64]; + + snprintf(name_dir_inner_file, sizeof(name_dir_inner_file), "%s/inner/3.txt", dir_prefix); + snprintf(name_dir_inner, sizeof(name_dir_inner), "%s/inner", dir_prefix); + snprintf(name_dir_file3, sizeof(name_dir_file2), "%s/boo.bin", dir_prefix); + snprintf(name_dir_file2, sizeof(name_dir_file2), "%s/2.txt", dir_prefix); + snprintf(name_dir_file1, sizeof(name_dir_file1), "%s/1.txt", dir_prefix); + + unlink(name_dir_inner_file); + rmdir(name_dir_inner); + unlink(name_dir_file1); + unlink(name_dir_file2); + unlink(name_dir_file3); + rmdir(dir_prefix); + + REQUIRE(0 == mkdir(dir_prefix, 0755)); + test_fatfs_create_file_with_text(name_dir_file1, "1\n"); + test_fatfs_create_file_with_text(name_dir_file2, "2\n"); + test_fatfs_create_file_with_text(name_dir_file3, "\01\02\03"); + REQUIRE(0 == mkdir(name_dir_inner, 0755)); + test_fatfs_create_file_with_text(name_dir_inner_file, "3\n"); + + DIR* dir = opendir(dir_prefix); + REQUIRE(dir != NULL); + int count = 0; + const char* names[4]; + while(count < 4) { + struct dirent* de = readdir(dir); + if (!de) { + break; + } + printf("found '%s'\n", de->d_name); + if (strcasecmp(de->d_name, "1.txt") == 0) { + REQUIRE(de->d_type == DT_REG); + names[count] = "1.txt"; + ++count; + } else if (strcasecmp(de->d_name, "2.txt") == 0) { + REQUIRE(de->d_type == DT_REG); + names[count] = "2.txt"; + ++count; + } else if (strcasecmp(de->d_name, "inner") == 0) { + REQUIRE(de->d_type == DT_DIR); + names[count] = "inner"; + ++count; + } else if (strcasecmp(de->d_name, "boo.bin") == 0) { + REQUIRE(de->d_type == DT_REG); + names[count] = "boo.bin"; + ++count; + } else { + printf("unexpected directory entry"); + } + } + REQUIRE(count == 4); + + rewinddir(dir); + struct dirent* de = readdir(dir); + REQUIRE(de != NULL); + REQUIRE(0 == strcasecmp(de->d_name, names[0])); + seekdir(dir, 3); + de = readdir(dir); + REQUIRE(de != NULL); + REQUIRE(0 == strcasecmp(de->d_name, names[3])); + seekdir(dir, 1); + de = readdir(dir); + REQUIRE(de != NULL); + REQUIRE(0 == strcasecmp(de->d_name, names[1])); + seekdir(dir, 2); + de = readdir(dir); + REQUIRE(de != NULL); + REQUIRE(0 == strcasecmp(de->d_name, names[2])); + + REQUIRE(0 == closedir(dir)); +} + +TEST_CASE("opendir, readdir, rewinddir, seekdir work as expected", "[fatfs]") +{ + test_setup(); + test_fatfs_opendir_readdir_rewinddir("/linux/dir"); + test_teardown(); +} + +TEST_CASE("can opendir, closedir and readdir of FS", "[fatfs]") +{ + test_setup(); + test_opendir_closedir_readdir(); + test_teardown(); +} + +static void test_truncate() +{ + const char *test_str = "0123456789\n"; + const char *filename = "/linux/truncate.txt"; + test_fatfs_create_file_with_text(filename, test_str); + + struct stat st; + size_t size; + REQUIRE(0 == stat(filename, &st)); + size = st.st_size; + REQUIRE(strlen(test_str) == size); + + size_t trunc_add = 2; + off_t new_size = strlen(test_str) + trunc_add; + REQUIRE(0 == truncate(filename, new_size)); + stat(filename, &st); + size = st.st_size; + REQUIRE(new_size == size); + + const char truncated_1[] = "01234"; + off_t truncated_len = strlen(truncated_1); + + REQUIRE(0 == truncate(filename, truncated_len)); + + stat(filename, &st); + size = st.st_size; + REQUIRE(strlen(truncated_1) == size); +} + +TEST_CASE("can truncate", "[fatfs]") +{ + test_setup(); + test_truncate(); + test_teardown(); +} + +static void test_ftruncate() +{ + const char *test_str = "0123456789\n"; + const char *filename = "/linux/ftrunc.txt"; + test_fatfs_create_file_with_text(filename, test_str); + + int fd = open(filename, O_RDWR); + struct stat st; + size_t size; + fstat(fd, &st); + size = st.st_size; + REQUIRE(strlen(test_str) == size); + close(fd); + + size_t trunc_add = 2; + off_t new_size = strlen(test_str) + trunc_add; + fd = open(filename, O_RDWR); + REQUIRE(0 == ftruncate(fd, new_size)); + fstat(fd, &st); + size = st.st_size; + REQUIRE(new_size == size); + close(fd); + + const char truncated_1[] = "01234"; + off_t truncated_len = strlen(truncated_1); + + fd = open(filename, O_RDWR); + REQUIRE(0 == ftruncate(fd, truncated_len)); + fstat(fd, &st); + size = st.st_size; + REQUIRE(strlen(truncated_1) == size); + close(fd); +} + +TEST_CASE("can ftruncate", "[fatfs]") +{ + test_setup(); + test_ftruncate(); + test_teardown(); +} + +void test_fatfs_utime(const char* filename, const char* root_dir) +{ + struct stat achieved_stat; + struct tm desired_tm; + struct utimbuf desired_time = { + .actime = 0, // access time is not supported + .modtime = 0, + }; + time_t false_now = 0; + memset(&desired_tm, 0, sizeof(struct tm)); + + // Setting up a false actual time - used when the file is created and for modification with the current time + desired_tm.tm_mon = 10 - 1; + desired_tm.tm_mday = 31; + desired_tm.tm_year = 2018 - 1900; + desired_tm.tm_hour = 10; + desired_tm.tm_min = 35; + desired_tm.tm_sec = 23; + + false_now = mktime(&desired_tm); + + struct timeval now = { .tv_sec = false_now, .tv_usec = 0}; + settimeofday(&now, NULL); + + test_fatfs_create_file_with_text(filename, ""); + + // 00:00:00. January 1st, 1980 - FATFS cannot handle earlier dates + desired_tm.tm_mon = 1 - 1; + desired_tm.tm_mday = 1; + desired_tm.tm_year = 1980 - 1900; + desired_tm.tm_hour = 0; + desired_tm.tm_min = 0; + desired_tm.tm_sec = 0; + printf("Testing mod. time: %s", asctime(&desired_tm)); + desired_time.modtime = mktime(&desired_tm); + REQUIRE(0 == utime(filename, &desired_time)); + REQUIRE(0 == stat(filename, &achieved_stat)); + REQUIRE(desired_time.modtime == achieved_stat.st_mtime); + + // current time + REQUIRE(0 == utime(filename, NULL)); + REQUIRE(0 == stat(filename, &achieved_stat)); + printf("Mod. time changed to (false actual time): %s", ctime(&achieved_stat.st_mtime)); + REQUIRE(desired_time.modtime != achieved_stat.st_mtime); + REQUIRE(false_now - achieved_stat.st_mtime <= 2); // two seconds of tolerance are given + + // 23:59:08. December 31st, 2037 + desired_tm.tm_mon = 12 - 1; + desired_tm.tm_mday = 31; + desired_tm.tm_year = 2037 - 1900; + desired_tm.tm_hour = 23; + desired_tm.tm_min = 59; + desired_tm.tm_sec = 8; + printf("Testing mod. time: %s", asctime(&desired_tm)); + desired_time.modtime = mktime(&desired_tm); + REQUIRE(0 == utime(filename, &desired_time)); + REQUIRE(0 == stat(filename, &achieved_stat)); + REQUIRE(desired_time.modtime == achieved_stat.st_mtime); + + //WARNING: it has the Unix Millennium bug (Y2K38) + + // 00:00:00. January 1st, 1970 - FATFS cannot handle years before 1980 + desired_tm.tm_mon = 1 - 1; + desired_tm.tm_mday = 1; + desired_tm.tm_year = 1970 - 1900; + desired_tm.tm_hour = 0; + desired_tm.tm_min = 0; + desired_tm.tm_sec = 0; + printf("Testing mod. time: %s", asctime(&desired_tm)); + desired_time.modtime = mktime(&desired_tm); + REQUIRE(-1 == utime(filename, &desired_time)); + REQUIRE(EINVAL == errno); +} + +TEST_CASE("utime sets modification time", "[fatfs]") +{ + test_setup(); + test_fatfs_utime("/linux/utime.txt", "/linux"); + test_teardown(); +} + +static void test_fstat() +{ + const char *test_str = "0123456789\n"; + const char *filename = "/linux/pwrite.txt"; + test_fatfs_create_file_with_text(filename, test_str); + + int fd = open(filename, O_RDWR); + + struct stat st; + REQUIRE(0 == fstat(fd, &st)); + + REQUIRE((st.st_mode & S_IFREG) == S_IFREG); + REQUIRE(st.st_size == strlen(test_str)); + + close(fd); +} + +TEST_CASE("fstat returns correct values", "[fatfs]") +{ + test_setup(); + test_fstat(); + test_teardown(); +} + +static void test_mkdir_rmdir() +{ + const char *dir_prefix = "/linux/dir"; + + REQUIRE(0 == mkdir(dir_prefix, 0777)); + + struct stat st; + REQUIRE(0 == stat(dir_prefix, &st)); + REQUIRE((st.st_mode & S_IFDIR) == S_IFDIR); + REQUIRE((st.st_mode & S_IFREG) != S_IFREG); + REQUIRE(0 == rmdir(dir_prefix)); + REQUIRE(-1 == stat(dir_prefix, &st)); +} + +TEST_CASE("can create and remove directories", "[fatfs]") +{ + test_setup(); + test_mkdir_rmdir(); + test_teardown(); +} diff --git a/components/fatfs/host_test/partition_table.csv b/components/fatfs/host_test/partition_table.csv index 777a3e67e9..e1155663bf 100644 --- a/components/fatfs/host_test/partition_table.csv +++ b/components/fatfs/host_test/partition_table.csv @@ -3,5 +3,5 @@ nvs, data, nvs, 0x9000, 0x6000, phy_init, data, phy, 0xf000, 0x1000, factory, app, factory, 0x10000, 1M, -storage, data, fat, , 32k, +storage, data, fat, , 1M, storage2, data, fat, , 32k, diff --git a/components/fatfs/src/ff.h b/components/fatfs/src/ff.h index f4493e031a..fb0ce02c71 100644 --- a/components/fatfs/src/ff.h +++ b/components/fatfs/src/ff.h @@ -46,7 +46,7 @@ typedef unsigned __int64 QWORD; #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) /* C99 or later */ #define FF_INTDEF 2 #include -typedef unsigned int UINT; /* int must be 16-bit or 32-bit */ +typedef size_t UINT; /* int must be 16-bit or 32-bit or 64 bit (Linux) */ typedef unsigned char BYTE; /* char must be 8-bit */ typedef uint16_t WORD; /* 16-bit unsigned integer */ typedef uint32_t DWORD; /* 32-bit unsigned integer */ diff --git a/components/fatfs/src/ffconf.h b/components/fatfs/src/ffconf.h index 3b7f02e005..94fab006fd 100644 --- a/components/fatfs/src/ffconf.h +++ b/components/fatfs/src/ffconf.h @@ -353,7 +353,7 @@ /* Some memory allocation functions are declared here in addition to ff.h, so that they can be used also by external code when LFN feature is disabled. */ -void* ff_memalloc (unsigned msize); +void* ff_memalloc (size_t msize); void ff_memfree(void*); diff --git a/components/fatfs/vfs/esp_vfs_fat.h b/components/fatfs/vfs/esp_vfs_fat.h index 332a131c80..0a0070cae8 100644 --- a/components/fatfs/vfs/esp_vfs_fat.h +++ b/components/fatfs/vfs/esp_vfs_fat.h @@ -7,8 +7,10 @@ #pragma once #include #include "esp_err.h" +#ifndef CONFIG_IDF_TARGET_LINUX #include "sd_protocol_types.h" #include "driver/sdspi_host.h" +#endif #include "ff.h" #include "wear_levelling.h" @@ -145,6 +147,7 @@ typedef struct { // Compatibility definition typedef esp_vfs_fat_mount_config_t esp_vfs_fat_sdmmc_mount_config_t; +#ifndef CONFIG_IDF_TARGET_LINUX /** * @brief Convenience function to get FAT filesystem on SD card registered in VFS * @@ -277,6 +280,7 @@ esp_err_t esp_vfs_fat_sdcard_format_cfg(const char *base_path, sdmmc_card_t *car * - ESP_FAIL: fail to format it, or fail to mount back */ esp_err_t esp_vfs_fat_sdcard_format(const char *base_path, sdmmc_card_t *card); +#endif /** * @brief Convenience function to initialize FAT filesystem in SPI flash and register it in VFS @@ -410,6 +414,7 @@ esp_err_t esp_vfs_fat_spiflash_unmount_ro(const char* base_path, const char* par * - ESP_ERR_INVALID_STATE if partition not found * - ESP_FAIL if another FRESULT error (saved in errno) */ + esp_err_t esp_vfs_fat_info(const char* base_path, uint64_t* out_total_bytes, uint64_t* out_free_bytes); /** @@ -444,6 +449,7 @@ esp_err_t esp_vfs_fat_create_contiguous_file(const char* base_path, const char* */ esp_err_t esp_vfs_fat_test_contiguous_file(const char* base_path, const char* full_path, bool* is_contiguous); +#ifndef CONFIG_IDF_TARGET_LINUX /** @cond */ /** * @deprecated Please use `esp_vfs_fat_register_cfg` instead @@ -481,6 +487,7 @@ esp_err_t esp_vfs_fat_rawflash_unmount(const char* base_path, const char* partit __attribute__((deprecated("esp_vfs_fat_rawflash_unmount is deprecated, please use esp_vfs_fat_spiflash_unmount_ro instead"))); /** @endcond */ +#endif #ifdef __cplusplus } #endif diff --git a/components/fatfs/vfs/vfs_fat.c b/components/fatfs/vfs/vfs_fat.c index aeab037f57..8a8d2cd3f7 100644 --- a/components/fatfs/vfs/vfs_fat.c +++ b/components/fatfs/vfs/vfs_fat.c @@ -455,7 +455,7 @@ static ssize_t vfs_fat_write(void* ctx, int fd, const void * data, size_t size) return -1; } } - unsigned written = 0; + UINT written = 0; res = f_write(file, data, size, &written); if (((written == 0) && (size != 0)) && (res == 0)) { errno = ENOSPC; @@ -490,7 +490,7 @@ static ssize_t vfs_fat_read(void* ctx, int fd, void * dst, size_t size) { vfs_fat_ctx_t* fat_ctx = (vfs_fat_ctx_t*) ctx; FIL* file = &fat_ctx->files[fd]; - unsigned read = 0; + UINT read = 0; FRESULT res = f_read(file, dst, size, &read); if (res != FR_OK) { ESP_LOGD(TAG, "%s: fresult=%d", __func__, res); @@ -518,7 +518,7 @@ static ssize_t vfs_fat_pread(void *ctx, int fd, void *dst, size_t size, off_t of goto pread_release; } - unsigned read = 0; + UINT read = 0; f_res = f_read(file, dst, size, &read); if (f_res == FR_OK) { ret = read; @@ -558,7 +558,7 @@ static ssize_t vfs_fat_pwrite(void *ctx, int fd, const void *src, size_t size, o goto pwrite_release; } - unsigned wr = 0; + UINT wr = 0; f_res = f_write(file, src, size, &wr); if (((wr == 0) && (size != 0)) && (f_res == 0)) { errno = ENOSPC; @@ -714,22 +714,23 @@ static void update_stat_struct(struct stat *st, FILINFO *info) memset(st, 0, sizeof(*st)); st->st_size = info->fsize; st->st_mode = get_stat_mode((info->fattrib & AM_DIR) != 0); - fat_date_t fdate = { .as_int = info->fdate }; - fat_time_t ftime = { .as_int = info->ftime }; - struct tm tm = { - .tm_mday = fdate.mday, - .tm_mon = fdate.mon - 1, /* unlike tm_mday, tm_mon is zero-based */ - .tm_year = fdate.year + 80, - .tm_sec = ftime.sec * 2, - .tm_min = ftime.min, - .tm_hour = ftime.hour, - /* FAT doesn't keep track if the time was DST or not, ask the C library - * to try to figure this out. Note that this may yield incorrect result - * in the hour before the DST comes in effect, when the local time can't - * be converted to UTC uniquely. - */ - .tm_isdst = -1 - }; + fat_date_t fdate = { 0 }; + fdate.as_int = info->fdate; + fat_time_t ftime = { 0 }; + ftime.as_int = info->ftime; + struct tm tm = { 0 }; + tm.tm_mday = fdate.mday; + tm.tm_mon = fdate.mon - 1; /* unlike tm_mday, tm_mon is zero-based */ + tm.tm_year = fdate.year + 80; + tm.tm_sec = ftime.sec * 2; + tm.tm_min = ftime.min; + tm.tm_hour = ftime.hour; + /* FAT doesn't keep track if the time was DST or not, ask the C library + * to try to figure this out. Note that this may yield incorrect result + * in the hour before the DST comes in effect, when the local time can't + * be converted to UTC uniquely. + */ + tm.tm_isdst = -1; st->st_mtime = mktime(&tm); st->st_atime = 0; st->st_ctime = 0; @@ -830,8 +831,8 @@ static int vfs_fat_link(void* ctx, const char* n1, const char* n2) size_t size_left = f_size(pf1); while (size_left > 0) { - size_t will_copy = (size_left < copy_buf_size) ? size_left : copy_buf_size; - size_t read; + UINT will_copy = (size_left < copy_buf_size) ? size_left : copy_buf_size; + UINT read = 0; res = f_read(pf1, buf, will_copy, &read); if (res != FR_OK) { goto close_both; @@ -839,7 +840,7 @@ static int vfs_fat_link(void* ctx, const char* n1, const char* n2) res = FR_DISK_ERR; goto close_both; } - size_t written; + UINT written = 0; res = f_write(pf2, buf, will_copy, &written); if (res != FR_OK) { goto close_both; @@ -1345,7 +1346,7 @@ static int vfs_fat_utime(void *ctx, const char *path, const struct utimbuf *time FILINFO filinfo_time; { - struct tm tm_time; + struct tm tm_time = { 0 }; if (times) { localtime_r(×->modtime, &tm_time); @@ -1369,7 +1370,7 @@ static int vfs_fat_utime(void *ctx, const char *path, const struct utimbuf *time fdate.mday = tm_time.tm_mday; fdate.mon = tm_time.tm_mon + 1; // January in fdate.mon is 1, and 0 in tm_time.tm_mon fdate.year = tm_time.tm_year - 80; // tm_time.tm_year=0 is 1900, tm_time.tm_year=0 is 1980 - ftime.sec = tm_time.tm_sec / 2, // ftime.sec counts seconds by 2 + ftime.sec = tm_time.tm_sec / 2; // ftime.sec counts seconds by 2 ftime.min = tm_time.tm_min; ftime.hour = tm_time.tm_hour; @@ -1550,7 +1551,7 @@ esp_err_t esp_vfs_fat_format_drive(uint8_t ldrv, const esp_vfs_fat_mount_config_ } size_t sector_size = 512; // default value - ff_diskio_get_sector_size(ldrv, §or_size); + ff_diskio_get_sector_size(ldrv, (UINT *)§or_size); size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(sector_size, mount_config->allocation_unit_size); ESP_LOGW(TAG, "formatting drive, allocation unit size=%d", alloc_unit_size); const MKFS_PARM opt = {(BYTE)FM_ANY, (mount_config->use_one_fat ? 1 : 2), 0, 0, alloc_unit_size}; diff --git a/components/fatfs/vfs/vfs_fat_internal.h b/components/fatfs/vfs/vfs_fat_internal.h index d20f64c438..e2f36a05f7 100644 --- a/components/fatfs/vfs/vfs_fat_internal.h +++ b/components/fatfs/vfs/vfs_fat_internal.h @@ -9,7 +9,9 @@ #include "esp_vfs_fat.h" #include "diskio_impl.h" #include "esp_partition.h" +#ifndef CONFIG_IDF_TARGET_LINUX #include "sdmmc_cmd.h" +#endif #include #include @@ -23,15 +25,6 @@ typedef struct vfs_fat_spiflash_ctx_t { vfs_fat_x_ctx_flags_t flags; //Flags } vfs_fat_spiflash_ctx_t; -typedef struct vfs_fat_sd_ctx_t { - BYTE pdrv; //Drive number that is mounted - esp_vfs_fat_mount_config_t mount_config; //Mount configuration - FATFS *fs; //FAT structure pointer that is registered - sdmmc_card_t *card; //Card info - char *base_path; //Path where partition is registered - vfs_fat_x_ctx_flags_t flags; //Flags -} vfs_fat_sd_ctx_t; - static inline size_t esp_vfs_fat_get_allocation_unit_size( size_t sector_size, size_t requested_size) { @@ -43,6 +36,16 @@ static inline size_t esp_vfs_fat_get_allocation_unit_size( return alloc_unit_size; } +#ifndef CONFIG_IDF_TARGET_LINUX +typedef struct vfs_fat_sd_ctx_t { + BYTE pdrv; //Drive number that is mounted + esp_vfs_fat_mount_config_t mount_config; //Mount configuration + FATFS *fs; //FAT structure pointer that is registered + sdmmc_card_t *card; //Card info + char *base_path; //Path where partition is registered + vfs_fat_x_ctx_flags_t flags; //Flags +} vfs_fat_sd_ctx_t; + vfs_fat_spiflash_ctx_t* get_vfs_fat_spiflash_ctx(wl_handle_t wlhandle); vfs_fat_sd_ctx_t* get_vfs_fat_get_sd_ctx(const sdmmc_card_t *card); @@ -130,3 +133,4 @@ esp_err_t esp_vfs_fat_format_drive(uint8_t ldrv, const esp_vfs_fat_mount_config_ * - ESP_FAIL if f_fdisk failed */ esp_err_t esp_vfs_fat_partition_drive(uint8_t pdrv, const esp_vfs_fat_drive_divide_arr_t drive_divide); +#endif diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 7b8290bd76..64e9f52b55 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -449,7 +449,6 @@ components/esp_rom/patches/esp_rom_longjmp.S components/esp_system/ubsan.c components/esp_wifi/src/mesh_event.c components/fatfs/diskio/diskio.c -components/fatfs/diskio/diskio_impl.h components/fatfs/diskio/diskio_rawflash.h components/fatfs/diskio/diskio_wl.h components/fatfs/port/freertos/ffsystem.c