-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Expand file tree
/
Copy pathFSTools.cpp
More file actions
254 lines (214 loc) · 5.73 KB
/
FSTools.cpp
File metadata and controls
254 lines (214 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "FSTools.h"
#include "LittleFS.h"
#include <spiffs_api.h>
#include <Esp.h>
#if defined(DEBUG_ESP_CORE)
#define FSTOOLSDEBUG(_1, ...) { DEBUG_ESP_PORT.printf_P( PSTR(_1),##__VA_ARGS__); }
#else
#define FSTOOLSDEBUG(...) {}
#endif
FSTools::FSTools()
{
}
FSTools::~FSTools()
{
reset();
}
bool FSTools::attemptToMountFS(fs::FS & fs)
{
LittleFSConfig littleFSCfg(false);
SPIFFSConfig SPIFFSCfg(false);
// try to apply the "safe" no format config to the FS... doesn't matter which.. just need one to apply correctly..
if (!fs.setConfig(littleFSCfg) && ! fs.setConfig(SPIFFSCfg))
{
return false;
}
return fs.begin();
}
bool FSTools::mountAlternativeFS(FST::FS_t type, const FST::layout & layout, bool keepMounted)
{
FSConfig * pCfg{nullptr};
LittleFSConfig littleFSCfg(false);
SPIFFSConfig SPIFFSCfg(false);
reset();
switch (type)
{
case FST::SPIFFS :
{
_pFS.reset(new FS(FSImplPtr(new spiffs_impl::SPIFFSImpl(_getStartAddr(layout), _getSize(layout), layout.page, layout.block, 5))));
pCfg = &SPIFFSCfg;
break;
}
case FST::LITTLEFS :
{
_pFS.reset(new FS(FSImplPtr(new littlefs_impl::LittleFSImpl(_getStartAddr(layout), _getSize(layout), layout.page, layout.block, 5))));
pCfg = &littleFSCfg;
break;
}
};
if (_pFS && pCfg && _pFS->setConfig(*pCfg) && _pFS->begin())
{
if (!keepMounted)
{
_pFS->end();
}
_mounted = true;
_layout = &layout;
return true;
}
if (_pFS)
{
_pFS.reset();
}
_mounted = false;
return false;
};
bool FSTools::mounted()
{
return _mounted;
};
bool FSTools::moveFS(fs::FS & destinationFS)
{
uint32_t sourceFileCount = 0;
uint32_t sourceByteTotal = 0;
bool result = false;
if (!_mounted || !_pFS)
{
FSTOOLSDEBUG("Source FS not mounted\n");
return false;
}
uint32_t startSector = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
uint32_t lowestFSStart = 0x40300000;
if (_layout)
{
lowestFSStart = _layout->startAddr;
FSTOOLSDEBUG("_layout->startADDR = 0x%08x\n", _layout->startAddr);
}
uint32_t endSector = lowestFSStart - 0x40200000;
uint32_t tempFSsize = endSector - startSector;
FSTOOLSDEBUG("TempFS: start: %u, end: %u, size: %u, sketchSize = %u, _FS_start = %u\n", startSector, endSector, tempFSsize, ESP.getSketchSize(), (uint32_t)&_FS_start);
fileListIterator(*_pFS, "/", [&sourceFileCount, &sourceByteTotal, this](File & f)
{
if (f)
{
sourceFileCount++;
sourceByteTotal += f.size();
#ifdef DEBUG_ESP_CORE
_dumpFileInfo(f);
#endif
}
});
FSTOOLSDEBUG("%u Files Found Total Size = %u\n", sourceFileCount, sourceByteTotal);
FSTOOLSDEBUG("Size of dummy FS = %u\n", tempFSsize);
FS tempFS = FS(FSImplPtr(new littlefs_impl::LittleFSImpl(startSector, tempFSsize, FS_PHYS_PAGE, FS_PHYS_BLOCK, 5)));
if (tempFS.format() && tempFS.begin())
{
if (_copyFS(*_pFS, tempFS))
{
FSTOOLSDEBUG("Files copied to temp File System\n");
reset();
if (destinationFS.format() && destinationFS.begin()) // must format then mount the new FS
{
if (_copyFS(tempFS, destinationFS))
{
FSTOOLSDEBUG("Files copied back to new FS\n");
result = true;
}
}
else
{
FSTOOLSDEBUG("Error Mounting\n");
}
}
else
{
FSTOOLSDEBUG("Copy Failed\n");
}
tempFS.end();
}
else
{
FSTOOLSDEBUG("Failed to begin() TempFS\n");
}
return result;
};
void FSTools::reset()
{
_mounted = false;
_layout = nullptr;
if (_pFS)
{
_pFS->end();
_pFS.reset();
}
}
void FSTools::fileListIterator(FS & fs, const char * dirName, FST::FileCb Cb)
{
Dir dir = fs.openDir(dirName);
while (dir.next())
{
if (dir.isFile())
{
File f = dir.openFile("r");
if (Cb)
{
Cb(f);
}
}
else
{
fileListIterator(fs, dir.fileName().c_str(), Cb);
}
}
}
uint32_t FSTools::_getStartAddr(const FST::layout & layout)
{
return (layout.startAddr - 0x40200000);
}
uint32_t FSTools::_getSize(const FST::layout & layout)
{
return (layout.endAddr - layout.startAddr);
}
#ifdef DEBUG_ESP_CORE
void FSTools::_dumpFileInfo(File & f)
{
if (f)
{
DEBUG_ESP_PORT.printf_P(PSTR(" File: %-30s [%8uB]\n"), f.fullName(), f.size());
}
}
#endif
bool FSTools::_copyFS(FS & sourceFS, FS & destFS)
{
uint32_t sourceFileCount = 0;
uint32_t sourceByteTotal = 0;
fileListIterator(sourceFS, "/", [&sourceFileCount, &sourceByteTotal](File & f)
{
if (f)
{
sourceFileCount++;
sourceByteTotal += f.size();
}
});
size_t count = 0;
fileListIterator(sourceFS, "/", [&count, &destFS](File & sourceFile)
{
if (sourceFile)
{
File destFile = destFS.open(sourceFile.fullName(), "w");
if (destFile)
{
destFile.setTimeout(5000); // this value was chosen empirically as it failed with default timeout.
size_t written = destFile.write(sourceFile);
if (written == sourceFile.size())
{
count++;
}
}
destFile.close();
sourceFile.close();
yield();
}
});
return (count == sourceFileCount);
}