Skip to content

Commit 66a9933

Browse files
committed
unpack dirs recursievly
1 parent 2ecc11f commit 66a9933

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

main.cpp

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ int addFiles(const char* dirname, const char* subPath) {
149149
// Open directory
150150
if ((dir = opendir (dirPath.c_str())) != NULL) {
151151

152-
// Read files from directory.
152+
// Read files from directory.
153153
while ((ent = readdir (dir)) != NULL) {
154-
// Ignore dir itself.
154+
// Ignore dir itself.
155155
if (ent->d_name[0] == '.')
156156
continue;
157157

@@ -237,6 +237,32 @@ bool dirExists(const char* path) {
237237
return false;
238238
}
239239

240+
/**
241+
* @brief Create directory if it not exists.
242+
* @param path Directory path.
243+
* @return True or false.
244+
*
245+
* @author Pascal Gollor (http://www.pgollor.de/cms/)
246+
*/
247+
bool dirCreate(const char* path) {
248+
// Check if directory also exists.
249+
if (dirExists(path)) {
250+
return false;
251+
}
252+
253+
// platform stuff...
254+
#if defined(_WIN32)
255+
if (_mkdir(sDest.c_str()) != 0) {
256+
#else
257+
if (mkdir(path, S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH) != 0) {
258+
#endif
259+
std::cerr << "Can not create directory!!!" << std::endl;
260+
return false;
261+
}
262+
263+
return true;
264+
}
265+
240266
/**
241267
* @brief Unpack file from file system.
242268
* @param spiffsFile SPIFFS dir entry pointer.
@@ -294,13 +320,7 @@ bool unpackFiles(std::string sDest) {
294320
std::cout << "Directory " << sDest << " does not exists. Try to create it." << std::endl;
295321

296322
// Try to create directory.
297-
// platform stuff...
298-
#if defined(_WIN32)
299-
if (_mkdir(sDest.c_str()) != 0) {
300-
#else
301-
if (mkdir(sDest.c_str(), S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH) != 0) {
302-
#endif
303-
std::cerr << "Can not create directory!!!" << std::endl;
323+
if (! dirCreate(sDest.c_str())) {
304324
return false;
305325
}
306326
}
@@ -313,7 +333,23 @@ bool unpackFiles(std::string sDest) {
313333
while (it) {
314334
// Check if content is a file.
315335
if ((int)(it->type) == 1) {
316-
std::string sDestFilePath = sDest + (const char*)(it->name);
336+
std::string name = (const char*)(it->name);
337+
std::string sDestFilePath = sDest + name;
338+
size_t pos = name.find_last_of("/");
339+
340+
// If file is in sub directory?
341+
if (pos > 0) {
342+
// Subdir path.
343+
std::string path = sDest;
344+
path += name.substr(0, pos);
345+
346+
// Create subddir if subdir not exists.
347+
if (!dirExists(path.c_str())) {
348+
if (!dirCreate(path.c_str())) {
349+
return false;
350+
}
351+
}
352+
}
317353

318354
// Unpack file to destination directory.
319355
if (! unpackFile(it, sDestFilePath.c_str()) ) {
@@ -331,8 +367,9 @@ bool unpackFiles(std::string sDest) {
331367
<< std::endl;
332368
}
333369

370+
// Get next file handle.
334371
it = SPIFFS_readdir(&dir, &ent);
335-
}
372+
} // end while
336373

337374
// Close directory.
338375
SPIFFS_closedir(&dir);

0 commit comments

Comments
 (0)