Skip to content

Commit f0c65da

Browse files
committed
Fix allocation algorithm
1 parent 46aff55 commit f0c65da

File tree

6 files changed

+54
-37
lines changed

6 files changed

+54
-37
lines changed

examples/AnalogBinLogger/AnalogBinLogger.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ void binaryToCsv() {
436436
return;
437437
}
438438
binFile.rewind();
439-
if (!binFile.read(&buf , 512) == 512) {
439+
if (binFile.read(&buf , 512) != 512) {
440440
error("Read metadata failed");
441441
}
442442
// Create a new csv file.
@@ -517,7 +517,7 @@ void checkOverrun() {
517517
binFile.rewind();
518518
Serial.println();
519519
Serial.println(F("Checking overrun errors - type any character to stop"));
520-
if (!binFile.read(&buf , 512) == 512) {
520+
if (binFile.read(&buf , 512) != 512) {
521521
error("Read metadata failed");
522522
}
523523
bn++;

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SdFat
2-
version=1.0.7
2+
version=1.0.8
33
author=Bill Greiman <[email protected]>
44
maintainer=Bill Greiman <[email protected]>
55
sentence=FAT16/FAT32 file system for SD cards.

src/FatLib/FatApiConstants.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
*/
2525
#ifndef FatApiConstants_h
2626
#define FatApiConstants_h
27+
// Temp fix for particle mesh.
28+
#ifdef O_RDONLY
29+
#undef O_RDONLY
30+
#endif // O_RDONLY
31+
#ifdef O_RDWR
32+
#undef O_RDWR
33+
#endif // O_RDWR
34+
#ifdef O_WRONLY
35+
#undef O_WRONLY
36+
#endif // O_WRONLY
2737
//------------------------------------------------------------------------------
2838
// use the gnu style oflag in open()
2939
/** open() oflag for reading */

src/FatLib/FatVolume.cpp

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,33 @@ bool FatCache::sync() {
7171
}
7272
//------------------------------------------------------------------------------
7373
bool FatVolume::allocateCluster(uint32_t current, uint32_t* next) {
74-
uint32_t find = current ? current : m_allocSearchStart;
75-
uint32_t start = find;
74+
uint32_t find;
75+
bool setStart;
76+
if (m_allocSearchStart < current) {
77+
// Try to keep file contiguous. Start just after current cluster.
78+
find = current;
79+
setStart = false;
80+
} else {
81+
find = m_allocSearchStart;
82+
setStart = true;
83+
}
7684
while (1) {
7785
find++;
78-
// If at end of FAT go to beginning of FAT.
7986
if (find > m_lastCluster) {
80-
find = 2;
87+
if (setStart) {
88+
// Can't find space, checked all clusters.
89+
DBG_FAIL_MACRO;
90+
goto fail;
91+
}
92+
find = m_allocSearchStart;
93+
setStart = true;
94+
continue;
8195
}
96+
if (find == current) {
97+
// Can't find space, already searched clusters after current.
98+
DBG_FAIL_MACRO;
99+
goto fail;
100+
}
82101
uint32_t f;
83102
int8_t fg = fatGet(find, &f);
84103
if (fg < 0) {
@@ -88,27 +107,22 @@ bool FatVolume::allocateCluster(uint32_t current, uint32_t* next) {
88107
if (fg && f == 0) {
89108
break;
90109
}
91-
if (find == start) {
92-
// Can't find space checked all clusters.
93-
DBG_FAIL_MACRO;
94-
goto fail;
95-
}
96110
}
97-
// mark end of chain
111+
if (setStart) {
112+
m_allocSearchStart = find;
113+
}
114+
// Mark end of chain.
98115
if (!fatPutEOC(find)) {
99116
DBG_FAIL_MACRO;
100117
goto fail;
101118
}
102119
if (current) {
103-
// link clusters
120+
// Link clusters.
104121
if (!fatPut(current, find)) {
105122
DBG_FAIL_MACRO;
106123
goto fail;
107124
}
108-
} else {
109-
// Remember place for search start.
110-
m_allocSearchStart = find;
111-
}
125+
}
112126
updateFreeClusterCount(-1);
113127
*next = find;
114128
return true;
@@ -126,14 +140,14 @@ bool FatVolume::allocContiguous(uint32_t count, uint32_t* firstCluster) {
126140
// end of group
127141
uint32_t endCluster;
128142
// Start at cluster after last allocated cluster.
129-
uint32_t startCluster = m_allocSearchStart;
130-
endCluster = bgnCluster = startCluster + 1;
143+
endCluster = bgnCluster = m_allocSearchStart + 1;
131144

132145
// search the FAT for free clusters
133146
while (1) {
134-
// If past end - start from beginning of FAT.
135147
if (endCluster > m_lastCluster) {
136-
bgnCluster = endCluster = 2;
148+
// Can't find space.
149+
DBG_FAIL_MACRO;
150+
goto fail;
137151
}
138152
uint32_t f;
139153
int8_t fg = fatGet(endCluster, &f);
@@ -142,29 +156,22 @@ bool FatVolume::allocContiguous(uint32_t count, uint32_t* firstCluster) {
142156
goto fail;
143157
}
144158
if (f || fg == 0) {
145-
// cluster in use try next cluster as bgnCluster
146-
bgnCluster = endCluster + 1;
147-
148159
// don't update search start if unallocated clusters before endCluster.
149160
if (bgnCluster != endCluster) {
150161
setStart = false;
151162
}
163+
// cluster in use try next cluster as bgnCluster
164+
bgnCluster = endCluster + 1;
152165
} else if ((endCluster - bgnCluster + 1) == count) {
153166
// done - found space
154167
break;
155168
}
156-
// Can't find space if all clusters checked.
157-
if (startCluster == endCluster) {
158-
DBG_FAIL_MACRO;
159-
goto fail;
160-
}
161169
endCluster++;
162170
}
163-
// remember possible next free cluster
171+
// Remember possible next free cluster.
164172
if (setStart) {
165-
m_allocSearchStart = endCluster + 1;
173+
m_allocSearchStart = endCluster;
166174
}
167-
168175
// mark end of chain
169176
if (!fatPutEOC(endCluster)) {
170177
DBG_FAIL_MACRO;
@@ -355,8 +362,8 @@ bool FatVolume::freeChain(uint32_t cluster) {
355362
// Add one to count of free clusters.
356363
updateFreeClusterCount(1);
357364

358-
if (cluster < m_allocSearchStart) {
359-
m_allocSearchStart = cluster;
365+
if (cluster <= m_allocSearchStart) {
366+
m_allocSearchStart = cluster - 1;
360367
}
361368
cluster = next;
362369
} while (fg);

src/FatLib/FatVolume.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class FatVolume {
255255
*
256256
* \param[in] n cluster number.
257257
* \param[out] v value of entry
258-
* \return true for success or false for failure
258+
* \return -1 error, 0 EOC, else 1.
259259
*/
260260
int8_t dbgFat(uint32_t n, uint32_t* v) {
261261
return fatGet(n, v);

src/SdFat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#endif // INCLUDE_SDIOS
3838
//------------------------------------------------------------------------------
3939
/** SdFat version */
40-
#define SD_FAT_VERSION "1.0.7"
40+
#define SD_FAT_VERSION "1.0.8"
4141
//==============================================================================
4242
/**
4343
* \class SdBaseFile

0 commit comments

Comments
 (0)