add shared target + improve error handling in lib
This commit is contained in:
parent
509dd9ec01
commit
65096b1987
24
Makefile
24
Makefile
@ -1,23 +1,39 @@
|
|||||||
TARGET_LIB ?= libqments.a
|
STATIC_LIB ?= libqments.a
|
||||||
|
SHARED_LIB ?= libqments.so
|
||||||
|
|
||||||
BD ?= build
|
BD ?= build
|
||||||
SRC_DIRS ?= src
|
SRC_DIRS ?= src
|
||||||
INCLUDE_DIRS ?= include
|
INCLUDE_DIRS ?= include
|
||||||
|
|
||||||
|
SBD ?= $(BD)/shared
|
||||||
|
|
||||||
SRC_FILES := $(shell find $(SRC_DIRS) -name *.c)
|
SRC_FILES := $(shell find $(SRC_DIRS) -name *.c)
|
||||||
OBJ_FILES := $(patsubst %.c,$(BD)/%.o,$(SRC_FILES))
|
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)
|
mkdir -p $(@D)
|
||||||
ar rcs $@ $(OBJ_FILES)
|
ar rcs $@ $(OBJ_FILES)
|
||||||
|
|
||||||
|
$(SBD)/%.o: %.c
|
||||||
|
mkdir -p $(@D)
|
||||||
|
$(CC) $(CFLAGS) -fPIC -c $< -o $@ -I $(INCLUDE_DIRS)
|
||||||
|
|
||||||
$(BD)/%.o: %.c
|
$(BD)/%.o: %.c
|
||||||
mkdir -p $(@D)
|
mkdir -p $(@D)
|
||||||
$(CC) $(CFLAGS) -c $< -o $@ -I $(INCLUDE_DIRS)
|
$(CC) $(CFLAGS) -c $< -o $@ -I $(INCLUDE_DIRS)
|
||||||
|
|
||||||
all: $(BD)/$(TARGET_LIB)
|
all: shared static
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rfI $(BD)/
|
rm -rfI $(BD)/
|
||||||
|
|
||||||
.PHONY: all clean
|
.PHONY: all clean shared static
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
/* should return (id > 0) on success, and negative number on error */
|
/* 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 */
|
/* retrieve header information by id */
|
||||||
int (*get_header)(void*, CommentHeader*, int);
|
int (*get_header)(void*, CommentHeader*, int);
|
||||||
/* retrieve text of comment by id */
|
/* retrieve text of comment by id */
|
||||||
|
@ -11,7 +11,7 @@ typedef struct {
|
|||||||
char *path;
|
char *path;
|
||||||
} UnixFsDriverData;
|
} 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);
|
int unix_fs_driver_get_header(void *driver_data_ptr, CommentHeader *header, int id);
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#define _GNU_SOURCE /* ahh, didn't know asprintf is a GNU extension */
|
#define _GNU_SOURCE /* ahh, didn't know asprintf is a GNU extension */
|
||||||
/* TODO: remove this bullshit */
|
/* TODO: remove this bullshit */
|
||||||
|
/* TODO: more robust error handling */
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
@ -15,6 +16,16 @@
|
|||||||
|
|
||||||
#define READ_BUF_SZ 256
|
#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
|
int
|
||||||
is_valid_directory(const char *path)
|
is_valid_directory(const char *path)
|
||||||
{
|
{
|
||||||
@ -25,26 +36,24 @@ is_valid_directory(const char *path)
|
|||||||
return S_ISDIR(path_stat.st_mode);
|
return S_ISDIR(path_stat.st_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: handle errors */
|
|
||||||
int
|
int
|
||||||
serialize_header(int fd, const CommentHeader *header)
|
serialize_header(int fd, const CommentHeader *header)
|
||||||
{
|
{
|
||||||
write(fd, &header->reply_id, sizeof(int));
|
SWRITE(fd, &header->reply_id, sizeof(int));
|
||||||
write(fd, &header->creation_time, sizeof(time_t));
|
SWRITE(fd, &header->creation_time, sizeof(time_t));
|
||||||
write(fd, &header->text_length, sizeof(size_t));
|
SWRITE(fd, &header->text_length, sizeof(size_t));
|
||||||
write(fd, header->user_sid, strlen(header->user_sid) + 1);
|
SWRITE(fd, header->user_sid, strlen(header->user_sid) + 1);
|
||||||
write(fd, header->user_displayname, strlen(header->user_displayname) + 1);
|
SWRITE(fd, header->user_displayname, strlen(header->user_displayname) + 1);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: handle errors */
|
|
||||||
/* header string buffers must be long enough */
|
/* header string buffers must be long enough */
|
||||||
int
|
int
|
||||||
deserialize_header(int fd, CommentHeader *header)
|
deserialize_header(int fd, CommentHeader *header)
|
||||||
{
|
{
|
||||||
char buf[READ_BUF_SZ], *dst_bufs[2];
|
char buf[READ_BUF_SZ], *dst_bufs[2];
|
||||||
int bytes_read;
|
int bytes_read;
|
||||||
size_t len, dst_idx, i;
|
size_t dst_idx, i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
@ -52,11 +61,14 @@ deserialize_header(int fd, CommentHeader *header)
|
|||||||
dst_bufs[1] = header->user_displayname;
|
dst_bufs[1] = header->user_displayname;
|
||||||
dst_idx = 0;
|
dst_idx = 0;
|
||||||
|
|
||||||
read(fd, &header->reply_id, sizeof(int));
|
SREAD(fd, &header->reply_id, sizeof(int));
|
||||||
read(fd, &header->creation_time, sizeof(time_t));
|
SREAD(fd, &header->creation_time, sizeof(time_t));
|
||||||
read(fd, &header->text_length, sizeof(size_t));
|
SREAD(fd, &header->text_length, sizeof(size_t));
|
||||||
|
|
||||||
while ((bytes_read = read(fd, buf, READ_BUF_SZ))) {
|
while ((bytes_read = read(fd, buf, READ_BUF_SZ))) {
|
||||||
|
if (bytes_read < 0) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
for (; i < bytes_read; ++i) {
|
for (; i < bytes_read; ++i) {
|
||||||
*(dst_bufs[dst_idx]++) = buf[i];
|
*(dst_bufs[dst_idx]++) = buf[i];
|
||||||
if (buf[i] == '\0') {
|
if (buf[i] == '\0') {
|
||||||
@ -64,17 +76,20 @@ deserialize_header(int fd, CommentHeader *header)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (dst_idx != 2) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: validate driver_data_ptr */
|
/* TODO: validate driver_data_ptr */
|
||||||
|
|
||||||
int
|
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;
|
char *path, *header_file_path, *text_file_path, *id_ctrl_file_path;
|
||||||
FILE *id_ctrl_file;
|
FILE *id_ctrl_file;
|
||||||
size_t path_len, text_len;
|
size_t text_len;
|
||||||
int max_id;
|
int max_id;
|
||||||
int retval;
|
int retval;
|
||||||
int header_fd, text_fd, id_ctrl_fd;
|
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;
|
goto defer;
|
||||||
}
|
}
|
||||||
|
|
||||||
path_len = strlen(path);
|
|
||||||
text_len = strlen(text);
|
text_len = strlen(text);
|
||||||
|
|
||||||
if (asprintf(&id_ctrl_file_path, "%s/%s", path, ID_CTRL_FILE) < 0) {
|
if (asprintf(&id_ctrl_file_path, "%s/%s", path, ID_CTRL_FILE) < 0) {
|
||||||
|
Loading…
Reference in New Issue
Block a user