From c317fbdcf9781baec52082b979645e47c2b9c8fe Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sun, 2 Jun 2019 16:22:07 -0400 Subject: [PATCH] cfspack: add pattern argument --- tools/cfspack/README.md | 5 ++++- tools/cfspack/cfspack.c | 22 +++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/tools/cfspack/README.md b/tools/cfspack/README.md index 7069eeb..d04c615 100644 --- a/tools/cfspack/README.md +++ b/tools/cfspack/README.md @@ -1,6 +1,6 @@ # cfspack -A tool/library to pack a directory into a CFS blob and unpack a CFS blob into +A tool/library to pack files into a CFS blob and unpack a CFS blob into a directory. ## Usage @@ -12,6 +12,9 @@ To pack a directory into a CFS blob, run: The blob is spit to stdout. If there are subdirectories, they will be prefixes to the filenames under it. +`cfspack` takes an optional second argument, a "fnmatch" pattern. If specified, +only files patching the pattern will be included. + The program errors out if a file name is too long (> 26 bytes) or too big (> 0x10000 - 0x20 bytes). diff --git a/tools/cfspack/cfspack.c b/tools/cfspack/cfspack.c index 106bd1b..af8b13a 100644 --- a/tools/cfspack/cfspack.c +++ b/tools/cfspack/cfspack.c @@ -1,6 +1,8 @@ +#define _GNU_SOURCE #include #include #include +#include #define BLKSIZE 0x100 #define HEADERSIZE 0x20 @@ -55,7 +57,7 @@ int spitblock(char *fullpath, char *fn) return 0; } -int spitdir(char *path, char *prefix) +int spitdir(char *path, char *prefix, char *pattern) { DIR *dp; struct dirent *ep; @@ -79,6 +81,12 @@ int spitdir(char *path, char *prefix) fprintf(stderr, "Filename too long: %s/%s\n", prefix, ep->d_name); return 1; } + if (pattern) { + if (fnmatch(pattern, ep->d_name, FNM_EXTMATCH) != 0) { + continue; + } + } + char fullpath[0x1000]; strcpy(fullpath, path); strcat(fullpath, "/"); @@ -90,7 +98,7 @@ int spitdir(char *path, char *prefix) } strcat(newprefix, ep->d_name); if (ep->d_type == DT_DIR) { - int r = spitdir(fullpath, newprefix); + int r = spitdir(fullpath, newprefix, pattern); if (r != 0) { return r; } @@ -107,11 +115,15 @@ int spitdir(char *path, char *prefix) int main(int argc, char *argv[]) { - if (argc != 2) { - fprintf(stderr, "Usage: cfspack /path/to/dir\n"); + if ((argc > 3) || (argc < 2)) { + fprintf(stderr, "Usage: cfspack /path/to/dir [pattern] \n"); return 1; } char *srcpath = argv[1]; - return spitdir(srcpath, ""); + char *pattern = NULL; + if (argc == 3) { + pattern = argv[2]; + } + return spitdir(srcpath, "", pattern); }