diff --git a/drivers/DirHandle.h b/drivers/DirHandle.h index 9a86adc176..521a7ff5ca 100644 --- a/drivers/DirHandle.h +++ b/drivers/DirHandle.h @@ -16,6 +16,8 @@ #ifndef MBED_DIRHANDLE_H #define MBED_DIRHANDLE_H +#include + #if defined(__ARMCC_VERSION) || defined(__ICCARM__) # define NAME_MAX 255 typedef int mode_t; @@ -28,6 +30,18 @@ typedef int mode_t; struct dirent { char d_name[NAME_MAX+1]; + uint8_t d_type; +}; + +enum { + DT_UNKNOWN, // The file type could not be determined. + DT_FIFO, // This is a named pipe (FIFO). + DT_CHR, // This is a character device. + DT_DIR, // This is a directory. + DT_BLK, // This is a block device. + DT_REG, // This is a regular file. + DT_LNK, // This is a symbolic link. + DT_SOCK, // This is a UNIX domain socket. }; namespace mbed { diff --git a/features/TESTS/filesystem/fat_file_system/main.cpp b/features/TESTS/filesystem/fat_file_system/main.cpp index 7a2cfff5c3..fff843ca70 100644 --- a/features/TESTS/filesystem/fat_file_system/main.cpp +++ b/features/TESTS/filesystem/fat_file_system/main.cpp @@ -77,6 +77,58 @@ void test_read_write() { TEST_ASSERT_EQUAL(0, err); } +// Simple test for iterating dir entries +void test_read_dir() { + FATFileSystem fs("fat"); + + int err = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, err); + + err = fs.mkdir("test_read_dir", S_IRWXU | S_IRWXG | S_IRWXO); + TEST_ASSERT_EQUAL(0, err); + + err = fs.mkdir("test_read_dir/test_dir", S_IRWXU | S_IRWXG | S_IRWXO); + TEST_ASSERT_EQUAL(0, err); + + FileHandle *file = fs.open("test_read_dir/test_file", O_WRONLY | O_CREAT); + TEST_ASSERT(file); + err = file->close(); + TEST_ASSERT_EQUAL(0, err); + + // Iterate over dir checking for known files + DirHandle *dir = fs.opendir("test_read_dir"); + TEST_ASSERT(dir); + + struct dirent *de; + bool test_dir_found = false; + bool test_file_found = true; + + while ((de = readdir(dir))) { + printf("d_name: %.32s, d_type: %x\n", de->d_name, de->d_type); + + if (strcmp(de->d_name, "test_dir") == 0) { + test_dir_found = true; + TEST_ASSERT_EQUAL(DT_DIR, de->d_type); + } else if (strcmp(de->d_name, "test_file") == 0) { + test_file_found = true; + TEST_ASSERT_EQUAL(DT_REG, de->d_type); + } else { + char *buf = new char[NAME_MAX]; + snprintf(buf, NAME_MAX, "Unexpected file \"%s\"", de->d_name); + TEST_ASSERT_MESSAGE(false, buf); + } + } + + TEST_ASSERT_MESSAGE(test_dir_found, "Could not find \"test_dir\""); + TEST_ASSERT_MESSAGE(test_file_found, "Could not find \"test_file\""); + + err = dir->closedir(); + TEST_ASSERT_EQUAL(0, err); + + err = fs.unmount(); + TEST_ASSERT_EQUAL(0, err); +} + // Test setup utest::v1::status_t test_setup(const size_t number_of_cases) { @@ -88,6 +140,7 @@ Case cases[] = { Case("Testing formating", test_format), Case("Testing read write < block", test_read_write), Case("Testing read write > block", test_read_write<2*BLOCK_SIZE>), + Case("Testing dir iteration", test_read_dir), }; Specification specification(test_setup, cases); diff --git a/features/filesystem/fat/FATDirHandle.cpp b/features/filesystem/fat/FATDirHandle.cpp index 6ad07642d0..69498d8013 100644 --- a/features/filesystem/fat/FATDirHandle.cpp +++ b/features/filesystem/fat/FATDirHandle.cpp @@ -50,6 +50,7 @@ struct dirent *FATDirHandle::readdir() { FRESULT res = f_readdir(&dir, &finfo); fat_filesystem_set_errno(res); + cur_entry.d_type = (finfo.fattrib & AM_DIR) ? DT_DIR : DT_REG; #if _USE_LFN if(res != 0 || finfo.fname[0]==0) {