Skip to content

Commit cbcc445

Browse files
gekysimonqhughes
authored andcommitted
Added d_type member of dirent struct to readdir
1 parent 0b0fc7d commit cbcc445

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

drivers/DirHandle.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#ifndef MBED_DIRHANDLE_H
1717
#define MBED_DIRHANDLE_H
1818

19+
#include <stdint.h>
20+
1921
#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
2022
# define NAME_MAX 255
2123
typedef int mode_t;
@@ -28,6 +30,18 @@ typedef int mode_t;
2830

2931
struct dirent {
3032
char d_name[NAME_MAX+1];
33+
uint8_t d_type;
34+
};
35+
36+
enum {
37+
DT_UNKNOWN, // The file type could not be determined.
38+
DT_FIFO, // This is a named pipe (FIFO).
39+
DT_CHR, // This is a character device.
40+
DT_DIR, // This is a directory.
41+
DT_BLK, // This is a block device.
42+
DT_REG, // This is a regular file.
43+
DT_LNK, // This is a symbolic link.
44+
DT_SOCK, // This is a UNIX domain socket.
3145
};
3246

3347
namespace mbed {

features/TESTS/filesystem/fat_file_system/main.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,58 @@ void test_read_write() {
7777
TEST_ASSERT_EQUAL(0, err);
7878
}
7979

80+
// Simple test for iterating dir entries
81+
void test_read_dir() {
82+
FATFileSystem fs("fat");
83+
84+
int err = fs.mount(&bd);
85+
TEST_ASSERT_EQUAL(0, err);
86+
87+
err = fs.mkdir("test_read_dir", S_IRWXU | S_IRWXG | S_IRWXO);
88+
TEST_ASSERT_EQUAL(0, err);
89+
90+
err = fs.mkdir("test_read_dir/test_dir", S_IRWXU | S_IRWXG | S_IRWXO);
91+
TEST_ASSERT_EQUAL(0, err);
92+
93+
FileHandle *file = fs.open("test_read_dir/test_file", O_WRONLY | O_CREAT);
94+
TEST_ASSERT(file);
95+
err = file->close();
96+
TEST_ASSERT_EQUAL(0, err);
97+
98+
// Iterate over dir checking for known files
99+
DirHandle *dir = fs.opendir("test_read_dir");
100+
TEST_ASSERT(dir);
101+
102+
struct dirent *de;
103+
bool test_dir_found = false;
104+
bool test_file_found = true;
105+
106+
while ((de = readdir(dir))) {
107+
printf("d_name: %.32s, d_type: %x\n", de->d_name, de->d_type);
108+
109+
if (strcmp(de->d_name, "test_dir") == 0) {
110+
test_dir_found = true;
111+
TEST_ASSERT_EQUAL(DT_DIR, de->d_type);
112+
} else if (strcmp(de->d_name, "test_file") == 0) {
113+
test_file_found = true;
114+
TEST_ASSERT_EQUAL(DT_REG, de->d_type);
115+
} else {
116+
char *buf = new char[NAME_MAX];
117+
snprintf(buf, NAME_MAX, "Unexpected file \"%s\"", de->d_name);
118+
TEST_ASSERT_MESSAGE(false, buf);
119+
}
120+
}
121+
122+
TEST_ASSERT_MESSAGE(test_dir_found, "Could not find \"test_dir\"");
123+
TEST_ASSERT_MESSAGE(test_file_found, "Could not find \"test_file\"");
124+
125+
err = dir->closedir();
126+
TEST_ASSERT_EQUAL(0, err);
127+
128+
err = fs.unmount();
129+
TEST_ASSERT_EQUAL(0, err);
130+
}
131+
80132

81133
// Test setup
82134
utest::v1::status_t test_setup(const size_t number_of_cases) {
@@ -88,6 +140,7 @@ Case cases[] = {
88140
Case("Testing formating", test_format),
89141
Case("Testing read write < block", test_read_write<BLOCK_SIZE/2>),
90142
Case("Testing read write > block", test_read_write<2*BLOCK_SIZE>),
143+
Case("Testing dir iteration", test_read_dir),
91144
};
92145

93146
Specification specification(test_setup, cases);

features/filesystem/fat/FATDirHandle.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct dirent *FATDirHandle::readdir() {
5050

5151
FRESULT res = f_readdir(&dir, &finfo);
5252
fat_filesystem_set_errno(res);
53+
cur_entry.d_type = (finfo.fattrib & AM_DIR) ? DT_DIR : DT_REG;
5354

5455
#if _USE_LFN
5556
if(res != 0 || finfo.fname[0]==0) {

0 commit comments

Comments
 (0)