A fork of the original SDFileSystem, added only stat() for getting file information.

Dependents:   FRDM_K64F_IOT

Added code to the original SDFileSystem to export the stat() command. It would now be possible to get the FILEINFO struct of a directory entry to get information such as file size, etc.

SDFileSystem2 usage

#include "SDFileSystem.h"
SDFileSystem sd(p5,p6,p7,p8,"sd"); // mosi, miso, sck, cs 

static void cmd_ls(Stream * chp, int argc, char * argv[])
{
   DIR * dp;
   struct dirent * dirp;
   FILINFO fileInfo;
   char dirroot[256];
   
   if (argc >= 1)
       sprintf(dirroot, "/sd/%s", argv[0]);
   else
       sprintf(dirroot, "/sd");
   
   chp->printf("Listing directory [%s]\r\n", dirroot);
   
   dp = opendir(dirroot);			
   while((dirp = readdir(dp)) != NULL)
   {
       if (sd.stat(dirp->d_name, &fileInfo) == 0)
       {
           if (fileInfo.fattrib & AM_DIR )
                   chp->printf("<DIR>\t\t");
           else
                   chp->printf("%ld\t\t", fileInfo.fsize);
       }
       chp->printf("%s\r\n", dirp->d_name);
   }
   closedir(dp);
}
Committer:
vpcola
Date:
Tue Apr 28 16:55:30 2015 +0000
Revision:
0:572d27f56fcd
Added stat() to get file information

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vpcola 0:572d27f56fcd 1 /*-----------------------------------------------------------------------*/
vpcola 0:572d27f56fcd 2 /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2007 */
vpcola 0:572d27f56fcd 3 /*-----------------------------------------------------------------------*/
vpcola 0:572d27f56fcd 4 /* This is a stub disk I/O module that acts as front end of the existing */
vpcola 0:572d27f56fcd 5 /* disk I/O modules and attach it to FatFs module with common interface. */
vpcola 0:572d27f56fcd 6 /*-----------------------------------------------------------------------*/
vpcola 0:572d27f56fcd 7 #include "ffconf.h"
vpcola 0:572d27f56fcd 8 #include "diskio.h"
vpcola 0:572d27f56fcd 9
vpcola 0:572d27f56fcd 10 #include "mbed_debug.h"
vpcola 0:572d27f56fcd 11 #include "FATFileSystem.h"
vpcola 0:572d27f56fcd 12
vpcola 0:572d27f56fcd 13 using namespace mbed;
vpcola 0:572d27f56fcd 14
vpcola 0:572d27f56fcd 15 DSTATUS disk_initialize (
vpcola 0:572d27f56fcd 16 BYTE drv /* Physical drive nmuber (0..) */
vpcola 0:572d27f56fcd 17 )
vpcola 0:572d27f56fcd 18 {
vpcola 0:572d27f56fcd 19 debug_if(FFS_DBG, "disk_initialize on drv [%d]\n", drv);
vpcola 0:572d27f56fcd 20 return (DSTATUS)FATFileSystem::_ffs[drv]->disk_initialize();
vpcola 0:572d27f56fcd 21 }
vpcola 0:572d27f56fcd 22
vpcola 0:572d27f56fcd 23 DSTATUS disk_status (
vpcola 0:572d27f56fcd 24 BYTE drv /* Physical drive nmuber (0..) */
vpcola 0:572d27f56fcd 25 )
vpcola 0:572d27f56fcd 26 {
vpcola 0:572d27f56fcd 27 debug_if(FFS_DBG, "disk_status on drv [%d]\n", drv);
vpcola 0:572d27f56fcd 28 return (DSTATUS)FATFileSystem::_ffs[drv]->disk_status();
vpcola 0:572d27f56fcd 29 }
vpcola 0:572d27f56fcd 30
vpcola 0:572d27f56fcd 31 DRESULT disk_read (
vpcola 0:572d27f56fcd 32 BYTE drv, /* Physical drive nmuber (0..) */
vpcola 0:572d27f56fcd 33 BYTE *buff, /* Data buffer to store read data */
vpcola 0:572d27f56fcd 34 DWORD sector, /* Sector address (LBA) */
vpcola 0:572d27f56fcd 35 BYTE count /* Number of sectors to read (1..255) */
vpcola 0:572d27f56fcd 36 )
vpcola 0:572d27f56fcd 37 {
vpcola 0:572d27f56fcd 38 debug_if(FFS_DBG, "disk_read(sector %d, count %d) on drv [%d]\n", sector, count, drv);
vpcola 0:572d27f56fcd 39 for(DWORD s=sector; s<sector+count; s++) {
vpcola 0:572d27f56fcd 40 debug_if(FFS_DBG, " disk_read(sector %d)\n", s);
vpcola 0:572d27f56fcd 41 int res = FATFileSystem::_ffs[drv]->disk_read((uint8_t*)buff, s);
vpcola 0:572d27f56fcd 42 if(res) {
vpcola 0:572d27f56fcd 43 return RES_PARERR;
vpcola 0:572d27f56fcd 44 }
vpcola 0:572d27f56fcd 45 buff += 512;
vpcola 0:572d27f56fcd 46 }
vpcola 0:572d27f56fcd 47 return RES_OK;
vpcola 0:572d27f56fcd 48 }
vpcola 0:572d27f56fcd 49
vpcola 0:572d27f56fcd 50 #if _READONLY == 0
vpcola 0:572d27f56fcd 51 DRESULT disk_write (
vpcola 0:572d27f56fcd 52 BYTE drv, /* Physical drive nmuber (0..) */
vpcola 0:572d27f56fcd 53 const BYTE *buff, /* Data to be written */
vpcola 0:572d27f56fcd 54 DWORD sector, /* Sector address (LBA) */
vpcola 0:572d27f56fcd 55 BYTE count /* Number of sectors to write (1..255) */
vpcola 0:572d27f56fcd 56 )
vpcola 0:572d27f56fcd 57 {
vpcola 0:572d27f56fcd 58 debug_if(FFS_DBG, "disk_write(sector %d, count %d) on drv [%d]\n", sector, count, drv);
vpcola 0:572d27f56fcd 59 for(DWORD s = sector; s < sector + count; s++) {
vpcola 0:572d27f56fcd 60 debug_if(FFS_DBG, " disk_write(sector %d)\n", s);
vpcola 0:572d27f56fcd 61 int res = FATFileSystem::_ffs[drv]->disk_write((uint8_t*)buff, s);
vpcola 0:572d27f56fcd 62 if(res) {
vpcola 0:572d27f56fcd 63 return RES_PARERR;
vpcola 0:572d27f56fcd 64 }
vpcola 0:572d27f56fcd 65 buff += 512;
vpcola 0:572d27f56fcd 66 }
vpcola 0:572d27f56fcd 67 return RES_OK;
vpcola 0:572d27f56fcd 68 }
vpcola 0:572d27f56fcd 69 #endif /* _READONLY */
vpcola 0:572d27f56fcd 70
vpcola 0:572d27f56fcd 71 DRESULT disk_ioctl (
vpcola 0:572d27f56fcd 72 BYTE drv, /* Physical drive nmuber (0..) */
vpcola 0:572d27f56fcd 73 BYTE ctrl, /* Control code */
vpcola 0:572d27f56fcd 74 void *buff /* Buffer to send/receive control data */
vpcola 0:572d27f56fcd 75 )
vpcola 0:572d27f56fcd 76 {
vpcola 0:572d27f56fcd 77 debug_if(FFS_DBG, "disk_ioctl(%d)\n", ctrl);
vpcola 0:572d27f56fcd 78 switch(ctrl) {
vpcola 0:572d27f56fcd 79 case CTRL_SYNC:
vpcola 0:572d27f56fcd 80 if(FATFileSystem::_ffs[drv] == NULL) {
vpcola 0:572d27f56fcd 81 return RES_NOTRDY;
vpcola 0:572d27f56fcd 82 } else if(FATFileSystem::_ffs[drv]->disk_sync()) {
vpcola 0:572d27f56fcd 83 return RES_ERROR;
vpcola 0:572d27f56fcd 84 }
vpcola 0:572d27f56fcd 85 return RES_OK;
vpcola 0:572d27f56fcd 86 case GET_SECTOR_COUNT:
vpcola 0:572d27f56fcd 87 if(FATFileSystem::_ffs[drv] == NULL) {
vpcola 0:572d27f56fcd 88 return RES_NOTRDY;
vpcola 0:572d27f56fcd 89 } else {
vpcola 0:572d27f56fcd 90 DWORD res = FATFileSystem::_ffs[drv]->disk_sectors();
vpcola 0:572d27f56fcd 91 if(res > 0) {
vpcola 0:572d27f56fcd 92 *((DWORD*)buff) = res; // minimum allowed
vpcola 0:572d27f56fcd 93 return RES_OK;
vpcola 0:572d27f56fcd 94 } else {
vpcola 0:572d27f56fcd 95 return RES_ERROR;
vpcola 0:572d27f56fcd 96 }
vpcola 0:572d27f56fcd 97 }
vpcola 0:572d27f56fcd 98 case GET_BLOCK_SIZE:
vpcola 0:572d27f56fcd 99 *((DWORD*)buff) = 1; // default when not known
vpcola 0:572d27f56fcd 100 return RES_OK;
vpcola 0:572d27f56fcd 101
vpcola 0:572d27f56fcd 102 }
vpcola 0:572d27f56fcd 103 return RES_PARERR;
vpcola 0:572d27f56fcd 104 }