Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added multithreading support for mkcomposefs #269

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 61 additions & 46 deletions libcomposefs/lcfs-writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,64 @@ static void digest_to_path(const uint8_t *csum, char *buf)
buf[j] = '\0';
}

int lcfs_node_set_from_content(struct lcfs_node_s *node, int dirfd,
const char *fname, int buildflags)
{
if (node == NULL) {
errno = EINVAL;
return -1;
}

bool compute_digest = (buildflags & LCFS_BUILD_COMPUTE_DIGEST) != 0;
bool by_digest = (buildflags & LCFS_BUILD_BY_DIGEST) != 0;
bool no_inline = (buildflags & LCFS_BUILD_NO_INLINE) != 0;
bool is_zerosized = node->inode.st_size == 0;
bool do_digest = !is_zerosized && (compute_digest || by_digest);
bool do_inline = !is_zerosized && !no_inline &&
node->inode.st_size <= LCFS_BUILD_INLINE_FILE_SIZE_LIMIT;
int r;

if (do_digest || do_inline) {
cleanup_fd int fd = openat(dirfd, fname, O_RDONLY | O_CLOEXEC);
if (fd < 0)
return -1;
if (do_digest) {
r = lcfs_node_set_fsverity_from_fd(node, fd);
if (r < 0)
return -1;

if (by_digest) {
const uint8_t *digest =
lcfs_node_get_fsverity_digest(node);
char digest_path[LCFS_DIGEST_SIZE * 2 + 2];
digest_to_path(digest, digest_path);
r = lcfs_node_set_payload(node, digest_path);
if (r < 0)
return -1;

/* We just computed digest to get the payoad path */
if (!compute_digest)
node->digest_set = false;
}

/* In case we re-read below */
lseek(fd, 0, SEEK_SET);
}
if (do_inline) {
uint8_t buf[LCFS_BUILD_INLINE_FILE_SIZE_LIMIT];

r = read_content(fd, node->inode.st_size, buf);
if (r < 0)
return -1;
r = lcfs_node_set_content(node, buf, node->inode.st_size);
if (r < 0)
return -1;
}
}

return 0;
}

struct lcfs_node_s *lcfs_load_node_from_file(int dirfd, const char *fname,
int buildflags)
{
Expand Down Expand Up @@ -643,52 +701,9 @@ struct lcfs_node_s *lcfs_load_node_from_file(int dirfd, const char *fname,
ret->inode.st_size = sb.st_size;

if ((sb.st_mode & S_IFMT) == S_IFREG) {
bool compute_digest = (buildflags & LCFS_BUILD_COMPUTE_DIGEST) != 0;
bool by_digest = (buildflags & LCFS_BUILD_BY_DIGEST) != 0;
bool no_inline = (buildflags & LCFS_BUILD_NO_INLINE) != 0;
bool is_zerosized = sb.st_size == 0;
bool do_digest = !is_zerosized && (compute_digest || by_digest);
bool do_inline = !is_zerosized && !no_inline &&
sb.st_size <= LCFS_BUILD_INLINE_FILE_SIZE_LIMIT;

if (do_digest || do_inline) {
cleanup_fd int fd =
openat(dirfd, fname, O_RDONLY | O_CLOEXEC);
if (fd < 0)
return NULL;
if (do_digest) {
r = lcfs_node_set_fsverity_from_fd(ret, fd);
if (r < 0)
return NULL;

if (by_digest) {
const uint8_t *digest =
lcfs_node_get_fsverity_digest(ret);
char digest_path[LCFS_DIGEST_SIZE * 2 + 2];
digest_to_path(digest, digest_path);
r = lcfs_node_set_payload(ret, digest_path);
if (r < 0)
return NULL;

/* We just computed digest to get the payoad path */
if (!compute_digest)
ret->digest_set = false;
}

/* In case we re-read below */
lseek(fd, 0, SEEK_SET);
}
if (do_inline) {
uint8_t buf[LCFS_BUILD_INLINE_FILE_SIZE_LIMIT];

r = read_content(fd, sb.st_size, buf);
if (r < 0)
return NULL;
r = lcfs_node_set_content(ret, buf, sb.st_size);
if (r < 0)
return NULL;
}
}
r = lcfs_node_set_from_content(ret, dirfd, fname, buildflags);
if (r < 0)
return NULL;
} else if ((sb.st_mode & S_IFMT) == S_IFLNK) {
char target[PATH_MAX + 1];

Expand Down
3 changes: 3 additions & 0 deletions libcomposefs/lcfs-writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,7 @@ LCFS_EXTERN int lcfs_compute_fsverity_from_fd(uint8_t *digest, int fd);
LCFS_EXTERN int lcfs_compute_fsverity_from_data(uint8_t *digest, uint8_t *data,
size_t data_len);

LCFS_EXTERN int lcfs_node_set_from_content(struct lcfs_node_s *node, int dirfd,
const char *fname, int buildflags);

#endif
4 changes: 4 additions & 0 deletions man/mkcomposefs.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ will be a mountable composefs image.
actual image format version used will be adjusted upwards if that
is beneficial for the image, up to the max version.

**\-\-threads**=*count*
: Number of threads to be used to calculate the file digests and copy.
Default thread count is the number of processors when *--threads* is not specified.

# FORMAT VERSIONING

Composefs images are binary reproduceable, meaning that for a given
Expand Down
Loading
Loading