Skip to content

Commit

Permalink
lib: chunkio: upgrade to v1.0.0
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Feb 14, 2020
1 parent 6e84e71 commit c92c75b
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 91 deletions.
10 changes: 10 additions & 0 deletions lib/chunkio/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
cmake_minimum_required(VERSION 2.8)
project(chunk-io)

set(CIO_VERSION_MAJOR 1)
set(CIO_VERSION_MINOR 0)
set(CIO_VERSION_PATCH 0)
set(CIO_VERSION_STR "${CIO_VERSION_MAJOR}.${CIO_VERSION_MINOR}.${CIO_VERSION_PATCH}")

# CFLAGS
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(MSVC)
Expand Down Expand Up @@ -83,6 +88,11 @@ if(CIO_HAVE_FALLOCATE)
CIO_DEFINITION(CIO_HAVE_FALLOCATE)
endif()

configure_file(
"${PROJECT_SOURCE_DIR}/include/chunkio/cio_version.h.in"
"${PROJECT_SOURCE_DIR}/include/chunkio/cio_version.h"
)

include_directories(
include
deps/
Expand Down
19 changes: 12 additions & 7 deletions lib/chunkio/include/chunkio/chunkio.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
#define CIO_TRUE !0

/* debug levels */
#define CIO_ERROR 1
#define CIO_WARN 2
#define CIO_INFO 3
#define CIO_DEBUG 4
#define CIO_TRACE 5
#define CIO_LOG_ERROR 1
#define CIO_LOG_WARN 2
#define CIO_LOG_INFO 3
#define CIO_LOG_DEBUG 4
#define CIO_LOG_TRACE 5

/* Storage backend */
#define CIO_STORE_FS 0
Expand All @@ -42,13 +42,18 @@
#define CIO_CHECKSUM 4 /* enable checksum verification (crc32) */
#define CIO_FULL_SYNC 8 /* force sync to fs through MAP_SYNC */

/* Return status */
#define CIO_CORRUPTED -3 /* Indicate that a chunk is corrupted */
#define CIO_RETRY -2 /* The operations needs to be retried */
#define CIO_ERROR -1 /* Generic error */
#define CIO_OK 0 /* OK */

/* defaults */
#define CIO_MAX_CHUNKS_UP 64 /* default limit for cio_ctx->max_chunks_up */

int cio_page_size;

struct cio_ctx {
int flags;
int page_size;
char *root_path;

/* logging */
Expand Down
4 changes: 3 additions & 1 deletion lib/chunkio/include/chunkio/cio_chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ struct cio_chunk {
};

struct cio_chunk *cio_chunk_open(struct cio_ctx *ctx, struct cio_stream *st,
const char *name, int flags, size_t size);
const char *name, int flags, size_t size,
int *err);
void cio_chunk_close(struct cio_chunk *ch, int delete);
int cio_chunk_write(struct cio_chunk *ch, const void *buf, size_t count);
int cio_chunk_write_at(struct cio_chunk *ch, off_t offset,
Expand All @@ -66,5 +67,6 @@ int cio_chunk_is_file(struct cio_chunk *ch);
int cio_chunk_up(struct cio_chunk *ch);
int cio_chunk_up_force(struct cio_chunk *ch);
int cio_chunk_down(struct cio_chunk *ch);
char *cio_version();

#endif
3 changes: 2 additions & 1 deletion lib/chunkio/include/chunkio/cio_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ struct cio_file *cio_file_open(struct cio_ctx *ctx,
struct cio_stream *st,
struct cio_chunk *ch,
int flags,
size_t size);
size_t size,
int *err);
void cio_file_close(struct cio_chunk *ch, int delete);
int cio_file_write(struct cio_chunk *ch, const void *buf, size_t count);
int cio_file_write_metadata(struct cio_chunk *ch, char *buf, size_t size);
Expand Down
12 changes: 6 additions & 6 deletions lib/chunkio/include/chunkio/cio_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ void cio_log_print(void *ctx, int level, const char *file, int line,
const char *fmt, ...);
int cio_errno_print(int errnum, const char *file, int line);

#define cio_log_error(ctx, fmt, ...) \
cio_log_print(ctx, CIO_ERROR, __FILENAME__, \
#define cio_log_error(ctx, fmt, ...) \
cio_log_print(ctx, CIO_LOG_ERROR, __FILENAME__, \
__LINE__, fmt, ##__VA_ARGS__)

#define cio_log_warn(ctx, fmt, ...) \
cio_log_print(ctx, CIO_WARN, __FILENAME__, \
cio_log_print(ctx, CIO_LOG_WARN, __FILENAME__, \
__LINE__, fmt, ##__VA_ARGS__)

#define cio_log_info(ctx, fmt, ...) \
cio_log_print(ctx, CIO_INFO, __FILENAME__, \
cio_log_print(ctx, CIO_LOG_INFO, __FILENAME__, \
__LINE__, fmt, ##__VA_ARGS__)

#define cio_log_debug(ctx, fmt, ...) \
cio_log_print(ctx, CIO_DEBUG, __FILENAME__, \
cio_log_print(ctx, CIO_LOG_DEBUG, __FILENAME__, \
__LINE__, fmt, ##__VA_ARGS__)

#define cio_log_trace(ctx, fmt, ...) \
cio_log_print(ctx, CIO_TRACE, __FILENAME__, \
cio_log_print(ctx, CIO_LOG_TRACE, __FILENAME__, \
__LINE__, fmt, ##__VA_ARGS__)

#ifdef __FILENAME__
Expand Down
36 changes: 36 additions & 0 deletions lib/chunkio/include/chunkio/cio_version.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Chunk I/O
* =========
* Copyright 2018-2020 Eduardo Silva <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef CIO_VERSION_H
#define CIO_VERSION_H

/* Helpers to convert/format version string */
#define STR_HELPER(s) #s
#define STR(s) STR_HELPER(s)

/* Chunk I/O Version */
#define CIO_VERSION_MAJOR @CIO_VERSION_MAJOR@
#define CIO_VERSION_MINOR @CIO_VERSION_MINOR@
#define CIO_VERSION_PATCH @CIO_VERSION_PATCH@
#define CIO_VERSION (CIO_VERSION_MAJOR * 10000 \
CIO_VERSION_MINOR * 100 \
CIO_VERSION_PATCH)
#define CIO_VERSION_STR "@CIO_VERSION_STR@"

#endif
8 changes: 4 additions & 4 deletions lib/chunkio/src/chunkio.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct cio_ctx *cio_create(const char *root_path,
int ret;
struct cio_ctx *ctx;

if (log_level < CIO_ERROR || log_level > CIO_TRACE) {
if (log_level < CIO_LOG_ERROR || log_level > CIO_LOG_TRACE) {
fprintf(stderr, "[cio] invalid log level, aborting");
return NULL;
}
Expand All @@ -70,14 +70,14 @@ struct cio_ctx *cio_create(const char *root_path,
}
#endif

cio_page_size = getpagesize();

/* Create context */
ctx = calloc(1, sizeof(struct cio_ctx));
if (!ctx) {
perror("calloc");
return NULL;
}

ctx->page_size = getpagesize();
ctx->max_chunks_up = CIO_MAX_CHUNKS_UP;
cio_set_log_callback(ctx, log_cb);
cio_set_log_level(ctx, log_level);
Expand Down Expand Up @@ -135,7 +135,7 @@ void cio_set_log_callback(struct cio_ctx *ctx, void (*log_cb))

int cio_set_log_level(struct cio_ctx *ctx, int level)
{
if (level < CIO_ERROR || level > CIO_TRACE) {
if (level < CIO_LOG_ERROR || level > CIO_LOG_TRACE) {
return -1;
}

Expand Down
44 changes: 26 additions & 18 deletions lib/chunkio/src/cio_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@

#include <chunkio/chunkio_compat.h>
#include <chunkio/chunkio.h>
#include <chunkio/cio_version.h>
#include <chunkio/cio_file.h>
#include <chunkio/cio_memfs.h>
#include <chunkio/cio_log.h>

#include <string.h>

struct cio_chunk *cio_chunk_open(struct cio_ctx *ctx, struct cio_stream *st,
const char *name, int flags, size_t size)
const char *name, int flags, size_t size,
int *err)
{
int len;
void *backend = NULL;
Expand Down Expand Up @@ -73,9 +75,10 @@ struct cio_chunk *cio_chunk_open(struct cio_ctx *ctx, struct cio_stream *st,

/* create backend context */
if (st->type == CIO_STORE_FS) {
backend = cio_file_open(ctx, st, ch, flags, size);
backend = cio_file_open(ctx, st, ch, flags, size, err);
}
else if (st->type == CIO_STORE_MEM) {
*err = CIO_OK;
backend = cio_memfs_open(ctx, st, ch, flags, size);
}

Expand Down Expand Up @@ -186,15 +189,15 @@ int cio_chunk_get_content(struct cio_chunk *ch, char **buf, size_t *size)
else if (type == CIO_STORE_FS) {
cf = ch->backend;
ret = cio_file_read_prepare(ch->ctx, ch);
if (ret == -1) {
return -1;
if (ret != CIO_OK) {
return ret;
}
*size = cf->data_size;
*buf = cio_file_st_get_content(cf->map);
return ret;
}

return -1;
return CIO_ERROR;
}

size_t cio_chunk_get_content_end_pos(struct cio_chunk *ch)
Expand Down Expand Up @@ -279,21 +282,21 @@ char *cio_chunk_hash(struct cio_chunk *ch)
int cio_chunk_lock(struct cio_chunk *ch)
{
if (ch->lock == CIO_TRUE) {
return -1;
return CIO_ERROR;
}

ch->lock = CIO_TRUE;
return 0;
return CIO_OK;
}

int cio_chunk_unlock(struct cio_chunk *ch)
{
if (ch->lock == CIO_FALSE) {
return -1;
return CIO_ERROR;
}

ch->lock = CIO_FALSE;
return 0;
return CIO_OK;
}

int cio_chunk_is_locked(struct cio_chunk *ch)
Expand All @@ -313,11 +316,11 @@ int cio_chunk_tx_begin(struct cio_chunk *ch)
struct cio_file *cf;

if (cio_chunk_is_locked(ch)) {
return -1;
return CIO_RETRY;
}

if (ch->tx_active == CIO_TRUE) {
return -1;
return CIO_OK;
}

ch->tx_active = CIO_TRUE;
Expand All @@ -333,7 +336,7 @@ int cio_chunk_tx_begin(struct cio_chunk *ch)
ch->tx_content_length = cf->data_size;
}

return 0;
return CIO_OK;
}

/*
Expand All @@ -346,11 +349,11 @@ int cio_chunk_tx_commit(struct cio_chunk *ch)

ret = cio_chunk_sync(ch);
if (ret == -1) {
return -1;
return CIO_ERROR;
}

ch->tx_active = CIO_FALSE;
return 0;
return CIO_OK;
}

/*
Expand Down Expand Up @@ -378,7 +381,7 @@ int cio_chunk_tx_rollback(struct cio_chunk *ch)
}

ch->tx_active = CIO_FALSE;
return 0;
return CIO_OK;
}

/*
Expand Down Expand Up @@ -424,7 +427,7 @@ int cio_chunk_down(struct cio_chunk *ch)
return cio_file_down(ch);
}

return 0;
return CIO_OK;
}

int cio_chunk_up(struct cio_chunk *ch)
Expand All @@ -436,7 +439,7 @@ int cio_chunk_up(struct cio_chunk *ch)
return cio_file_up(ch);
}

return 0;
return CIO_OK;
}

int cio_chunk_up_force(struct cio_chunk *ch)
Expand All @@ -448,5 +451,10 @@ int cio_chunk_up_force(struct cio_chunk *ch)
return cio_file_up_force(ch);
}

return 0;
return CIO_OK;
}

char *cio_version()
{
return CIO_VERSION_STR;
}
Loading

0 comments on commit c92c75b

Please sign in to comment.