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

Add a libcomposefs-internal #367

Merged
merged 2 commits into from
Sep 30, 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
60 changes: 60 additions & 0 deletions libcomposefs/lcfs-utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* lcfs
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.

This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */

#define _GNU_SOURCE

#include "config.h"

#include "lcfs-utils.h"
#include "lcfs-writer.h"

void digest_to_string(const uint8_t *csum, char *buf)
{
static const char hexchars[] = "0123456789abcdef";
uint32_t i, j;

for (i = 0, j = 0; i < LCFS_DIGEST_SIZE; i++, j += 2) {
uint8_t byte = csum[i];
buf[j] = hexchars[byte >> 4];
buf[j + 1] = hexchars[byte & 0xF];
}
buf[j] = '\0';
}

int digest_to_raw(const char *digest, uint8_t *raw, int max_size)
{
int size = 0;

while (*digest) {
char c1, c2;
int n1, n2;

if (size >= max_size)
return -1;

c1 = *digest++;
n1 = hexdigit(c1);
if (n1 < 0)
return -1;

c2 = *digest++;
n2 = hexdigit(c2);
if (n2 < 0)
return -1;

raw[size++] = (n1 & 0xf) << 4 | (n2 & 0xf);
}

return size;
}
30 changes: 4 additions & 26 deletions libcomposefs/lcfs-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
Expand Down Expand Up @@ -68,32 +70,8 @@ static inline int hexdigit(char c)
return -1;
}

static inline int digest_to_raw(const char *digest, uint8_t *raw, int max_size)
{
int size = 0;

while (*digest) {
char c1, c2;
int n1, n2;

if (size >= max_size)
return -1;

c1 = *digest++;
n1 = hexdigit(c1);
if (n1 < 0)
return -1;

c2 = *digest++;
n2 = hexdigit(c2);
if (n2 < 0)
return -1;

raw[size++] = (n1 & 0xf) << 4 | (n2 & 0xf);
}

return size;
}
void digest_to_string(const uint8_t *csum, char *buf);
int digest_to_raw(const char *digest, uint8_t *raw, int max_size);

static inline char *str_join(const char *a, const char *b)
{
Expand Down
14 changes: 13 additions & 1 deletion libcomposefs/meson.build
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
internal_source_files = files([
'lcfs-utils.h',
'lcfs-utils.c',
])

libcomposefs_internal = static_library('composefs-internal',
internal_source_files,
c_args : composefs_hash_cflags + hidden_visibility_cflags,
include_directories : config_inc,
install : false,
)

source_files = files([
'bitrotate.h',
'erofs_fs.h',
Expand All @@ -12,7 +24,6 @@ source_files = files([
'lcfs-writer-erofs.c',
'lcfs-writer.c',
'lcfs-writer.h',
'lcfs-utils.h',
'lcfs-mount.c',
'lcfs-mount.h',
'xalloc-oversized.h',
Expand All @@ -22,6 +33,7 @@ libcomposefs = both_libraries('composefs',
source_files,
c_args : composefs_hash_cflags + hidden_visibility_cflags,
dependencies : libcrypto_dep,
link_with: libcomposefs_internal,
version : libversion,
soversion : soversion,
include_directories : config_inc,
Expand Down
13 changes: 0 additions & 13 deletions tools/composefs-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,6 @@ static void print_node_handler(struct lcfs_node_s *node, void *data)
print_node(node, "");
}

static void digest_to_string(const uint8_t *csum, char *buf)
{
static const char hexchars[] = "0123456789abcdef";
uint32_t i, j;

for (i = 0, j = 0; i < LCFS_DIGEST_SIZE; i++, j += 2) {
uint8_t byte = csum[i];
buf[j] = hexchars[byte >> 4];
buf[j + 1] = hexchars[byte & 0xF];
}
buf[j] = '\0';
}

static char *node_build_path(struct lcfs_node_s *node)
{
size_t pathlen = 0;
Expand Down
4 changes: 4 additions & 0 deletions tools/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@ thread_dep = dependency('threads')
executable('mkcomposefs',
'mkcomposefs.c',
dependencies : [libcomposefs_dep, thread_dep],
link_with: [libcomposefs_internal],
install : true,
)

executable('mount.composefs',
'mountcomposefs.c',
dependencies : [libcomposefs_dep],
link_with: [libcomposefs_internal],
install : true,
install_dir : get_option('sbindir'),
)

executable('composefs-info',
['composefs-info.c', '../libcomposefs/hash.c'],
c_args : composefs_hash_cflags,
link_with: [libcomposefs_internal],
dependencies : [libcomposefs_dep],
install : true,
)

executable('composefs-dump',
'composefs-dump.c',
link_with: [libcomposefs_internal],
dependencies : [libcomposefs_dep],
install : false,
)
Expand Down
13 changes: 0 additions & 13 deletions tools/mkcomposefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1428,19 +1428,6 @@ static int fill_store(const int thread_count, struct lcfs_node_s *node,
return ret;
}

static void digest_to_string(const uint8_t *csum, char *buf)
{
static const char hexchars[] = "0123456789abcdef";
uint32_t i, j;

for (i = 0, j = 0; i < LCFS_DIGEST_SIZE; i++, j += 2) {
uint8_t byte = csum[i];
buf[j] = hexchars[byte >> 4];
buf[j + 1] = hexchars[byte & 0xF];
}
buf[j] = '\0';
}

static int get_cpu_count(void)
{
cpu_set_t set;
Expand Down
Loading