Skip to content

Commit 2e806e9

Browse files
committed
Merge branch 'master' into spiffs-0.3.3
2 parents 1149cad + 85f71fd commit 2e806e9

File tree

1 file changed

+85
-19
lines changed

1 file changed

+85
-19
lines changed

main.cpp

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -163,27 +163,55 @@ int addFile(char* name, const char* path) {
163163
return 0;
164164
}
165165

166-
int addFiles(const char* dirname){
166+
int addFiles(const char* dirname, const char* subPath) {
167167
DIR *dir;
168168
struct dirent *ent;
169169
bool error = false;
170-
if ((dir = opendir (dirname)) != NULL) {
170+
std::string dirPath = dirname;
171+
dirPath += subPath;
172+
173+
// Open directory
174+
if ((dir = opendir (dirPath.c_str())) != NULL) {
175+
176+
// Read files from directory.
171177
while ((ent = readdir (dir)) != NULL) {
178+
// Ignore dir itself.
172179
if (ent->d_name[0] == '.')
173180
continue;
174-
std::string fullpath = dirname;
175-
fullpath += '/';
181+
182+
std::string fullpath = dirPath;
176183
fullpath += ent->d_name;
177184
struct stat path_stat;
178185
stat (fullpath.c_str(), &path_stat);
186+
179187
if (!S_ISREG(path_stat.st_mode)) {
180-
std::cerr << "skipping " << ent->d_name << std::endl;
181-
continue;
188+
// Check if path is a directory.
189+
if (S_ISDIR(path_stat.st_mode)) {
190+
// Prepare new sub path.
191+
std::string newSubPath = subPath;
192+
newSubPath += ent->d_name;
193+
newSubPath += "/";
194+
195+
if (addFiles(dirname, newSubPath.c_str()) != 0)
196+
{
197+
std::cerr << "Error for adding content from " << ent->d_name << "!" << std::endl;
198+
}
199+
200+
continue;
201+
}
202+
else
203+
{
204+
std::cerr << "skipping " << ent->d_name << std::endl;
205+
continue;
206+
}
182207
}
183208

184-
std::string filepath = "/";
209+
// Filepath with dirname as root folder.
210+
std::string filepath = subPath;
185211
filepath += ent->d_name;
186212
std::cout << filepath << std::endl;
213+
214+
// Add File to image.
187215
if (addFile((char*)filepath.c_str(), fullpath.c_str()) != 0) {
188216
std::cerr << "error adding file!" << std::endl;
189217
error = true;
@@ -192,12 +220,13 @@ int addFiles(const char* dirname){
192220
}
193221
break;
194222
}
195-
}
223+
} // end while
196224
closedir (dir);
197225
} else {
198226
std::cerr << "warning: can't read source directory" << std::endl;
199227
return 1;
200228
}
229+
201230
return (error) ? 1 : 0;
202231
}
203232

@@ -235,6 +264,32 @@ bool dirExists(const char* path) {
235264
return false;
236265
}
237266

267+
/**
268+
* @brief Create directory if it not exists.
269+
* @param path Directory path.
270+
* @return True or false.
271+
*
272+
* @author Pascal Gollor (http://www.pgollor.de/cms/)
273+
*/
274+
bool dirCreate(const char* path) {
275+
// Check if directory also exists.
276+
if (dirExists(path)) {
277+
return false;
278+
}
279+
280+
// platform stuff...
281+
#if defined(_WIN32)
282+
if (_mkdir(path) != 0) {
283+
#else
284+
if (mkdir(path, S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH) != 0) {
285+
#endif
286+
std::cerr << "Can not create directory!!!" << std::endl;
287+
return false;
288+
}
289+
290+
return true;
291+
}
292+
238293
/**
239294
* @brief Unpack file from file system.
240295
* @param spiffsFile SPIFFS dir entry pointer.
@@ -283,7 +338,7 @@ bool unpackFiles(std::string sDest) {
283338
spiffs_dirent ent;
284339

285340
// Add "./" to path if is not given.
286-
if (sDest.find("./") == std::string::npos) {
341+
if (sDest.find("./") == std::string::npos && sDest.find("/") == std::string::npos) {
287342
sDest = "./" + sDest;
288343
}
289344

@@ -292,13 +347,7 @@ bool unpackFiles(std::string sDest) {
292347
std::cout << "Directory " << sDest << " does not exists. Try to create it." << std::endl;
293348

294349
// Try to create directory.
295-
// platform stuff...
296-
#if defined(_WIN32)
297-
if (_mkdir(sDest.c_str()) != 0) {
298-
#else
299-
if (mkdir(sDest.c_str(), S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH) != 0) {
300-
#endif
301-
std::cerr << "Can not create directory!!!" << std::endl;
350+
if (! dirCreate(sDest.c_str())) {
302351
return false;
303352
}
304353
}
@@ -311,7 +360,23 @@ bool unpackFiles(std::string sDest) {
311360
while (it) {
312361
// Check if content is a file.
313362
if ((int)(it->type) == 1) {
314-
std::string sDestFilePath = sDest + (const char*)(it->name);
363+
std::string name = (const char*)(it->name);
364+
std::string sDestFilePath = sDest + name;
365+
size_t pos = name.find_last_of("/");
366+
367+
// If file is in sub directory?
368+
if (pos > 0) {
369+
// Subdir path.
370+
std::string path = sDest;
371+
path += name.substr(0, pos);
372+
373+
// Create subddir if subdir not exists.
374+
if (!dirExists(path.c_str())) {
375+
if (!dirCreate(path.c_str())) {
376+
return false;
377+
}
378+
}
379+
}
315380

316381
// Unpack file to destination directory.
317382
if (! unpackFile(it, sDestFilePath.c_str()) ) {
@@ -329,8 +394,9 @@ bool unpackFiles(std::string sDest) {
329394
<< std::endl;
330395
}
331396

397+
// Get next file handle.
332398
it = SPIFFS_readdir(&dir, &ent);
333-
}
399+
} // end while
334400

335401
// Close directory.
336402
SPIFFS_closedir(&dir);
@@ -350,7 +416,7 @@ int actionPack() {
350416
}
351417

352418
spiffsFormat();
353-
int result = addFiles(s_dirName.c_str());
419+
int result = addFiles(s_dirName.c_str(), "/");
354420
spiffsUnmount();
355421

356422
fwrite(&s_flashmem[0], 4, s_flashmem.size()/4, fdres);

0 commit comments

Comments
 (0)