Revised to support ability to have both SD and USB drives mounted.
Dependents: Multi-FileSystem Multi-FileSystem
Fork of FATFileSystem by
Diff: FATFileSystem.cpp
- Revision:
- 10:e58f6254f8a2
- Parent:
- 9:4000fad3b21f
--- a/FATFileSystem.cpp Sun Mar 13 00:58:52 2016 +0000 +++ b/FATFileSystem.cpp Sun Mar 13 19:11:54 2016 +0000 @@ -28,6 +28,19 @@ #include "FATFileHandle.h" #include "FATDirHandle.h" +// Define the maximum length for a filename. +// +// Can accept the _MAX_LFN from ffconf, but this could be big (e.g. 255). +// Generally this only inflates the stack frame for these functions. +// Can also set a smaller number if you know the maximum is less. +// example: "0:/12345678.abc\0" would be 16. +// keep in mind that something +// like "/usb/12345678.abc" is translated to "0:/12345678.abc" +// +// Various APIs will return the failure code if you try to pass too long +// of a path/name in. +// +#define _MAX_FN_LEN _MAX_LFN //#define DEBUG "FtFS" // ... @@ -111,8 +124,10 @@ FileHandle *FATFileSystem::open(const char* name, int flags) { INFO("open(%s) on filesystem [%s], drv [%s]", name, _name, _fsid); - char n[_MAX_LFN]; - sprintf(n, "%s/%s", _fsid, name); + char n[_MAX_FN_LEN]; + int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, name); + if (sn < 0) + return NULL; INFO(" :: open(%s)", n); /* POSIX flags -> FatFS open mode */ @@ -146,8 +161,11 @@ int FATFileSystem::remove(const char *name) { INFO("remove(%s) on filesystem [%s], drv [%s]", name, _name, _fsid); - char n[_MAX_LFN]; - sprintf(n, "%s/%s", _fsid, name); + char n[_MAX_FN_LEN]; + int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, name); + if (sn < 0) + return -1; + INFO(" :: remove(%s)", n); FRESULT res = f_unlink(n); if (res) { @@ -158,14 +176,17 @@ } int FATFileSystem::rename(const char *oldname, const char *newname) { - char o[_MAX_LFN], n[_MAX_LFN]; - sprintf(o, "%s/%s", _fsid, oldname); - sprintf(n, "%s/%s", _fsid, newname); - INFO(" :: rename(%s,%s)", o, n); - FRESULT res = f_rename(oldname, newname); + char o[_MAX_FN_LEN], n[_MAX_FN_LEN]; + int sn = snprintf(o, _MAX_FN_LEN, "%s/%s", _fsid, oldname); + if (sn < 0) + return -1; + sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, newname); + if (sn < 0) + return -1; + FRESULT res = f_rename(o, n); if (res) { INFO("f_rename() failed: %d", res); - return -1; + return -res; } return 0; } @@ -181,8 +202,10 @@ DirHandle *FATFileSystem::opendir(const char *name) { FATFS_DIR dir; - char n[_MAX_LFN]; - sprintf(n, "%s/%s", _fsid, name); + char n[_MAX_FN_LEN]; + int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, name); + if (sn < 0) + return NULL; FRESULT res = f_opendir(&dir, n); if (res != 0) { return NULL; @@ -191,8 +214,10 @@ } int FATFileSystem::mkdir(const char *name, mode_t mode) { - char n[_MAX_LFN]; - sprintf(n, "%s/%s", _fsid, name); + char n[_MAX_FN_LEN]; + int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, name); + if (sn < 0) + return -1; FRESULT res = f_mkdir(n); return res == 0 ? 0 : -1; } @@ -209,3 +234,16 @@ return res == 0 ? 0 : -1; } +#if 0 +// I think this cannot work unless the FileSystemLike and other interfaces +// are updated. +int FATFileSystem::fstat(const char *path, FILINFO *fileinfo) { + char n[_MAX_FN_LEN]; + int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, path); + if (sn < 0) + return -1; + FRESULT res = f_stat(n, fileinfo); + printf("f_stat(%s) returned: %d\r\n", n, res); + return res == 0 ? 0 : -res; +} +#endif \ No newline at end of file