refactor: split lib in multiple lib

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-07-21 12:17:52 +00:00
parent b06aba1da8
commit a98288b28c
25 changed files with 201 additions and 29 deletions

View file

@ -524,7 +524,7 @@ TIMESTAMP = NO
# normally produced when WARNINGS is set to YES. # normally produced when WARNINGS is set to YES.
# The default value is: NO. # The default value is: NO.
EXTRACT_ALL = YES EXTRACT_ALL = NO
# If the EXTRACT_PRIVATE tag is set to YES, all private members of a class will # If the EXTRACT_PRIVATE tag is set to YES, all private members of a class will
# be included in the documentation. # be included in the documentation.
@ -949,7 +949,7 @@ WARN_LOGFILE =
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING # spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
# Note: If this tag is empty the current directory is searched. # Note: If this tag is empty the current directory is searched.
INPUT = lib src module win32 INPUT = libcrypto liblzp libstpdfs include module win32
# This tag can be used to specify the character encoding of the source files # This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses # that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses

View file

@ -1,4 +1,4 @@
SUBDIRS = lib tools tests SUBDIRS = lib libcrypto liblzp tools
if BUILD_FUSE if BUILD_FUSE
SUBDIRS += fuse SUBDIRS += fuse

View file

@ -44,8 +44,12 @@ AC_CHECK_FUNCS(m4_normalize([
AC_CONFIG_FILES([Makefile AC_CONFIG_FILES([Makefile
lib/Makefile lib/Makefile
libcrypto/Makefile
libcrypto/tests/Makefile
liblzp/Makefile
liblzp/tests/Makefile
libstpdfs/Makefile
tools/Makefile tools/Makefile
tests/Makefile
linux/Makefile linux/Makefile
fuse/Makefile]) fuse/Makefile])

View file

@ -22,6 +22,8 @@
# define STPDFS_ZONES_PER_BLOCK (STPDFS_BLOCK_SIZE / sizeof(uint32_t)) # define STPDFS_ZONES_PER_BLOCK (STPDFS_BLOCK_SIZE / sizeof(uint32_t))
# define STPDFS_DIRENT_PER_BLOCK (STPDFS_BLOCK_SIZE / (sizeof(struct stpdfs_dirent))) # define STPDFS_DIRENT_PER_BLOCK (STPDFS_BLOCK_SIZE / (sizeof(struct stpdfs_dirent)))
# define STPDFS_SB_BLOCK 1
# define STPDFS_SB_MAGIC 0x44505453 /**< Superblock magic number */ # define STPDFS_SB_MAGIC 0x44505453 /**< Superblock magic number */
# define STPDFS_SB_REV STPDFS_SB_REV_1 /**< current revision */ # define STPDFS_SB_REV STPDFS_SB_REV_1 /**< current revision */
# define STPDFS_SB_REV_1 1 # define STPDFS_SB_REV_1 1

View file

@ -4,7 +4,4 @@ libstpdfs_a_SOURCES = block.c \
inode.c \ inode.c \
dir.c \ dir.c \
data.c \ data.c \
superblock.c \ superblock.c
compression/lzp.c \
crypto/hchacha.c \
crypto/xchacha.c

5
libcrypto/Makefile.am Normal file
View file

@ -0,0 +1,5 @@
SUBDIRS = tests
noinst_LIBRARIES = libcrypto.a
AM_CFLAGS = -I$(top_srcdir)
libcrypto_a_SOURCES = hchacha.c xchacha.c

View file

@ -1,5 +1,8 @@
#ifndef STPDFS_CRYPTO_CHACHA_H /**
# define STPDFS_CRYPTO_CHACHA_H 1 * \file chacha.h
*/
#ifndef CHACHA_H
# define CHACHA_H 1
# include <stdint.h> # include <stdint.h>
# include <stddef.h> # include <stddef.h>
@ -38,4 +41,4 @@ void
xchacha12(uint8_t *out, uint8_t key[CHACHA_KEY_BYTES], uint8_t nonce[24], xchacha12(uint8_t *out, uint8_t key[CHACHA_KEY_BYTES], uint8_t nonce[24],
uint8_t *in, size_t inlen); uint8_t *in, size_t inlen);
#endif /* STPDFS_CRYPTO_CHACHA_H */ #endif /* CHACHA_H */

View file

@ -0,0 +1,9 @@
TESTS = test_hchacha test_xchacha
check_PROGRAMS = test_hchacha test_xchacha
AM_CFLAGS = -I../
LDADD = -lcmocka ../libcrypto.a
test_hchacha_SOURCES = test_hchacha.c
test_xchacha_SOURCES = test_xchacha.c

View file

@ -4,7 +4,7 @@
#include <setjmp.h> #include <setjmp.h>
#include <cmocka.h> #include <cmocka.h>
#include <crypto/chacha.h> #include "chacha.h"
/* test from https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha-03#section-2.2 */ /* test from https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha-03#section-2.2 */

View file

@ -5,7 +5,7 @@
#include <cmocka.h> #include <cmocka.h>
#include <string.h> #include <string.h>
#include <crypto/chacha.h> #include "chacha.h"
void xchacha(uint8_t *out, uint8_t key[CHACHA_KEY_BYTES], uint8_t nonce[24], void xchacha(uint8_t *out, uint8_t key[CHACHA_KEY_BYTES], uint8_t nonce[24],
uint8_t *in, size_t inlen, int round); uint8_t *in, size_t inlen, int round);

4
liblzp/Makefile.am Normal file
View file

@ -0,0 +1,4 @@
SUBDIRS = tests
noinst_LIBRARIES = liblzp.a
liblzp_a_SOURCES = lzp.c

View file

@ -1,5 +1,8 @@
#ifndef STPDFS_COMPRESSION_LZP_H /**
# define STPDFS_COMPRESSION_LZP_H 1 * \file lzp.h
*/
#ifndef LZP_H
# define LZP_H 1
# include <stddef.h> # include <stddef.h>
# include <stdint.h> # include <stdint.h>
@ -7,4 +10,4 @@
void lzp_compress(uint8_t *out, size_t *outsz, const uint8_t *in, size_t insz); void lzp_compress(uint8_t *out, size_t *outsz, const uint8_t *in, size_t insz);
void lzp_decompress(uint8_t *out, size_t *outsz, const uint8_t *in, size_t insz); void lzp_decompress(uint8_t *out, size_t *outsz, const uint8_t *in, size_t insz);
#endif /* STPDF_COMPRESSION_LZP_H */ #endif /* LZP_H */

9
liblzp/tests/Makefile.am Normal file
View file

@ -0,0 +1,9 @@
AM_CPPFLAGS = -I$(srcdir)/..
TESTS = test_lzp
check_PROGRAMS = test_lzp
LDADD = -lcmocka ../liblzp.a
test_lzp_SOURCES = test_lzp.c

2
libstpdfs/Makefile.am Normal file
View file

@ -0,0 +1,2 @@
noinst_LIBRARIES = libstpdfs.a
liblzp_a_SOURCES = super.c bio.c inode.c file.c dir.c

89
libstpdfs/bio.c Normal file
View file

@ -0,0 +1,89 @@
/**
* file: block.c
*/
#include <stupidfs.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
size_t
stpdfs_write(int fd, uint32_t blocknum, void *data, size_t size)
{
uint8_t buffer[STPDFS_BLOCK_SIZE];
if (size > STPDFS_BLOCK_SIZE) return (-1);
lseek(fd, blocknum * STPDFS_BLOCK_SIZE, SEEK_SET);
if (size > 0)
{
memcpy(buffer, data, size);
return (write(fd, buffer, STPDFS_BLOCK_SIZE));
}
return (write(fd, data, size));
}
size_t
stpdfs_read(int fd, uint32_t blocknum, void *data, size_t size)
{
lseek(fd, blocknum * STPDFS_BLOCK_SIZE, SEEK_SET);
return (read(fd, data, size));
}
uint32_t
stpdfs_alloc_block(int fd, struct stpdfs_sb *sb)
{
uint32_t blocknum;
struct stpdfs_free freelist;
sb->state = STPDFS_DIRTY; /* mark state dirty */
redo:
sb->freelist.nfree--;
blocknum = sb->freelist.free[sb->freelist.nfree];
if (sb->freelist.nfree == 0 && blocknum != 0)
{
stpdfs_read(fd, blocknum, &freelist, sizeof(struct stpdfs_free));
memcpy(sb->freelist.free, &freelist, sizeof(uint32_t) * 100);
sb->freelist.nfree = freelist.nfree;
goto redo;
}
sb->time = time(NULL);
return (blocknum);
}
int
stpdfs_free_block(int fd, struct stpdfs_sb *sb, uint32_t blocknum)
{
struct stpdfs_free copy;
if (blocknum == 0 || blocknum >= sb->fsize)
{
return (-1);
}
sb->state = STPDFS_DIRTY; /* mark state dirty */
if (sb->freelist.nfree == 100)
{
memcpy(&copy, sb->freelist.free, sizeof(uint32_t) * 100);
copy.nfree = sb->freelist.nfree;
stpdfs_write(fd, blocknum, &copy, sizeof(struct stpdfs_free));
sb->freelist.nfree = 1;
sb->freelist.free[0] = blocknum;
}
else
{
sb->freelist.free[sb->freelist.nfree++] = blocknum;
}
sb->time = time(NULL);
return (0);
}

0
libstpdfs/dir.c Normal file
View file

0
libstpdfs/file.c Normal file
View file

13
libstpdfs/inode.c Normal file
View file

@ -0,0 +1,13 @@
#include <stupidfs.h>
struct inode *
stpdfs_inode_get(struct stpdfs_sb *sb, uint32_t ino)
{
return (NULL);
}
struct stpdfs_dirent *
stpdfs_lookup(struct stpdfs_inode *dir, struct stpdfs_dirent *dentry, int flags)
{
return (NULL);
}

18
libstpdfs/stpdfs.h Normal file
View file

@ -0,0 +1,18 @@
/**
* \file stpdfs.h
*/
#ifndef STPDFS_H
# define STPDFS_H 1
# include <stupidfs.h>
struct stpdfs_super_info {
struct stpdfs_sb sb;
int fd;
};
int stpdfs_read_super(struct stpdfs_super_info *sbi, int fd);
int stpdfs_super_validate(struct stpdfs_sb *sb);
int stpdfs_kill_super(struct stpdfs_super_info *sbi);
#endif /* !STPDFS_H */

27
libstpdfs/super.c Normal file
View file

@ -0,0 +1,27 @@
#include <stupidfs.h>
int
stpdfs_super_validate(struct stpdfs_sb *sb)
{
if (sb->magic != STPDFS_SB_MAGIC)
{
return (-1);
}
if (sb->revision != STPDFS_SB_REV)
{
return (-1);
}
if (sb->fsize == 0 || sb->isize == 0)
{
return (-1);
}
if (sb->isize > sb->fsize)
{
return (-1);
}
return (0);
}

View file

@ -1,13 +0,0 @@
AM_CPPFLAGS = -I$(top_srcdir)/lib -I$(top_srcdir)/include
TESTS = test_lzp test_hchacha test_xchacha
check_PROGRAMS = test_lzp test_hchacha test_xchacha
LDADD = -lcmocka ../lib/libstpdfs.a
test_lzp_SOURCES = test_lzp.c
test_hchacha_SOURCES = test_hchacha.c
test_xchacha_SOURCES = test_xchacha.c