mbedmicro/mbed — pull request #629 by PrzemekWirkus (2014-10-31 15:13:45+00:00)
Title: Filesystem stat functionality

## Description
Simple detection if file system file is actually file or directory was missing. To do this I followed POSIX's 'struct dirent' and added new standardized field d_type to store type flag.
Please see: http://www.gsp.com/cgi-bin/man.cgi?section=5&topic=dir for 'struct dirent'.
### Things done:
* Extended readdir() function by adding extra field in 'struct dirent' returned .
* Added new field in 'struct dirent'. New field 'd_type' was added to provide field which could store file type (file, directory, unknown etc.)
* Added dirent::d_type related file types in header file.
* In code for now we only use this flags to determine if file is actually a file:
* DT_UNKNOWN - unknown (in our case mostly 'not supported'),
* DT_DIR - directory,
* DT_REG - regular file.
* Added new test MBED_35 to cover status flag detection.
## How to use it? :)
```c++
SDFileSystem sd(D11, D12, D13, D10, "sd");
int main()
{
bool result = true;
DIR *dir;
struct dirent *ent;
if ((dir = opendir ("/sd/")) != NULL) {
// print all the files and directories within directory
while ((ent = readdir (dir)) != NULL) {
//////////////////// USAGE ////////////////////
if (ent->d_type == DT_UNKNOWN) {
// If we still get unknown SD card file type...
result = false;
}
else {
// Print file type and name
bool is_dir = (ent->d_type == DT_DIR);
const char *file_dir = is_dir ? "dir" : "file";
printf("%s:\t%s\r\n", file_dir, ent->d_name);
}
//////////////////// USAGE ////////////////////
}
closedir (dir);
} else {
// could not open directory
printf("Could not open directory");
result = false;
}
return result;
}
```
### Future work (What we can add to file system functionality)
This functionality could be extended to support stat functionality from , or at least part of it. This pull request is a simple base ground to do stuff below:

* Add support for stat function:
* int stat(const char *path, struct stat *buf).
* Related to stat() structures:
* struct stat itself,
* At least S_ISDIR, S_ISREG macros,
* See: http://linux.die.net/man/2/stat.
* Provide a way for users to get file creation time, modification time, file size, file attributes.

### Testing results
```
[git:filesystem-stat-functionality*] c:\Work\mbed-przemek\workspace_tools> git branch
* filesystem-stat-functionality
host-test-improvements-part-2
master

[git:filesystem-stat-functionality*] c:\Work\mbed-przemek\workspace_tools> singletest.py -i test_spec.json -M muts_all.json -P -c cp
Building library CMSIS (NUCLEO_F401RE, uARM)
Building library MBED (NUCLEO_F401RE, uARM)
Building library FAT (NUCLEO_F401RE, uARM)
Building project SD_STAT (NUCLEO_F401RE, uARM)
TargetTest::NUCLEO_F401RE::uARM::MBED_35::FileSystem stat functionality [OK] in 1.42 of 15 sec
Building library FAT (NUCLEO_F401RE, uARM)
Building project SD (NUCLEO_F401RE, uARM)
TargetTest::NUCLEO_F401RE::uARM::MBED_A12::SD File System [OK] in 2.42 of 15 sec
Building library FAT (NUCLEO_F401RE, uARM)
Building project SD_PERF_STDIO (NUCLEO_F401RE, uARM)
Compile: main.cpp
Link: sd_perf_stdio
Elf2Bin: sd_perf_stdio
TargetTest::NUCLEO_F401RE::uARM::PERF_1::SD Stdio R/W Speed [OK] in 5.42 of 15 sec
Building library FAT (NUCLEO_F401RE, uARM)
Building project SD_PERF_FHANDLE (NUCLEO_F401RE, uARM)
Compile: main.cpp
Link: sd_perf_fhandle
Elf2Bin: sd_perf_fhandle
TargetTest::NUCLEO_F401RE::uARM::PERF_2::SD FileHandle R/W Speed [OK] in 5.42 of 15 sec
Building library FAT (NUCLEO_F401RE, uARM)
Building project SD_PERF_FATFS (NUCLEO_F401RE, uARM)
Compile: main.cpp
Link: sd_perf_fatfs
Elf2Bin: sd_perf_fatfs
TargetTest::NUCLEO_F401RE::uARM::PERF_3::SD FatFS R/W Speed [OK] in 5.42 of 15 sec
Test summary:
+--------+---------------+-----------+----------+-------------------------------+--------------------+---------------+-------+
| Result | Target | Toolchain | Test ID | Test Description | Elapsed Time (sec) | Timeout (sec) | Loops |
+--------+---------------+-----------+----------+-------------------------------+--------------------+---------------+-------+
| OK | NUCLEO_F401RE | uARM | MBED_35 | FileSystem stat functionality | 1.42 | 15 | 1/1 |
| OK | NUCLEO_F401RE | uARM | MBED_A12 | SD File System | 2.42 | 15 | 1/1 |
| OK | NUCLEO_F401RE | uARM | PERF_1 | SD Stdio R/W Speed | 5.42 | 15 | 1/1 |
| OK | NUCLEO_F401RE | uARM | PERF_2 | SD FileHandle R/W Speed | 5.42 | 15 | 1/1 |
| OK | NUCLEO_F401RE | uARM | PERF_3 | SD FatFS R/W Speed | 5.42 | 15 | 1/1 |
+--------+---------------+-----------+----------+-------------------------------+--------------------+---------------+-------+
Result: 5 OK

Completed in 39.05 sec
```