Skip to content

Commit

Permalink
Support fake directory names
Browse files Browse the repository at this point in the history
Use same renaming scheme to make subdir markers into 0x01/vice versa.
  • Loading branch information
earlephilhower committed Dec 17, 2018
1 parent d0bbd87 commit 73d0597
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ int lfs_flash_sync(const struct lfs_config *c) {
}


// Because "/" is an actual full-fledged dir separator in LFS, we need to
// replace it with a reserved character to avoid dir-not-found/etc.
// problems when using ported SPIFFS apps.
static char *spiffsNameToLFS(const char *src, char *dst) {
char *ret = dst;
char c;
while (0 != (c = *(src++))) {
*(dst++) = (c=='/')?0x01:c;
}
*dst = 0;
// Strip leading "/"s
while ((*ret) == 0x01) ret++;
return ret;
}
static char *lfsNameToSPIFFS(const char *src, char *dst) {
char *ret = dst;
char c;
while (0 != (c = *(src++))) {
*(dst++) = (c==0x01)?'/':c;
}
*dst = 0;
return ret;
}


// Implementation
Expand Down Expand Up @@ -139,8 +162,9 @@ int addFile(char* name, const char* path) {
return 1;
}

char lfsName[LFS_NAME_MAX+1];
lfs_file_t dst;
int ret = lfs_file_open(&s_fs, &dst, name, LFS_O_CREAT | LFS_O_TRUNC | LFS_O_WRONLY);
int ret = lfs_file_open(&s_fs, &dst, spiffsNameToLFS(name, lfsName), LFS_O_CREAT | LFS_O_TRUNC | LFS_O_WRONLY);
if (ret < 0) {
std::cerr << "unable to open '" << name << "." << std::endl;
return 1;
Expand Down Expand Up @@ -298,7 +322,7 @@ void listFiles() {
continue;
}

std::cout << it.size << '\t' << it.name << std::endl;
std::cout << it.size << '\t' << lfsNameToSPIFFS(it.name, it.name) << std::endl;
}
lfs_dir_close(&s_fs, &dir);
lfs_unmount(&s_fs);
Expand Down Expand Up @@ -428,7 +452,9 @@ bool unpackFiles(std::string sDest) {

// Check if content is a file.
if ((int)(ent.type) == LFS_TYPE_REG) {
std::string name = (const char*)(ent.name);
char realName[LFS_NAME_MAX+1];
lfsNameToSPIFFS(ent.name, realName);
std::string name = (const char*)(realName);
std::string sDestFilePath = sDest + name;
size_t pos = name.find_first_of("/", 1);

Expand Down Expand Up @@ -456,7 +482,7 @@ bool unpackFiles(std::string sDest) {

// Output stuff.
std::cout
<< ent.name
<< realName
<< '\t'
<< " > " << sDestFilePath
<< '\t'
Expand Down

0 comments on commit 73d0597

Please sign in to comment.