From b5560759ec841de72f75234070bcb12fdc2d56ea Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 19 May 2019 08:43:23 -0700 Subject: [PATCH] Re-add original SD FAT info access methods (#6092) Fixes #6081 The SD rewrite blanked out some of the internal FAT info.. Restore the function calls and return proper values. Because size() is used in many printf()s, we can't just change its return type to uint64. Instead, when size is > size-max warn. Add SD.size64 which can be used by new apps who care about >4GB cards. Prints a warning if debugging enabled --- cores/esp8266/FS.h | 4 ++++ libraries/SD/src/SD.h | 28 +++++++++++++++++++++------- libraries/SDFS/src/SDFS.h | 24 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index 2cc6acd12..7bf71692c 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -24,6 +24,8 @@ #include #include +class SDClass; + namespace fs { class File; @@ -208,8 +210,10 @@ public: bool gc(); + friend class ::SDClass; // More of a frenemy, but SD needs internal implementation to get private FAT bits protected: FSImplPtr _impl; + FSImplPtr getImpl() { return _impl; } }; } // namespace fs diff --git a/libraries/SD/src/SD.h b/libraries/SD/src/SD.h index 4f2dc5a7a..fd4b61e88 100644 --- a/libraries/SD/src/SD.h +++ b/libraries/SD/src/SD.h @@ -84,19 +84,23 @@ public: } uint8_t type() { - return 0;//card.type(); + sdfs::SDFSImpl* sd = static_cast(SDFS.getImpl().get()); + return sd->type(); } uint8_t fatType() { - return 0;//volume.fatType(); + sdfs::SDFSImpl* sd = static_cast(SDFS.getImpl().get()); + return sd->fatType(); } size_t blocksPerCluster() { - return 0;//volume.blocksPerCluster(); + sdfs::SDFSImpl* sd = static_cast(SDFS.getImpl().get()); + return sd->blocksPerCluster(); } size_t totalClusters() { - return 0;//volume.clusterCount(); + sdfs::SDFSImpl* sd = static_cast(SDFS.getImpl().get()); + return sd->totalClusters(); } size_t blockSize() { @@ -104,15 +108,25 @@ public: } size_t totalBlocks() { - return 0;//(totalClusters() / blocksPerCluster()); + return (totalClusters() / blocksPerCluster()); } size_t clusterSize() { - return 0;//blocksPerCluster() * blockSize(); + return blocksPerCluster() * blockSize(); } size_t size() { - return 0;//(clusterSize() * totalClusters()); + uint64_t sz = size64(); +#ifdef DEBUG_ESP_PORT + if (sz > (uint64_t)SIZE_MAX) { + DEBUG_ESP_PORT.printf_P(PSTR("WARNING: SD card size overflow (%lld>= 4GB). Please update source to use size64().\n"), sz); + } +#endif + return (size_t)sz; + } + + uint64_t size64() { + return ((uint64_t)clusterSize() * (uint64_t)totalClusters()); } private: diff --git a/libraries/SDFS/src/SDFS.h b/libraries/SDFS/src/SDFS.h index 44b017a1b..ecae30732 100644 --- a/libraries/SDFS/src/SDFS.h +++ b/libraries/SDFS/src/SDFS.h @@ -159,6 +159,30 @@ public: bool format() override; + // The following are not common FS interfaces, but are needed only to + // support the older SD.h exports + uint8_t type() { + return _fs.card()->type(); + } + uint8_t fatType() { + return _fs.vol()->fatType(); + } + size_t blocksPerCluster() { + return _fs.vol()->blocksPerCluster(); + } + size_t totalClusters() { + return _fs.vol()->clusterCount(); + } + size_t totalBlocks() { + return (totalClusters() / blocksPerCluster()); + } + size_t clusterSize() { + return blocksPerCluster() * 512; // 512b block size + } + size_t size() { + return (clusterSize() * totalClusters()); + } + protected: friend class SDFileImpl; friend class SDFSDirImpl;