Skip to content

Commit d16745a

Browse files
committed
Exclude some files and directories by default, add flag to include them
By default, don't add the following to the archive: - .DS_Store files - .git directories, .gitignore and .gitmodules files Add new flag, -a/--all-files, which forces such files/directories to be included anyway.
1 parent 25f253a commit d16745a

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,19 @@ clean:
8181
SPIFFS_TEST_FS_CONFIG := -s 0x100000 -p 512 -b 0x2000
8282

8383
test: $(TARGET)
84+
rm -rf spiffs/.git
85+
rm -f spiffs/.DS_Store
8486
ls -1 spiffs > out.list0
87+
touch spiffs/.DS_Store
88+
mkdir -p spiffs/.git
89+
touch spiffs/.git/foo
8590
./mkspiffs -c spiffs $(SPIFFS_TEST_FS_CONFIG) out.spiffs | sort | sed s/^\\/// > out.list1
8691
./mkspiffs -u spiffs_u $(SPIFFS_TEST_FS_CONFIG) out.spiffs | sort | sed s/^\\/// > out.list_u
8792
./mkspiffs -l $(SPIFFS_TEST_FS_CONFIG) out.spiffs | cut -f 2 | sort | sed s/^\\/// > out.list2
8893
diff --strip-trailing-cr out.list0 out.list1
8994
diff --strip-trailing-cr out.list0 out.list2
95+
rm -rf spiffs/.git
96+
rm -f spiffs/.DS_Store
9097
diff spiffs spiffs_u
9198
rm -f out.{list0,list1,list2,list_u,spiffs}
9299
rm -R spiffs_u

main.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ static std::vector<uint8_t> s_spiffsWorkBuf;
3838
static std::vector<uint8_t> s_spiffsFds;
3939
static std::vector<uint8_t> s_spiffsCache;
4040

41+
static int s_debugLevel = 0;
42+
static bool s_addAllFiles;
43+
44+
// Unless -a flag is given, these files/directories will not be included into the image
45+
static const char* ignored_file_names[] = {
46+
".DS_Store",
47+
".git",
48+
".gitignore",
49+
".gitmodules"
50+
};
4151

4252
static s32_t api_spiffs_read(u32_t addr, u32_t size, u8_t *dst){
4353
memcpy(dst, &s_flashmem[0] + addr, size);
@@ -55,7 +65,6 @@ static s32_t api_spiffs_erase(u32_t addr, u32_t size){
5565
}
5666

5767

58-
int g_debugLevel = 0;
5968

6069

6170
//implementation
@@ -121,7 +130,7 @@ int addFile(char* name, const char* path) {
121130
size_t size = ftell(src);
122131
fseek(src, 0, SEEK_SET);
123132

124-
if (g_debugLevel > 0) {
133+
if (s_debugLevel > 0) {
125134
std::cout << "file size: " << size << std::endl;
126135
}
127136

@@ -146,7 +155,7 @@ int addFile(char* name, const char* path) {
146155
}
147156
std::cerr << std::endl;
148157

149-
if (g_debugLevel > 0) {
158+
if (s_debugLevel > 0) {
150159
std::cout << "data left: " << left << std::endl;
151160
}
152161

@@ -181,6 +190,21 @@ int addFiles(const char* dirname, const char* subPath) {
181190
continue;
182191
}
183192

193+
if (!s_addAllFiles) {
194+
bool skip = false;
195+
size_t ignored_file_names_count = sizeof(ignored_file_names) / sizeof(ignored_file_names[0]);
196+
for (size_t i = 0; i < ignored_file_names_count; ++i) {
197+
if (strcmp(ent->d_name, ignored_file_names[i]) == 0) {
198+
std::cerr << "skipping " << ent->d_name << std::endl;
199+
skip = true;
200+
break;
201+
}
202+
}
203+
if (skip) {
204+
continue;
205+
}
206+
}
207+
184208
std::string fullpath = dirPath;
185209
fullpath += ent->d_name;
186210
struct stat path_stat;
@@ -217,7 +241,7 @@ int addFiles(const char* dirname, const char* subPath) {
217241
if (addFile((char*)filepath.c_str(), fullpath.c_str()) != 0) {
218242
std::cerr << "error adding file!" << std::endl;
219243
error = true;
220-
if (g_debugLevel > 0) {
244+
if (s_debugLevel > 0) {
221245
std::cout << std::endl;
222246
}
223247
break;
@@ -516,20 +540,22 @@ void processArgs(int argc, const char** argv) {
516540
TCLAP::ValueArg<int> imageSizeArg( "s", "size", "fs image size, in bytes", false, 0x10000, "number" );
517541
TCLAP::ValueArg<int> pageSizeArg( "p", "page", "fs page size, in bytes", false, 256, "number" );
518542
TCLAP::ValueArg<int> blockSizeArg( "b", "block", "fs block size, in bytes", false, 4096, "number" );
543+
TCLAP::SwitchArg addAllFilesArg( "a", "all-files", "when creating an image, include files which are normally ignored; currently only applies to '.DS_Store' files and '.git' directories", false);
519544
TCLAP::ValueArg<int> debugArg( "d", "debug", "Debug level. 0 means no debug output.", false, 0, "0-5" );
520545

521546
cmd.add( imageSizeArg );
522547
cmd.add( pageSizeArg );
523548
cmd.add( blockSizeArg );
524-
cmd.add(debugArg);
549+
cmd.add( addAllFilesArg );
550+
cmd.add( debugArg );
525551
std::vector<TCLAP::Arg*> args = {&packArg, &unpackArg, &listArg, &visualizeArg};
526552
cmd.xorAdd( args );
527553
cmd.add( outNameArg );
528554
cmd.parse( argc, argv );
529555

530556
if (debugArg.getValue() > 0) {
531557
std::cout << "Debug output enabled" << std::endl;
532-
g_debugLevel = debugArg.getValue();
558+
s_debugLevel = debugArg.getValue();
533559
}
534560

535561
if (packArg.isSet()) {
@@ -548,6 +574,7 @@ void processArgs(int argc, const char** argv) {
548574
s_imageSize = imageSizeArg.getValue();
549575
s_pageSize = pageSizeArg.getValue();
550576
s_blockSize = blockSizeArg.getValue();
577+
s_addAllFiles = addAllFilesArg.isSet();
551578
}
552579

553580
int main(int argc, const char * argv[]) {

0 commit comments

Comments
 (0)