Move compressors to utility library

- Remove compressor registration interface. Doesn't work in a static
   library, since all the files containing no exported members are
   optimized away at link time.
 - Repack as seperate utility library.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
David Oberhollenzer 2019-04-05 12:13:12 +02:00
parent 63bad9786a
commit 1205ca0f3b
12 changed files with 86 additions and 76 deletions

View File

@ -29,16 +29,8 @@ typedef struct compressor_t {
compressor_stream_t *(*uncompression_stream)(struct compressor_t *cmp);
} compressor_t;
void compressor_register(compressor_t *compressor);
compressor_t *compressor_by_name(const char *name);
compressor_t *compressor_by_id(PKG_COMPRESSION id);
#define REGISTER_COMPRESSOR(compressor) \
static void __attribute__((constructor)) register_##compressor(void) \
{ \
compressor_register((compressor_t *)&compressor); \
}
#endif /* COMPRESSOR_H */

View File

@ -4,8 +4,8 @@
#include <stdbool.h>
#include "comp/compressor.h"
#include "pkgformat.h"
#include "compressor.h"
typedef struct pkg_writer_t pkg_writer_t;

View File

@ -9,4 +9,23 @@ libfilelist_a_SOURCES = lib/filelist/dump_toc.c lib/filelist/image_entry.c
libfilelist_a_SOURCES += lib/filelist/image_entry_sort.c
libfilelist_a_SOURCES += include/filelist/image_entry.h
noinst_LIBRARIES += libutil.a libfilelist.a
libcomp_a_SOURCES = lib/comp/compressor.c lib/comp/none.c
libcomp_a_SOURCES += include/comp/compressor.h lib/comp/internal.h
libcomp_a_CFLAGS = $(AM_CFLAGS)
libcomp_a_CPPFLAGS = $(AM_CPPFLAGS)
if WITH_ZLIB
libcomp_a_SOURCES += lib/comp/zlib.c
libcomp_a_CFLAGS += $(ZLIB_CFLAGS)
libcomp_a_CPPFLAGS += -DWITH_ZLIB
endif
if WITH_LZMA
libcomp_a_SOURCES += lib/comp/lzma.c
libcomp_a_CFLAGS += $(XZ_CFLAGS)
libcomp_a_CPPFLAGS += -DWITH_LZMA
endif
noinst_LIBRARIES += libutil.a libfilelist.a libcomp.a

39
lib/comp/compressor.c Normal file
View File

@ -0,0 +1,39 @@
/* SPDX-License-Identifier: ISC */
#include <string.h>
#include "internal.h"
static compressor_t *compressors[] = {
[PKG_COMPRESSION_NONE] = &comp_none,
#ifdef WITH_ZLIB
[PKG_COMPRESSION_ZLIB] = &comp_zlib,
#endif
#ifdef WITH_LZMA
[PKG_COMPRESSION_LZMA] = &comp_lzma,
#endif
};
compressor_t *compressor_by_name(const char *name)
{
size_t i;
for (i = 0; i < sizeof(compressors) / sizeof(compressors[0]); ++i) {
if (compressors[i] == NULL)
continue;
if (strcmp(compressors[i]->name, name) == 0)
return compressors[i];
}
return NULL;
}
compressor_t *compressor_by_id(PKG_COMPRESSION id)
{
if ((int)id < 0)
return NULL;
if ((size_t)id >= sizeof(compressors) / sizeof(compressors[0]))
return NULL;
return compressors[id];
}

11
lib/comp/internal.h Normal file
View File

@ -0,0 +1,11 @@
/* SPDX-License-Identifier: ISC */
#ifndef INTERNAL_H
#define INTERNAL_H
#include "comp/compressor.h"
extern compressor_t comp_lzma;
extern compressor_t comp_none;
extern compressor_t comp_zlib;
#endif /* INTERNAL_H */

View File

@ -5,7 +5,7 @@
#include <stdio.h>
#include <lzma.h>
#include "compressor.h"
#include "internal.h"
#define CHUNK_SIZE 16384
@ -160,11 +160,9 @@ static compressor_stream_t *lzma_uncompress(compressor_t *cmp)
return create_stream(false);
}
static compressor_t lzma = {
compressor_t comp_lzma = {
.name = "lzma",
.id = PKG_COMPRESSION_LZMA,
.compression_stream = lzma_compress,
.uncompression_stream = lzma_uncompress,
};
REGISTER_COMPRESSOR(lzma)

View File

@ -4,7 +4,7 @@
#include <stdio.h>
#include <zlib.h>
#include "compressor.h"
#include "internal.h"
#define CHUNK_SIZE 16384
@ -81,11 +81,9 @@ static compressor_stream_t *create_dummy_stream(compressor_t *cmp)
return base;
}
static compressor_t none = {
compressor_t comp_none = {
.name = "none",
.id = PKG_COMPRESSION_NONE,
.compression_stream = create_dummy_stream,
.uncompression_stream = create_dummy_stream,
};
REGISTER_COMPRESSOR(none)

View File

@ -5,7 +5,7 @@
#include <stdio.h>
#include <zlib.h>
#include "compressor.h"
#include "internal.h"
#define CHUNK_SIZE 16384
@ -159,11 +159,9 @@ static compressor_stream_t *zlib_uncompress(compressor_t *cmp)
return create_stream(false);
}
static compressor_t zlib = {
compressor_t comp_zlib = {
.name = "zlib",
.id = PKG_COMPRESSION_ZLIB,
.compression_stream = zlib_compress,
.uncompression_stream = zlib_uncompress,
};
REGISTER_COMPRESSOR(zlib)

View File

@ -1,13 +1,12 @@
pkg_SOURCES = include/pkgformat.h include/pkgreader.h include/pkgio.h
pkg_SOURCES += include/compressor.h include/command.h include/pkgwriter.h
pkg_SOURCES += include/depgraph.h
pkg_SOURCES += main/pkg.c main/compressor.c main/command.c main/pkgreader.c
pkg_SOURCES += include/command.h include/pkgwriter.h include/depgraph.h
pkg_SOURCES += main/pkg.c main/command.c main/pkgreader.c
pkg_SOURCES += main/pkgwriter.c main/pkgio_rd_image_entry.c main/pkg_unpack.c
pkg_SOURCES += main/depgraph/collect.c main/depgraph/pkglist.c
pkg_SOURCES += main/depgraph/tsort.c
pkg_CFLAGS = $(AM_CFLAGS)
pkg_LDADD = libutil.a libfilelist.a
pkg_LDADD = libutil.a libfilelist.a libcomp.a
##### commands #####
@ -40,24 +39,12 @@ pkg_SOURCES += main/cmd/unpack.c
pkg_SOURCES += main/cmd/help.c
##### compressors #####
# dummy compressor
pkg_SOURCES += main/compressors/none.c
# zlib compressor
if WITH_ZLIB
pkg_SOURCES += main/compressors/zlib.c
pkg_CFLAGS += $(ZLIB_CFLAGS)
pkg_LDADD += $(ZLIB_LIBS)
endif
# lzma compressor
if WITH_LZMA
pkg_SOURCES += main/compressors/lzma.c
pkg_CFLAGS += $(XZ_CFLAGS)
pkg_LDADD += $(XZ_LIBS)
endif
if WITH_ZLIB
pkg_LDADD += $(ZLIB_LIBS)
endif
bin_PROGRAMS += pkg

View File

@ -21,7 +21,7 @@
#include "filelist/image_entry.h"
#include "compressor.h"
#include "comp/compressor.h"
#include "pkgformat.h"
#include "pkgwriter.h"
#include "command.h"

View File

@ -1,32 +0,0 @@
/* SPDX-License-Identifier: ISC */
#include <string.h>
#include "compressor.h"
static compressor_t *list = NULL;
void compressor_register(compressor_t *compressor)
{
compressor->next = list;
list = compressor;
}
compressor_t *compressor_by_name(const char *name)
{
compressor_t *it = list;
while (it != NULL && strcmp(it->name, name) != 0)
it = it->next;
return it;
}
compressor_t *compressor_by_id(PKG_COMPRESSION id)
{
compressor_t *it = list;
while (it != NULL && it->id != id)
it = it->next;
return it;
}

View File

@ -8,9 +8,9 @@
#include <errno.h>
#include <ctype.h>
#include "comp/compressor.h"
#include "util/util.h"
#include "pkgreader.h"
#include "compressor.h"
struct pkg_reader_t {
int fd;