@@ -163,27 +163,55 @@ int addFile(char* name, const char* path) {
163
163
return 0 ;
164
164
}
165
165
166
- int addFiles (const char * dirname) {
166
+ int addFiles (const char * dirname, const char * subPath) {
167
167
DIR *dir;
168
168
struct dirent *ent;
169
169
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.
171
177
while ((ent = readdir (dir)) != NULL ) {
178
+ // Ignore dir itself.
172
179
if (ent->d_name [0 ] == ' .' )
173
180
continue ;
174
- std::string fullpath = dirname;
175
- fullpath += ' / ' ;
181
+
182
+ std::string fullpath = dirPath ;
176
183
fullpath += ent->d_name ;
177
184
struct stat path_stat;
178
185
stat (fullpath.c_str (), &path_stat);
186
+
179
187
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
+ }
182
207
}
183
208
184
- std::string filepath = " /" ;
209
+ // Filepath with dirname as root folder.
210
+ std::string filepath = subPath;
185
211
filepath += ent->d_name ;
186
212
std::cout << filepath << std::endl;
213
+
214
+ // Add File to image.
187
215
if (addFile ((char *)filepath.c_str (), fullpath.c_str ()) != 0 ) {
188
216
std::cerr << " error adding file!" << std::endl;
189
217
error = true ;
@@ -192,12 +220,13 @@ int addFiles(const char* dirname){
192
220
}
193
221
break ;
194
222
}
195
- }
223
+ } // end while
196
224
closedir (dir);
197
225
} else {
198
226
std::cerr << " warning: can't read source directory" << std::endl;
199
227
return 1 ;
200
228
}
229
+
201
230
return (error) ? 1 : 0 ;
202
231
}
203
232
@@ -235,6 +264,32 @@ bool dirExists(const char* path) {
235
264
return false ;
236
265
}
237
266
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
+
238
293
/* *
239
294
* @brief Unpack file from file system.
240
295
* @param spiffsFile SPIFFS dir entry pointer.
@@ -283,7 +338,7 @@ bool unpackFiles(std::string sDest) {
283
338
spiffs_dirent ent;
284
339
285
340
// 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 ) {
287
342
sDest = " ./" + sDest ;
288
343
}
289
344
@@ -292,13 +347,7 @@ bool unpackFiles(std::string sDest) {
292
347
std::cout << " Directory " << sDest << " does not exists. Try to create it." << std::endl;
293
348
294
349
// 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 ())) {
302
351
return false ;
303
352
}
304
353
}
@@ -311,7 +360,23 @@ bool unpackFiles(std::string sDest) {
311
360
while (it) {
312
361
// Check if content is a file.
313
362
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
+ }
315
380
316
381
// Unpack file to destination directory.
317
382
if (! unpackFile (it, sDestFilePath .c_str ()) ) {
@@ -329,8 +394,9 @@ bool unpackFiles(std::string sDest) {
329
394
<< std::endl;
330
395
}
331
396
397
+ // Get next file handle.
332
398
it = SPIFFS_readdir (&dir, &ent);
333
- }
399
+ } // end while
334
400
335
401
// Close directory.
336
402
SPIFFS_closedir (&dir);
@@ -350,7 +416,7 @@ int actionPack() {
350
416
}
351
417
352
418
spiffsFormat ();
353
- int result = addFiles (s_dirName.c_str ());
419
+ int result = addFiles (s_dirName.c_str (), " / " );
354
420
spiffsUnmount ();
355
421
356
422
fwrite (&s_flashmem[0 ], 4 , s_flashmem.size ()/4 , fdres);
0 commit comments