@@ -139,38 +139,67 @@ int addFile(char* name, const char* path) {
139
139
return 0 ;
140
140
}
141
141
142
- int addFiles (const char * dirname) {
142
+ int addFiles (const char * dirname, const char * subPath) {
143
143
DIR *dir;
144
144
struct dirent *ent;
145
145
bool error = false ;
146
- if ((dir = opendir (dirname)) != NULL ) {
146
+ std::string dirPath = dirname;
147
+ dirPath += subPath;
148
+
149
+ // Open directory
150
+ if ((dir = opendir (dirPath.c_str ())) != NULL ) {
151
+
152
+ // Read files from directory.
147
153
while ((ent = readdir (dir)) != NULL ) {
154
+ // Ignore dir itself.
148
155
if (ent->d_name [0 ] == ' .' )
149
156
continue ;
150
- std::string fullpath = dirname;
151
- fullpath += ' / ' ;
157
+
158
+ std::string fullpath = dirPath ;
152
159
fullpath += ent->d_name ;
153
160
struct stat path_stat;
154
161
stat (fullpath.c_str (), &path_stat);
162
+
155
163
if (!S_ISREG (path_stat.st_mode )) {
156
- std::cerr << " skipping " << ent->d_name << std::endl;
157
- continue ;
164
+ // Check if path is a directory.
165
+ if (S_ISDIR (path_stat.st_mode )) {
166
+ // Prepare new sub path.
167
+ std::string newSubPath = subPath;
168
+ newSubPath += ent->d_name ;
169
+ newSubPath += " /" ;
170
+
171
+ if (addFiles (dirname, newSubPath.c_str ()) != 0 )
172
+ {
173
+ std::cerr << " Error for adding content from " << ent->d_name << " !" << std::endl;
174
+ }
175
+
176
+ continue ;
177
+ }
178
+ else
179
+ {
180
+ std::cerr << " skipping " << ent->d_name << std::endl;
181
+ continue ;
182
+ }
158
183
}
159
184
160
- std::string filepath = " /" ;
185
+ // Filepath with dirname as root folder.
186
+ std::string filepath = subPath;
161
187
filepath += ent->d_name ;
162
188
std::cout << filepath << std::endl;
189
+
190
+ // Add File to image.
163
191
if (addFile ((char *)filepath.c_str (), fullpath.c_str ()) != 0 ) {
164
192
std::cerr << " error adding file!" << std::endl;
165
193
error = true ;
166
194
break ;
167
195
}
168
- }
196
+ } // end while
169
197
closedir (dir);
170
198
} else {
171
199
std::cerr << " warning: can't read source directory" << std::endl;
172
200
return 1 ;
173
201
}
202
+
174
203
return (error) ? 1 : 0 ;
175
204
}
176
205
@@ -208,6 +237,32 @@ bool dirExists(const char* path) {
208
237
return false ;
209
238
}
210
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 (path) != 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
+
211
266
/* *
212
267
* @brief Unpack file from file system.
213
268
* @param spiffsFile SPIFFS dir entry pointer.
@@ -256,7 +311,7 @@ bool unpackFiles(std::string sDest) {
256
311
spiffs_dirent ent;
257
312
258
313
// Add "./" to path if is not given.
259
- if (sDest .find (" ./" ) == std::string::npos) {
314
+ if (sDest .find (" ./" ) == std::string::npos && sDest . find ( " / " ) == std::string::npos ) {
260
315
sDest = " ./" + sDest ;
261
316
}
262
317
@@ -265,13 +320,7 @@ bool unpackFiles(std::string sDest) {
265
320
std::cout << " Directory " << sDest << " does not exists. Try to create it." << std::endl;
266
321
267
322
// Try to create directory.
268
- // platform stuff...
269
- #if defined(_WIN32)
270
- if (_mkdir (sDest .c_str ()) != 0 ) {
271
- #else
272
- if (mkdir (sDest .c_str (), S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH) != 0 ) {
273
- #endif
274
- std::cerr << " Can not create directory!!!" << std::endl;
323
+ if (! dirCreate (sDest .c_str ())) {
275
324
return false ;
276
325
}
277
326
}
@@ -284,7 +333,23 @@ bool unpackFiles(std::string sDest) {
284
333
while (it) {
285
334
// Check if content is a file.
286
335
if ((int )(it->type ) == 1 ) {
287
- 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
+ }
288
353
289
354
// Unpack file to destination directory.
290
355
if (! unpackFile (it, sDestFilePath .c_str ()) ) {
@@ -302,8 +367,9 @@ bool unpackFiles(std::string sDest) {
302
367
<< std::endl;
303
368
}
304
369
370
+ // Get next file handle.
305
371
it = SPIFFS_readdir (&dir, &ent);
306
- }
372
+ } // end while
307
373
308
374
// Close directory.
309
375
SPIFFS_closedir (&dir);
@@ -323,7 +389,7 @@ int actionPack() {
323
389
}
324
390
325
391
spiffsFormat ();
326
- int result = addFiles (s_dirName.c_str ());
392
+ int result = addFiles (s_dirName.c_str (), " / " );
327
393
spiffsUnmount ();
328
394
329
395
fwrite (&s_flashmem[0 ], 4 , s_flashmem.size ()/4 , fdres);
0 commit comments