Skip to content

Commit

Permalink
Merge pull request #202 from alexlarsson/portability-issues
Browse files Browse the repository at this point in the history
Fix various portability issues
  • Loading branch information
alexlarsson authored Sep 26, 2023
2 parents 4b7695b + ae3a021 commit a95115b
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 126 deletions.
4 changes: 1 addition & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ PKG_CHECK_MODULES(LCFS_DEP_CRYPTO, libcrypto,[
])
AM_CONDITIONAL(USE_OPENSSL, test $with_openssl != no)

AC_CHECK_HEADERS([sys/capability.h])
AC_CHECK_HEADERS([sys/capability.h endian.h sys/endian.h machine/endian.h])

AC_DEFUN([CC_CHECK_FLAG_APPEND], [
AC_CACHE_CHECK([if $CC supports flag $3 in envvar $2],
Expand Down Expand Up @@ -241,9 +241,7 @@ AM_CONDITIONAL([USE_FUSE3],[test "$have_fuse3" = "yes"])
AC_FUNC_ERROR_AT_LINE
AC_FUNC_FSEEKO
AC_HEADER_MAJOR
AC_FUNC_MALLOC
AC_FUNC_MMAP
AC_FUNC_REALLOC
AC_CHECK_FUNCS([getcwd memset munmap strdup])

AC_SUBST(PKGCONFIG_REQUIRES)
Expand Down
6 changes: 3 additions & 3 deletions libcomposefs/lcfs-fsverity.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <endian.h>
#include <assert.h>
#include <string.h>
#include <sys/param.h>
Expand Down Expand Up @@ -298,6 +297,7 @@ static void sha256_sum_close(Sha256sum *sha256, uint8_t *digest)

#endif /* SHA256 fallback implementation */

#include "lcfs-internal.h" /* for endian.h */
#include "lcfs-fsverity.h"

struct fsverity_descriptor {
Expand All @@ -306,7 +306,7 @@ struct fsverity_descriptor {
uint8_t log_blocksize;
uint8_t salt_size;
uint32_t reserved1;
uint64_t data_size_be;
uint64_t data_size_le;
uint8_t root_hash[64];
uint8_t salt[32];
uint8_t reserved2[144];
Expand Down Expand Up @@ -448,7 +448,7 @@ void lcfs_fsverity_context_get_digest(FsVerityContext *ctx,
descriptor.hash_algorithm = 1;
descriptor.log_blocksize = 12;
descriptor.salt_size = 0;
descriptor.data_size_be = htole64(ctx->file_size);
descriptor.data_size_le = htole64(ctx->file_size);

do_sha256(ctx, ctx->buffer[ctx->max_level], FSVERITY_BLOCK_SIZE,
descriptor.root_hash);
Expand Down
10 changes: 10 additions & 0 deletions libcomposefs/lcfs-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@
#ifndef _LCFS_INTERNAL_H
#define _LCFS_INTERNAL_H

#ifdef HAVE_MACHINE_ENDIAN_H
#include <machine/endian.h>
#endif

#ifdef HAVE_SYS_ENDIAN_H
#include <sys/endian.h>
#endif

#ifdef HAVE_ENDIAN_H
#include <endian.h>
#endif

#include "lcfs-writer.h"
#include "lcfs-fsverity.h"
Expand Down
8 changes: 7 additions & 1 deletion tests/gendir
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import shutil
import stat
import string
import sys
import errno

adjectives = ["adorable", "adventurous", "aggressive", "agreeable", "alert", "alive", "amused", "angry", "annoyed", "annoying", "anxious", "arrogant", "ashamed", "attractive", "average", "awful", "bad", "beautiful", "better", "bewildered", "black", "bloody", "blue", "blue-eyed", "blushing", "bored", "brainy", "brave", "breakable", "bright", "busy", "calm", "careful", "cautious", "charming", "cheerful", "clean", "clear", "clever", "cloudy", "clumsy", "colorful", "combative", "comfortable", "concerned", "condemned", "confused", "cooperative", "courageous", "crazy", "creepy", "crowded", "cruel", "curious", "cute", "dangerous", "dark", "dead", "defeated", "defiant", "delightful", "depressed", "determined", "different", "difficult", "disgusted", "distinct", "disturbed", "dizzy", "doubtful", "drab", "dull", "eager", "easy", "elated", "elegant", "embarrassed", "enchanting", "encouraging", "energetic", "enthusiastic", "envious", "evil", "excited", "expensive", "exuberant", "fair", "faithful", "famous", "fancy", "fantastic", "fierce", "filthy", "fine", "foolish", "fragile", "frail", "frantic", "friendly", "frightened", "funny", "gentle", "gifted", "glamorous", "gleaming", "glorious", "good", "gorgeous", "graceful", "grieving", "grotesque", "grumpy", "handsome", "happy", "healthy", "helpful", "helpless", "hilarious", "homeless", "homely", "horrible", "hungry", "hurt", "ill", "important", "impossible", "inexpensive", "innocent", "inquisitive", "itchy", "jealous", "jittery", "jolly", "joyous", "kind", "lazy", "light", "lively", "lonely", "long", "lovely", "lucky", "magnificent", "misty", "modern", "motionless", "muddy", "mushy", "mysterious", "nasty", "naughty", "nervous", "nice", "nutty", "obedient", "obnoxious", "odd", "old-fashioned", "open", "outrageous", "outstanding", "panicky", "perfect", "plain", "pleasant", "poised", "poor", "powerful", "precious", "prickly", "proud", "putrid", "puzzled", "quaint", "real", "relieved", "repulsive", "rich", "scary", "selfish", "shiny", "shy", "silly", "sleepy", "smiling", "smoggy", "sore", "sparkling", "splendid", "spotless", "stormy", "strange", "stupid", "successful", "super", "talented", "tame", "tasty", "tender", "tense", "terrible", "thankful", "thoughtful", "thoughtless", "tired", "tough", "troubled", "ugliest", "ugly", "uninterested", "unsightly", "unusual", "upset", "uptight", "vast", "victorious", "vivacious", "wandering", "weary", "wicked", "wide-eyed", "wild", "witty", "worried", "worrisome", "wrong", "zany", "zealous"]

Expand Down Expand Up @@ -101,7 +102,12 @@ def set_user_xattr(path):
for i in range(n_xattrs):
name = "user." + gen_xattrname()
value = gen_xattrdata()
os.setxattr(path, name, value, follow_symlinks=False)
try:
os.setxattr(path, name, value, follow_symlinks=False)
except OSError as e:
# Not much we can do if the backing fs doesn't allow xattrs
if e.errno not in (errno.EPERM, errno.ENOTSUP):
raise

old_files = []
def make_regular_file(path):
Expand Down
27 changes: 8 additions & 19 deletions tools/cfs-fuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "config.h"

#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
Expand Down Expand Up @@ -48,18 +49,6 @@ uint64_t erofs_build_time;
uint32_t erofs_build_time_nsec;
int basedir_fd;

static void printexit(const char *format, ...) __attribute__((noreturn));
static void printexit(const char *format, ...)
{
va_list args;

va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);

exit(1);
}

struct cfs_data {
const char *source;
const char *basedir;
Expand Down Expand Up @@ -1171,34 +1160,34 @@ int main(int argc, char *argv[])

fd = open(data.source, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
printexit("Failed to open %s\n", data.source);
errx(EXIT_FAILURE, "Failed to open %s\n", data.source);
}

r = fstat(fd, &s);
if (r < 0) {
printexit("Failed to stat %s\n", data.source);
errx(EXIT_FAILURE, "Failed to stat %s\n", data.source);
}
erofs_data_size = s.st_size;

if (erofs_data_size < EROFS_BLKSIZ) {
printexit("To small image\n");
errx(EXIT_FAILURE, "To small image\n");
}

/* Memory-map the file. */
erofs_data = mmap(0, erofs_data_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (erofs_data == MAP_FAILED) {
printexit("Failed to mmap %s\n", argv[1]);
errx(EXIT_FAILURE, "Failed to mmap %s\n", argv[1]);
}
close(fd);

basedir_fd = open(data.basedir, O_RDONLY | O_PATH);
if (basedir_fd < 0) {
printexit("Failed to open basedir %s\n", data.basedir);
errx(EXIT_FAILURE, "Failed to open basedir %s\n", data.basedir);
}

cfs_header = (struct lcfs_erofs_header_s *)(erofs_data);
if (lcfs_u32_from_file(cfs_header->magic) != LCFS_EROFS_MAGIC) {
printexit("Wrong cfs magic");
errx(EXIT_FAILURE, "Wrong cfs magic");
}

cfs_flags = lcfs_u32_from_file(cfs_header->flags);
Expand All @@ -1208,7 +1197,7 @@ int main(int argc, char *argv[])
erofs_super = (struct erofs_super_block *)(erofs_data + EROFS_SUPER_OFFSET);

if (lcfs_u32_from_file(erofs_super->magic) != EROFS_SUPER_MAGIC_V1) {
printexit("Wrong erofs magic");
errx(EXIT_FAILURE, "Wrong erofs magic");
}

erofs_metadata = erofs_data + lcfs_u32_from_file(erofs_super->meta_blkaddr) *
Expand Down
5 changes: 2 additions & 3 deletions tools/composefs-dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <stdio.h>
#include <string.h>
#include <err.h>
#include <error.h>
#include <errno.h>
#include <fcntl.h>

Expand Down Expand Up @@ -80,13 +79,13 @@ int main(int argc, char **argv)

FILE *out_file = fopen(dst_path, "we");
if (out_file == NULL)
error(EXIT_FAILURE, errno, "failed to open '%s'", dst_path);
err(EXIT_FAILURE, "failed to open '%s'", dst_path);

options.file = out_file;
options.file_write_cb = write_cb;

if (lcfs_write_to(root, &options) < 0)
error(EXIT_FAILURE, errno, "cannot write file");
err(EXIT_FAILURE, "cannot write file");

lcfs_node_unref(root);

Expand Down
Loading

0 comments on commit a95115b

Please sign in to comment.