Skip to content

Commit

Permalink
mkcomposefs: Add --print-digest-only
Browse files Browse the repository at this point in the history
In some cases, it's very useful to just see what the composefs
digest of an input tree is, without actually writing an image.  One
could do this externally by allocating a tempfile, but it's nicer for
us to do internally as we can entirely avoid writing at all.

Signed-off-by: Colin Walters <walters@verbum.org>
  • Loading branch information
cgwalters committed Jun 16, 2023
1 parent 58f326a commit 5042564
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
7 changes: 5 additions & 2 deletions tests/integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ nonhardlink_ls() {
ls "$@" | sed -e 's,^\([^ ]*\) *\([0-9][0-9]*\)\(.*\)$,\1\3,'
}

cfsroot=/composefs
cfsroot=${cfsroot:-/composefs}
rm ${cfsroot}/tmp -rf
mkdir -p ${cfsroot}/{objects,roots,tmp}

cd ${cfsroot}/tmp
testsrc=/usr/bin
mkcomposefs --digest-store=${cfsroot}/objects ${testsrc} ${cfsroot}/roots/test.cfs
mkcomposefs --print-digest --digest-store=${cfsroot}/objects ${testsrc} ${cfsroot}/roots/test.cfs | tee digest.txt
prev_digest=$(cat digest.txt)
new_digest=$(mkcomposefs --by-digest --print-digest-only ${testsrc})
test "$prev_digest" = "$new_digest"

mkdir -p mnt
mount.composefs -o basedir=${cfsroot}/objects ${cfsroot}/roots/test.cfs mnt
Expand Down
46 changes: 38 additions & 8 deletions tools/mkcomposefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ static int fill_payload(struct lcfs_node_s *node, const char *path, size_t len,
static void usage(const char *argv0)
{
fprintf(stderr,
"usage: %s [--use-epoch] [--skip-xattrs] [--absolute] [--by-digest] [--digest-store=path] [--print-digest] [--skip-devices] [--compute-digest] SOURCEDIR IMAGE\n",
"usage: %s [--use-epoch] [--skip-xattrs] [--absolute] [--by-digest] [--digest-store=path] [--print-digest] [--print-digest-only] [--skip-devices] [--compute-digest] SOURCEDIR IMAGE\n",
argv0);
}

Expand All @@ -382,6 +382,7 @@ static void usage(const char *argv0)
#define OPT_DIGEST_STORE 108
#define OPT_PRINT_DIGEST 109
#define OPT_FORMAT 110
#define OPT_PRINT_DIGEST_ONLY 111

static ssize_t write_cb(void *_file, void *buf, size_t count)
{
Expand Down Expand Up @@ -441,6 +442,12 @@ int main(int argc, char **argv)
flag: NULL,
val: OPT_PRINT_DIGEST
},
{
name: "print-digest-only",
has_arg: no_argument,
flag: NULL,
val: OPT_PRINT_DIGEST_ONLY
},
{
name: "format",
has_arg: required_argument,
Expand All @@ -455,6 +462,7 @@ int main(int argc, char **argv)
bool absolute_path = false;
bool by_digest = false;
bool print_digest = false;
bool print_digest_only = false;
const char *format = "erofs";
struct lcfs_node_s *root;
const char *out = NULL;
Expand Down Expand Up @@ -496,6 +504,9 @@ int main(int argc, char **argv)
case OPT_PRINT_DIGEST:
print_digest = true;
break;
case OPT_PRINT_DIGEST_ONLY:
print_digest = print_digest_only = true;
break;
case ':':
fprintf(stderr, "option needs a value\n");
exit(EXIT_FAILURE);
Expand All @@ -513,15 +524,30 @@ int main(int argc, char **argv)
usage(bin);
exit(1);
}
dir_path = argv[0];

if (argc != 2) {
fprintf(stderr, "No destination path specified\n");
if (argc > 2) {
fprintf(stderr, "Too many arguments\n");
usage(bin);
exit(1);
}
if (argc == 1) {
if (!print_digest_only) {
fprintf(stderr, "No destination path specified\n");
usage(bin);
exit(1);
}
} else if (!print_digest_only) {
assert(argc == 2);
out = argv[1];
} else {
fprintf(stderr,
"Cannot specify destination path with --print-digest-only\n");
usage(bin);
exit(1);
}

dir_path = argv[0];
out = argv[1];
assert(out || print_digest_only);

if (digest_store_path != NULL)
by_digest = true; /* implied */
Expand All @@ -533,7 +559,9 @@ int main(int argc, char **argv)
error(EXIT_FAILURE, 0,
"Can't specify both --absolute and --by-digest");

if (strcmp(out, "-") == 0) {
if (print_digest_only) {
out_file = NULL;
} else if (strcmp(out, "-") == 0) {
if (isatty(1))
error(EXIT_FAILURE, 0, "stdout is a tty. Refusing to use it");
out_file = stdout;
Expand Down Expand Up @@ -590,8 +618,10 @@ int main(int argc, char **argv)
error(EXIT_FAILURE, errno, "Unknown format %s", format);
}

options.file = out_file;
options.file_write_cb = write_cb;
if (out_file) {
options.file = out_file;
options.file_write_cb = write_cb;
}
if (print_digest)
options.digest_out = digest;

Expand Down

0 comments on commit 5042564

Please sign in to comment.