Skip to content

Commit a0160df

Browse files
committed
Merge pull request igrr#9 from pgollor/recursive_dir
Add support to pack and unpack files recursievly.
2 parents 5b3fc0c + 1979a7a commit a0160df

File tree

1 file changed

+85
-19
lines changed

1 file changed

+85
-19
lines changed

main.cpp

Lines changed: 85 additions & 19 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

@@ -208,6 +237,32 @@ bool dirExists(const char* path) {
208237
return false;
209238
}
210239

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+
211266
/**
212267
* @brief Unpack file from file system.
213268
* @param spiffsFile SPIFFS dir entry pointer.
@@ -256,7 +311,7 @@ bool unpackFiles(std::string sDest) {
256311
spiffs_dirent ent;
257312

258313
// 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) {
260315
sDest = "./" + sDest;
261316
}
262317

@@ -265,13 +320,7 @@ bool unpackFiles(std::string sDest) {
265320
std::cout << "Directory " << sDest << " does not exists. Try to create it." << std::endl;
266321

267322
// 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())) {
275324
return false;
276325
}
277326
}
@@ -284,7 +333,23 @@ bool unpackFiles(std::string sDest) {
284333
while (it) {
285334
// Check if content is a file.
286335
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+
}
288353

289354
// Unpack file to destination directory.
290355
if (! unpackFile(it, sDestFilePath.c_str()) ) {
@@ -302,8 +367,9 @@ bool unpackFiles(std::string sDest) {
302367
<< std::endl;
303368
}
304369

370+
// Get next file handle.
305371
it = SPIFFS_readdir(&dir, &ent);
306-
}
372+
} // end while
307373

308374
// Close directory.
309375
SPIFFS_closedir(&dir);
@@ -323,7 +389,7 @@ int actionPack() {
323389
}
324390

325391
spiffsFormat();
326-
int result = addFiles(s_dirName.c_str());
392+
int result = addFiles(s_dirName.c_str(), "/");
327393
spiffsUnmount();
328394

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

0 commit comments

Comments
 (0)