Skip to content

Commit b556075

Browse files
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
1 parent 82adc95 commit b556075

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

cores/esp8266/FS.h

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <memory>
2525
#include <Arduino.h>
2626

27+
class SDClass;
28+
2729
namespace fs {
2830

2931
class File;
@@ -208,8 +210,10 @@ class FS
208210

209211
bool gc();
210212

213+
friend class ::SDClass; // More of a frenemy, but SD needs internal implementation to get private FAT bits
211214
protected:
212215
FSImplPtr _impl;
216+
FSImplPtr getImpl() { return _impl; }
213217
};
214218

215219
} // namespace fs

libraries/SD/src/SD.h

+21-7
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,49 @@ class SDClass {
8484
}
8585

8686
uint8_t type() {
87-
return 0;//card.type();
87+
sdfs::SDFSImpl* sd = static_cast<sdfs::SDFSImpl*>(SDFS.getImpl().get());
88+
return sd->type();
8889
}
8990

9091
uint8_t fatType() {
91-
return 0;//volume.fatType();
92+
sdfs::SDFSImpl* sd = static_cast<sdfs::SDFSImpl*>(SDFS.getImpl().get());
93+
return sd->fatType();
9294
}
9395

9496
size_t blocksPerCluster() {
95-
return 0;//volume.blocksPerCluster();
97+
sdfs::SDFSImpl* sd = static_cast<sdfs::SDFSImpl*>(SDFS.getImpl().get());
98+
return sd->blocksPerCluster();
9699
}
97100

98101
size_t totalClusters() {
99-
return 0;//volume.clusterCount();
102+
sdfs::SDFSImpl* sd = static_cast<sdfs::SDFSImpl*>(SDFS.getImpl().get());
103+
return sd->totalClusters();
100104
}
101105

102106
size_t blockSize() {
103107
return 512;
104108
}
105109

106110
size_t totalBlocks() {
107-
return 0;//(totalClusters() / blocksPerCluster());
111+
return (totalClusters() / blocksPerCluster());
108112
}
109113

110114
size_t clusterSize() {
111-
return 0;//blocksPerCluster() * blockSize();
115+
return blocksPerCluster() * blockSize();
112116
}
113117

114118
size_t size() {
115-
return 0;//(clusterSize() * totalClusters());
119+
uint64_t sz = size64();
120+
#ifdef DEBUG_ESP_PORT
121+
if (sz > (uint64_t)SIZE_MAX) {
122+
DEBUG_ESP_PORT.printf_P(PSTR("WARNING: SD card size overflow (%lld>= 4GB). Please update source to use size64().\n"), sz);
123+
}
124+
#endif
125+
return (size_t)sz;
126+
}
127+
128+
uint64_t size64() {
129+
return ((uint64_t)clusterSize() * (uint64_t)totalClusters());
116130
}
117131

118132
private:

libraries/SDFS/src/SDFS.h

+24
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,30 @@ class SDFSImpl : public FSImpl
159159

160160
bool format() override;
161161

162+
// The following are not common FS interfaces, but are needed only to
163+
// support the older SD.h exports
164+
uint8_t type() {
165+
return _fs.card()->type();
166+
}
167+
uint8_t fatType() {
168+
return _fs.vol()->fatType();
169+
}
170+
size_t blocksPerCluster() {
171+
return _fs.vol()->blocksPerCluster();
172+
}
173+
size_t totalClusters() {
174+
return _fs.vol()->clusterCount();
175+
}
176+
size_t totalBlocks() {
177+
return (totalClusters() / blocksPerCluster());
178+
}
179+
size_t clusterSize() {
180+
return blocksPerCluster() * 512; // 512b block size
181+
}
182+
size_t size() {
183+
return (clusterSize() * totalClusters());
184+
}
185+
162186
protected:
163187
friend class SDFileImpl;
164188
friend class SDFSDirImpl;

0 commit comments

Comments
 (0)