add shared target + improve error handling in lib

This commit is contained in:
thematdev 2023-06-19 21:39:56 +03:00
parent 509dd9ec01
commit 65096b1987
Signed by: thematdev
GPG Key ID: D12878639B090D90
4 changed files with 50 additions and 20 deletions

View File

@ -1,23 +1,39 @@
TARGET_LIB ?= libqments.a
STATIC_LIB ?= libqments.a
SHARED_LIB ?= libqments.so
BD ?= build
SRC_DIRS ?= src
INCLUDE_DIRS ?= include
SBD ?= $(BD)/shared
SRC_FILES := $(shell find $(SRC_DIRS) -name *.c)
OBJ_FILES := $(patsubst %.c,$(BD)/%.o,$(SRC_FILES))
$(BD)/$(TARGET_LIB): $(OBJ_FILES)
OBJ_FILES := $(patsubst %.c,$(BD)/%.o,$(SRC_FILES))
SHARED_OBJ_FILES := $(patsubst %.c,$(SBD)/%.o,$(SRC_FILES))
shared: $(SBD)/$(SHARED_LIB)
static: $(BD)/$(STATIC_LIB)
$(SBD)/$(SHARED_LIB): $(SHARED_OBJ_FILES)
$(CC) $(CFLAGS) -shared -o $@ -Wl,--whole-archive $^ -Wl,--no-whole-archive
$(BD)/$(STATIC_LIB): $(OBJ_FILES)
mkdir -p $(@D)
ar rcs $@ $(OBJ_FILES)
$(SBD)/%.o: %.c
mkdir -p $(@D)
$(CC) $(CFLAGS) -fPIC -c $< -o $@ -I $(INCLUDE_DIRS)
$(BD)/%.o: %.c
mkdir -p $(@D)
$(CC) $(CFLAGS) -c $< -o $@ -I $(INCLUDE_DIRS)
all: $(BD)/$(TARGET_LIB)
all: shared static
clean:
rm -rfI $(BD)/
.PHONY: all clean
.PHONY: all clean shared static

View File

@ -5,7 +5,7 @@
typedef struct {
/* should return (id > 0) on success, and negative number on error */
int (*leave_comment)(void*, CommentHeader*, char*);
int (*leave_comment)(void*, const CommentHeader*, const char*);
/* retrieve header information by id */
int (*get_header)(void*, CommentHeader*, int);
/* retrieve text of comment by id */

View File

@ -11,7 +11,7 @@ typedef struct {
char *path;
} UnixFsDriverData;
int unix_fs_driver_leave_comment(void *driver_data_ptr, CommentHeader *header, char *text);
int unix_fs_driver_leave_comment(void *driver_data_ptr, const CommentHeader *header, const char *text);
int unix_fs_driver_get_header(void *driver_data_ptr, CommentHeader *header, int id);

View File

@ -1,5 +1,6 @@
#define _GNU_SOURCE /* ahh, didn't know asprintf is a GNU extension */
/* TODO: remove this bullshit */
/* TODO: more robust error handling */
#include <sys/types.h>
#include <sys/stat.h>
@ -15,6 +16,16 @@
#define READ_BUF_SZ 256
#define SWRITE(fd, buf, count) \
if (write(fd, buf, count) < count) { \
return -EINVAL; \
}
#define SREAD(fd, buf, count) \
if (read(fd, buf, count) < count) { \
return -EINVAL; \
}
int
is_valid_directory(const char *path)
{
@ -25,26 +36,24 @@ is_valid_directory(const char *path)
return S_ISDIR(path_stat.st_mode);
}
/* TODO: handle errors */
int
serialize_header(int fd, const CommentHeader *header)
{
write(fd, &header->reply_id, sizeof(int));
write(fd, &header->creation_time, sizeof(time_t));
write(fd, &header->text_length, sizeof(size_t));
write(fd, header->user_sid, strlen(header->user_sid) + 1);
write(fd, header->user_displayname, strlen(header->user_displayname) + 1);
SWRITE(fd, &header->reply_id, sizeof(int));
SWRITE(fd, &header->creation_time, sizeof(time_t));
SWRITE(fd, &header->text_length, sizeof(size_t));
SWRITE(fd, header->user_sid, strlen(header->user_sid) + 1);
SWRITE(fd, header->user_displayname, strlen(header->user_displayname) + 1);
return 0;
}
/* TODO: handle errors */
/* header string buffers must be long enough */
int
deserialize_header(int fd, CommentHeader *header)
{
char buf[READ_BUF_SZ], *dst_bufs[2];
int bytes_read;
size_t len, dst_idx, i;
size_t dst_idx, i;
i = 0;
@ -52,11 +61,14 @@ deserialize_header(int fd, CommentHeader *header)
dst_bufs[1] = header->user_displayname;
dst_idx = 0;
read(fd, &header->reply_id, sizeof(int));
read(fd, &header->creation_time, sizeof(time_t));
read(fd, &header->text_length, sizeof(size_t));
SREAD(fd, &header->reply_id, sizeof(int));
SREAD(fd, &header->creation_time, sizeof(time_t));
SREAD(fd, &header->text_length, sizeof(size_t));
while ((bytes_read = read(fd, buf, READ_BUF_SZ))) {
if (bytes_read < 0) {
return -EINVAL;
}
for (; i < bytes_read; ++i) {
*(dst_bufs[dst_idx]++) = buf[i];
if (buf[i] == '\0') {
@ -64,17 +76,20 @@ deserialize_header(int fd, CommentHeader *header)
}
}
}
if (dst_idx != 2) {
return -EINVAL;
}
return 0;
}
/* TODO: validate driver_data_ptr */
int
unix_fs_driver_leave_comment(void *driver_data_ptr, CommentHeader *header, char *text)
unix_fs_driver_leave_comment(void *driver_data_ptr, const CommentHeader *header, const char *text)
{
char *path, *header_file_path, *text_file_path, *id_ctrl_file_path;
FILE *id_ctrl_file;
size_t path_len, text_len;
size_t text_len;
int max_id;
int retval;
int header_fd, text_fd, id_ctrl_fd;
@ -91,7 +106,6 @@ unix_fs_driver_leave_comment(void *driver_data_ptr, CommentHeader *header, char
goto defer;
}
path_len = strlen(path);
text_len = strlen(text);
if (asprintf(&id_ctrl_file_path, "%s/%s", path, ID_CTRL_FILE) < 0) {