Skip to content

Commit

Permalink
Write: record used composefs version in erofs image and allow reading…
Browse files Browse the repository at this point in the history
… it back

Signed-off-by: Alexander Larsson <[email protected]>
  • Loading branch information
alexlarsson committed Jan 26, 2024
1 parent 64cc42e commit 1c00763
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 10 deletions.
3 changes: 2 additions & 1 deletion libcomposefs/lcfs-erofs.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ struct lcfs_erofs_header_s {
uint32_t magic;
uint32_t version;
uint32_t flags;
uint32_t unused[5];
uint32_t composefs_version;
uint32_t unused[4];
} __attribute__((__packed__));

#endif
1 change: 1 addition & 0 deletions libcomposefs/lcfs-writer-erofs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,7 @@ int lcfs_write_erofs_to(struct lcfs_ctx_s *ctx)
struct lcfs_erofs_header_s header = {
.magic = lcfs_u32_to_file(LCFS_EROFS_MAGIC),
.version = lcfs_u32_to_file(LCFS_EROFS_VERSION),
.composefs_version = lcfs_u32_to_file(ctx->options->version),
};
uint32_t header_flags;
struct erofs_super_block superblock = {
Expand Down
19 changes: 19 additions & 0 deletions libcomposefs/lcfs-writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "lcfs-writer.h"
#include "lcfs-utils.h"
#include "lcfs-fsverity.h"
#include "lcfs-erofs.h"
#include "hash.h"

#include <errno.h>
Expand Down Expand Up @@ -680,6 +681,24 @@ struct lcfs_node_s *lcfs_load_node_from_file(int dirfd, const char *fname,
return steal_pointer(&ret);
}

int lcfs_version_from_fd(int fd)
{
struct lcfs_erofs_header_s *header;

header = mmap(0, sizeof(struct lcfs_erofs_header_s), PROT_READ,
MAP_PRIVATE, fd, 0);
if (header == MAP_FAILED) {
return -1;
}
if (lcfs_u32_from_file(header->magic) != LCFS_EROFS_MAGIC ||
lcfs_u32_from_file(header->version) != LCFS_EROFS_VERSION) {
errno = EINVAL;
return -1;
}

return lcfs_u32_from_file(header->composefs_version);
}

struct lcfs_node_s *lcfs_load_node_from_fd(int fd)
{
struct lcfs_node_s *node;
Expand Down
1 change: 1 addition & 0 deletions libcomposefs/lcfs-writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ LCFS_EXTERN struct lcfs_node_s *lcfs_load_node_from_file(int dirfd, const char *
LCFS_EXTERN struct lcfs_node_s *lcfs_load_node_from_image(const uint8_t *image_data,
size_t image_data_size);
LCFS_EXTERN struct lcfs_node_s *lcfs_load_node_from_fd(int fd);
LCFS_EXTERN int lcfs_version_from_fd(int fd);

LCFS_EXTERN const char *lcfs_node_get_xattr(struct lcfs_node_s *node,
const char *name, size_t *length);
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/special_v1.dump.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ee8028939ca3151a50bbbbd377c52c46ab183013610c8909e624840bf496975f
2b1dfb9ffe6938cb3134c339059572e38cbd8cc515d54758fdff55cad5a37427
2 changes: 1 addition & 1 deletion tests/test-checksums.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ for format in erofs ; do
fi

# Ensure dump reproduces the same file
${VALGRIND_PREFIX} ${BINDIR}/composefs-dump $tmpfile $tmpfile2 $VERSION
${VALGRIND_PREFIX} ${BINDIR}/composefs-dump $tmpfile $tmpfile2
if ! cmp $tmpfile $tmpfile2; then
echo Dump is not reproducible
exit 1
Expand Down
14 changes: 7 additions & 7 deletions tools/composefs-dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ static ssize_t write_cb(void *_file, void *buf, size_t count)
int main(int argc, char **argv)
{
const char *bin = argv[0];
int fd;
int fd, version;
struct lcfs_node_s *root;
const char *src_path = NULL;
const char *dst_path = NULL;
struct lcfs_write_options_s options = { 0 };
int format_version = LCFS_VERSION_MAX;

if (argc <= 1) {
fprintf(stderr, "No source path specified\n");
Expand All @@ -64,15 +63,16 @@ int main(int argc, char **argv)
}
dst_path = argv[2];

if (argc > 3) {
format_version = atoi(argv[3]);
}

fd = open(src_path, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
err(EXIT_FAILURE, "Failed to open '%s'", src_path);
}

version = lcfs_version_from_fd(fd);
if (version < 0) {
err(EXIT_FAILURE, "Failed to get image version '%s'", src_path);
}

root = lcfs_load_node_from_fd(fd);
if (root == NULL) {
err(EXIT_FAILURE, "Failed to load '%s'", src_path);
Expand All @@ -81,7 +81,7 @@ int main(int argc, char **argv)
close(fd);

options.format = LCFS_FORMAT_EROFS;
options.version = format_version;
options.version = version;

FILE *out_file = fopen(dst_path, "we");
if (out_file == NULL)
Expand Down

0 comments on commit 1c00763

Please sign in to comment.