Skip to content

Commit 2ecc11f

Browse files
committed
pack files recursively
1 parent 5b3fc0c commit 2ecc11f

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

main.cpp

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,38 +139,67 @@ int addFile(char* name, const char* path) {
139139
return 0;
140140
}
141141

142-
int addFiles(const char* dirname){
142+
int addFiles(const char* dirname, const char* subPath) {
143143
DIR *dir;
144144
struct dirent *ent;
145145
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.
147153
while ((ent = readdir (dir)) != NULL) {
154+
// Ignore dir itself.
148155
if (ent->d_name[0] == '.')
149156
continue;
150-
std::string fullpath = dirname;
151-
fullpath += '/';
157+
158+
std::string fullpath = dirPath;
152159
fullpath += ent->d_name;
153160
struct stat path_stat;
154161
stat (fullpath.c_str(), &path_stat);
162+
155163
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+
}
158183
}
159184

160-
std::string filepath = "/";
185+
// Filepath with dirname as root folder.
186+
std::string filepath = subPath;
161187
filepath += ent->d_name;
162188
std::cout << filepath << std::endl;
189+
190+
// Add File to image.
163191
if (addFile((char*)filepath.c_str(), fullpath.c_str()) != 0) {
164192
std::cerr << "error adding file!" << std::endl;
165193
error = true;
166194
break;
167195
}
168-
}
196+
} // end while
169197
closedir (dir);
170198
} else {
171199
std::cerr << "warning: can't read source directory" << std::endl;
172200
return 1;
173201
}
202+
174203
return (error) ? 1 : 0;
175204
}
176205

@@ -256,7 +285,7 @@ bool unpackFiles(std::string sDest) {
256285
spiffs_dirent ent;
257286

258287
// 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) {
260289
sDest = "./" + sDest;
261290
}
262291

@@ -323,7 +352,7 @@ int actionPack() {
323352
}
324353

325354
spiffsFormat();
326-
int result = addFiles(s_dirName.c_str());
355+
int result = addFiles(s_dirName.c_str(), "/");
327356
spiffsUnmount();
328357

329358
fwrite(&s_flashmem[0], 4, s_flashmem.size()/4, fdres);

0 commit comments

Comments
 (0)