@@ -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
@@ -256,7 +285,7 @@ bool unpackFiles(std::string sDest) {
256
285
spiffs_dirent ent;
257
286
258
287
// Add "./" to path if is not given.
259
- if (sDest .find (" ./" ) == std::string::npos) {
288
+ if (sDest .find (" ./" ) == std::string::npos && sDest . find ( " / " ) == std::string::npos ) {
260
289
sDest = " ./" + sDest ;
261
290
}
262
291
@@ -323,7 +352,7 @@ int actionPack() {
323
352
}
324
353
325
354
spiffsFormat ();
326
- int result = addFiles (s_dirName.c_str ());
355
+ int result = addFiles (s_dirName.c_str (), " / " );
327
356
spiffsUnmount ();
328
357
329
358
fwrite (&s_flashmem[0 ], 4 , s_flashmem.size ()/4 , fdres);
0 commit comments