@@ -149,9 +149,9 @@ int addFiles(const char* dirname, const char* subPath) {
149
149
// Open directory
150
150
if ((dir = opendir (dirPath.c_str ())) != NULL ) {
151
151
152
- // Read files from directory.
152
+ // Read files from directory.
153
153
while ((ent = readdir (dir)) != NULL ) {
154
- // Ignore dir itself.
154
+ // Ignore dir itself.
155
155
if (ent->d_name [0 ] == ' .' )
156
156
continue ;
157
157
@@ -237,6 +237,32 @@ bool dirExists(const char* path) {
237
237
return false ;
238
238
}
239
239
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
+
240
266
/* *
241
267
* @brief Unpack file from file system.
242
268
* @param spiffsFile SPIFFS dir entry pointer.
@@ -294,13 +320,7 @@ bool unpackFiles(std::string sDest) {
294
320
std::cout << " Directory " << sDest << " does not exists. Try to create it." << std::endl;
295
321
296
322
// 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 ())) {
304
324
return false ;
305
325
}
306
326
}
@@ -313,7 +333,23 @@ bool unpackFiles(std::string sDest) {
313
333
while (it) {
314
334
// Check if content is a file.
315
335
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
+ }
317
353
318
354
// Unpack file to destination directory.
319
355
if (! unpackFile (it, sDestFilePath .c_str ()) ) {
@@ -331,8 +367,9 @@ bool unpackFiles(std::string sDest) {
331
367
<< std::endl;
332
368
}
333
369
370
+ // Get next file handle.
334
371
it = SPIFFS_readdir (&dir, &ent);
335
- }
372
+ } // end while
336
373
337
374
// Close directory.
338
375
SPIFFS_closedir (&dir);
0 commit comments